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
20 //kbuild:lib-$(CONFIG_XARGS) += xargs.o
23 //config: bool "xargs"
26 //config: xargs is used to execute a specified command for
27 //config: every item from standard input.
29 //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
30 //config: bool "Enable -p: prompt and confirmation"
32 //config: depends on XARGS
34 //config: Support -p: prompt the user whether to run each command
35 //config: line and read a line from the terminal.
37 //config:config FEATURE_XARGS_SUPPORT_QUOTES
38 //config: bool "Enable single and double quotes and backslash"
40 //config: depends on XARGS
42 //config: Support quoting in the input.
44 //config:config FEATURE_XARGS_SUPPORT_TERMOPT
45 //config: bool "Enable -x: exit if -s or -n is exceeded"
47 //config: depends on XARGS
49 //config: Support -x: exit if the command size (see the -s or -n option)
50 //config: is exceeded.
52 //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
53 //config: bool "Enable -0: NUL-terminated input"
55 //config: depends on XARGS
57 //config: Support -0: input items are terminated by a NUL character
58 //config: instead of whitespace, and the quotes and backslash
59 //config: are not special.
63 /* This is a NOEXEC applet. Be very careful! */
66 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
67 We try to make it as large as possible. */
68 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
69 # define ARG_MAX sysconf(_SC_ARG_MAX)
77 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
78 # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
80 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
81 # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
83 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
84 # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
86 # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
87 # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
92 This function has special algorithm.
93 Don't use fork and include to main!
95 static int xargs_exec(char **args)
99 status = spawn_and_wait(args);
101 bb_simple_perror_msg(args[0]);
102 return errno == ENOENT ? 127 : 126;
105 bb_error_msg("%s: exited with status 255; aborting", args[0]);
108 if (status >= 0x180) {
109 bb_error_msg("%s: terminated by signal %d",
110 args[0], status - 0x180);
119 typedef struct xlist_t {
120 struct xlist_t *link;
125 static smallint eof_stdin_detected;
127 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
128 #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
129 || (c) == '\f' || (c) == '\v')
131 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
132 static xlist_t *process_stdin(xlist_t *list_arg,
133 const char *eof_str, size_t mc, char *buf)
140 char *s = NULL; /* start word */
141 char *p = NULL; /* pointer to end word */
142 char q = '\0'; /* quote char */
144 char eof_str_detected = 0;
145 size_t line_l = 0; /* size loaded args line */
146 int c; /* current char */
150 prev = cur = list_arg;
154 line_l += cur->length;
158 while (!eof_stdin_detected) {
161 eof_stdin_detected = 1;
166 if (eof_str_detected)
168 if (state == BACKSLASH) {
171 } else if (state == QUOTE) {
176 } else { /* if (state == NORM) */
189 } else if (c == '\'' || c == '"') {
194 if ((size_t)(p - buf) >= mc)
195 bb_error_msg_and_die("argument line too long");
200 if (state == SPACE) { /* word's delimiter or EOF detected */
202 bb_error_msg_and_die("unmatched %s quote",
203 q == '\'' ? "single" : "double");
207 eof_str_detected = (strcmp(s, eof_str) == 0);
209 if (!eof_str_detected) {
210 size_t length = (p - buf);
211 /* Dont xzalloc - it can be quite big */
212 cur = xmalloc(offsetof(xlist_t, xstr) + length);
214 cur->length = length;
215 memcpy(cur->xstr, s, length);
224 /* stop memory usage :-) */
235 /* The variant does not support single quotes, double quotes or backslash */
236 static xlist_t *process_stdin(xlist_t *list_arg,
237 const char *eof_str, size_t mc, char *buf)
240 int c; /* current char */
241 char eof_str_detected = 0;
242 char *s = NULL; /* start word */
243 char *p = NULL; /* pointer to end word */
244 size_t line_l = 0; /* size loaded args line */
248 prev = cur = list_arg;
252 line_l += cur->length;
256 while (!eof_stdin_detected) {
259 eof_stdin_detected = 1;
261 if (eof_str_detected)
263 if (c == EOF || ISSPACE(c)) {
270 if ((size_t)(p - buf) >= mc)
271 bb_error_msg_and_die("argument line too long");
272 *p++ = (c == EOF ? '\0' : c);
273 if (c == EOF) { /* word's delimiter or EOF detected */
276 eof_str_detected = (strcmp(s, eof_str) == 0);
278 if (!eof_str_detected) {
279 size_t length = (p - buf);
280 /* Dont xzalloc - it can be quite big */
281 cur = xmalloc(offsetof(xlist_t, xstr) + length);
283 cur->length = length;
284 memcpy(cur->xstr, s, length);
293 /* stop memory usage :-) */
302 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
305 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
306 /* Prompt the user for a response, and
307 if the user responds affirmatively, return true;
308 otherwise, return false. Uses "/dev/tty", not stdin. */
309 static int xargs_ask_confirmation(void)
314 tty_stream = xfopen_for_read(CURRENT_TTY);
315 fputs(" ?...", stderr);
317 c = savec = getc(tty_stream);
318 while (c != EOF && c != '\n')
319 c = getc(tty_stream);
321 return (savec == 'y' || savec == 'Y');
324 # define xargs_ask_confirmation() 1
325 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
327 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
328 static xlist_t *process0_stdin(xlist_t *list_arg,
329 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
331 int c; /* current char */
332 char *s = NULL; /* start word */
333 char *p = NULL; /* pointer to end word */
334 size_t line_l = 0; /* size loaded args line */
338 prev = cur = list_arg;
342 line_l += cur->length;
346 while (!eof_stdin_detected) {
349 eof_stdin_detected = 1;
356 if ((size_t)(p - buf) >= mc)
357 bb_error_msg_and_die("argument line too long");
359 if (c == '\0') { /* word's delimiter or EOF detected */
361 size_t length = (p - buf);
362 /* Dont xzalloc - it can be quite big */
363 cur = xmalloc(offsetof(xlist_t, xstr) + length);
365 cur->length = length;
366 memcpy(cur->xstr, s, length);
375 /* stop memory usage :-) */
383 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
385 /* Correct regardless of combination of CONFIG_xxx */
393 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
394 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
395 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
397 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
398 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
399 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
400 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
401 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
402 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
403 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
404 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
405 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
407 #define OPTION_STR "+trn:s:e::E:" \
408 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
409 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
410 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
412 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
413 int xargs_main(int argc, char **argv)
417 xlist_t *list = NULL;
420 char *max_args, *max_chars;
422 const char *eof_str = NULL;
425 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
426 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
428 #define read_args process_stdin
431 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
433 /* -E ""? You may wonder why not just omit -E?
434 * This is used for portability:
435 * old xargs was using "_" as default for -E / -e */
436 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
439 if (opt & OPT_ZEROTERM)
440 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
445 /* default behavior is to echo all the filenames */
446 *argv = (char*)"echo";
450 n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */
451 if (n_max_chars < 4*1024); /* paranoia */
452 n_max_chars = LONG_MAX;
453 /* The Open Group Base Specifications Issue 6:
454 * "The xargs utility shall limit the command line length such that
455 * when the command line is invoked, the combined argument
456 * and environment lists (see the exec family of functions
457 * in the System Interfaces volume of IEEE Std 1003.1-2001)
458 * shall not exceed {ARG_MAX}-2048 bytes".
461 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
462 * have it at 1 meg). Things will work fine with a large ARG_MAX but it
463 * will probably hurt the system more than it needs to; an array of this
466 if (n_max_chars > 20 * 1024)
467 n_max_chars = 20 * 1024;
469 if (opt & OPT_UPTO_SIZE) {
471 n_max_chars = xatoul_range(max_chars, 1, n_max_chars);
472 for (i = 0; i < argc; i++) {
473 n_chars += strlen(*argv) + 1;
475 if (n_max_chars <= n_chars) {
476 bb_error_msg_and_die("can't fit single argument within argument list size limit");
478 n_max_chars -= n_chars;
480 max_chars = xmalloc(n_max_chars);
482 if (opt & OPT_UPTO_NUMBER) {
483 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
485 n_max_arg = n_max_chars;
488 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
489 !(opt & OPT_NO_EMPTY))
494 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
495 for (cur = list; cur;) {
496 n_chars += cur->length;
499 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
500 if (opt & OPT_TERMINATE)
501 bb_error_msg_and_die("argument list too long");
506 for (cur = list; cur; cur = cur->link) {
507 n_chars += cur->length;
509 if (n_chars > n_max_chars || n == n_max_arg) {
513 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
515 /* allocate pointers for execvp:
516 argc*arg, n*arg from stdin, NULL */
517 args = xzalloc((n + argc + 1) * sizeof(char *));
519 /* store the command to be executed
520 (taken from the command line) */
521 for (i = 0; i < argc; i++)
523 /* (taken from stdin) */
524 for (cur = list; n; cur = cur->link) {
525 args[i++] = cur->xstr;
529 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
530 for (i = 0; args[i]; i++) {
533 fputs(args[i], stderr);
535 if (!(opt & OPT_INTERACTIVE))
538 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
539 child_error = xargs_exec(args);
543 for (i = argc; args[i]; i++) {
549 if (child_error > 0 && child_error != 123) {
553 if (ENABLE_FEATURE_CLEAN_UP)
561 const char *applet_name = "debug stuff usage";
563 void bb_show_usage(void)
565 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
570 int main(int argc, char **argv)
572 return xargs_main(argc, argv);