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 /* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
68 if (WIFSTOPPED(status)) {
69 bb_error_msg("%s: stopped by signal %d",
70 args[0], WSTOPSIG(status));
75 bb_error_msg("%s: terminated by signal %d",
76 args[0], status - 1000);
85 typedef struct xlist_t {
91 static smallint eof_stdin_detected;
93 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
94 #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
95 || (c) == '\f' || (c) == '\v')
97 #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
98 static xlist_t *process_stdin(xlist_t *list_arg,
99 const char *eof_str, size_t mc, char *buf)
106 char *s = NULL; /* start word */
107 char *p = NULL; /* pointer to end word */
108 char q = '\0'; /* quote char */
110 char eof_str_detected = 0;
111 size_t line_l = 0; /* size loaded args line */
112 int c; /* current char */
116 prev = cur = list_arg;
120 line_l += cur->length;
124 while (!eof_stdin_detected) {
127 eof_stdin_detected = 1;
132 if (eof_str_detected)
134 if (state == BACKSLASH) {
137 } else if (state == QUOTE) {
142 } else { /* if (state == NORM) */
155 } else if (c == '\'' || c == '"') {
160 if ((size_t)(p - buf) >= mc)
161 bb_error_msg_and_die("argument line too long");
166 if (state == SPACE) { /* word's delimiter or EOF detected */
168 bb_error_msg_and_die("unmatched %s quote",
169 q == '\'' ? "single" : "double");
173 eof_str_detected = (strcmp(s, eof_str) == 0);
175 if (!eof_str_detected) {
176 size_t length = (p - buf);
177 /* Dont xzalloc - it can be quite big */
178 cur = xmalloc(offsetof(xlist_t, xstr) + length);
180 cur->length = length;
181 memcpy(cur->xstr, s, length);
190 /* stop memory usage :-) */
201 /* The variant does not support single quotes, double quotes or backslash */
202 static xlist_t *process_stdin(xlist_t *list_arg,
203 const char *eof_str, size_t mc, char *buf)
206 int c; /* current char */
207 char eof_str_detected = 0;
208 char *s = NULL; /* start word */
209 char *p = NULL; /* pointer to end word */
210 size_t line_l = 0; /* size loaded args line */
214 prev = cur = list_arg;
218 line_l += cur->length;
222 while (!eof_stdin_detected) {
225 eof_stdin_detected = 1;
227 if (eof_str_detected)
229 if (c == EOF || ISSPACE(c)) {
236 if ((size_t)(p - buf) >= mc)
237 bb_error_msg_and_die("argument line too long");
238 *p++ = (c == EOF ? '\0' : c);
239 if (c == EOF) { /* word's delimiter or EOF detected */
242 eof_str_detected = (strcmp(s, eof_str) == 0);
244 if (!eof_str_detected) {
245 size_t length = (p - buf);
246 /* Dont xzalloc - it can be quite big */
247 cur = xmalloc(offsetof(xlist_t, xstr) + length);
249 cur->length = length;
250 memcpy(cur->xstr, s, length);
259 /* stop memory usage :-) */
268 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
271 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
272 /* Prompt the user for a response, and
273 if the user responds affirmatively, return true;
274 otherwise, return false. Uses "/dev/tty", not stdin. */
275 static int xargs_ask_confirmation(void)
280 tty_stream = xfopen_for_read(CURRENT_TTY);
281 fputs(" ?...", stderr);
283 c = savec = getc(tty_stream);
284 while (c != EOF && c != '\n')
285 c = getc(tty_stream);
287 return (savec == 'y' || savec == 'Y');
290 # define xargs_ask_confirmation() 1
291 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
293 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
294 static xlist_t *process0_stdin(xlist_t *list_arg,
295 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
297 int c; /* current char */
298 char *s = NULL; /* start word */
299 char *p = NULL; /* pointer to end word */
300 size_t line_l = 0; /* size loaded args line */
304 prev = cur = list_arg;
308 line_l += cur->length;
312 while (!eof_stdin_detected) {
315 eof_stdin_detected = 1;
322 if ((size_t)(p - buf) >= mc)
323 bb_error_msg_and_die("argument line too long");
325 if (c == '\0') { /* word's delimiter or EOF detected */
327 size_t length = (p - buf);
328 /* Dont xzalloc - it can be quite big */
329 cur = xmalloc(offsetof(xlist_t, xstr) + length);
331 cur->length = length;
332 memcpy(cur->xstr, s, length);
341 /* stop memory usage :-) */
349 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
351 /* Correct regardless of combination of CONFIG_xxx */
359 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
360 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
361 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
363 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
364 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
365 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
366 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
367 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
368 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
369 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
370 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
371 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
373 #define OPTION_STR "+trn:s:e::E:" \
374 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
375 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
376 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
378 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
379 int xargs_main(int argc, char **argv)
383 xlist_t *list = NULL;
386 char *max_args, *max_chars;
390 const char *eof_str = NULL;
393 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
394 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
396 #define read_args process_stdin
399 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
401 /* -E ""? You may wonder why not just omit -E?
402 * This is used for portability:
403 * old xargs was using "_" as default for -E / -e */
404 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
407 if (opt & OPT_ZEROTERM)
408 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
413 /* default behavior is to echo all the filenames */
414 *argv = (char*)"echo";
418 orig_arg_max = ARG_MAX;
419 if (orig_arg_max == -1)
420 orig_arg_max = LONG_MAX;
421 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
423 if (opt & OPT_UPTO_SIZE) {
424 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
425 for (i = 0; i < argc; i++) {
426 n_chars += strlen(*argv) + 1;
428 if (n_max_chars < n_chars) {
429 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
431 n_max_chars -= n_chars;
433 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
434 have it at 1 meg). Things will work fine with a large ARG_MAX but it
435 will probably hurt the system more than it needs to; an array of this
436 size is allocated. */
437 if (orig_arg_max > 20 * 1024)
438 orig_arg_max = 20 * 1024;
439 n_max_chars = orig_arg_max;
441 max_chars = xmalloc(n_max_chars);
443 if (opt & OPT_UPTO_NUMBER) {
444 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
446 n_max_arg = n_max_chars;
449 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
450 !(opt & OPT_NO_EMPTY))
455 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
456 for (cur = list; cur;) {
457 n_chars += cur->length;
460 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
461 if (opt & OPT_TERMINATE)
462 bb_error_msg_and_die("argument list too long");
467 for (cur = list; cur; cur = cur->link) {
468 n_chars += cur->length;
470 if (n_chars > n_max_chars || n == n_max_arg) {
474 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
476 /* allocate pointers for execvp:
477 argc*arg, n*arg from stdin, NULL */
478 args = xzalloc((n + argc + 1) * sizeof(char *));
480 /* store the command to be executed
481 (taken from the command line) */
482 for (i = 0; i < argc; i++)
484 /* (taken from stdin) */
485 for (cur = list; n; cur = cur->link) {
486 args[i++] = cur->xstr;
490 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
491 for (i = 0; args[i]; i++) {
494 fputs(args[i], stderr);
496 if (!(opt & OPT_INTERACTIVE))
499 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
500 child_error = xargs_exec(args);
504 for (i = argc; args[i]; i++) {
510 if (child_error > 0 && child_error != 123) {
514 if (ENABLE_FEATURE_CLEAN_UP)
522 const char *applet_name = "debug stuff usage";
524 void bb_show_usage(void)
526 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
531 int main(int argc, char **argv)
533 return xargs_main(argc, argv);