1 /* vi: set sw=4 ts=4: */
3 * Mini xargs implementation for busybox
5 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
8 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
9 * - Mike Rendell <michael@cs.mun.ca>
10 * and David MacKenzie <djm@gnu.ai.mit.edu>.
12 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 * xargs is described in the Single Unix Specification v3 at
15 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
19 //config: bool "xargs"
22 //config: xargs is used to execute a specified command for
23 //config: every item from standard input.
25 //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
26 //config: bool "Enable -p: prompt and confirmation"
28 //config: depends on XARGS
30 //config: Support -p: prompt the user whether to run each command
31 //config: line and read a line from the terminal.
33 //config:config FEATURE_XARGS_SUPPORT_QUOTES
34 //config: bool "Enable single and double quotes and backslash"
36 //config: depends on XARGS
38 //config: Support quoting in the input.
40 //config:config FEATURE_XARGS_SUPPORT_TERMOPT
41 //config: bool "Enable -x: exit if -s or -n is exceeded"
43 //config: depends on XARGS
45 //config: Support -x: exit if the command size (see the -s or -n option)
46 //config: is exceeded.
48 //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
49 //config: bool "Enable -0: NUL-terminated input"
51 //config: depends on XARGS
53 //config: Support -0: input items are terminated by a NUL character
54 //config: instead of whitespace, and the quotes and backslash
55 //config: are not special.
57 //config:config FEATURE_XARGS_SUPPORT_REPL_STR
58 //config: bool "Enable -I STR: string to replace"
60 //config: depends on XARGS
62 //config: Support -I STR and -i[STR] options.
64 //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
66 //kbuild:lib-$(CONFIG_XARGS) += xargs.o
70 /* This is a NOEXEC applet. Be very careful! */
73 //#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
74 #define dbg_msg(...) ((void)0)
78 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
79 # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
81 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
82 # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
84 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
85 # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
87 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
88 # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
95 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
103 #define G (*(struct globals*)&bb_common_bufsiz1)
104 #define INIT_G() do { \
105 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
106 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
107 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
111 static int xargs_exec(void)
115 status = spawn_and_wait(G.args);
117 bb_simple_perror_msg(G.args[0]);
118 return errno == ENOENT ? 127 : 126;
121 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
124 if (status >= 0x180) {
125 bb_error_msg("%s: terminated by signal %d",
126 G.args[0], status - 0x180);
134 /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
135 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
137 #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
139 static void store_param(char *s)
141 /* Grow by 256 elements at once */
142 if (!(G.idx & 0xff)) { /* G.idx == N*256 */
143 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
144 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
150 * Read characters into buf[n_max_chars+1], and when parameter delimiter
151 * is seen, store the address of a new parameter to args[].
152 * If reading discovers that last chars do not form the complete
153 * parameter, the pointer to the first such "tail character" is returned.
154 * (buf has extra byte at the end to accomodate terminating NUL
155 * of "tail characters" string).
156 * Otherwise, the returned pointer points to NUL byte.
157 * On entry, buf[] may contain some "seed chars" which are to become
158 * the beginning of the first parameter.
161 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
162 static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
168 char q = '\0'; /* quote char */
170 char *s = buf; /* start of the word */
171 char *p = s + strlen(buf); /* end of the word */
173 buf += n_max_chars; /* past buffer's end */
175 /* "goto ret" is used instead of "break" to make control flow
185 if (state == BACKSLASH) {
189 if (state == QUOTE) {
194 } else { /* if (state == NORM) */
205 } else if (c == '\'' || c == '"') {
214 if (state == SPACE) { /* word's delimiter or EOF detected */
216 bb_error_msg_and_die("unmatched %s quote",
217 q == '\'' ? "single" : "double");
219 /* A full word is loaded */
221 if (strcmp(s, G.eof_str) == 0) {
222 while (getchar() != EOF)
229 dbg_msg("args[]:'%s'", s);
232 if (n_max_arg == 0) {
243 /* store_param(NULL) - caller will do it */
244 dbg_msg("return:'%s'", s);
248 /* The variant does not support single quotes, double quotes or backslash */
249 static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
251 char *s = buf; /* start of the word */
252 char *p = s + strlen(buf); /* end of the word */
254 buf += n_max_chars; /* past buffer's end */
262 if (c == EOF || ISSPACE(c)) {
267 *p++ = (c == EOF ? '\0' : c);
268 if (c == EOF) { /* word's delimiter or EOF detected */
269 /* A full word is loaded */
271 if (strcmp(s, G.eof_str) == 0) {
272 while (getchar() != EOF)
279 dbg_msg("args[]:'%s'", s);
282 if (n_max_arg == 0) {
292 /* store_param(NULL) - caller will do it */
293 dbg_msg("return:'%s'", s);
296 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
298 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
299 static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
301 char *s = buf; /* start of the word */
302 char *p = s + strlen(buf); /* end of the word */
304 buf += n_max_chars; /* past buffer's end */
314 if (c == '\0') { /* NUL or EOF detected */
315 /* A full word is loaded */
317 dbg_msg("args[]:'%s'", s);
320 if (n_max_arg == 0) {
330 /* store_param(NULL) - caller will do it */
331 dbg_msg("return:'%s'", s);
334 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
336 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
338 * Used if -I<repl> was specified.
339 * In this mode, words aren't appended to PROG ARGS.
340 * Instead, entire input line is read, then <repl> string
341 * in every PROG and ARG is replaced with the line:
342 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
343 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
344 * -n MAX_ARGS seems to be ignored.
345 * Tested with GNU findutils 4.5.10.
347 //FIXME: n_max_chars is not handled the same way as in GNU findutils.
348 //FIXME: quoting is not implemented.
349 static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
354 /* Free strings from last invocation, if any */
355 for (i = 0; G.args && G.args[i]; i++)
356 if (G.args[i] != G.argv[i])
359 end = buf + n_max_chars;
364 if (c == EOF || c == G.eol_ch) {
366 goto ret; /* empty line */
370 if (c == '\0') { /* EOL or EOF detected */
373 char *arg = G.argv[i];
374 int count = count_strstr(arg, G.repl_str);
376 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
378 dbg_msg("args[]:'%s'", arg);
390 /* store_param(NULL) - caller will do it */
391 dbg_msg("return:'%s'", buf);
396 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
397 /* Prompt the user for a response, and
398 * if user responds affirmatively, return true;
399 * otherwise, return false. Uses "/dev/tty", not stdin.
401 static int xargs_ask_confirmation(void)
406 tty_stream = xfopen_for_read(CURRENT_TTY);
407 fputs(" ?...", stderr);
409 c = savec = getc(tty_stream);
410 while (c != EOF && c != '\n')
411 c = getc(tty_stream);
413 return (savec == 'y' || savec == 'Y');
416 # define xargs_ask_confirmation() 1
419 //usage:#define xargs_trivial_usage
420 //usage: "[OPTIONS] [PROG ARGS]"
421 //usage:#define xargs_full_usage "\n\n"
422 //usage: "Run PROG on every item given by stdin\n"
423 //usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
424 //usage: "\n -p Ask user whether to run each command"
426 //usage: "\n -r Don't run command if input is empty"
427 //usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
428 //usage: "\n -0 Input is separated by NUL characters"
430 //usage: "\n -t Print the command on stderr before execution"
431 //usage: "\n -e[STR] STR stops input processing"
432 //usage: "\n -n N Pass no more than N args to PROG"
433 //usage: "\n -s N Pass command line of no more than N bytes"
434 //usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
435 //usage: "\n -I STR Replace STR within PROG ARGS with input line"
437 //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
438 //usage: "\n -x Exit if size is exceeded"
440 //usage:#define xargs_example_usage
441 //usage: "$ ls | xargs gzip\n"
442 //usage: "$ find . -name '*.c' -print | xargs rm\n"
444 /* Correct regardless of combination of CONFIG_xxx */
452 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
453 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
454 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
455 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
456 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
458 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
459 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
460 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
461 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
462 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
463 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
464 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
465 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
466 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
467 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
468 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
470 #define OPTION_STR "+trn:s:e::E:" \
471 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
472 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
473 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
474 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::")
476 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
477 int xargs_main(int argc, char **argv)
487 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
488 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
489 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
491 #define read_args process_stdin
496 #if ENABLE_DESKTOP && ENABLE_LONG_OPTS
497 /* For example, Fedora's build system uses --no-run-if-empty */
498 applet_long_options =
499 "no-run-if-empty\0" No_argument "r"
502 opt = getopt32(argv, OPTION_STR,
503 &max_args, &max_chars, &G.eof_str, &G.eof_str
504 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
507 /* -E ""? You may wonder why not just omit -E?
508 * This is used for portability:
509 * old xargs was using "_" as default for -E / -e */
510 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
513 if (opt & OPT_ZEROTERM) {
514 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
515 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
521 /* default behavior is to echo all the filenames */
522 *--argv = (char*)"echo";
527 * The Open Group Base Specifications Issue 6:
528 * "The xargs utility shall limit the command line length such that
529 * when the command line is invoked, the combined argument
530 * and environment lists (see the exec family of functions
531 * in the System Interfaces volume of IEEE Std 1003.1-2001)
532 * shall not exceed {ARG_MAX}-2048 bytes".
534 n_max_chars = bb_arg_max();
535 if (n_max_chars > 32 * 1024)
536 n_max_chars = 32 * 1024;
538 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
539 * so that the process may safely modify its environment.
543 if (opt & OPT_UPTO_SIZE) {
544 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
546 /* Account for prepended fixed arguments */
549 for (i = 0; argv[i]; i++) {
550 n_chars += strlen(argv[i]) + 1;
552 n_max_chars -= n_chars;
555 if (n_max_chars <= 0) {
556 bb_error_msg_and_die("can't fit single argument within argument list size limit");
559 buf = xzalloc(n_max_chars + 1);
561 n_max_arg = n_max_chars;
562 if (opt & OPT_UPTO_NUMBER) {
563 n_max_arg = xatou_range(max_args, 1, INT_MAX);
564 /* Not necessary, we use growable args[]: */
565 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
568 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
569 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
572 * Unmodified args are kept in G.argv[i],
573 * G.args[i] receives malloced G.argv[i] with <str> replaced
574 * with input line. Setting this up:
579 read_args = process_stdin_with_replace;
583 /* Allocate pointers for execvp.
584 * We can statically allocate (argc + n_max_arg + 1) elements
585 * and do not bother with resizing args[], but on 64-bit machines
586 * this results in args[] vector which is ~8 times bigger
587 * than n_max_chars! That is, with n_max_chars == 20k,
588 * args[] will take 160k (!), which will most likely be
589 * almost entirely unused.
591 * See store_param() for matching 256-step growth logic
593 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
594 /* Store the command to be executed, part 1 */
595 for (i = 0; argv[i]; i++)
603 rem = read_args(n_max_chars, n_max_arg, buf);
608 bb_error_msg_and_die("argument line too long");
609 if (opt & OPT_NO_EMPTY)
614 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
615 const char *fmt = " %s" + 1;
616 char **args = G.args;
617 for (i = 0; args[i]; i++) {
618 fprintf(stderr, fmt, args[i]);
621 if (!(opt & OPT_INTERACTIVE))
622 bb_putchar_stderr('\n');
625 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
626 child_error = xargs_exec();
629 if (child_error > 0 && child_error != 123) {
633 overlapping_strcpy(buf, rem);
636 if (ENABLE_FEATURE_CLEAN_UP) {
647 const char *applet_name = "debug stuff usage";
649 void bb_show_usage(void)
651 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
656 int main(int argc, char **argv)
658 return xargs_main(argc, argv);