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(CURRENT_TTY, "r");
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 ATTRIBUTE_UNUSED, 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 */
358 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
359 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
360 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
362 OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
363 OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
364 OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
365 OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
366 OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
367 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
368 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
369 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
371 #define OPTION_STR "+trn:s:e::" \
372 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
373 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
374 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
376 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
377 int xargs_main(int argc, char **argv)
381 xlist_t *list = NULL;
384 char *max_args, *max_chars;
388 const char *eof_str = "_";
391 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
392 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
394 #define read_args process_stdin
397 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str);
399 if (opt & OPT_ZEROTERM)
400 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
405 /* default behavior is to echo all the filenames */
406 *argv = (char*)"echo";
410 orig_arg_max = ARG_MAX;
411 if (orig_arg_max == -1)
412 orig_arg_max = LONG_MAX;
413 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
415 if (opt & OPT_UPTO_SIZE) {
416 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
417 for (i = 0; i < argc; i++) {
418 n_chars += strlen(*argv) + 1;
420 if (n_max_chars < n_chars) {
421 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
423 n_max_chars -= n_chars;
425 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
426 have it at 1 meg). Things will work fine with a large ARG_MAX but it
427 will probably hurt the system more than it needs to; an array of this
428 size is allocated. */
429 if (orig_arg_max > 20 * 1024)
430 orig_arg_max = 20 * 1024;
431 n_max_chars = orig_arg_max;
433 max_chars = xmalloc(n_max_chars);
435 if (opt & OPT_UPTO_NUMBER) {
436 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
438 n_max_arg = n_max_chars;
441 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
442 !(opt & OPT_NO_EMPTY))
447 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
448 for (cur = list; cur;) {
449 n_chars += cur->length;
452 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
453 if (opt & OPT_TERMINATE)
454 bb_error_msg_and_die("argument list too long");
459 for (cur = list; cur; cur = cur->link) {
460 n_chars += cur->length;
462 if (n_chars > n_max_chars || n == n_max_arg) {
466 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
468 /* allocate pointers for execvp:
469 argc*arg, n*arg from stdin, NULL */
470 args = xzalloc((n + argc + 1) * sizeof(char *));
472 /* store the command to be executed
473 (taken from the command line) */
474 for (i = 0; i < argc; i++)
476 /* (taken from stdin) */
477 for (cur = list; n; cur = cur->link) {
478 args[i++] = cur->xstr;
482 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
483 for (i = 0; args[i]; i++) {
486 fputs(args[i], stderr);
488 if (!(opt & OPT_INTERACTIVE))
491 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
492 child_error = xargs_exec(args);
496 for (i = argc; args[i]; i++) {
502 if (child_error > 0 && child_error != 123) {
506 if (ENABLE_FEATURE_CLEAN_UP)
514 const char *applet_name = "debug stuff usage";
516 void bb_show_usage(void)
518 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
523 int main(int argc, char **argv)
525 return xargs_main(argc, argv);