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 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
23 We try to make it as large as possible. */
24 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
25 #define ARG_MAX sysconf (_SC_ARG_MAX)
33 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
34 # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
36 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
37 # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
39 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
40 # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
42 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
43 # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
48 This function have special algorithm.
49 Don`t use fork and include to main!
51 static int xargs_exec(char *const *args)
54 volatile int exec_errno = 0; /* shared vfork stack */
56 if ((p = vfork()) >= 0) {
59 execvp(args[0], args);
60 exec_errno = errno; /* set error to shared stack */
66 while (wait(&status) == (pid_t) - 1)
71 bb_perror_msg("%s", args[0]);
72 return exec_errno == ENOENT ? 127 : 126;
74 if (WEXITSTATUS(status) == 255) {
75 bb_error_msg("%s: exited with status 255; aborting", args[0]);
78 if (WIFSTOPPED(status)) {
79 bb_error_msg("%s: stopped by signal %d",
80 args[0], WSTOPSIG(status));
83 if (WIFSIGNALED(status)) {
84 bb_error_msg("%s: terminated by signal %d",
85 args[0], WTERMSIG(status));
88 if (WEXITSTATUS(status) != 0)
94 bb_perror_msg_and_die("vfork");
99 typedef struct xlist_s {
102 struct xlist_s *link;
105 static int eof_stdin_detected;
107 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
108 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
109 || (c) == '\f' || (c) == '\v')
111 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
112 static xlist_t *process_stdin(xlist_t * list_arg,
113 const char *eof_str, size_t mc, char *buf)
120 char *s = NULL; /* start word */
121 char *p = NULL; /* pointer to end word */
122 char q = 0; /* quote char */
124 char eof_str_detected = 0;
125 size_t line_l = 0; /* size loaded args line */
126 int c; /* current char */
130 for (prev = cur = list_arg; cur; cur = cur->link) {
131 line_l += cur->lenght; /* previous allocated */
136 while (!eof_stdin_detected) {
139 eof_stdin_detected++;
144 if (eof_str_detected)
146 if (state == BACKSLASH) {
149 } else if (state == QUOTE) {
156 } else { /* if(state == NORM) */
170 } else if (c == '\'' || c == '"') {
175 if ((size_t)(p - buf) >= mc)
176 bb_error_msg_and_die("argument line too long");
181 if (state == SPACE) { /* word's delimiter or EOF detected */
183 bb_error_msg_and_die("unmatched %s quote",
184 q == '\'' ? "single" : "double");
188 eof_str_detected = strcmp(s, eof_str) == 0;
190 if (!eof_str_detected) {
191 size_t lenght = (p - buf);
193 cur = xmalloc(sizeof(xlist_t) + lenght);
194 cur->data = memcpy(cur + 1, s, lenght);
195 cur->lenght = lenght;
205 /* stop memory usage :-) */
216 /* The variant does not support single quotes, double quotes or backslash */
217 static xlist_t *process_stdin(xlist_t * list_arg,
218 const char *eof_str, size_t mc, char *buf)
221 int c; /* current char */
222 int eof_str_detected = 0;
223 char *s = NULL; /* start word */
224 char *p = NULL; /* pointer to end word */
225 size_t line_l = 0; /* size loaded args line */
229 for (prev = cur = list_arg; cur; cur = cur->link) {
230 line_l += cur->lenght; /* previous allocated */
235 while (!eof_stdin_detected) {
238 eof_stdin_detected++;
240 if (eof_str_detected)
242 if (c == EOF || ISSPACE(c)) {
250 bb_error_msg_and_die("argument line too long");
251 *p++ = c == EOF ? 0 : c;
252 if (c == EOF) { /* word's delimiter or EOF detected */
255 eof_str_detected = strcmp(s, eof_str) == 0;
257 if (!eof_str_detected) {
258 size_t lenght = (p - buf);
260 cur = xmalloc(sizeof(xlist_t) + lenght);
261 cur->data = memcpy(cur + 1, s, lenght);
262 cur->lenght = lenght;
272 /* stop memory usage :-) */
281 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
284 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
285 /* Prompt the user for a response, and
286 if the user responds affirmatively, return true;
287 otherwise, return false. Used "/dev/tty", not stdin. */
288 static int xargs_ask_confirmation(void)
290 static FILE *tty_stream;
294 tty_stream = xfopen(CURRENT_TTY, "r");
295 /* pranoidal security by vodz */
296 fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
298 fputs(" ?...", stderr);
300 c = savec = getc(tty_stream);
301 while (c != EOF && c != '\n')
302 c = getc(tty_stream);
303 if (savec == 'y' || savec == 'Y')
311 # define xargs_ask_confirmation() 1
312 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
314 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
320 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
321 static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED,
322 size_t mc, char *buf)
324 int c; /* current char */
325 char *s = NULL; /* start word */
326 char *p = NULL; /* pointer to end word */
327 size_t line_l = 0; /* size loaded args line */
331 for (prev = cur = list_arg; cur; cur = cur->link) {
332 line_l += cur->lenght; /* previous allocated */
337 while (!eof_stdin_detected) {
340 eof_stdin_detected++;
347 if ((size_t)(p - buf) >= mc)
348 bb_error_msg_and_die("argument line too long");
350 if (c == 0) { /* word's delimiter or EOF detected */
352 size_t lenght = (p - buf);
354 cur = xmalloc(sizeof(xlist_t) + lenght);
355 cur->data = memcpy(cur + 1, s, lenght);
356 cur->lenght = lenght;
366 /* stop memory usage :-) */
375 # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc)
376 # define OPT_INC_0 1 /* future use */
378 # define OPT_INC_0 0 /* future use */
379 # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc)
380 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
383 #define OPT_VERBOSE (1<<0)
384 #define OPT_NO_EMPTY (1<<1)
385 #define OPT_UPTO_NUMBER (1<<2)
386 #define OPT_UPTO_SIZE (1<<3)
387 #define OPT_EOF_STRING (1<<4)
388 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
389 #define OPT_INTERACTIVE (1<<5)
391 #define OPT_INTERACTIVE (0) /* require for algorithm &| */
393 #define OPT_TERMINATE (1<<(5+OPT_INC_P))
394 #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X))
396 #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0))
399 int xargs_main(int argc, char **argv)
403 xlist_t *list = NULL;
406 char *max_args, *max_chars;
410 const char *eof_str = "_";
414 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
415 xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin;
418 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
419 bb_opt_complementally = "pt";
422 opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::"
423 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
426 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
429 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
432 ,&max_args, &max_chars, &eof_str);
437 /* default behavior is to echo all the filenames */
442 orig_arg_max = ARG_MAX;
443 if (orig_arg_max == -1)
444 orig_arg_max = LONG_MAX;
445 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
446 if ((opt & OPT_UPTO_SIZE)) {
447 n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max);
448 for (i = 0; i < a; i++) {
449 n_chars += strlen(*argv) + 1;
451 if (n_max_chars < n_chars) {
452 bb_error_msg_and_die("can not fit single argument within argument list size limit");
454 n_max_chars -= n_chars;
456 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
457 have it at 1 meg). Things will work fine with a large ARG_MAX but it
458 will probably hurt the system more than it needs to; an array of this
459 size is allocated. */
460 if (orig_arg_max > 20 * 1024)
461 orig_arg_max = 20 * 1024;
462 n_max_chars = orig_arg_max;
464 max_chars = xmalloc(n_max_chars);
466 if ((opt & OPT_UPTO_NUMBER)) {
467 n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX);
469 n_max_arg = n_max_chars;
472 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
473 if (opt & OPT_ZEROTERM)
474 read_args = process0_stdin;
477 while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL ||
478 (opt & OPT_NO_EMPTY) == 0)
483 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
484 for (cur = list; cur;) {
485 n_chars += cur->lenght;
488 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
489 if (opt & OPT_TERMINATE)
490 bb_error_msg_and_die("argument list too long");
495 for (cur = list; cur; cur = cur->link) {
496 n_chars += cur->lenght;
498 if (n_chars > n_max_chars || n == n_max_arg) {
502 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
504 /* allocating pointers for execvp:
505 a*arg, n*arg from stdin, NULL */
506 args = xzalloc((n + a + 1) * sizeof(char *));
508 /* Store the command to be executed
509 (taken from the command line) */
510 for (i = 0; i < a; i++)
512 /* (taken from stdin) */
513 for (cur = list; n; cur = cur->link) {
514 args[i++] = cur->data;
518 if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) {
519 for (i = 0; args[i]; i++) {
522 fputs(args[i], stderr);
524 if ((opt & OPT_INTERACTIVE) == 0)
527 if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) {
528 child_error = xargs_exec(args);
532 for (i = a; args[i]; i++) {
538 if (child_error > 0 && child_error != 123) {
542 #ifdef CONFIG_FEATURE_CLEAN_UP
551 const char *bb_applet_name = "debug stuff usage";
553 void bb_show_usage(void)
555 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
560 int main(int argc, char **argv)
562 return xargs_main(argc, argv);