1 /* xargs for busybox -- using GNU xargs till we can get something
2 * better. minix xargs is a bit smaller... */
4 /* xargs -- build and execute command lines from standard input
5 Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* Written by Mike Rendell <michael@cs.mun.ca>
22 and David MacKenzie <djm@gnu.ai.mit.edu>. */
26 #define HAVE_STRING_H 1
27 #define HAVE_SYS_WAIT_H 1
28 #define HAVE_UNISTD_H 1
29 #define HAVE_LIMITS_H 1
30 #define STDC_HEADERS 1
32 #include <sys/types.h> /* For pid_t. */
38 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
41 #define WIFSIGNALED(w) (((w) & 0xff) != 0x7f && ((w) & 0xff) != 0)
44 #define WIFEXITED(w) (((w) & 0xff) == 0)
48 #define WSTOPSIG(w) (((w) >> 8) & 0xff)
51 #define WTERMSIG(w) ((w) & 0x7f)
54 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
65 #if !defined (isascii) || defined (STDC_HEADERS)
73 #define ISBLANK(c) (isascii (c) && isblank (c))
75 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
78 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
79 || (c) == '\f' || (c) == '\v')
85 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
87 #if !defined(STDC_HEADERS)
92 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
96 #include <sys/param.h>
104 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
113 #if !defined(SIGCHLD) && defined(SIGCLD)
114 #define SIGCHLD SIGCLD
117 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
118 We try to make it as large as possible. */
119 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
120 #define ARG_MAX sysconf (_SC_ARG_MAX)
123 #define ARG_MAX NCARGS
126 /* States for read_line. */
138 /* Return nonzero if S is the EOF string. */
139 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
141 extern char **environ;
143 /* Not char because of type promotion; NeXT gcc can't handle it. */
154 VOID *xmalloc P_ ((size_t n));
155 VOID *xrealloc P_ ((VOID * p, size_t n));
157 /* The name this program was run with. */
160 /* Buffer for reading arguments from stdin. */
161 static char *linebuf;
163 /* Line number in stdin since the last command was executed. */
164 static int lineno = 0;
166 /* If nonzero, then instead of putting the args from stdin at
167 the end of the command argument list, they are each stuck into the
168 initial args, replacing each occurrence of the `replace_pat' in the
170 static char *replace_pat = NULL;
172 /* The length of `replace_pat'. */
173 static size_t rplen = 0;
175 /* If nonzero, when this string is read on stdin it is treated as
177 I don't like this - it should default to NULL. */
178 static char *eof_str = "_";
180 /* If nonzero, the maximum number of nonblank lines from stdin to use
182 static long lines_per_exec = 0;
184 /* The maximum number of arguments to use per command line. */
185 static long args_per_exec = 1024;
187 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
188 static boolean exit_if_size_exceeded = false;
190 /* The maximum number of characters that can be used per command line. */
193 /* Storage for elements of `cmd_argv'. */
196 /* The list of args being built. */
197 static char **cmd_argv = NULL;
199 /* Number of elements allocated for `cmd_argv'. */
200 static int cmd_argv_alloc = 0;
202 /* Number of valid elements in `cmd_argv'. */
203 static int cmd_argc = 0;
205 /* Number of chars being used in `cmd_argv'. */
206 static int cmd_argv_chars = 0;
208 /* Number of initial arguments given on the command line. */
209 static int initial_argc = 0;
211 /* Number of chars in the initial args. */
212 static int initial_argv_chars = 0;
214 /* true when building up initial arguments in `cmd_argv'. */
215 static boolean initial_args = true;
217 /* If nonzero, the maximum number of child processes that can be running
219 static int proc_max = 1;
221 /* Total number of child processes that have been executed. */
222 static int procs_executed = 0;
224 /* The number of elements in `pids'. */
225 static int procs_executing = 0;
227 /* List of child processes currently executing. */
228 static pid_t *pids = NULL;
230 /* The number of allocated elements in `pids'. */
231 static int pids_alloc = 0;
233 /* Exit status; nonzero if any child process exited with a
235 static int child_error = 0;
237 /* If true, print each command on stderr before executing it. */
238 static boolean print_command = false;
240 /* If true, query the user before executing each command, and only
241 execute the command if the user responds affirmatively. */
242 static boolean query_before_executing = false;
244 static struct option const longopts[] =
246 {"null", no_argument, NULL, '0'},
247 {"eof", optional_argument, NULL, 'e'},
248 {"replace", optional_argument, NULL, 'i'},
249 {"max-lines", optional_argument, NULL, 'l'},
250 {"max-args", required_argument, NULL, 'n'},
251 {"interactive", no_argument, NULL, 'p'},
252 {"no-run-if-empty", no_argument, NULL, 'r'},
253 {"max-chars", required_argument, NULL, 's'},
254 {"verbose", no_argument, NULL, 't'},
255 {"exit", no_argument, NULL, 'x'},
256 {"max-procs", required_argument, NULL, 'P'},
257 {"help", no_argument, NULL, 'h'},
258 {NULL, no_argument, NULL, 0}
261 static int read_line P_ ((void));
262 static int read_string P_ ((void));
263 static void do_insert P_ ((char *arg, size_t arglen, size_t lblen));
264 static void push_arg P_ ((char *arg, size_t len));
265 static boolean print_args P_ ((boolean ask));
266 static void do_exec P_ ((void));
267 static void add_proc P_ ((pid_t pid));
268 static void wait_for_proc P_ ((boolean all));
269 static long parse_num P_ ((char *str, int option, long min, long max));
270 static long env_size P_ ((char **envp));
272 int xargs_main (argc, argv)
277 int always_run_command = 1;
279 char *default_cmd = "/bin/echo";
280 int (*read_args) P_ ((void)) = read_line;
282 program_name = argv[0];
284 orig_arg_max = ARG_MAX;
285 if (orig_arg_max == -1)
286 orig_arg_max = LONG_MAX;
287 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
288 arg_max = orig_arg_max;
290 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
291 have it at 1 meg). Things will work fine with a large ARG_MAX but it
292 will probably hurt the system more than it needs to; an array of this
293 size is allocated. */
294 if (arg_max > 20 * 1024)
297 /* Take the size of the environment into account. */
298 arg_max -= env_size (environ);
300 fatalError("environment is too large for exec");
302 while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
303 longopts, (int *) 0)) != -1)
308 read_args = read_string;
323 replace_pat = optarg;
326 /* -i excludes -n -l. */
333 lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
336 /* -l excludes -i -n. */
342 args_per_exec = parse_num (optarg, 'n', 1L, -1L);
343 /* -n excludes -i -l. */
349 arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
353 print_command = true;
357 exit_if_size_exceeded = true;
361 query_before_executing = true;
362 print_command = true;
366 always_run_command = 0;
370 proc_max = parse_num (optarg, 'P', 0L, -1L);
378 if (replace_pat || lines_per_exec)
379 exit_if_size_exceeded = true;
388 linebuf = (char *) xmalloc (arg_max + 1);
389 argbuf = (char *) xmalloc (arg_max + 1);
391 /* Make sure to listen for the kids. */
392 signal (SIGCHLD, SIG_DFL);
396 for (; optind < argc; optind++)
397 push_arg (argv[optind], strlen (argv[optind]) + 1);
398 initial_args = false;
399 initial_argc = cmd_argc;
400 initial_argv_chars = cmd_argv_chars;
402 while ((*read_args) () != -1)
403 if (lines_per_exec && lineno >= lines_per_exec)
409 /* SYSV xargs seems to do at least one exec, even if the
411 if (cmd_argc != initial_argc
412 || (always_run_command && procs_executed == 0))
419 size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
421 for (i = optind; i < argc; i++)
422 arglen[i] = strlen(argv[i]);
423 rplen = strlen (replace_pat);
424 while ((len = (*read_args) ()) != -1)
426 /* Don't do insert on the command name. */
427 push_arg (argv[optind], arglen[optind] + 1);
429 for (i = optind + 1; i < argc; i++)
430 do_insert (argv[i], arglen[i], len);
435 wait_for_proc (true);
439 /* Read a line of arguments from stdin and add them to the list of
440 arguments to pass to the command. Ignore blank lines and initial blanks.
441 Single and double quotes and backslashes quote metacharacters and blanks
442 as they do in the shell.
443 Return -1 if eof (either physical or logical) is reached,
444 otherwise the length of the last string read (including the null). */
449 static boolean eof = false;
450 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
451 int state = SPACE; /* The type of character we last read. */
452 int prevc; /* The previous value of c. */
453 int quotc = 0; /* The last quote character read. */
455 boolean first = true; /* true if reading first arg on line. */
458 /* Including the NUL, the args must not grow past this point. */
459 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
469 /* COMPAT: SYSV seems to ignore stuff on a line that
470 ends without a \n; we don't. */
476 /* FIXME we don't check for unterminated quotes here. */
477 if (first && EOF_STR (linebuf))
480 push_arg (linebuf, len);
494 if (!ISBLANK (prevc))
495 lineno++; /* For -l. */
504 if (EOF_STR (linebuf))
507 return first ? -1 : len;
510 push_arg (linebuf, len);
513 if (!replace_pat && ISSPACE (c))
517 if (EOF_STR (linebuf))
520 return first ? -1 : len;
522 push_arg (linebuf, len);
544 fatalError ("unmatched %s quote", quotc == '"' ? "double" : "single");
557 fatalError ("argument line too long");
562 /* Read a null-terminated string from stdin and add it to the list of
563 arguments to pass to the command.
564 Return -1 if eof (either physical or logical) is reached,
565 otherwise the length of the string read (including the null). */
570 static boolean eof = false;
573 /* Including the NUL, the args must not grow past this point. */
574 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
580 int c = getc (stdin);
589 push_arg (linebuf, len);
594 lineno++; /* For -l. */
598 push_arg (linebuf, len);
602 fatalError ("argument line too long");
607 /* Replace all instances of `replace_pat' in ARG with `linebuf',
608 and add the resulting string to the list of arguments for the command
610 ARGLEN is the length of ARG, not including the null.
611 LBLEN is the length of `linebuf', not including the null.
613 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
614 and a max of 5 occurences of replace_pat in the initial-arguments.
615 Those restrictions do not exist here. */
618 do_insert (arg, arglen, lblen)
623 /* Temporary copy of each arg with the replace pattern replaced by the
625 static char *insertbuf;
627 int bytes_left = arg_max - 1; /* Bytes left on the command line. */
630 insertbuf = (char *) xmalloc (arg_max + 1);
635 size_t len; /* Length in ARG before `replace_pat'. */
636 char *s = strstr (arg, replace_pat);
645 strncpy (p, arg, len);
663 fatalError ("command too long");
665 push_arg (insertbuf, p - insertbuf);
668 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
670 LEN is the length of ARG, including the terminating null.
671 If this brings the list up to its maximum size, execute the command. */
680 if (cmd_argv_chars + len > arg_max)
682 if (initial_args || cmd_argc == initial_argc)
683 fatalError ("can not fit single argument within argument list size limit");
685 || (exit_if_size_exceeded &&
686 (lines_per_exec || args_per_exec)))
687 fatalError ("argument list too long");
690 if (!initial_args && args_per_exec &&
691 cmd_argc - initial_argc == args_per_exec)
695 if (cmd_argc >= cmd_argv_alloc)
700 cmd_argv = (char **) xmalloc (sizeof (char *) * cmd_argv_alloc);
705 cmd_argv = (char **) xrealloc (cmd_argv,
706 sizeof (char *) * cmd_argv_alloc);
711 cmd_argv[cmd_argc++] = NULL;
714 cmd_argv[cmd_argc++] = argbuf + cmd_argv_chars;
715 strcpy (argbuf + cmd_argv_chars, arg);
716 cmd_argv_chars += len;
720 /* Print the arguments of the command to execute.
721 If ASK is nonzero, prompt the user for a response, and
722 if the user responds affirmatively, return true;
723 otherwise, return false. */
731 for (i = 0; i < cmd_argc - 1; i++)
732 fprintf (stderr, "%s ", cmd_argv[i]);
735 static FILE *tty_stream;
740 tty_stream = fopen ("/dev/tty", "r");
742 fatalError (" Could not open /dev/tty");
744 fputs ("?...", stderr);
746 c = savec = getc (tty_stream);
747 while (c != EOF && c != '\n')
748 c = getc (tty_stream);
749 if (savec == 'y' || savec == 'Y')
758 /* Execute the command that has been built in `cmd_argv'. This may involve
759 waiting for processes that were previously executed. */
766 push_arg ((char *) NULL, 0); /* Null terminate the arg list. */
767 if (!query_before_executing || print_args (true))
769 if (proc_max && procs_executing >= proc_max)
770 wait_for_proc (false);
771 if (!query_before_executing && print_command)
773 /* If we run out of processes, wait for a child to return and
775 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
776 wait_for_proc (false);
780 fatalError ("cannot fork");
783 execvp (cmd_argv[0], cmd_argv);
784 errorMsg ("failed to exec '%s'", cmd_argv[0]);
785 _exit (errno == ENOENT ? 127 : 126);
790 cmd_argc = initial_argc;
791 cmd_argv_chars = initial_argv_chars;
794 /* Add the process with id PID to the list of processes that have
803 /* Find an empty slot. */
804 for (i = 0; i < pids_alloc && pids[i]; i++)
810 pids_alloc = proc_max ? proc_max : 64;
811 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
816 pids = (pid_t *) xrealloc (pids,
817 sizeof (pid_t) * pids_alloc);
819 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
826 /* If ALL is true, wait for all child processes to finish;
827 otherwise, wait for one child process to finish.
828 Remove the processes that finish from the list of executing processes. */
834 while (procs_executing)
842 pid = wait (&status);
844 fatalError ("error waiting for child process");
846 /* Find the entry in `pids' for the child process
848 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
851 while (i == pids_alloc); /* A child died that we didn't start? */
853 /* Remove the child from the list. */
857 if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
858 exit (WEXITSTATUS (status)); /* Can't find or run the command. */
859 if (WEXITSTATUS (status) == 255)
860 fatalError ( "%s: exited with status 255; aborting", cmd_argv[0]);
861 if (WIFSTOPPED (status))
862 fatalError ( "%s: stopped by signal %d", cmd_argv[0], WSTOPSIG (status));
863 if (WIFSIGNALED (status))
864 fatalError ("%s: terminated by signal %d", cmd_argv[0], WTERMSIG (status));
865 if (WEXITSTATUS (status) != 0)
873 /* Return the value of the number represented in STR.
874 OPTION is the command line option to which STR is the argument.
875 If the value does not fall within the boundaries MIN and MAX,
876 Print an error message mentioning OPTION and exit. */
879 parse_num (str, option, min, max)
888 val = strtol (str, &eptr, 10);
889 if (eptr == str || *eptr)
891 fprintf (stderr, "%s: invalid number for -%c option\n",
892 program_name, option);
897 fprintf (stderr, "%s: value for -%c option must be >= %ld\n",
898 program_name, option, min);
901 else if (max >= 0 && val > max)
903 fprintf (stderr, "%s: value for -%c option must be < %ld\n",
904 program_name, option, max);
910 /* Return how much of ARG_MAX is used by the environment. */
919 len += strlen (*envp++) + 1;