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 /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
126 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
128 #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
130 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
131 static xlist_t* process_stdin(xlist_t *list_arg,
132 const char *eof_str, size_t mc, char *buf)
138 char *s = NULL; /* start of the word */
139 char *p = NULL; /* pointer to end of the word */
140 char q = '\0'; /* quote char */
142 char eof_str_detected = 0;
143 size_t line_l = 0; /* size of loaded args */
147 prev = cur = list_arg;
150 line_l += cur->length;
161 if (eof_str_detected) /* skip till EOF */
163 if (state == BACKSLASH) {
167 if (state == QUOTE) {
172 } else { /* if (state == NORM) */
185 } else if (c == '\'' || c == '"') {
190 if ((size_t)(p - buf) >= mc)
191 bb_error_msg_and_die("argument line too long");
196 if (state == SPACE) { /* word's delimiter or EOF detected */
198 bb_error_msg_and_die("unmatched %s quote",
199 q == '\'' ? "single" : "double");
201 /* A full word is loaded */
203 eof_str_detected = (strcmp(s, eof_str) == 0);
205 if (!eof_str_detected) {
206 size_t length = (p - buf);
207 /* Dont xzalloc - it can be quite big */
208 cur = xmalloc(offsetof(xlist_t, xstr) + length);
210 cur->length = length;
211 memcpy(cur->xstr, s, length);
219 if (line_l > mc) /* limit stop memory usage */
229 /* The variant does not support single quotes, double quotes or backslash */
230 static xlist_t* process_stdin(xlist_t *list_arg,
231 const char *eof_str, size_t mc, char *buf)
233 char eof_str_detected = 0;
234 char *s = NULL; /* start of the word */
235 char *p = NULL; /* pointer to end of the word */
236 size_t line_l = 0; /* size of loaded args */
240 prev = cur = list_arg;
243 line_l += cur->length;
253 if (eof_str_detected) { /* skip till EOF */
256 if (c == EOF || ISSPACE(c)) {
263 if ((size_t)(p - buf) >= mc)
264 bb_error_msg_and_die("argument line too long");
265 *p++ = (c == EOF ? '\0' : c);
266 if (c == EOF) { /* word's delimiter or EOF detected */
267 /* A full word is loaded */
269 eof_str_detected = (strcmp(s, eof_str) == 0);
271 if (!eof_str_detected) {
272 size_t length = (p - buf);
273 /* Dont xzalloc - it can be quite big */
274 cur = xmalloc(offsetof(xlist_t, xstr) + length);
276 cur->length = length;
277 memcpy(cur->xstr, s, length);
285 if (line_l > mc) /* limit stop memory usage */
293 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
296 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
297 /* Prompt the user for a response, and
298 if the user responds affirmatively, return true;
299 otherwise, return false. Uses "/dev/tty", not stdin. */
300 static int xargs_ask_confirmation(void)
305 tty_stream = xfopen_for_read(CURRENT_TTY);
306 fputs(" ?...", stderr);
308 c = savec = getc(tty_stream);
309 while (c != EOF && c != '\n')
310 c = getc(tty_stream);
312 return (savec == 'y' || savec == 'Y');
315 # define xargs_ask_confirmation() 1
316 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
318 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
319 static xlist_t* process0_stdin(xlist_t *list_arg,
320 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
322 char *s = NULL; /* start of the word */
323 char *p = NULL; /* pointer to end of the word */
324 size_t line_l = 0; /* size of loaded args */
328 prev = cur = list_arg;
331 line_l += cur->length;
344 if ((size_t)(p - buf) >= mc)
345 bb_error_msg_and_die("argument line too long");
347 if (c == '\0') { /* word's delimiter or EOF detected */
348 /* A full word is loaded */
349 size_t length = (p - buf);
350 /* Dont xzalloc - it can be quite big */
351 cur = xmalloc(offsetof(xlist_t, xstr) + length);
353 cur->length = length;
354 memcpy(cur->xstr, s, length);
362 if (line_l > mc) /* limit stop memory usage */
369 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
371 /* Correct regardless of combination of CONFIG_xxx */
379 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
380 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
381 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
383 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
384 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
385 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
386 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
387 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
388 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
389 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
390 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
391 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
393 #define OPTION_STR "+trn:s:e::E:" \
394 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
395 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
396 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
398 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
399 int xargs_main(int argc, char **argv)
401 xlist_t *list = NULL;
407 const char *eof_str = NULL;
410 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
411 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
413 #define read_args process_stdin
416 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
418 /* -E ""? You may wonder why not just omit -E?
419 * This is used for portability:
420 * old xargs was using "_" as default for -E / -e */
421 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
424 if (opt & OPT_ZEROTERM)
425 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
430 /* default behavior is to echo all the filenames */
431 *--argv = (char*)"echo";
435 /* The Open Group Base Specifications Issue 6:
436 * "The xargs utility shall limit the command line length such that
437 * when the command line is invoked, the combined argument
438 * and environment lists (see the exec family of functions
439 * in the System Interfaces volume of IEEE Std 1003.1-2001)
440 * shall not exceed {ARG_MAX}-2048 bytes".
442 n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */
443 if (n_max_chars < 4*1024); /* paranoia */
444 n_max_chars = 4*1024;
446 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
447 * have it at 1 meg). Things will work fine with a large ARG_MAX
448 * but it will probably hurt the system more than it needs to;
449 * an array of this size is allocated.
451 if (n_max_chars > 20 * 1024)
452 n_max_chars = 20 * 1024;
454 if (opt & OPT_UPTO_SIZE) {
457 n_max_chars = xatoul_range(max_chars, 1, INT_MAX);
458 for (i = 0; argv[i]; i++) {
459 n_chars += strlen(argv[i]) + 1;
461 n_max_chars -= n_chars;
462 if ((ssize_t)n_max_chars <= 0) {
463 bb_error_msg_and_die("can't fit single argument within argument list size limit");
467 buf = xmalloc(n_max_chars);
469 if (opt & OPT_UPTO_NUMBER) {
470 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
472 n_max_arg = n_max_chars;
475 while ((list = read_args(list, eof_str, n_max_chars, buf)) != NULL
476 || !(opt & OPT_NO_EMPTY)
485 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
486 for (cur = list; cur;) {
487 n_chars += cur->length;
490 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
491 if (opt & OPT_TERMINATE)
492 bb_error_msg_and_die("argument list too long");
497 for (cur = list; cur; cur = cur->link) {
498 n_chars += cur->length;
500 if (n_chars > n_max_chars || n == n_max_arg) {
506 /* allocate pointers for execvp */
507 args = xzalloc(sizeof(args[0]) * (argc + n + 1));
509 /* store the command to be executed
510 * (taken from the command line) */
511 for (i = 0; argv[i]; i++)
513 /* (taken from stdin) */
514 for (cur = list; n; cur = cur->link) {
515 args[i++] = cur->xstr;
519 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
520 for (i = 0; args[i]; i++) {
522 bb_putchar_stderr(' ');
523 fputs(args[i], stderr);
525 if (!(opt & OPT_INTERACTIVE))
526 bb_putchar_stderr('\n');
529 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
530 child_error = xargs_exec(args);
534 for (i = argc; args[i]; i++) {
541 if (child_error > 0 && child_error != 123) {
546 if (ENABLE_FEATURE_CLEAN_UP)
555 const char *applet_name = "debug stuff usage";
557 void bb_show_usage(void)
559 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
564 int main(int argc, char **argv)
566 return xargs_main(argc, argv);