Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 2 | * Mini xargs implementation for busybox |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 3 | * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 5 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 7 | * Special thanks |
| 8 | * - Mark Whitley and Glenn McGrath for stimul to rewrote :) |
| 9 | * - Mike Rendell <michael@cs.mun.ca> |
| 10 | * and David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 16 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 21 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 26 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 30 | #include <string.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <getopt.h> |
| 33 | #include <errno.h> |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 34 | #include <fcntl.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
| 36 | #include <sys/wait.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 38 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 39 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. |
| 40 | We try to make it as large as possible. */ |
| 41 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) |
| 42 | #define ARG_MAX sysconf (_SC_ARG_MAX) |
| 43 | #endif |
| 44 | #ifndef ARG_MAX |
| 45 | #define ARG_MAX 470 |
| 46 | #endif |
| 47 | |
| 48 | |
| 49 | #ifdef TEST |
| 50 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 51 | # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 52 | # endif |
| 53 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
| 54 | # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
| 55 | # endif |
| 56 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 57 | # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 58 | # endif |
| 59 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 60 | # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 61 | # endif |
| 62 | #endif |
| 63 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 64 | /* |
| 65 | This function have special algorithm. |
| 66 | Don`t use fork and include to main! |
| 67 | */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 68 | static int xargs_exec(char *const *args) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 69 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 70 | pid_t p; |
| 71 | volatile int exec_errno = 0; /* shared vfork stack */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 73 | if ((p = vfork()) >= 0) { |
| 74 | if (p == 0) { |
| 75 | /* vfork -- child */ |
| 76 | execvp(args[0], args); |
| 77 | exec_errno = errno; /* set error to shared stack */ |
| 78 | _exit(1); |
| 79 | } else { |
| 80 | /* vfork -- parent */ |
| 81 | int status; |
| 82 | |
| 83 | while (wait(&status) == (pid_t) - 1) |
| 84 | if (errno != EINTR) |
| 85 | break; |
| 86 | if (exec_errno) { |
| 87 | errno = exec_errno; |
| 88 | bb_perror_msg("%s", args[0]); |
| 89 | return exec_errno == ENOENT ? 127 : 126; |
| 90 | } else { |
| 91 | if (WEXITSTATUS(status) == 255) { |
| 92 | bb_error_msg("%s: exited with status 255; aborting", |
| 93 | args[0]); |
| 94 | return 124; |
| 95 | } |
| 96 | if (WIFSTOPPED(status)) { |
| 97 | bb_error_msg("%s: stopped by signal %d", args[0], |
| 98 | WSTOPSIG(status)); |
| 99 | return 125; |
| 100 | } |
| 101 | if (WIFSIGNALED(status)) { |
| 102 | bb_error_msg("%s: terminated by signal %d", args[0], |
| 103 | WTERMSIG(status)); |
| 104 | return 125; |
| 105 | } |
| 106 | if (WEXITSTATUS(status) != 0) |
| 107 | return 123; |
| 108 | return 0; |
| 109 | } |
| 110 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 111 | } else { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 112 | bb_perror_msg_and_die("vfork"); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 113 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 114 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 115 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 116 | |
| 117 | typedef struct xlist_s { |
| 118 | char *data; |
| 119 | size_t lenght; |
| 120 | struct xlist_s *link; |
| 121 | } xlist_t; |
| 122 | |
| 123 | static int eof_stdin_detected; |
| 124 | |
| 125 | #define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
| 126 | #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \ |
| 127 | || (c) == '\f' || (c) == '\v') |
| 128 | |
| 129 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 130 | static xlist_t *process_stdin(xlist_t * list_arg, const char *eof_str, |
| 131 | size_t mc, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 132 | { |
| 133 | #define NORM 0 |
| 134 | #define QUOTE 1 |
| 135 | #define BACKSLASH 2 |
| 136 | #define SPACE 4 |
| 137 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 138 | char *s = NULL; /* start word */ |
| 139 | char *p = NULL; /* pointer to end word */ |
| 140 | char q = 0; /* quote char */ |
| 141 | char state = NORM; |
| 142 | char eof_str_detected = 0; |
| 143 | size_t line_l = 0; /* size loaded args line */ |
| 144 | int c; /* current char */ |
| 145 | xlist_t *cur; |
| 146 | xlist_t *prev; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 147 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 148 | for (prev = cur = list_arg; cur; cur = cur->link) { |
| 149 | line_l += cur->lenght; /* previous allocated */ |
| 150 | if (prev != cur) |
| 151 | prev = prev->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 152 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 153 | |
| 154 | while (!eof_stdin_detected) { |
| 155 | c = getchar(); |
| 156 | if (c == EOF) { |
| 157 | eof_stdin_detected++; |
| 158 | if (s) |
| 159 | goto unexpected_eof; |
| 160 | break; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 161 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 162 | if (eof_str_detected) |
| 163 | continue; |
| 164 | if (state == BACKSLASH) { |
| 165 | state = NORM; |
| 166 | goto set; |
| 167 | } else if (state == QUOTE) { |
| 168 | if (c == q) { |
| 169 | q = 0; |
| 170 | state = NORM; |
| 171 | } else { |
| 172 | goto set; |
| 173 | } |
| 174 | } else { /* if(state == NORM) */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 175 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 176 | if (ISSPACE(c)) { |
| 177 | if (s) { |
| 178 | unexpected_eof: |
| 179 | state = SPACE; |
| 180 | c = 0; |
| 181 | goto set; |
| 182 | } |
| 183 | } else { |
| 184 | if (s == NULL) |
| 185 | s = p = buf; |
| 186 | if (c == '\\') { |
| 187 | state = BACKSLASH; |
| 188 | } else if (c == '\'' || c == '"') { |
| 189 | q = c; |
| 190 | state = QUOTE; |
| 191 | } else { |
| 192 | set: |
| 193 | if ((p - buf) >= mc) |
| 194 | bb_error_msg_and_die("argument line too long"); |
| 195 | *p++ = c; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | if (state == SPACE) { /* word's delimiter or EOF detected */ |
| 200 | if (q) |
| 201 | bb_error_msg_and_die("unmatched %s quote", |
| 202 | q == '\'' ? "single" : "double"); |
| 203 | /* word loaded */ |
| 204 | if (eof_str) { |
| 205 | eof_str_detected = strcmp(s, eof_str) == 0; |
| 206 | } |
| 207 | if (!eof_str_detected) { |
| 208 | size_t lenght = (p - buf); |
| 209 | |
| 210 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 211 | cur->data = memcpy(cur + 1, s, lenght); |
| 212 | cur->lenght = lenght; |
| 213 | cur->link = NULL; |
| 214 | if (prev == NULL) { |
| 215 | list_arg = cur; |
| 216 | } else { |
| 217 | prev->link = cur; |
| 218 | } |
| 219 | prev = cur; |
| 220 | line_l += lenght; |
| 221 | if (line_l > mc) { |
| 222 | /* stop memory usage :-) */ |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | s = NULL; |
| 227 | state = NORM; |
| 228 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 229 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 230 | return list_arg; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 231 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 232 | #else |
| 233 | /* The variant is unsupport single, double quotes and backslash */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 234 | static xlist_t *process_stdin(xlist_t * list_arg, const char *eof_str, |
| 235 | size_t mc, char *buf) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 236 | { |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 237 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 238 | int c; /* current char */ |
| 239 | int eof_str_detected = 0; |
| 240 | char *s = NULL; /* start word */ |
| 241 | char *p = NULL; /* pointer to end word */ |
| 242 | size_t line_l = 0; /* size loaded args line */ |
| 243 | xlist_t *cur; |
| 244 | xlist_t *prev; |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 245 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 246 | for (prev = cur = list_arg; cur; cur = cur->link) { |
| 247 | line_l += cur->lenght; /* previous allocated */ |
| 248 | if (prev != cur) |
| 249 | prev = prev->link; |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 250 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 251 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 252 | while (!eof_stdin_detected) { |
| 253 | c = getchar(); |
| 254 | if (c == EOF) { |
| 255 | eof_stdin_detected++; |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 256 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 257 | if (eof_str_detected) |
| 258 | continue; |
| 259 | if (c == EOF || ISSPACE(c)) { |
| 260 | if (s == NULL) |
| 261 | continue; |
| 262 | c = EOF; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 263 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 264 | if (s == NULL) |
| 265 | s = p = buf; |
| 266 | if ((p - buf) >= mc) |
| 267 | bb_error_msg_and_die("argument line too long"); |
| 268 | *p++ = c == EOF ? 0 : c; |
| 269 | if (c == EOF) { /* word's delimiter or EOF detected */ |
| 270 | /* word loaded */ |
| 271 | if (eof_str) { |
| 272 | eof_str_detected = strcmp(s, eof_str) == 0; |
| 273 | } |
| 274 | if (!eof_str_detected) { |
| 275 | size_t lenght = (p - buf); |
| 276 | |
| 277 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 278 | cur->data = memcpy(cur + 1, s, lenght); |
| 279 | cur->lenght = lenght; |
| 280 | cur->link = NULL; |
| 281 | if (prev == NULL) { |
| 282 | list_arg = cur; |
| 283 | } else { |
| 284 | prev->link = cur; |
| 285 | } |
| 286 | prev = cur; |
| 287 | line_l += lenght; |
| 288 | if (line_l > mc) { |
| 289 | /* stop memory usage :-) */ |
| 290 | break; |
| 291 | } |
| 292 | s = NULL; |
| 293 | } |
| 294 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 295 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 296 | return list_arg; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 297 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 298 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 302 | /* Prompt the user for a response, and |
| 303 | if the user responds affirmatively, return true; |
| 304 | otherwise, return false. Used "/dev/tty", not stdin. */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 305 | static int xargs_ask_confirmation(void) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 306 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 307 | static FILE *tty_stream; |
| 308 | int c, savec; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 309 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 310 | if (!tty_stream) { |
| 311 | tty_stream = fopen("/dev/tty", "r"); |
| 312 | if (!tty_stream) |
| 313 | bb_perror_msg_and_die("/dev/tty"); |
| 314 | /* pranoidal security by vodz */ |
| 315 | fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC); |
| 316 | } |
| 317 | fputs(" ?...", stderr); |
| 318 | fflush(stderr); |
| 319 | c = savec = getc(tty_stream); |
| 320 | while (c != EOF && c != '\n') |
| 321 | c = getc(tty_stream); |
| 322 | if (savec == 'y' || savec == 'Y') |
| 323 | return 1; |
| 324 | return 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 325 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 326 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 327 | # define OPT_INC_P 1 |
| 328 | #else |
| 329 | # define OPT_INC_P 0 |
| 330 | # define xargs_ask_confirmation() 1 |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 331 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 332 | |
| 333 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 334 | # define OPT_INC_X 1 |
| 335 | #else |
| 336 | # define OPT_INC_X 0 |
| 337 | #endif |
| 338 | |
| 339 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 340 | static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str, |
| 341 | size_t mc, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 342 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 343 | int c; /* current char */ |
| 344 | char *s = NULL; /* start word */ |
| 345 | char *p = NULL; /* pointer to end word */ |
| 346 | size_t line_l = 0; /* size loaded args line */ |
| 347 | xlist_t *cur; |
| 348 | xlist_t *prev; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 349 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 350 | for (prev = cur = list_arg; cur; cur = cur->link) { |
| 351 | line_l += cur->lenght; /* previous allocated */ |
| 352 | if (prev != cur) |
| 353 | prev = prev->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 354 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 355 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 356 | while (!eof_stdin_detected) { |
| 357 | c = getchar(); |
| 358 | if (c == EOF) { |
| 359 | eof_stdin_detected++; |
| 360 | if (s == NULL) |
| 361 | break; |
| 362 | c = 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 363 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 364 | if (s == NULL) |
| 365 | s = p = buf; |
| 366 | if ((p - buf) >= mc) |
| 367 | bb_error_msg_and_die("argument line too long"); |
| 368 | *p++ = c; |
| 369 | if (c == 0) { /* word's delimiter or EOF detected */ |
| 370 | /* word loaded */ |
| 371 | size_t lenght = (p - buf); |
| 372 | |
| 373 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 374 | cur->data = memcpy(cur + 1, s, lenght); |
| 375 | cur->lenght = lenght; |
| 376 | cur->link = NULL; |
| 377 | if (prev == NULL) { |
| 378 | list_arg = cur; |
| 379 | } else { |
| 380 | prev->link = cur; |
| 381 | } |
| 382 | prev = cur; |
| 383 | line_l += lenght; |
| 384 | if (line_l > mc) { |
| 385 | /* stop memory usage :-) */ |
| 386 | break; |
| 387 | } |
| 388 | s = NULL; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 389 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 390 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 391 | return list_arg; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 392 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 393 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 394 | # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 395 | # define OPT_INC_0 1 /* future use */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 396 | #else |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 397 | # define OPT_INC_0 0 /* future use */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 398 | # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 399 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 400 | |
| 401 | |
| 402 | #define OPT_VERBOSE (1<<0) |
| 403 | #define OPT_NO_EMPTY (1<<1) |
| 404 | #define OPT_UPTO_NUMBER (1<<2) |
| 405 | #define OPT_UPTO_SIZE (1<<3) |
| 406 | #define OPT_EOF_STRING (1<<4) |
| 407 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 408 | #define OPT_INTERACTIVE (1<<5) |
| 409 | #else |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 410 | #define OPT_INTERACTIVE (0) /* require for algorithm &| */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 411 | #endif |
| 412 | #define OPT_TERMINATE (1<<(5+OPT_INC_P)) |
| 413 | #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X)) |
| 414 | /* next future |
| 415 | #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0)) |
| 416 | */ |
| 417 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 418 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 419 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 420 | char **args; |
| 421 | int i, a, n; |
| 422 | xlist_t *list = NULL; |
| 423 | xlist_t *cur; |
| 424 | int child_error = 0; |
| 425 | char *max_args, *max_chars; |
| 426 | int n_max_arg; |
| 427 | size_t n_chars = 0; |
| 428 | long orig_arg_max; |
| 429 | const char *eof_str = "_"; |
| 430 | unsigned long opt; |
| 431 | size_t n_max_chars; |
| 432 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 433 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 434 | xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = |
| 435 | process_stdin; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 436 | #endif |
| 437 | |
| 438 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 439 | bb_opt_complementaly = "pt"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 440 | #endif |
| 441 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 442 | opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 443 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 444 | "p" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 445 | #endif |
| 446 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 447 | "x" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 448 | #endif |
| 449 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 450 | "0" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 451 | #endif |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 452 | , &max_args, &max_chars, &eof_str); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 453 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 454 | a = argc - optind; |
| 455 | argv += optind; |
| 456 | if (a == 0) { |
| 457 | /* default behavior is to echo all the filenames */ |
| 458 | *argv = "echo"; |
| 459 | a++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 462 | orig_arg_max = ARG_MAX; |
| 463 | if (orig_arg_max == -1) |
| 464 | orig_arg_max = LONG_MAX; |
| 465 | orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */ |
| 466 | if ((opt & OPT_UPTO_SIZE)) { |
| 467 | n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max); |
| 468 | for (i = 0; i < a; i++) { |
| 469 | n_chars += strlen(*argv) + 1; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 470 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 471 | if (n_max_chars < n_chars) { |
| 472 | bb_error_msg_and_die |
| 473 | ("can not fit single argument within argument list size limit"); |
| 474 | } |
| 475 | n_max_chars -= n_chars; |
| 476 | } else { |
| 477 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which |
| 478 | have it at 1 meg). Things will work fine with a large ARG_MAX but it |
| 479 | will probably hurt the system more than it needs to; an array of this |
| 480 | size is allocated. */ |
| 481 | if (orig_arg_max > 20 * 1024) |
| 482 | orig_arg_max = 20 * 1024; |
| 483 | n_max_chars = orig_arg_max; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 484 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 485 | max_chars = xmalloc(n_max_chars); |
| 486 | |
| 487 | if ((opt & OPT_UPTO_NUMBER)) { |
| 488 | n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX); |
| 489 | } else { |
| 490 | n_max_arg = n_max_chars; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 493 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 494 | if (opt & OPT_ZEROTERM) |
| 495 | read_args = process0_stdin; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 496 | #endif |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 497 | |
| 498 | while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL |
| 499 | || (opt & OPT_NO_EMPTY) == 0) { |
| 500 | opt |= OPT_NO_EMPTY; |
| 501 | n = 0; |
| 502 | n_chars = 0; |
| 503 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 504 | for (cur = list; cur;) { |
| 505 | n_chars += cur->lenght; |
| 506 | n++; |
| 507 | cur = cur->link; |
| 508 | if (n_chars > n_max_chars || (n == n_max_arg && cur)) { |
| 509 | if (opt & OPT_TERMINATE) |
| 510 | bb_error_msg_and_die("argument list too long"); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | #else |
| 515 | for (cur = list; cur; cur = cur->link) { |
| 516 | n_chars += cur->lenght; |
| 517 | n++; |
| 518 | if (n_chars > n_max_chars || n == n_max_arg) { |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ |
| 523 | |
| 524 | /* allocating pointers for execvp: |
| 525 | a*arg, n*arg from stdin, NULL */ |
| 526 | args = xcalloc(n + a + 1, sizeof(char *)); |
| 527 | |
| 528 | /* Store the command to be executed |
| 529 | (taken from the command line) */ |
| 530 | for (i = 0; i < a; i++) |
| 531 | args[i] = argv[i]; |
| 532 | /* (taken from stdin) */ |
| 533 | for (cur = list; n; cur = cur->link) { |
| 534 | args[i++] = cur->data; |
| 535 | n--; |
| 536 | } |
| 537 | |
| 538 | if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) { |
| 539 | for (i = 0; args[i]; i++) { |
| 540 | if (i) |
| 541 | fputc(' ', stderr); |
| 542 | fputs(args[i], stderr); |
| 543 | } |
| 544 | if ((opt & OPT_INTERACTIVE) == 0) |
| 545 | fputc('\n', stderr); |
| 546 | } |
| 547 | if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) { |
| 548 | child_error = xargs_exec(args); |
| 549 | } |
| 550 | |
| 551 | /* clean up */ |
| 552 | for (i = a; args[i]; i++) { |
| 553 | cur = list; |
| 554 | list = list->link; |
| 555 | free(cur); |
| 556 | } |
| 557 | free(args); |
| 558 | if (child_error > 0 && child_error != 123) { |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 563 | free(max_chars); |
| 564 | #endif |
| 565 | return child_error; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
| 569 | #ifdef TEST |
| 570 | |
| 571 | const char *bb_applet_name = "debug stuff usage"; |
| 572 | |
| 573 | void bb_show_usage(void) |
| 574 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 575 | fprintf(stderr, |
| 576 | "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", |
| 577 | bb_applet_name); |
| 578 | exit(1); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | int main(int argc, char **argv) |
| 582 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 583 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 584 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame^] | 585 | #endif /* TEST */ |