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 stimul to rewrote :)
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
35 #include <sys/types.h>
39 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
40 We try to make it as large as possible. */
41 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
42 #define ARG_MAX sysconf (_SC_ARG_MAX)
50 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
51 # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
53 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
54 # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
56 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
57 # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
59 # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
60 # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
65 This function have special algorithm.
66 Don`t use fork and include to main!
69 xargs_exec (char *const *args)
72 volatile int exec_errno = 0; /* shared vfork stack */
74 if ((p = vfork ()) >= 0) {
77 execvp (args[0], args);
78 exec_errno = errno; /* set error to shared stack */
84 while (wait (&status) == (pid_t) - 1)
89 bb_perror_msg ("%s", args[0]);
90 return exec_errno == ENOENT ? 127 : 126;
92 if (WEXITSTATUS (status) == 255) {
93 bb_error_msg ("%s: exited with status 255; aborting", args[0]);
96 if (WIFSTOPPED (status)) {
97 bb_error_msg ("%s: stopped by signal %d", args[0],
101 if (WIFSIGNALED (status)) {
102 bb_error_msg ("%s: terminated by signal %d", args[0],
106 if (WEXITSTATUS (status) != 0)
112 bb_perror_msg_and_die ("vfork");
117 typedef struct xlist_s {
120 struct xlist_s *link;
123 static int eof_stdin_detected;
125 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
126 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
127 || (c) == '\f' || (c) == '\v')
129 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
131 process_stdin (xlist_t *list_arg, const char *eof_str, size_t mc, char *buf)
138 char *s = NULL; /* start word */
139 char *p = NULL; /* pointer to end word */
140 char q = 0; /* quote char */
142 char eof_str_detected = 0;
143 size_t line_l = 0; /* size loaded args line */
144 int c; /* current char */
148 for(prev = cur = list_arg; cur; cur = cur->link) {
149 line_l += cur->lenght; /* previous allocated */
154 while(!eof_stdin_detected) {
157 eof_stdin_detected++;
164 if (state == BACKSLASH) {
167 } else if (state == QUOTE) {
174 } else /* if(state == NORM) */ {
187 } else if (c == '\'' || c == '"') {
193 bb_error_msg_and_die ("argument line too long");
198 if (state == SPACE) { /* word's delimiter or EOF detected */
200 bb_error_msg_and_die ("unmatched %s quote",
201 q == '\'' ? "single" : "double");
204 eof_str_detected = strcmp(s, eof_str) == 0;
206 if(!eof_str_detected) {
207 size_t lenght = (p-buf);
209 cur = xmalloc(sizeof(xlist_t) + lenght);
210 cur->data = memcpy(cur + 1, s, lenght);
211 cur->lenght = lenght;
221 /* stop memory usage :-) */
232 /* The variant is unsupport single, double quotes and backslash */
234 process_stdin (xlist_t *list_arg, const char *eof_str, size_t mc, char *buf)
237 int c; /* current char */
238 int eof_str_detected = 0;
239 char *s = NULL; /* start word */
240 char *p = NULL; /* pointer to end word */
241 size_t line_l = 0; /* size loaded args line */
245 for(prev = cur = list_arg; cur; cur = cur->link) {
246 line_l += cur->lenght; /* previous allocated */
251 while(!eof_stdin_detected) {
254 eof_stdin_detected++;
258 if (c == EOF || ISSPACE (c)) {
266 bb_error_msg_and_die ("argument line too long");
267 *p++ = c == EOF ? 0 : c;
268 if (c == EOF) { /* word's delimiter or EOF detected */
271 eof_str_detected = strcmp(s, eof_str) == 0;
273 if(!eof_str_detected) {
274 size_t lenght = (p-buf);
276 cur = xmalloc(sizeof(xlist_t) + lenght);
277 cur->data = memcpy(cur + 1, s, lenght);
278 cur->lenght = lenght;
288 /* stop memory usage :-) */
297 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
300 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
301 /* Prompt the user for a response, and
302 if the user responds affirmatively, return true;
303 otherwise, return false. Used "/dev/tty", not stdin. */
305 xargs_ask_confirmation (void)
307 static FILE *tty_stream;
311 tty_stream = fopen ("/dev/tty", "r");
313 bb_perror_msg_and_die ("/dev/tty");
314 /* pranoidal security by vodz */
315 fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
317 fputs (" ?...", stderr);
319 c = savec = getc (tty_stream);
320 while (c != EOF && c != '\n')
321 c = getc (tty_stream);
322 if (savec == 'y' || savec == 'Y')
329 # define xargs_ask_confirmation() 1
330 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
332 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
338 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
340 process0_stdin (xlist_t *list_arg, const char *eof_str, size_t mc, char *buf)
342 int c; /* current char */
343 char *s = NULL; /* start word */
344 char *p = NULL; /* pointer to end word */
345 size_t line_l = 0; /* size loaded args line */
349 for(prev = cur = list_arg; cur; cur = cur->link) {
350 line_l += cur->lenght; /* previous allocated */
355 while(!eof_stdin_detected) {
358 eof_stdin_detected++;
366 bb_error_msg_and_die ("argument line too long");
368 if (c == 0) { /* word's delimiter or EOF detected */
370 size_t lenght = (p-buf);
372 cur = xmalloc(sizeof(xlist_t) + lenght);
373 cur->data = memcpy(cur + 1, s, lenght);
374 cur->lenght = lenght;
384 /* stop memory usage :-) */
392 # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc)
393 # define OPT_INC_0 1 /* future use */
395 # define OPT_INC_0 0 /* future use */
396 # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc)
397 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
400 #define OPT_VERBOSE (1<<0)
401 #define OPT_NO_EMPTY (1<<1)
402 #define OPT_UPTO_NUMBER (1<<2)
403 #define OPT_UPTO_SIZE (1<<3)
404 #define OPT_EOF_STRING (1<<4)
405 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
406 #define OPT_INTERACTIVE (1<<5)
408 #define OPT_INTERACTIVE (0) /* require for algorithm &| */
410 #define OPT_TERMINATE (1<<(5+OPT_INC_P))
411 #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X))
413 #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0))
417 xargs_main (int argc, char **argv)
421 xlist_t *list = NULL;
424 char *max_args, *max_chars;
428 const char *eof_str = "_";
431 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
432 xlist_t * (*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin;
435 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
436 bb_opt_complementaly = "pt";
439 opt = bb_getopt_ulflags (argc, argv, "+trn:s:e::"
440 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
443 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
446 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
449 , &max_args, &max_chars, &eof_str);
454 /* default behavior is to echo all the filenames */
459 orig_arg_max = ARG_MAX;
460 if (orig_arg_max == -1)
461 orig_arg_max = LONG_MAX;
462 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
463 if ((opt & OPT_UPTO_SIZE)) {
464 n_max_chars = bb_xgetularg10_bnd (max_chars, 1, orig_arg_max);
465 for (i = 0; i < a; i++) {
466 n_chars += strlen (*argv) + 1;
468 if (n_max_chars < n_chars) {
470 ("can not fit single argument within argument list size limit");
472 n_max_chars -= n_chars;
474 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
475 have it at 1 meg). Things will work fine with a large ARG_MAX but it
476 will probably hurt the system more than it needs to; an array of this
477 size is allocated. */
478 if (orig_arg_max > 20 * 1024)
479 orig_arg_max = 20 * 1024;
480 n_max_chars = orig_arg_max;
482 max_chars = xmalloc(n_max_chars);
484 if ((opt & OPT_UPTO_NUMBER)) {
485 n_max_arg = bb_xgetularg10_bnd (max_args, 1, INT_MAX);
487 n_max_arg = n_max_chars;
490 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
491 if(opt & OPT_ZEROTERM)
492 read_args = process0_stdin;
495 while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL
496 || (opt & OPT_NO_EMPTY) == 0) {
500 #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
501 for (cur = list; cur; ) {
502 n_chars += cur->lenght;
505 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
506 if (opt & OPT_TERMINATE)
507 bb_error_msg_and_die ("argument list too long");
512 for (cur = list; cur; cur = cur->link) {
513 n_chars += cur->lenght;
515 if (n_chars > n_max_chars || n == n_max_arg) {
519 #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
521 /* allocating pointers for execvp:
522 a*arg, n*arg from stdin, NULL */
523 args = xcalloc (n + a + 1, sizeof (char *));
525 /* Store the command to be executed
526 (taken from the command line) */
527 for (i = 0; i < a; i++)
529 /* (taken from stdin) */
530 for (cur = list; n; cur = cur->link) {
531 args[i++] = cur->data;
535 if ((opt & (OPT_INTERACTIVE|OPT_VERBOSE))) {
536 for (i = 0; args[i]; i++) {
539 fputs (args[i], stderr);
541 if((opt & OPT_INTERACTIVE) == 0)
542 fputc ('\n', stderr);
544 if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation () != 0) {
545 child_error = xargs_exec (args);
549 for (i = a; args[i]; i++) {
555 if (child_error > 0 && child_error != 123) {
559 #ifdef CONFIG_FEATURE_CLEAN_UP
568 const char *bb_applet_name = "debug stuff usage";
570 void bb_show_usage(void)
572 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", bb_applet_name);
576 int main(int argc, char **argv)
578 return xargs_main(argc, argv);