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_simple_perror_msg(args[0]);
61 return errno == ENOENT ? 127 : 126;
64 bb_error_msg("%s: exited with status 255; aborting", args[0]);
67 if (status >= 0x180) {
68 bb_error_msg("%s: terminated by signal %d",
69 args[0], status - 0x180);
78 typedef struct xlist_t {
84 static smallint eof_stdin_detected;
86 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
87 #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
88 || (c) == '\f' || (c) == '\v')
90 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
91 static xlist_t *process_stdin(xlist_t *list_arg,
92 const char *eof_str, size_t mc, char *buf)
99 char *s = NULL; /* start word */
100 char *p = NULL; /* pointer to end word */
101 char q = '\0'; /* quote char */
103 char eof_str_detected = 0;
104 size_t line_l = 0; /* size loaded args line */
105 int c; /* current char */
109 prev = cur = list_arg;
113 line_l += cur->length;
117 while (!eof_stdin_detected) {
120 eof_stdin_detected = 1;
125 if (eof_str_detected)
127 if (state == BACKSLASH) {
130 } else if (state == QUOTE) {
135 } else { /* if (state == NORM) */
148 } else if (c == '\'' || c == '"') {
153 if ((size_t)(p - buf) >= mc)
154 bb_error_msg_and_die("argument line too long");
159 if (state == SPACE) { /* word's delimiter or EOF detected */
161 bb_error_msg_and_die("unmatched %s quote",
162 q == '\'' ? "single" : "double");
166 eof_str_detected = (strcmp(s, eof_str) == 0);
168 if (!eof_str_detected) {
169 size_t length = (p - buf);
170 /* Dont xzalloc - it can be quite big */
171 cur = xmalloc(offsetof(xlist_t, xstr) + length);
173 cur->length = length;
174 memcpy(cur->xstr, s, length);
183 /* stop memory usage :-) */
194 /* The variant does not support single quotes, double quotes or backslash */
195 static xlist_t *process_stdin(xlist_t *list_arg,
196 const char *eof_str, size_t mc, char *buf)
199 int c; /* current char */
200 char eof_str_detected = 0;
201 char *s = NULL; /* start word */
202 char *p = NULL; /* pointer to end word */
203 size_t line_l = 0; /* size loaded args line */
207 prev = cur = list_arg;
211 line_l += cur->length;
215 while (!eof_stdin_detected) {
218 eof_stdin_detected = 1;
220 if (eof_str_detected)
222 if (c == EOF || ISSPACE(c)) {
229 if ((size_t)(p - buf) >= mc)
230 bb_error_msg_and_die("argument line too long");
231 *p++ = (c == EOF ? '\0' : c);
232 if (c == EOF) { /* word's delimiter or EOF detected */
235 eof_str_detected = (strcmp(s, eof_str) == 0);
237 if (!eof_str_detected) {
238 size_t length = (p - buf);
239 /* Dont xzalloc - it can be quite big */
240 cur = xmalloc(offsetof(xlist_t, xstr) + length);
242 cur->length = length;
243 memcpy(cur->xstr, s, length);
252 /* stop memory usage :-) */
261 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
264 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
265 /* Prompt the user for a response, and
266 if the user responds affirmatively, return true;
267 otherwise, return false. Uses "/dev/tty", not stdin. */
268 static int xargs_ask_confirmation(void)
273 tty_stream = xfopen_for_read(CURRENT_TTY);
274 fputs(" ?...", stderr);
276 c = savec = getc(tty_stream);
277 while (c != EOF && c != '\n')
278 c = getc(tty_stream);
280 return (savec == 'y' || savec == 'Y');
283 # define xargs_ask_confirmation() 1
284 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
286 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
287 static xlist_t *process0_stdin(xlist_t *list_arg,
288 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
290 int c; /* current char */
291 char *s = NULL; /* start word */
292 char *p = NULL; /* pointer to end word */
293 size_t line_l = 0; /* size loaded args line */
297 prev = cur = list_arg;
301 line_l += cur->length;
305 while (!eof_stdin_detected) {
308 eof_stdin_detected = 1;
315 if ((size_t)(p - buf) >= mc)
316 bb_error_msg_and_die("argument line too long");
318 if (c == '\0') { /* word's delimiter or EOF detected */
320 size_t length = (p - buf);
321 /* Dont xzalloc - it can be quite big */
322 cur = xmalloc(offsetof(xlist_t, xstr) + length);
324 cur->length = length;
325 memcpy(cur->xstr, s, length);
334 /* stop memory usage :-) */
342 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
344 /* Correct regardless of combination of CONFIG_xxx */
352 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
353 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
354 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
356 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
357 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
358 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
359 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
360 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
361 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
362 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
363 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
364 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
366 #define OPTION_STR "+trn:s:e::E:" \
367 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
368 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
369 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
371 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
372 int xargs_main(int argc, char **argv)
376 xlist_t *list = NULL;
379 char *max_args, *max_chars;
383 const char *eof_str = NULL;
386 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
387 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
389 #define read_args process_stdin
392 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
394 /* -E ""? You may wonder why not just omit -E?
395 * This is used for portability:
396 * old xargs was using "_" as default for -E / -e */
397 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
400 if (opt & OPT_ZEROTERM)
401 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
406 /* default behavior is to echo all the filenames */
407 *argv = (char*)"echo";
411 orig_arg_max = ARG_MAX;
412 if (orig_arg_max == -1)
413 orig_arg_max = LONG_MAX;
414 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
416 if (opt & OPT_UPTO_SIZE) {
417 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
418 for (i = 0; i < argc; i++) {
419 n_chars += strlen(*argv) + 1;
421 if (n_max_chars < n_chars) {
422 bb_error_msg_and_die("can't fit single argument within argument list size limit");
424 n_max_chars -= n_chars;
426 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
427 have it at 1 meg). Things will work fine with a large ARG_MAX but it
428 will probably hurt the system more than it needs to; an array of this
429 size is allocated. */
430 if (orig_arg_max > 20 * 1024)
431 orig_arg_max = 20 * 1024;
432 n_max_chars = orig_arg_max;
434 max_chars = xmalloc(n_max_chars);
436 if (opt & OPT_UPTO_NUMBER) {
437 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
439 n_max_arg = n_max_chars;
442 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
443 !(opt & OPT_NO_EMPTY))
448 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
449 for (cur = list; cur;) {
450 n_chars += cur->length;
453 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
454 if (opt & OPT_TERMINATE)
455 bb_error_msg_and_die("argument list too long");
460 for (cur = list; cur; cur = cur->link) {
461 n_chars += cur->length;
463 if (n_chars > n_max_chars || n == n_max_arg) {
467 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
469 /* allocate pointers for execvp:
470 argc*arg, n*arg from stdin, NULL */
471 args = xzalloc((n + argc + 1) * sizeof(char *));
473 /* store the command to be executed
474 (taken from the command line) */
475 for (i = 0; i < argc; i++)
477 /* (taken from stdin) */
478 for (cur = list; n; cur = cur->link) {
479 args[i++] = cur->xstr;
483 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
484 for (i = 0; args[i]; i++) {
487 fputs(args[i], stderr);
489 if (!(opt & OPT_INTERACTIVE))
492 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
493 child_error = xargs_exec(args);
497 for (i = argc; args[i]; i++) {
503 if (child_error > 0 && child_error != 123) {
507 if (ENABLE_FEATURE_CLEAN_UP)
515 const char *applet_name = "debug stuff usage";
517 void bb_show_usage(void)
519 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
524 int main(int argc, char **argv)
526 return xargs_main(argc, argv);