2 * Mini xargs implementation for busybox
3 * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
5 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
8 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
9 * - Mike Rendell <michael@cs.mun.ca>
10 * and David MacKenzie <djm@gnu.ai.mit.edu>.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * xargs is described in the Single Unix Specification v3 at
27 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
38 #include <sys/types.h>
42 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
43 We try to make it as large as possible. */
44 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
45 #define ARG_MAX sysconf (_SC_ARG_MAX)
53 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
54 # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
56 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
57 # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
59 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
60 # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
62 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
63 # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
68 This function have special algorithm.
69 Don`t use fork and include to main!
71 static int xargs_exec(char *const *args)
74 volatile int exec_errno = 0; /* shared vfork stack */
76 if ((p = vfork()) >= 0) {
79 execvp(args[0], args);
80 exec_errno = errno; /* set error to shared stack */
86 while (wait(&status) == (pid_t) - 1)
91 bb_perror_msg("%s", args[0]);
92 return exec_errno == ENOENT ? 127 : 126;
94 if (WEXITSTATUS(status) == 255) {
95 bb_error_msg("%s: exited with status 255; aborting", args[0]);
98 if (WIFSTOPPED(status)) {
99 bb_error_msg("%s: stopped by signal %d",
100 args[0], WSTOPSIG(status));
103 if (WIFSIGNALED(status)) {
104 bb_error_msg("%s: terminated by signal %d",
105 args[0], WTERMSIG(status));
108 if (WEXITSTATUS(status) != 0)
114 bb_perror_msg_and_die("vfork");
119 typedef struct xlist_s {
122 struct xlist_s *link;
125 static int eof_stdin_detected;
127 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
128 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
129 || (c) == '\f' || (c) == '\v')
131 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
132 static xlist_t *process_stdin(xlist_t * list_arg,
133 const char *eof_str, size_t mc, char *buf)
140 char *s = NULL; /* start word */
141 char *p = NULL; /* pointer to end word */
142 char q = 0; /* quote char */
144 char eof_str_detected = 0;
145 size_t line_l = 0; /* size loaded args line */
146 int c; /* current char */
150 for (prev = cur = list_arg; cur; cur = cur->link) {
151 line_l += cur->lenght; /* previous allocated */
156 while (!eof_stdin_detected) {
159 eof_stdin_detected++;
164 if (eof_str_detected)
166 if (state == BACKSLASH) {
169 } else if (state == QUOTE) {
176 } else { /* if(state == NORM) */
190 } else if (c == '\'' || c == '"') {
196 bb_error_msg_and_die("argument line too long");
201 if (state == SPACE) { /* word's delimiter or EOF detected */
203 bb_error_msg_and_die("unmatched %s quote",
204 q == '\'' ? "single" : "double");
208 eof_str_detected = strcmp(s, eof_str) == 0;
210 if (!eof_str_detected) {
211 size_t lenght = (p - buf);
213 cur = xmalloc(sizeof(xlist_t) + lenght);
214 cur->data = memcpy(cur + 1, s, lenght);
215 cur->lenght = lenght;
225 /* stop memory usage :-) */
236 /* The variant does not support single quotes, double quotes or backslash */
237 static xlist_t *process_stdin(xlist_t * list_arg,
238 const char *eof_str, size_t mc, char *buf)
241 int c; /* current char */
242 int eof_str_detected = 0;
243 char *s = NULL; /* start word */
244 char *p = NULL; /* pointer to end word */
245 size_t line_l = 0; /* size loaded args line */
249 for (prev = cur = list_arg; cur; cur = cur->link) {
250 line_l += cur->lenght; /* previous allocated */
255 while (!eof_stdin_detected) {
258 eof_stdin_detected++;
260 if (eof_str_detected)
262 if (c == EOF || ISSPACE(c)) {
270 bb_error_msg_and_die("argument line too long");
271 *p++ = c == EOF ? 0 : c;
272 if (c == EOF) { /* word's delimiter or EOF detected */
275 eof_str_detected = strcmp(s, eof_str) == 0;
277 if (!eof_str_detected) {
278 size_t lenght = (p - buf);
280 cur = xmalloc(sizeof(xlist_t) + lenght);
281 cur->data = memcpy(cur + 1, s, lenght);
282 cur->lenght = lenght;
292 /* stop memory usage :-) */
301 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
304 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
305 /* Prompt the user for a response, and
306 if the user responds affirmatively, return true;
307 otherwise, return false. Used "/dev/tty", not stdin. */
308 static int xargs_ask_confirmation(void)
310 static FILE *tty_stream;
314 tty_stream = fopen("/dev/tty", "r");
316 bb_perror_msg_and_die("/dev/tty");
317 /* pranoidal security by vodz */
318 fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
320 fputs(" ?...", stderr);
322 c = savec = getc(tty_stream);
323 while (c != EOF && c != '\n')
324 c = getc(tty_stream);
325 if (savec == 'y' || savec == 'Y')
333 # define xargs_ask_confirmation() 1
334 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
336 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
342 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
343 static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str,
344 size_t mc, char *buf)
346 int c; /* current char */
347 char *s = NULL; /* start word */
348 char *p = NULL; /* pointer to end word */
349 size_t line_l = 0; /* size loaded args line */
353 for (prev = cur = list_arg; cur; cur = cur->link) {
354 line_l += cur->lenght; /* previous allocated */
359 while (!eof_stdin_detected) {
362 eof_stdin_detected++;
370 bb_error_msg_and_die("argument line too long");
372 if (c == 0) { /* word's delimiter or EOF detected */
374 size_t lenght = (p - buf);
376 cur = xmalloc(sizeof(xlist_t) + lenght);
377 cur->data = memcpy(cur + 1, s, lenght);
378 cur->lenght = lenght;
388 /* stop memory usage :-) */
397 # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc)
398 # define OPT_INC_0 1 /* future use */
400 # define OPT_INC_0 0 /* future use */
401 # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc)
402 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
405 #define OPT_VERBOSE (1<<0)
406 #define OPT_NO_EMPTY (1<<1)
407 #define OPT_UPTO_NUMBER (1<<2)
408 #define OPT_UPTO_SIZE (1<<3)
409 #define OPT_EOF_STRING (1<<4)
410 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
411 #define OPT_INTERACTIVE (1<<5)
413 #define OPT_INTERACTIVE (0) /* require for algorithm &| */
415 #define OPT_TERMINATE (1<<(5+OPT_INC_P))
416 #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X))
418 #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0))
421 int xargs_main(int argc, char **argv)
425 xlist_t *list = NULL;
428 char *max_args, *max_chars;
432 const char *eof_str = "_";
436 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
437 xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin;
440 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
441 bb_opt_complementaly = "pt";
444 opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::"
445 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
448 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
451 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
454 ,&max_args, &max_chars, &eof_str);
459 /* default behavior is to echo all the filenames */
464 orig_arg_max = ARG_MAX;
465 if (orig_arg_max == -1)
466 orig_arg_max = LONG_MAX;
467 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
468 if ((opt & OPT_UPTO_SIZE)) {
469 n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max);
470 for (i = 0; i < a; i++) {
471 n_chars += strlen(*argv) + 1;
473 if (n_max_chars < n_chars) {
474 bb_error_msg_and_die("can not fit single argument within argument list size limit");
476 n_max_chars -= n_chars;
478 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
479 have it at 1 meg). Things will work fine with a large ARG_MAX but it
480 will probably hurt the system more than it needs to; an array of this
481 size is allocated. */
482 if (orig_arg_max > 20 * 1024)
483 orig_arg_max = 20 * 1024;
484 n_max_chars = orig_arg_max;
486 max_chars = xmalloc(n_max_chars);
488 if ((opt & OPT_UPTO_NUMBER)) {
489 n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX);
491 n_max_arg = n_max_chars;
494 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
495 if (opt & OPT_ZEROTERM)
496 read_args = process0_stdin;
499 while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL ||
500 (opt & OPT_NO_EMPTY) == 0)
505 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
506 for (cur = list; cur;) {
507 n_chars += cur->lenght;
510 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
511 if (opt & OPT_TERMINATE)
512 bb_error_msg_and_die("argument list too long");
517 for (cur = list; cur; cur = cur->link) {
518 n_chars += cur->lenght;
520 if (n_chars > n_max_chars || n == n_max_arg) {
524 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
526 /* allocating pointers for execvp:
527 a*arg, n*arg from stdin, NULL */
528 args = xcalloc(n + a + 1, sizeof(char *));
530 /* Store the command to be executed
531 (taken from the command line) */
532 for (i = 0; i < a; i++)
534 /* (taken from stdin) */
535 for (cur = list; n; cur = cur->link) {
536 args[i++] = cur->data;
540 if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) {
541 for (i = 0; args[i]; i++) {
544 fputs(args[i], stderr);
546 if ((opt & OPT_INTERACTIVE) == 0)
549 if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) {
550 child_error = xargs_exec(args);
554 for (i = a; args[i]; i++) {
560 if (child_error > 0 && child_error != 123) {
564 #ifdef CONFIG_FEATURE_CLEAN_UP
573 const char *bb_applet_name = "debug stuff usage";
575 void bb_show_usage(void)
577 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
582 int main(int argc, char **argv)
584 return xargs_main(argc, argv);