1 /* vi: set sw=4 ts=4: */
3 * Mini xargs implementation for busybox
4 * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
6 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
9 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
10 * - Mike Rendell <michael@cs.mun.ca>
11 * and David MacKenzie <djm@gnu.ai.mit.edu>.
13 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
15 * xargs is described in the Single Unix Specification v3 at
16 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
22 /* This is a NOEXEC applet. Be very careful! */
25 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
26 We try to make it as large as possible. */
27 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
28 #define ARG_MAX sysconf (_SC_ARG_MAX)
36 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
37 # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
39 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
40 # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
42 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
43 # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
45 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
46 # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
51 This function has special algorithm.
52 Don't use fork and include to main!
54 static int xargs_exec(char **args)
58 status = spawn_and_wait(args);
60 bb_perror_msg("%s", args[0]);
61 return errno == ENOENT ? 127 : 126;
64 bb_error_msg("%s: exited with status 255; aborting", args[0]);
67 /* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
68 if (WIFSTOPPED(status)) {
69 bb_error_msg("%s: stopped by signal %d",
70 args[0], WSTOPSIG(status));
75 bb_error_msg("%s: terminated by signal %d",
76 args[0], status - 1000);
85 typedef struct xlist_t {
91 static smallint eof_stdin_detected;
93 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
94 #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
95 || (c) == '\f' || (c) == '\v')
97 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
98 static xlist_t *process_stdin(xlist_t *list_arg,
99 const char *eof_str, size_t mc, char *buf)
106 char *s = NULL; /* start word */
107 char *p = NULL; /* pointer to end word */
108 char q = 0; /* quote char */
110 char eof_str_detected = 0;
111 size_t line_l = 0; /* size loaded args line */
112 int c; /* current char */
116 prev = cur = list_arg;
120 line_l += cur->length;
124 while (!eof_stdin_detected) {
127 eof_stdin_detected = 1;
132 if (eof_str_detected)
134 if (state == BACKSLASH) {
137 } else if (state == QUOTE) {
144 } else { /* if(state == NORM) */
157 } else if (c == '\'' || c == '"') {
162 if ((size_t)(p - buf) >= mc)
163 bb_error_msg_and_die("argument line too long");
168 if (state == SPACE) { /* word's delimiter or EOF detected */
170 bb_error_msg_and_die("unmatched %s quote",
171 q == '\'' ? "single" : "double");
175 eof_str_detected = (strcmp(s, eof_str) == 0);
177 if (!eof_str_detected) {
178 size_t length = (p - buf);
180 cur = xzalloc(sizeof(xlist_t) + length);
181 cur->data = memcpy(cur + 1, s, length);
182 cur->length = length;
183 /*cur->link = NULL;*/
192 /* stop memory usage :-) */
203 /* The variant does not support single quotes, double quotes or backslash */
204 static xlist_t *process_stdin(xlist_t *list_arg,
205 const char *eof_str, size_t mc, char *buf)
208 int c; /* current char */
209 char eof_str_detected = 0;
210 char *s = NULL; /* start word */
211 char *p = NULL; /* pointer to end word */
212 size_t line_l = 0; /* size loaded args line */
216 prev = cur = list_arg;
220 line_l += cur->length;
224 while (!eof_stdin_detected) {
227 eof_stdin_detected = 1;
229 if (eof_str_detected)
231 if (c == EOF || ISSPACE(c)) {
239 bb_error_msg_and_die("argument line too long");
240 *p++ = c == EOF ? 0 : c;
241 if (c == EOF) { /* word's delimiter or EOF detected */
244 eof_str_detected = (strcmp(s, eof_str) == 0);
246 if (!eof_str_detected) {
247 size_t length = (p - buf);
249 cur = xzalloc(sizeof(xlist_t) + length);
250 cur->data = memcpy(cur + 1, s, length);
251 cur->length = length;
252 /*cur->link = NULL;*/
261 /* stop memory usage :-) */
270 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
273 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
274 /* Prompt the user for a response, and
275 if the user responds affirmatively, return true;
276 otherwise, return false. Uses "/dev/tty", not stdin. */
277 static int xargs_ask_confirmation(void)
282 tty_stream = xfopen(CURRENT_TTY, "r");
283 fputs(" ?...", stderr);
285 c = savec = getc(tty_stream);
286 while (c != EOF && c != '\n')
287 c = getc(tty_stream);
289 return (savec == 'y' || savec == 'Y');
292 # define xargs_ask_confirmation() 1
293 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
295 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
296 static xlist_t *process0_stdin(xlist_t *list_arg,
297 const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
299 int c; /* current char */
300 char *s = NULL; /* start word */
301 char *p = NULL; /* pointer to end word */
302 size_t line_l = 0; /* size loaded args line */
306 prev = cur = list_arg;
310 line_l += cur->length;
314 while (!eof_stdin_detected) {
317 eof_stdin_detected = 1;
324 if ((size_t)(p - buf) >= mc)
325 bb_error_msg_and_die("argument line too long");
327 if (c == 0) { /* word's delimiter or EOF detected */
329 size_t length = (p - buf);
331 cur = xzalloc(sizeof(xlist_t) + length);
332 cur->data = memcpy(cur + 1, s, length);
333 cur->length = length;
334 /*cur->link = NULL;*/
343 /* stop memory usage :-) */
351 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
353 /* Correct regardless of combination of CONFIG_xxx */
360 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
361 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
362 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
364 OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
365 OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
366 OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
367 OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
368 OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
369 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
370 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
371 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
373 #define OPTION_STR "+trn:s:e::" \
374 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
375 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
376 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
378 int xargs_main(int argc, char **argv);
379 int xargs_main(int argc, char **argv)
383 xlist_t *list = NULL;
386 char *max_args, *max_chars;
390 const char *eof_str = "_";
393 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
394 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
396 #define read_args process_stdin
399 opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str);
401 if (opt & OPT_ZEROTERM)
402 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
407 /* default behavior is to echo all the filenames */
408 *argv = (char*)"echo";
412 orig_arg_max = ARG_MAX;
413 if (orig_arg_max == -1)
414 orig_arg_max = LONG_MAX;
415 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
417 if (opt & OPT_UPTO_SIZE) {
418 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
419 for (i = 0; i < argc; i++) {
420 n_chars += strlen(*argv) + 1;
422 if (n_max_chars < n_chars) {
423 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
425 n_max_chars -= n_chars;
427 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
428 have it at 1 meg). Things will work fine with a large ARG_MAX but it
429 will probably hurt the system more than it needs to; an array of this
430 size is allocated. */
431 if (orig_arg_max > 20 * 1024)
432 orig_arg_max = 20 * 1024;
433 n_max_chars = orig_arg_max;
435 max_chars = xmalloc(n_max_chars);
437 if (opt & OPT_UPTO_NUMBER) {
438 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
440 n_max_arg = n_max_chars;
443 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
444 !(opt & OPT_NO_EMPTY))
449 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
450 for (cur = list; cur;) {
451 n_chars += cur->length;
454 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
455 if (opt & OPT_TERMINATE)
456 bb_error_msg_and_die("argument list too long");
461 for (cur = list; cur; cur = cur->link) {
462 n_chars += cur->length;
464 if (n_chars > n_max_chars || n == n_max_arg) {
468 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
470 /* allocate pointers for execvp:
471 argc*arg, n*arg from stdin, NULL */
472 args = xzalloc((n + argc + 1) * sizeof(char *));
474 /* store the command to be executed
475 (taken from the command line) */
476 for (i = 0; i < argc; i++)
478 /* (taken from stdin) */
479 for (cur = list; n; cur = cur->link) {
480 args[i++] = cur->data;
484 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
485 for (i = 0; args[i]; i++) {
488 fputs(args[i], stderr);
490 if (!(opt & OPT_INTERACTIVE))
493 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
494 child_error = xargs_exec(args);
498 for (i = argc; args[i]; i++) {
504 if (child_error > 0 && child_error != 123) {
508 if (ENABLE_FEATURE_CLEAN_UP)
516 const char *applet_name = "debug stuff usage";
518 void bb_show_usage(void)
520 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
525 int main(int argc, char **argv)
527 return xargs_main(argc, argv);