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
18 //config: bool "xargs (6.7 kb)"
21 //config: xargs is used to execute a specified command for
22 //config: every item from standard input.
24 //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
25 //config: bool "Enable -p: prompt and confirmation"
27 //config: depends on XARGS
29 //config: Support -p: prompt the user whether to run each command
30 //config: line and read a line from the terminal.
32 //config:config FEATURE_XARGS_SUPPORT_QUOTES
33 //config: bool "Enable single and double quotes and backslash"
35 //config: depends on XARGS
37 //config: Support quoting in the input.
39 //config:config FEATURE_XARGS_SUPPORT_TERMOPT
40 //config: bool "Enable -x: exit if -s or -n is exceeded"
42 //config: depends on XARGS
44 //config: Support -x: exit if the command size (see the -s or -n option)
45 //config: is exceeded.
47 //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
48 //config: bool "Enable -0: NUL-terminated input"
50 //config: depends on XARGS
52 //config: Support -0: input items are terminated by a NUL character
53 //config: instead of whitespace, and the quotes and backslash
54 //config: are not special.
56 //config:config FEATURE_XARGS_SUPPORT_REPL_STR
57 //config: bool "Enable -I STR: string to replace"
59 //config: depends on XARGS
61 //config: Support -I STR and -i[STR] options.
63 //config:config FEATURE_XARGS_SUPPORT_PARALLEL
64 //config: bool "Enable -P N: processes to run in parallel"
66 //config: depends on XARGS
68 //config:config FEATURE_XARGS_SUPPORT_ARGS_FILE
69 //config: bool "Enable -a FILE: use FILE instead of stdin"
71 //config: depends on XARGS
73 //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
75 //kbuild:lib-$(CONFIG_XARGS) += xargs.o
78 #include "common_bufsiz.h"
80 /* This is a NOEXEC applet. Be very careful! */
83 //#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
84 #define dbg_msg(...) ((void)0)
88 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
89 # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
91 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
92 # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
94 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
95 # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
97 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
98 # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
105 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
107 const char *repl_str;
112 #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
116 smalluint xargs_exitcode;
118 #define G (*(struct globals*)bb_common_bufsiz1)
119 #define INIT_G() do { \
120 setup_common_bufsiz(); \
121 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
123 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
124 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
125 G.xargs_exitcode = 0; \
126 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
127 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
132 * Returns 0 if xargs should continue (but may set G.xargs_exitcode to 123).
133 * Else sets G.xargs_exitcode to error code and returns nonzero.
135 * If G.max_procs == 0, performs final waitpid() loop for all children.
137 static int xargs_exec(void)
141 #if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
142 status = spawn_and_wait(G.args);
144 if (G.max_procs == 1) {
145 status = spawn_and_wait(G.args);
150 if (G.running_procs >= G.max_procs)
151 pid = safe_waitpid(-1, &wstat, 0);
153 pid = wait_any_nohang(&wstat);
155 /* We may have children we don't know about:
156 * sh -c 'sleep 1 & exec xargs ...'
157 * Do not make G.running_procs go negative.
159 if (G.running_procs != 0)
161 status = WIFSIGNALED(wstat)
162 ? 0x180 + WTERMSIG(wstat)
163 : WEXITSTATUS(wstat);
164 if (status > 0 && status < 255) {
165 /* See below why 123 does not abort */
166 G.xargs_exitcode = 123;
170 goto again; /* maybe we have more children? */
171 /* else: "bad" status, will bail out */
172 } else if (G.max_procs != 0) {
173 /* Not in final waitpid() loop,
174 * and G.running_procs < G.max_procs: start more procs
176 status = spawn(G.args);
177 /* here "status" actually holds pid, or -1 */
182 /* else: status == -1 (failed to fork or exec) */
184 /* final waitpid() loop: must be ECHILD "no more children" */
190 * """xargs exits with the following status:
192 * 123 if any invocation of the command exited with status 1-125
193 * 124 if the command exited with status 255
194 * ("""If any invocation of the command exits with a status of 255,
195 * xargs will stop immediately without reading any further input.
196 * An error message is issued on stderr when this happens.""")
197 * 125 if the command is killed by a signal
198 * 126 if the command cannot be run
199 * 127 if the command is not found
200 * 1 if some other error occurred."""
203 bb_simple_perror_msg(G.args[0]);
204 status = (errno == ENOENT) ? 127 : 126;
206 else if (status >= 0x180) {
207 bb_error_msg("'%s' terminated by signal %d",
208 G.args[0], status - 0x180);
211 else if (status != 0) {
213 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
216 /* "123 if any invocation of the command exited with status 1-125"
217 * This implies that nonzero exit code is remembered,
218 * but does not cause xargs to stop: we return 0.
220 G.xargs_exitcode = 123;
225 G.xargs_exitcode = status;
229 /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
230 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
232 #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
234 static void store_param(char *s)
236 /* Grow by 256 elements at once */
237 if (!(G.idx & 0xff)) { /* G.idx == N*256? */
238 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
239 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
245 * Read characters into buf[n_max_chars+1], and when parameter delimiter
246 * is seen, store the address of a new parameter to args[].
247 * If reading discovers that last chars do not form the complete
248 * parameter, the pointer to the first such "tail character" is returned.
249 * (buf has extra byte at the end to accommodate terminating NUL
250 * of "tail characters" string).
251 * Otherwise, the returned pointer points to NUL byte.
252 * On entry, buf[] may contain some "seed chars" which are to become
253 * the beginning of the first parameter.
256 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
257 static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
263 char q = '\0'; /* quote char */
265 char *s = buf; /* start of the word */
266 char *p = s + strlen(buf); /* end of the word */
268 buf += n_max_chars; /* past buffer's end */
270 /* "goto ret" is used instead of "break" to make control flow
280 if (state == BACKSLASH) {
284 if (state == QUOTE) {
289 } else { /* if (state == NORM) */
300 } else if (c == '\'' || c == '"') {
309 if (state == SPACE) { /* word's delimiter or EOF detected */
311 bb_error_msg_and_die("unmatched %s quote",
312 q == '\'' ? "single" : "double");
314 /* A full word is loaded */
316 if (strcmp(s, G.eof_str) == 0) {
317 while (getchar() != EOF)
324 dbg_msg("args[]:'%s'", s);
327 if (n_max_arg == 0) {
338 /* store_param(NULL) - caller will do it */
339 dbg_msg("return:'%s'", s);
343 /* The variant does not support single quotes, double quotes or backslash */
344 static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
346 char *s = buf; /* start of the word */
347 char *p = s + strlen(buf); /* end of the word */
349 buf += n_max_chars; /* past buffer's end */
357 if (c == EOF || ISSPACE(c)) {
362 *p++ = (c == EOF ? '\0' : c);
363 if (c == EOF) { /* word's delimiter or EOF detected */
364 /* A full word is loaded */
366 if (strcmp(s, G.eof_str) == 0) {
367 while (getchar() != EOF)
374 dbg_msg("args[]:'%s'", s);
377 if (n_max_arg == 0) {
387 /* store_param(NULL) - caller will do it */
388 dbg_msg("return:'%s'", s);
391 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
393 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
394 static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
396 char *s = buf; /* start of the word */
397 char *p = s + strlen(buf); /* end of the word */
399 buf += n_max_chars; /* past buffer's end */
409 if (c == '\0') { /* NUL or EOF detected */
410 /* A full word is loaded */
412 dbg_msg("args[]:'%s'", s);
415 if (n_max_arg == 0) {
425 /* store_param(NULL) - caller will do it */
426 dbg_msg("return:'%s'", s);
429 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
431 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
433 * Used if -I<repl> was specified.
434 * In this mode, words aren't appended to PROG ARGS.
435 * Instead, entire input line is read, then <repl> string
436 * in every PROG and ARG is replaced with the line:
437 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
438 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
439 * -n MAX_ARGS seems to be ignored.
440 * Tested with GNU findutils 4.5.10.
442 //FIXME: n_max_chars is not handled the same way as in GNU findutils.
443 //FIXME: quoting is not implemented.
444 static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
449 /* Free strings from last invocation, if any */
450 for (i = 0; G.args && G.args[i]; i++)
451 if (G.args[i] != G.argv[i])
454 end = buf + n_max_chars;
459 if (c == EOF || c == G.eol_ch) {
461 goto ret; /* empty line */
465 if (c == '\0') { /* EOL or EOF detected */
468 char *arg = G.argv[i];
469 int count = count_strstr(arg, G.repl_str);
471 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
473 dbg_msg("args[]:'%s'", arg);
485 /* store_param(NULL) - caller will do it */
486 dbg_msg("return:'%s'", buf);
491 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
492 /* Prompt the user for a response, and
493 * if user responds affirmatively, return true;
494 * otherwise, return false. Uses "/dev/tty", not stdin.
496 static int xargs_ask_confirmation(void)
501 tty_stream = xfopen_for_read(CURRENT_TTY);
502 fputs(" ?...", stderr);
504 c = savec = getc(tty_stream);
505 while (c != EOF && c != '\n')
506 c = getc(tty_stream);
508 return (savec == 'y' || savec == 'Y');
511 # define xargs_ask_confirmation() 1
514 //usage:#define xargs_trivial_usage
515 //usage: "[OPTIONS] [PROG ARGS]"
516 //usage:#define xargs_full_usage "\n\n"
517 //usage: "Run PROG on every item given by stdin\n"
518 //usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
519 //usage: "\n -p Ask user whether to run each command"
521 //usage: "\n -r Don't run command if input is empty"
522 //usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
523 //usage: "\n -0 Input is separated by NUL characters"
525 //usage: IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
526 //usage: "\n -a FILE Read from FILE instead of stdin"
528 //usage: "\n -t Print the command on stderr before execution"
529 //usage: "\n -e[STR] STR stops input processing"
530 //usage: "\n -n N Pass no more than N args to PROG"
531 //usage: "\n -s N Pass command line of no more than N bytes"
532 //usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
533 //usage: "\n -I STR Replace STR within PROG ARGS with input line"
535 //usage: IF_FEATURE_XARGS_SUPPORT_PARALLEL(
536 //usage: "\n -P N Run up to N PROGs in parallel"
538 //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
539 //usage: "\n -x Exit if size is exceeded"
541 //usage:#define xargs_example_usage
542 //usage: "$ ls | xargs gzip\n"
543 //usage: "$ find . -name '*.c' -print | xargs rm\n"
545 /* Correct regardless of combination of CONFIG_xxx */
553 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
554 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
555 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
556 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
557 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
559 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
560 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
561 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
562 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
563 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
564 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
565 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
566 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
567 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
568 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
569 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
571 #define OPTION_STR "+trn:s:e::E:" \
572 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
573 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
574 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
575 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \
576 IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \
577 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:")
579 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
580 int xargs_main(int argc UNUSED_PARAM, char **argv)
590 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
591 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
592 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
594 #define read_args process_stdin
596 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
600 opt = getopt32long(argv, OPTION_STR,
601 "no-run-if-empty\0" No_argument "r",
602 &max_args, &max_chars, &G.eof_str, &G.eof_str
603 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
604 IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
605 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
608 #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
609 if (G.max_procs <= 0) /* -P0 means "run lots of them" */
610 G.max_procs = 100; /* let's not go crazy high */
613 #if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
615 xmove_fd(xopen(opt_a, O_RDONLY), 0);
618 /* -E ""? You may wonder why not just omit -E?
619 * This is used for portability:
620 * old xargs was using "_" as default for -E / -e */
621 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
624 if (opt & OPT_ZEROTERM) {
625 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
626 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
632 /* default behavior is to echo all the filenames */
633 *--argv = (char*)"echo";
638 * The Open Group Base Specifications Issue 6:
639 * "The xargs utility shall limit the command line length such that
640 * when the command line is invoked, the combined argument
641 * and environment lists (see the exec family of functions
642 * in the System Interfaces volume of IEEE Std 1003.1-2001)
643 * shall not exceed {ARG_MAX}-2048 bytes".
645 n_max_chars = bb_arg_max();
646 if (n_max_chars > 32 * 1024)
647 n_max_chars = 32 * 1024;
649 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
650 * so that the process may safely modify its environment.
654 if (opt & OPT_UPTO_SIZE) {
655 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
657 /* Account for prepended fixed arguments */
660 for (i = 0; argv[i]; i++) {
661 n_chars += strlen(argv[i]) + 1;
663 n_max_chars -= n_chars;
666 if (n_max_chars <= 0) {
667 bb_error_msg_and_die("can't fit single argument within argument list size limit");
670 buf = xzalloc(n_max_chars + 1);
672 n_max_arg = n_max_chars;
673 if (opt & OPT_UPTO_NUMBER) {
674 n_max_arg = xatou_range(max_args, 1, INT_MAX);
675 /* Not necessary, we use growable args[]: */
676 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
679 #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
680 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
683 * Unmodified args are kept in G.argv[i],
684 * G.args[i] receives malloced G.argv[i] with <str> replaced
685 * with input line. Setting this up:
689 read_args = process_stdin_with_replace;
690 /* Make -I imply -r. GNU findutils seems to do the same: */
691 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
696 /* Store the command to be executed, part 1.
697 * We can statically allocate (argc + n_max_arg + 1) elements
698 * and do not bother with resizing args[], but on 64-bit machines
699 * this results in args[] vector which is ~8 times bigger
700 * than n_max_chars! That is, with n_max_chars == 20k,
701 * args[] will take 160k (!), which will most likely be
702 * almost entirely unused.
704 for (i = 0; argv[i]; i++)
705 store_param(argv[i]);
713 rem = read_args(n_max_chars, n_max_arg, buf);
716 if (!G.args[initial_idx]) { /* not even one ARG was added? */
718 bb_error_msg_and_die("argument line too long");
719 if (opt & OPT_NO_EMPTY)
724 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
725 const char *fmt = " %s" + 1;
726 char **args = G.args;
727 for (i = 0; args[i]; i++) {
728 fprintf(stderr, fmt, args[i]);
731 if (!(opt & OPT_INTERACTIVE))
732 bb_putchar_stderr('\n');
735 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
736 if (xargs_exec() != 0)
737 break; /* G.xargs_exitcode is set by xargs_exec() */
740 overlapping_strcpy(buf, rem);
743 if (ENABLE_FEATURE_CLEAN_UP) {
748 #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
750 xargs_exec(); /* final waitpid() loop */
753 return G.xargs_exitcode;
759 const char *applet_name = "debug stuff usage";
761 void bb_show_usage(void)
763 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
768 int main(int argc, char **argv)
770 return xargs_main(argc, argv);