1 /* xargs for busybox */
3 /* xargs -- build and execute command lines from standard input
4 Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Written by Mike Rendell <michael@cs.mun.ca>
21 and David MacKenzie <djm@gnu.ai.mit.edu>. */
25 #define HAVE_STRING_H 1
26 #define HAVE_SYS_WAIT_H 1
27 #define HAVE_UNISTD_H 1
28 #define HAVE_LIMITS_H 1
29 #define STDC_HEADERS 1
31 #include <sys/types.h> /* For pid_t. */
37 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
40 #define WIFSIGNALED(w) (((w) & 0xff) != 0x7f && ((w) & 0xff) != 0)
43 #define WIFEXITED(w) (((w) & 0xff) == 0)
47 #define WSTOPSIG(w) (((w) >> 8) & 0xff)
50 #define WTERMSIG(w) ((w) & 0x7f)
53 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
64 #if !defined (isascii) || defined (STDC_HEADERS)
72 #define ISBLANK(c) (isascii (c) && isblank (c))
74 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
77 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
78 || (c) == '\f' || (c) == '\v')
84 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
86 #if !defined(STDC_HEADERS)
91 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
95 #include <sys/param.h>
103 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
112 #if !defined(SIGCHLD) && defined(SIGCLD)
113 #define SIGCHLD SIGCLD
116 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
117 We try to make it as large as possible. */
118 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
119 #define ARG_MAX sysconf (_SC_ARG_MAX)
122 #define ARG_MAX NCARGS
125 /* States for read_line. */
137 /* Return nonzero if S is the EOF string. */
138 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
140 extern char **environ;
142 /* Not char because of type promotion; NeXT gcc can't handle it. */
153 VOID *xmalloc P_ ((size_t n));
154 VOID *xrealloc P_ ((VOID * p, size_t n));
156 /* The name this program was run with. */
159 /* Buffer for reading arguments from stdin. */
160 static char *linebuf;
162 /* Line number in stdin since the last command was executed. */
163 static int lineno = 0;
165 /* If nonzero, then instead of putting the args from stdin at
166 the end of the command argument list, they are each stuck into the
167 initial args, replacing each occurrence of the `replace_pat' in the
169 static char *replace_pat = NULL;
171 /* The length of `replace_pat'. */
172 static size_t rplen = 0;
174 /* If nonzero, when this string is read on stdin it is treated as
176 I don't like this - it should default to NULL. */
177 static char *eof_str = "_";
179 /* If nonzero, the maximum number of nonblank lines from stdin to use
181 static long lines_per_exec = 0;
183 /* The maximum number of arguments to use per command line. */
184 static long args_per_exec = 1024;
186 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
187 static boolean exit_if_size_exceeded = false;
189 /* The maximum number of characters that can be used per command line. */
192 /* Storage for elements of `cmd_argv'. */
195 /* The list of args being built. */
196 static char **cmd_argv = NULL;
198 /* Number of elements allocated for `cmd_argv'. */
199 static int cmd_argv_alloc = 0;
201 /* Number of valid elements in `cmd_argv'. */
202 static int cmd_argc = 0;
204 /* Number of chars being used in `cmd_argv'. */
205 static int cmd_argv_chars = 0;
207 /* Number of initial arguments given on the command line. */
208 static int initial_argc = 0;
210 /* Number of chars in the initial args. */
211 static int initial_argv_chars = 0;
213 /* true when building up initial arguments in `cmd_argv'. */
214 static boolean initial_args = true;
216 /* If nonzero, the maximum number of child processes that can be running
218 static int proc_max = 1;
220 /* Total number of child processes that have been executed. */
221 static int procs_executed = 0;
223 /* The number of elements in `pids'. */
224 static int procs_executing = 0;
226 /* List of child processes currently executing. */
227 static pid_t *pids = NULL;
229 /* The number of allocated elements in `pids'. */
230 static int pids_alloc = 0;
232 /* Exit status; nonzero if any child process exited with a
234 static int child_error = 0;
236 /* If true, print each command on stderr before executing it. */
237 static boolean print_command = false;
239 /* If true, query the user before executing each command, and only
240 execute the command if the user responds affirmatively. */
241 static boolean query_before_executing = false;
243 static struct option const longopts[] =
245 {"null", no_argument, NULL, '0'},
246 {"eof", optional_argument, NULL, 'e'},
247 {"replace", optional_argument, NULL, 'i'},
248 {"max-lines", optional_argument, NULL, 'l'},
249 {"max-args", required_argument, NULL, 'n'},
250 {"interactive", no_argument, NULL, 'p'},
251 {"no-run-if-empty", no_argument, NULL, 'r'},
252 {"max-chars", required_argument, NULL, 's'},
253 {"verbose", no_argument, NULL, 't'},
254 {"exit", no_argument, NULL, 'x'},
255 {"max-procs", required_argument, NULL, 'P'},
256 {"help", no_argument, NULL, 'h'},
257 {NULL, no_argument, NULL, 0}
260 static int read_line P_ ((void));
261 static int read_string P_ ((void));
262 static void do_insert P_ ((char *arg, size_t arglen, size_t lblen));
263 static void push_arg P_ ((char *arg, size_t len));
264 static boolean print_args P_ ((boolean ask));
265 static void do_exec P_ ((void));
266 static void add_proc P_ ((pid_t pid));
267 static void wait_for_proc P_ ((boolean all));
268 static long parse_num P_ ((char *str, int option, long min, long max));
269 static long env_size P_ ((char **envp));
271 int xargs_main (argc, argv)
276 int always_run_command = 1;
278 char *default_cmd = "/bin/echo";
279 int (*read_args) P_ ((void)) = read_line;
281 program_name = argv[0];
283 orig_arg_max = ARG_MAX;
284 if (orig_arg_max == -1)
285 orig_arg_max = LONG_MAX;
286 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
287 arg_max = orig_arg_max;
289 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
290 have it at 1 meg). Things will work fine with a large ARG_MAX but it
291 will probably hurt the system more than it needs to; an array of this
292 size is allocated. */
293 if (arg_max > 20 * 1024)
296 /* Take the size of the environment into account. */
297 arg_max -= env_size (environ);
299 fatalError("environment is too large for exec");
301 while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
302 longopts, (int *) 0)) != -1)
307 read_args = read_string;
322 replace_pat = optarg;
325 /* -i excludes -n -l. */
332 lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
335 /* -l excludes -i -n. */
341 args_per_exec = parse_num (optarg, 'n', 1L, -1L);
342 /* -n excludes -i -l. */
348 arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
352 print_command = true;
356 exit_if_size_exceeded = true;
360 query_before_executing = true;
361 print_command = true;
365 always_run_command = 0;
369 proc_max = parse_num (optarg, 'P', 0L, -1L);
377 if (replace_pat || lines_per_exec)
378 exit_if_size_exceeded = true;
387 linebuf = (char *) xmalloc (arg_max + 1);
388 argbuf = (char *) xmalloc (arg_max + 1);
390 /* Make sure to listen for the kids. */
391 signal (SIGCHLD, SIG_DFL);
395 for (; optind < argc; optind++)
396 push_arg (argv[optind], strlen (argv[optind]) + 1);
397 initial_args = false;
398 initial_argc = cmd_argc;
399 initial_argv_chars = cmd_argv_chars;
401 while ((*read_args) () != -1)
402 if (lines_per_exec && lineno >= lines_per_exec)
408 /* SYSV xargs seems to do at least one exec, even if the
410 if (cmd_argc != initial_argc
411 || (always_run_command && procs_executed == 0))
418 size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
420 for (i = optind; i < argc; i++)
421 arglen[i] = strlen(argv[i]);
422 rplen = strlen (replace_pat);
423 while ((len = (*read_args) ()) != -1)
425 /* Don't do insert on the command name. */
426 push_arg (argv[optind], arglen[optind] + 1);
428 for (i = optind + 1; i < argc; i++)
429 do_insert (argv[i], arglen[i], len);
434 wait_for_proc (true);
438 /* Read a line of arguments from stdin and add them to the list of
439 arguments to pass to the command. Ignore blank lines and initial blanks.
440 Single and double quotes and backslashes quote metacharacters and blanks
441 as they do in the shell.
442 Return -1 if eof (either physical or logical) is reached,
443 otherwise the length of the last string read (including the null). */
448 static boolean eof = false;
449 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
450 int state = SPACE; /* The type of character we last read. */
451 int prevc; /* The previous value of c. */
452 int quotc = 0; /* The last quote character read. */
454 boolean first = true; /* true if reading first arg on line. */
457 /* Including the NUL, the args must not grow past this point. */
458 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
468 /* COMPAT: SYSV seems to ignore stuff on a line that
469 ends without a \n; we don't. */
475 /* FIXME we don't check for unterminated quotes here. */
476 if (first && EOF_STR (linebuf))
479 push_arg (linebuf, len);
493 if (!ISBLANK (prevc))
494 lineno++; /* For -l. */
503 if (EOF_STR (linebuf))
506 return first ? -1 : len;
509 push_arg (linebuf, len);
512 if (!replace_pat && ISSPACE (c))
516 if (EOF_STR (linebuf))
519 return first ? -1 : len;
521 push_arg (linebuf, len);
543 fatalError ("unmatched %s quote", quotc == '"' ? "double" : "single");
556 fatalError ("argument line too long");
561 /* Read a null-terminated string from stdin and add it to the list of
562 arguments to pass to the command.
563 Return -1 if eof (either physical or logical) is reached,
564 otherwise the length of the string read (including the null). */
569 static boolean eof = false;
572 /* Including the NUL, the args must not grow past this point. */
573 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
579 int c = getc (stdin);
588 push_arg (linebuf, len);
593 lineno++; /* For -l. */
597 push_arg (linebuf, len);
601 fatalError ("argument line too long");
606 /* Replace all instances of `replace_pat' in ARG with `linebuf',
607 and add the resulting string to the list of arguments for the command
609 ARGLEN is the length of ARG, not including the null.
610 LBLEN is the length of `linebuf', not including the null.
612 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
613 and a max of 5 occurences of replace_pat in the initial-arguments.
614 Those restrictions do not exist here. */
617 do_insert (arg, arglen, lblen)
622 /* Temporary copy of each arg with the replace pattern replaced by the
624 static char *insertbuf;
626 int bytes_left = arg_max - 1; /* Bytes left on the command line. */
629 insertbuf = (char *) xmalloc (arg_max + 1);
634 size_t len; /* Length in ARG before `replace_pat'. */
635 char *s = strstr (arg, replace_pat);
644 strncpy (p, arg, len);
662 fatalError ("command too long");
664 push_arg (insertbuf, p - insertbuf);
667 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
669 LEN is the length of ARG, including the terminating null.
670 If this brings the list up to its maximum size, execute the command. */
679 if (cmd_argv_chars + len > arg_max)
681 if (initial_args || cmd_argc == initial_argc)
682 fatalError ("can not fit single argument within argument list size limit");
684 || (exit_if_size_exceeded &&
685 (lines_per_exec || args_per_exec)))
686 fatalError ("argument list too long");
689 if (!initial_args && args_per_exec &&
690 cmd_argc - initial_argc == args_per_exec)
694 if (cmd_argc >= cmd_argv_alloc)
699 cmd_argv = (char **) xmalloc (sizeof (char *) * cmd_argv_alloc);
704 cmd_argv = (char **) xrealloc (cmd_argv,
705 sizeof (char *) * cmd_argv_alloc);
710 cmd_argv[cmd_argc++] = NULL;
713 cmd_argv[cmd_argc++] = argbuf + cmd_argv_chars;
714 strcpy (argbuf + cmd_argv_chars, arg);
715 cmd_argv_chars += len;
719 /* Print the arguments of the command to execute.
720 If ASK is nonzero, prompt the user for a response, and
721 if the user responds affirmatively, return true;
722 otherwise, return false. */
730 for (i = 0; i < cmd_argc - 1; i++)
731 fprintf (stderr, "%s ", cmd_argv[i]);
734 static FILE *tty_stream;
739 tty_stream = fopen ("/dev/tty", "r");
741 fatalError (" Could not open /dev/tty");
743 fputs ("?...", stderr);
745 c = savec = getc (tty_stream);
746 while (c != EOF && c != '\n')
747 c = getc (tty_stream);
748 if (savec == 'y' || savec == 'Y')
757 /* Execute the command that has been built in `cmd_argv'. This may involve
758 waiting for processes that were previously executed. */
765 push_arg ((char *) NULL, 0); /* Null terminate the arg list. */
766 if (!query_before_executing || print_args (true))
768 if (proc_max && procs_executing >= proc_max)
769 wait_for_proc (false);
770 if (!query_before_executing && print_command)
772 /* If we run out of processes, wait for a child to return and
774 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
775 wait_for_proc (false);
779 fatalError ("cannot fork");
782 execvp (cmd_argv[0], cmd_argv);
783 errorMsg ("failed to exec '%s'", cmd_argv[0]);
784 _exit (errno == ENOENT ? 127 : 126);
789 cmd_argc = initial_argc;
790 cmd_argv_chars = initial_argv_chars;
793 /* Add the process with id PID to the list of processes that have
802 /* Find an empty slot. */
803 for (i = 0; i < pids_alloc && pids[i]; i++)
809 pids_alloc = proc_max ? proc_max : 64;
810 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
815 pids = (pid_t *) xrealloc (pids,
816 sizeof (pid_t) * pids_alloc);
818 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
825 /* If ALL is true, wait for all child processes to finish;
826 otherwise, wait for one child process to finish.
827 Remove the processes that finish from the list of executing processes. */
833 while (procs_executing)
841 pid = wait (&status);
843 fatalError ("error waiting for child process");
845 /* Find the entry in `pids' for the child process
847 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
850 while (i == pids_alloc); /* A child died that we didn't start? */
852 /* Remove the child from the list. */
856 if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
857 exit (WEXITSTATUS (status)); /* Can't find or run the command. */
858 if (WEXITSTATUS (status) == 255)
859 fatalError ( "%s: exited with status 255; aborting", cmd_argv[0]);
860 if (WIFSTOPPED (status))
861 fatalError ( "%s: stopped by signal %d", cmd_argv[0], WSTOPSIG (status));
862 if (WIFSIGNALED (status))
863 fatalError ("%s: terminated by signal %d", cmd_argv[0], WTERMSIG (status));
864 if (WEXITSTATUS (status) != 0)
872 /* Return the value of the number represented in STR.
873 OPTION is the command line option to which STR is the argument.
874 If the value does not fall within the boundaries MIN and MAX,
875 Print an error message mentioning OPTION and exit. */
878 parse_num (str, option, min, max)
887 val = strtol (str, &eptr, 10);
888 if (eptr == str || *eptr)
890 fprintf (stderr, "%s: invalid number for -%c option\n",
891 program_name, option);
896 fprintf (stderr, "%s: value for -%c option must be >= %ld\n",
897 program_name, option, min);
900 else if (max >= 0 && val > max)
902 fprintf (stderr, "%s: value for -%c option must be < %ld\n",
903 program_name, option, max);
909 /* Return how much of ARG_MAX is used by the environment. */
918 len += strlen (*envp++) + 1;