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_perror_msg("%s", 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 */
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) {
144 } else { /* if(state == NORM) */
158 } else if (c == '\'' || c == '"') {
163 if ((size_t)(p - buf) >= mc)
164 bb_error_msg_and_die("argument line too long");
169 if (state == SPACE) { /* word's delimiter or EOF detected */
171 bb_error_msg_and_die("unmatched %s quote",
172 q == '\'' ? "single" : "double");
176 eof_str_detected = (strcmp(s, eof_str) == 0);
178 if (!eof_str_detected) {
179 size_t length = (p - buf);
181 cur = xzalloc(sizeof(xlist_t) + length);
182 cur->data = memcpy(cur + 1, s, length);
183 cur->length = length;
184 /*cur->link = NULL;*/
193 /* stop memory usage :-) */
204 /* The variant does not support single quotes, double quotes or backslash */
205 static xlist_t *process_stdin(xlist_t *list_arg,
206 const char *eof_str, size_t mc, char *buf)
209 int c; /* current char */
210 char eof_str_detected = 0;
211 char *s = NULL; /* start word */
212 char *p = NULL; /* pointer to end word */
213 size_t line_l = 0; /* size loaded args line */
221 line_l += cur->length;
225 while (!eof_stdin_detected) {
228 eof_stdin_detected = 1;
230 if (eof_str_detected)
232 if (c == EOF || ISSPACE(c)) {
240 bb_error_msg_and_die("argument line too long");
241 *p++ = c == EOF ? 0 : c;
242 if (c == EOF) { /* word's delimiter or EOF detected */
245 eof_str_detected = (strcmp(s, eof_str) == 0);
247 if (!eof_str_detected) {
248 size_t length = (p - buf);
250 cur = xzalloc(sizeof(xlist_t) + length);
251 cur->data = memcpy(cur + 1, s, length);
252 cur->length = length;
253 /*cur->link = NULL;*/
262 /* stop memory usage :-) */
271 #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
274 #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
275 /* Prompt the user for a response, and
276 if the user responds affirmatively, return true;
277 otherwise, return false. Uses "/dev/tty", not stdin. */
278 static int xargs_ask_confirmation(void)
283 tty_stream = xfopen(CURRENT_TTY, "r");
284 fputs(" ?...", stderr);
286 c = savec = getc(tty_stream);
287 while (c != EOF && c != '\n')
288 c = getc(tty_stream);
290 return (savec == 'y' || savec == 'Y');
293 # define xargs_ask_confirmation() 1
294 #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
296 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
297 static xlist_t *process0_stdin(xlist_t *list_arg,
298 const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
300 int c; /* current char */
301 char *s = NULL; /* start word */
302 char *p = NULL; /* pointer to end word */
303 size_t line_l = 0; /* size loaded args line */
311 line_l += cur->length;
315 while (!eof_stdin_detected) {
318 eof_stdin_detected = 1;
325 if ((size_t)(p - buf) >= mc)
326 bb_error_msg_and_die("argument line too long");
328 if (c == 0) { /* word's delimiter or EOF detected */
330 size_t length = (p - buf);
332 cur = xzalloc(sizeof(xlist_t) + length);
333 cur->data = memcpy(cur + 1, s, length);
334 cur->length = length;
335 /*cur->link = NULL;*/
344 /* stop memory usage :-) */
352 #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
354 /* Correct regardless of combination of CONFIG_xxx */
361 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
362 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
363 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
365 OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
366 OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
367 OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
368 OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
369 OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
370 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
371 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
372 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
374 #define OPTION_STR "+trn:s:e::" \
375 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
376 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
377 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
379 int xargs_main(int argc, char **argv);
380 int xargs_main(int argc, char **argv)
384 xlist_t *list = NULL;
387 char *max_args, *max_chars;
391 const char *eof_str = "_";
394 #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
395 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
397 #define read_args process_stdin
400 opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str);
402 if (opt & OPT_ZEROTERM)
403 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
408 /* default behavior is to echo all the filenames */
409 *argv = (char*)"echo";
413 orig_arg_max = ARG_MAX;
414 if (orig_arg_max == -1)
415 orig_arg_max = LONG_MAX;
416 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
418 if (opt & OPT_UPTO_SIZE) {
419 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
420 for (i = 0; i < argc; i++) {
421 n_chars += strlen(*argv) + 1;
423 if (n_max_chars < n_chars) {
424 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
426 n_max_chars -= n_chars;
428 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
429 have it at 1 meg). Things will work fine with a large ARG_MAX but it
430 will probably hurt the system more than it needs to; an array of this
431 size is allocated. */
432 if (orig_arg_max > 20 * 1024)
433 orig_arg_max = 20 * 1024;
434 n_max_chars = orig_arg_max;
436 max_chars = xmalloc(n_max_chars);
438 if (opt & OPT_UPTO_NUMBER) {
439 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
441 n_max_arg = n_max_chars;
444 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
445 !(opt & OPT_NO_EMPTY))
450 #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
451 for (cur = list; cur;) {
452 n_chars += cur->length;
455 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
456 if (opt & OPT_TERMINATE)
457 bb_error_msg_and_die("argument list too long");
462 for (cur = list; cur; cur = cur->link) {
463 n_chars += cur->length;
465 if (n_chars > n_max_chars || n == n_max_arg) {
469 #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
471 /* allocate pointers for execvp:
472 argc*arg, n*arg from stdin, NULL */
473 args = xzalloc((n + argc + 1) * sizeof(char *));
475 /* store the command to be executed
476 (taken from the command line) */
477 for (i = 0; i < argc; i++)
479 /* (taken from stdin) */
480 for (cur = list; n; cur = cur->link) {
481 args[i++] = cur->data;
485 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
486 for (i = 0; args[i]; i++) {
489 fputs(args[i], stderr);
491 if (!(opt & OPT_INTERACTIVE))
494 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
495 child_error = xargs_exec(args);
499 for (i = argc; args[i]; i++) {
505 if (child_error > 0 && child_error != 123) {
509 if (ENABLE_FEATURE_CLEAN_UP)
517 const char *applet_name = "debug stuff usage";
519 void bb_show_usage(void)
521 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
526 int main(int argc, char **argv)
528 return xargs_main(argc, argv);