Indicate that prep_child_for_exec() fixes Savannah bug #3992.
[findutils.git] / xargs / xargs.c
blob3210e2079853d0cd0d70af51fc5a9cfb1434874e
1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 91, 92, 93, 94, 2000,2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 9 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA.
20 /* Written by Mike Rendell <michael@cs.mun.ca>
21 and David MacKenzie <djm@gnu.ai.mit.edu>. */
23 #include <gnulib/config.h>
24 #undef VERSION
25 #undef PACKAGE_VERSION
26 #undef PACKAGE_TARNAME
27 #undef PACKAGE_STRING
28 #undef PACKAGE_NAME
29 #undef PACKAGE
30 #include <config.h>
32 # ifndef PARAMS
33 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
34 # define PARAMS(Args) Args
35 # else
36 # define PARAMS(Args) ()
37 # endif
38 # endif
40 #ifndef _GNU_SOURCE
41 #define _GNU_SOURCE
42 #endif
43 #include <ctype.h>
45 #if !defined (isascii) || defined (STDC_HEADERS)
46 #ifdef isascii
47 #undef isascii
48 #endif
49 #define isascii(c) 1
50 #endif
52 #ifdef isblank
53 #define ISBLANK(c) (isascii (c) && isblank (c))
54 #else
55 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
56 #endif
58 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
59 || (c) == '\f' || (c) == '\v')
61 #include <sys/types.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #include <getopt.h>
65 #include <fcntl.h>
67 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
68 #include <string.h>
69 #if !defined(STDC_HEADERS)
70 #include <memory.h>
71 #endif
72 #else
73 #include <strings.h>
74 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
75 #endif
77 #ifndef _POSIX_SOURCE
78 #include <sys/param.h>
79 #endif
81 #ifdef HAVE_LIMITS_H
82 #include <limits.h>
83 #endif
85 #ifndef LONG_MAX
86 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
87 #endif
89 #ifdef HAVE_UNISTD_H
90 #include <unistd.h>
91 #endif
93 #include <signal.h>
95 #if !defined(SIGCHLD) && defined(SIGCLD)
96 #define SIGCHLD SIGCLD
97 #endif
99 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
100 We try to make it as large as possible. */
101 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
102 #define ARG_MAX sysconf (_SC_ARG_MAX)
103 #endif
104 #ifndef ARG_MAX
105 #define ARG_MAX NCARGS
106 #endif
108 #include "wait.h"
110 /* States for read_line. */
111 #define NORM 0
112 #define SPACE 1
113 #define QUOTE 2
114 #define BACKSLASH 3
116 #ifdef STDC_HEADERS
117 #include <stdlib.h>
118 #else
119 extern int errno;
120 #endif
122 #ifdef HAVE_LOCALE_H
123 #include <locale.h>
124 #endif
125 #if ENABLE_NLS
126 # include <libintl.h>
127 # define _(Text) gettext (Text)
128 #else
129 # define _(Text) Text
130 #define textdomain(Domain)
131 #define bindtextdomain(Package, Directory)
132 #endif
133 #ifdef gettext_noop
134 # define N_(String) gettext_noop (String)
135 #else
136 # define N_(String) (String)
137 #endif
139 /* Return nonzero if S is the EOF string. */
140 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
142 extern char **environ;
144 /* Do multibyte processing if multibyte characters are supported,
145 unless multibyte sequences are search safe. Multibyte sequences
146 are search safe if searching for a substring using the byte
147 comparison function 'strstr' gives no false positives. All 8-bit
148 encodings and the UTF-8 multibyte encoding are search safe, but
149 the EUC encodings are not.
150 BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
151 #if defined __BEOS__
152 # define MULTIBYTE_IS_SEARCH_SAFE 1
153 #endif
154 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
156 #if DO_MULTIBYTE
157 # if HAVE_MBRLEN
158 # include <wchar.h>
159 # else
160 /* Simulate mbrlen with mblen as best we can. */
161 # define mbstate_t int
162 # define mbrlen(s, n, ps) mblen (s, n)
163 # endif
164 #endif
166 /* Not char because of type promotion; NeXT gcc can't handle it. */
167 typedef int boolean;
168 #define true 1
169 #define false 0
171 #if __STDC__
172 #define VOID void
173 #else
174 #define VOID char
175 #endif
177 #include <xalloc.h>
178 void error PARAMS ((int status, int errnum, char *message,...));
180 extern char *version_string;
182 /* The name this program was run with. */
183 char *program_name;
185 /* Buffer for reading arguments from stdin. */
186 static char *linebuf;
188 /* Line number in stdin since the last command was executed. */
189 static int lineno = 0;
191 /* If nonzero, then instead of putting the args from stdin at
192 the end of the command argument list, they are each stuck into the
193 initial args, replacing each occurrence of the `replace_pat' in the
194 initial args. */
195 static char *replace_pat = NULL;
197 /* The length of `replace_pat'. */
198 static size_t rplen = 0;
200 /* If nonzero, when this string is read on stdin it is treated as
201 end of file.
202 I don't like this - it should default to NULL. */
203 static char *eof_str = "_";
205 /* If nonzero, the maximum number of nonblank lines from stdin to use
206 per command line. */
207 static long lines_per_exec = 0;
209 /* The maximum number of arguments to use per command line. */
210 static long args_per_exec = 1024;
212 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
213 static boolean exit_if_size_exceeded = false;
215 /* The maximum number of characters that can be used per command line. */
216 static long arg_max;
218 /* Storage for elements of `cmd_argv'. */
219 static char *argbuf;
221 /* The list of args being built. */
222 static char **cmd_argv = NULL;
224 /* Number of elements allocated for `cmd_argv'. */
225 static int cmd_argv_alloc = 0;
227 /* Number of valid elements in `cmd_argv'. */
228 static int cmd_argc = 0;
230 /* Number of chars being used in `cmd_argv'. */
231 static int cmd_argv_chars = 0;
233 /* Number of initial arguments given on the command line. */
234 static int initial_argc = 0;
236 /* Number of chars in the initial args. */
237 static int initial_argv_chars = 0;
239 /* true when building up initial arguments in `cmd_argv'. */
240 static boolean initial_args = true;
242 /* If nonzero, the maximum number of child processes that can be running
243 at once. */
244 static int proc_max = 1;
246 /* Total number of child processes that have been executed. */
247 static int procs_executed = 0;
249 /* The number of elements in `pids'. */
250 static int procs_executing = 0;
252 /* List of child processes currently executing. */
253 static pid_t *pids = NULL;
255 /* The number of allocated elements in `pids'. */
256 static int pids_alloc = 0;
258 /* Exit status; nonzero if any child process exited with a
259 status of 1-125. */
260 static int child_error = 0;
262 /* If true, print each command on stderr before executing it. */
263 static boolean print_command = false;
265 /* If true, query the user before executing each command, and only
266 execute the command if the user responds affirmatively. */
267 static boolean query_before_executing = false;
269 static struct option const longopts[] =
271 {"null", no_argument, NULL, '0'},
272 {"eof", optional_argument, NULL, 'e'},
273 {"replace", optional_argument, NULL, 'i'},
274 {"max-lines", optional_argument, NULL, 'l'},
275 {"max-args", required_argument, NULL, 'n'},
276 {"interactive", no_argument, NULL, 'p'},
277 {"no-run-if-empty", no_argument, NULL, 'r'},
278 {"max-chars", required_argument, NULL, 's'},
279 {"verbose", no_argument, NULL, 't'},
280 {"exit", no_argument, NULL, 'x'},
281 {"max-procs", required_argument, NULL, 'P'},
282 {"version", no_argument, NULL, 'v'},
283 {"help", no_argument, NULL, 'h'},
284 {NULL, no_argument, NULL, 0}
287 static int read_line PARAMS ((void));
288 static int read_string PARAMS ((void));
289 static char *mbstrstr PARAMS ((const char *haystack, const char *needle));
290 static void do_insert PARAMS ((char *arg, size_t arglen, size_t lblen));
291 static void push_arg PARAMS ((char *arg, size_t len));
292 static boolean print_args PARAMS ((boolean ask));
293 static void do_exec PARAMS ((void));
294 static void add_proc PARAMS ((pid_t pid));
295 static void wait_for_proc PARAMS ((boolean all));
296 static long parse_num PARAMS ((char *str, int option, long min, long max));
297 static long env_size PARAMS ((char **envp));
298 static void usage PARAMS ((FILE * stream, int status));
301 main (int argc, char **argv)
303 int optc;
304 int always_run_command = 1;
305 long orig_arg_max;
306 char *default_cmd = "/bin/echo";
307 int (*read_args) PARAMS ((void)) = read_line;
309 program_name = argv[0];
311 #ifdef HAVE_SETLOCALE
312 setlocale (LC_ALL, "");
313 #endif
314 bindtextdomain (PACKAGE, LOCALEDIR);
315 textdomain (PACKAGE);
317 orig_arg_max = ARG_MAX;
318 if (orig_arg_max == -1)
319 orig_arg_max = LONG_MAX;
320 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
321 arg_max = orig_arg_max;
323 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
324 have it at 1 meg). Things will work fine with a large ARG_MAX but it
325 will probably hurt the system more than it needs to; an array of this
326 size is allocated. */
327 if (arg_max > 20 * 1024)
328 arg_max = 20 * 1024;
330 /* Take the size of the environment into account. */
331 arg_max -= env_size (environ);
332 if (arg_max <= 0)
333 error (1, 0, _("environment is too large for exec"));
335 while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
336 longopts, (int *) 0)) != -1)
338 switch (optc)
340 case '0':
341 read_args = read_string;
342 break;
344 case 'e':
345 if (optarg)
346 eof_str = optarg;
347 else
348 eof_str = 0;
349 break;
351 case 'h':
352 usage (stdout, 0);
354 case 'i':
355 if (optarg)
356 replace_pat = optarg;
357 else
358 replace_pat = "{}";
359 /* -i excludes -n -l. */
360 args_per_exec = 0;
361 lines_per_exec = 0;
362 break;
364 case 'l':
365 if (optarg)
366 lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
367 else
368 lines_per_exec = 1;
369 /* -l excludes -i -n. */
370 args_per_exec = 0;
371 replace_pat = NULL;
372 break;
374 case 'n':
375 args_per_exec = parse_num (optarg, 'n', 1L, -1L);
376 /* -n excludes -i -l. */
377 lines_per_exec = 0;
378 if (args_per_exec == 1 && replace_pat)
379 /* ignore -n1 in '-i -n1' */
380 args_per_exec = 0;
381 else
382 replace_pat = NULL;
383 break;
385 case 's':
386 arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
387 break;
389 case 't':
390 print_command = true;
391 break;
393 case 'x':
394 exit_if_size_exceeded = true;
395 break;
397 case 'p':
398 query_before_executing = true;
399 print_command = true;
400 break;
402 case 'r':
403 always_run_command = 0;
404 break;
406 case 'P':
407 proc_max = parse_num (optarg, 'P', 0L, -1L);
408 break;
410 case 'v':
411 printf (_("GNU xargs version %s\n"), version_string);
412 exit (0);
414 default:
415 usage (stderr, 1);
419 if (replace_pat || lines_per_exec)
420 exit_if_size_exceeded = true;
422 if (optind == argc)
424 optind = 0;
425 argc = 1;
426 argv = &default_cmd;
429 linebuf = (char *) xmalloc (arg_max + 1);
430 argbuf = (char *) xmalloc (arg_max + 1);
432 /* Make sure to listen for the kids. */
433 signal (SIGCHLD, SIG_DFL);
435 if (!replace_pat)
437 for (; optind < argc; optind++)
438 push_arg (argv[optind], strlen (argv[optind]) + 1);
439 initial_args = false;
440 initial_argc = cmd_argc;
441 initial_argv_chars = cmd_argv_chars;
443 while ((*read_args) () != -1)
444 if (lines_per_exec && lineno >= lines_per_exec)
446 do_exec ();
447 lineno = 0;
450 /* SYSV xargs seems to do at least one exec, even if the
451 input is empty. */
452 if (cmd_argc != initial_argc
453 || (always_run_command && procs_executed == 0))
454 do_exec ();
456 else
458 int i;
459 size_t len;
460 size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
462 for (i = optind; i < argc; i++)
463 arglen[i] = strlen(argv[i]);
464 rplen = strlen (replace_pat);
465 while ((len = (*read_args) ()) != -1)
467 /* Don't do insert on the command name. */
468 push_arg (argv[optind], arglen[optind] + 1);
469 len--;
470 for (i = optind + 1; i < argc; i++)
471 do_insert (argv[i], arglen[i], len);
472 do_exec ();
476 wait_for_proc (true);
477 exit (child_error);
480 /* Read a line of arguments from stdin and add them to the list of
481 arguments to pass to the command. Ignore blank lines and initial blanks.
482 Single and double quotes and backslashes quote metacharacters and blanks
483 as they do in the shell.
484 Return -1 if eof (either physical or logical) is reached,
485 otherwise the length of the last string read (including the null). */
487 static int
488 read_line (void)
490 static boolean eof = false;
491 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
492 int state = SPACE; /* The type of character we last read. */
493 int prevc; /* The previous value of c. */
494 int quotc = 0; /* The last quote character read. */
495 int c = EOF;
496 boolean first = true; /* true if reading first arg on line. */
497 int len;
498 char *p = linebuf;
499 /* Including the NUL, the args must not grow past this point. */
500 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
502 if (eof)
503 return -1;
504 while (1)
506 prevc = c;
507 c = getc (stdin);
508 if (c == EOF)
510 /* COMPAT: SYSV seems to ignore stuff on a line that
511 ends without a \n; we don't. */
512 eof = true;
513 if (p == linebuf)
514 return -1;
515 *p++ = '\0';
516 len = p - linebuf;
517 /* FIXME we don't check for unterminated quotes here. */
518 if (first && EOF_STR (linebuf))
519 return -1;
520 if (!replace_pat)
521 push_arg (linebuf, len);
522 return len;
524 switch (state)
526 case SPACE:
527 if (ISSPACE (c))
528 continue;
529 state = NORM;
530 /* aaahhhh.... */
532 case NORM:
533 if (c == '\n')
535 if (!ISBLANK (prevc))
536 lineno++; /* For -l. */
537 if (p == linebuf)
539 /* Blank line. */
540 state = SPACE;
541 continue;
543 *p++ = '\0';
544 len = p - linebuf;
545 if (EOF_STR (linebuf))
547 eof = true;
548 return first ? -1 : len;
550 if (!replace_pat)
551 push_arg (linebuf, len);
552 return len;
554 if (!replace_pat && ISSPACE (c))
556 *p++ = '\0';
557 len = p - linebuf;
558 if (EOF_STR (linebuf))
560 eof = true;
561 return first ? -1 : len;
563 push_arg (linebuf, len);
564 p = linebuf;
565 state = SPACE;
566 first = false;
567 continue;
569 switch (c)
571 case '\\':
572 state = BACKSLASH;
573 continue;
575 case '\'':
576 case '"':
577 state = QUOTE;
578 quotc = c;
579 continue;
581 break;
583 case QUOTE:
584 if (c == '\n')
585 error (1, 0, _("unmatched %s quote"),
586 quotc == '"' ? _("double") : _("single"));
587 if (c == quotc)
589 state = NORM;
590 continue;
592 break;
594 case BACKSLASH:
595 state = NORM;
596 break;
598 if (p >= endbuf)
599 error (1, 0, _("argument line too long"));
600 *p++ = c;
604 /* Read a null-terminated string from stdin and add it to the list of
605 arguments to pass to the command.
606 Return -1 if eof (either physical or logical) is reached,
607 otherwise the length of the string read (including the null). */
609 static int
610 read_string (void)
612 static boolean eof = false;
613 int len;
614 char *p = linebuf;
615 /* Including the NUL, the args must not grow past this point. */
616 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
618 if (eof)
619 return -1;
620 while (1)
622 int c = getc (stdin);
623 if (c == EOF)
625 eof = true;
626 if (p == linebuf)
627 return -1;
628 *p++ = '\0';
629 len = p - linebuf;
630 if (!replace_pat)
631 push_arg (linebuf, len);
632 return len;
634 if (c == '\0')
636 lineno++; /* For -l. */
637 *p++ = '\0';
638 len = p - linebuf;
639 if (!replace_pat)
640 push_arg (linebuf, len);
641 return len;
643 if (p >= endbuf)
644 error (1, 0, _("argument line too long"));
645 *p++ = c;
649 /* Finds the first occurrence of the substring NEEDLE in the string
650 HAYSTACK. Both strings can be multibyte strings. */
652 static char *
653 mbstrstr (const char *haystack, const char *needle)
655 #if DO_MULTIBYTE
656 if (MB_CUR_MAX > 1)
658 size_t hlen = strlen (haystack);
659 size_t nlen = strlen (needle);
660 mbstate_t mbstate;
661 size_t step;
663 memset (&mbstate, 0, sizeof (mbstate_t));
664 while (hlen >= nlen)
666 if (memcmp (haystack, needle, nlen) == 0)
667 return (char *) haystack;
668 step = mbrlen (haystack, hlen, &mbstate);
669 if (step <= 0)
670 break;
671 haystack += step;
672 hlen -= step;
674 return NULL;
676 #endif
677 return strstr (haystack, needle);
680 /* Replace all instances of `replace_pat' in ARG with `linebuf',
681 and add the resulting string to the list of arguments for the command
682 to execute.
683 ARGLEN is the length of ARG, not including the null.
684 LBLEN is the length of `linebuf', not including the null.
686 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
687 and a max of 5 occurrences of replace_pat in the initial-arguments.
688 Those restrictions do not exist here. */
690 static void
691 do_insert (char *arg, size_t arglen, size_t lblen)
693 /* Temporary copy of each arg with the replace pattern replaced by the
694 real arg. */
695 static char *insertbuf;
696 char *p;
697 int bytes_left = arg_max - 1; /* Bytes left on the command line. */
699 if (!insertbuf)
700 insertbuf = (char *) xmalloc (arg_max + 1);
701 p = insertbuf;
705 size_t len; /* Length in ARG before `replace_pat'. */
706 char *s = mbstrstr (arg, replace_pat);
707 if (s)
708 len = s - arg;
709 else
710 len = arglen;
711 bytes_left -= len;
712 if (bytes_left <= 0)
713 break;
715 strncpy (p, arg, len);
716 p += len;
717 arg += len;
718 arglen -= len;
720 if (s)
722 bytes_left -= lblen;
723 if (bytes_left <= 0)
724 break;
725 strcpy (p, linebuf);
726 arg += rplen;
727 arglen -= rplen;
728 p += lblen;
731 while (*arg);
732 if (*arg)
733 error (1, 0, _("command too long"));
734 *p++ = '\0';
735 push_arg (insertbuf, p - insertbuf);
738 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
739 to the command.
740 LEN is the length of ARG, including the terminating null.
741 If this brings the list up to its maximum size, execute the command. */
743 static void
744 push_arg (char *arg, size_t len)
746 if (arg)
748 if (cmd_argv_chars + len > arg_max)
750 if (initial_args || cmd_argc == initial_argc)
751 error (1, 0, _("can not fit single argument within argument list size limit"));
752 if (replace_pat
753 || (exit_if_size_exceeded &&
754 (lines_per_exec || args_per_exec)))
755 error (1, 0, _("argument list too long"));
756 do_exec ();
758 if (!initial_args && args_per_exec &&
759 cmd_argc - initial_argc == args_per_exec)
760 do_exec ();
763 if (cmd_argc >= cmd_argv_alloc)
765 if (!cmd_argv)
767 cmd_argv_alloc = 64;
768 cmd_argv = (char **) xmalloc (sizeof (char *) * cmd_argv_alloc);
770 else
772 cmd_argv_alloc *= 2;
773 cmd_argv = (char **) xrealloc (cmd_argv,
774 sizeof (char *) * cmd_argv_alloc);
778 if (!arg)
779 cmd_argv[cmd_argc++] = NULL;
780 else
782 cmd_argv[cmd_argc++] = argbuf + cmd_argv_chars;
783 strcpy (argbuf + cmd_argv_chars, arg);
784 cmd_argv_chars += len;
788 /* Print the arguments of the command to execute.
789 If ASK is nonzero, prompt the user for a response, and
790 if the user responds affirmatively, return true;
791 otherwise, return false. */
793 static boolean
794 print_args (boolean ask)
796 int i;
798 for (i = 0; i < cmd_argc - 1; i++)
799 fprintf (stderr, "%s ", cmd_argv[i]);
800 if (ask)
802 static FILE *tty_stream;
803 int c, savec;
805 if (!tty_stream)
807 tty_stream = fopen ("/dev/tty", "r");
808 if (!tty_stream)
809 error (1, errno, "/dev/tty");
811 fputs ("?...", stderr);
812 fflush (stderr);
813 c = savec = getc (tty_stream);
814 while (c != EOF && c != '\n')
815 c = getc (tty_stream);
816 if (savec == 'y' || savec == 'Y')
817 return true;
819 else
820 putc ('\n', stderr);
822 return false;
826 /* Close stdin and attach /dev/null to it.
827 * This resolves Savannah bug #3992.
829 static void
830 prep_child_for_exec (void)
832 const char inputfile[] = "/dev/null";
833 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
835 close(0);
836 if (open(inputfile, O_RDONLY) < 0)
838 /* This is not entirely fatal, since
839 * executing the child with a closed
840 * stdin is almost as good as executing it
841 * with its stdin attached to /dev/null.
843 error (0, errno, "%s", inputfile);
848 /* Execute the command that has been built in `cmd_argv'. This may involve
849 waiting for processes that were previously executed. */
851 static void
852 do_exec (void)
854 pid_t child;
856 push_arg ((char *) NULL, 0); /* Null terminate the arg list. */
857 if (!query_before_executing || print_args (true))
859 if (proc_max && procs_executing >= proc_max)
860 wait_for_proc (false);
861 if (!query_before_executing && print_command)
862 print_args (false);
863 /* If we run out of processes, wait for a child to return and
864 try again. */
865 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
866 wait_for_proc (false);
867 switch (child)
869 case -1:
870 error (1, errno, _("cannot fork"));
872 case 0: /* Child. */
873 prep_child_for_exec();
874 execvp (cmd_argv[0], cmd_argv);
875 error (0, errno, "%s", cmd_argv[0]);
876 _exit (errno == ENOENT ? 127 : 126);
878 add_proc (child);
881 cmd_argc = initial_argc;
882 cmd_argv_chars = initial_argv_chars;
885 /* Add the process with id PID to the list of processes that have
886 been executed. */
888 static void
889 add_proc (pid_t pid)
891 int i;
893 /* Find an empty slot. */
894 for (i = 0; i < pids_alloc && pids[i]; i++)
896 if (i == pids_alloc)
898 if (pids_alloc == 0)
900 pids_alloc = proc_max ? proc_max : 64;
901 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
903 else
905 pids_alloc *= 2;
906 pids = (pid_t *) xrealloc (pids,
907 sizeof (pid_t) * pids_alloc);
909 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
911 pids[i] = pid;
912 procs_executing++;
913 procs_executed++;
916 /* If ALL is true, wait for all child processes to finish;
917 otherwise, wait for one child process to finish.
918 Remove the processes that finish from the list of executing processes. */
920 static void
921 wait_for_proc (boolean all)
923 while (procs_executing)
925 int i, status;
929 pid_t pid;
931 while ((pid = wait (&status)) == (pid_t) -1)
932 if (errno != EINTR)
933 error (1, errno, _("error waiting for child process"));
935 /* Find the entry in `pids' for the child process
936 that exited. */
937 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
940 while (i == pids_alloc); /* A child died that we didn't start? */
942 /* Remove the child from the list. */
943 pids[i] = 0;
944 procs_executing--;
946 if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
947 exit (WEXITSTATUS (status)); /* Can't find or run the command. */
948 if (WEXITSTATUS (status) == 255)
949 error (124, 0, _("%s: exited with status 255; aborting"), cmd_argv[0]);
950 if (WIFSTOPPED (status))
951 error (125, 0, _("%s: stopped by signal %d"), cmd_argv[0], WSTOPSIG (status));
952 if (WIFSIGNALED (status))
953 error (125, 0, _("%s: terminated by signal %d"), cmd_argv[0], WTERMSIG (status));
954 if (WEXITSTATUS (status) != 0)
955 child_error = 123;
957 if (!all)
958 break;
962 /* Return the value of the number represented in STR.
963 OPTION is the command line option to which STR is the argument.
964 If the value does not fall within the boundaries MIN and MAX,
965 Print an error message mentioning OPTION and exit. */
967 static long
968 parse_num (char *str, int option, long int min, long int max)
970 char *eptr;
971 long val;
973 val = strtol (str, &eptr, 10);
974 if (eptr == str || *eptr)
976 fprintf (stderr, _("%s: invalid number for -%c option\n"),
977 program_name, option);
978 usage (stderr, 1);
980 else if (val < min)
982 fprintf (stderr, _("%s: value for -%c option must be >= %ld\n"),
983 program_name, option, min);
984 usage (stderr, 1);
986 else if (max >= 0 && val > max)
988 fprintf (stderr, _("%s: value for -%c option must be < %ld\n"),
989 program_name, option, max);
990 usage (stderr, 1);
992 return val;
995 /* Return how much of ARG_MAX is used by the environment. */
997 static long
998 env_size (char **envp)
1000 long len = 0;
1002 while (*envp)
1003 len += strlen (*envp++) + 1;
1005 return len;
1008 static void
1009 usage (FILE *stream, int status)
1011 fprintf (stream, _("\
1012 Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
1013 [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
1014 [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
1015 [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
1016 [--max-args=max-args] [--no-run-if-empty] [--version] [--help]\n\
1017 [command [initial-arguments]]\n"),
1018 program_name);
1019 fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);
1020 exit (status);