packaging: remove old options to find hidden files
[platform/upstream/findutils.git] / xargs / xargs.c
1 /* xargs -- build and execute command lines from standard input
2    Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000, 2003, 2004, 2005,
3    2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* Written by Mike Rendell <michael@cs.mun.ca>
20    and David MacKenzie <djm@gnu.org>.
21    Modifications by
22         James Youngman
23         Dmitry V. Levin
24 */
25
26 /* config.h must be included first. */
27 #include <config.h>
28
29 /* system headers. */
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <getopt.h>
35 #include <inttypes.h>
36 #include <limits.h>
37 #include <locale.h>
38 #include <signal.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <unistd.h>
46 #include <wchar.h>
47
48 /* gnulib headers. */
49 #include "closein.h"
50 #include "error.h"
51 #include "gettext.h"
52 #include "progname.h"
53 #include "quotearg.h"
54 #include "safe-read.h"
55 #include "verify.h"
56 #include "xalloc.h"
57
58 /* find headers. */
59 #include "buildcmd.h"
60 #include "findutils-version.h"
61
62 #if ENABLE_NLS
63 # include <libintl.h>
64 # define _(Text) gettext (Text)
65 #else
66 # define _(Text) Text
67 #define textdomain(Domain)
68 #define bindtextdomain(Package, Directory)
69 #endif
70 #ifdef gettext_noop
71 # define N_(String) gettext_noop(String)
72 #else
73 /* See locate.c for explanation as to why not use (String) */
74 # define N_(String) String
75 #endif
76
77 #ifndef LONG_MAX
78 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
79 #endif
80
81 #define ISBLANK(c) (isascii (c) && isblank (c))
82 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
83                     || (c) == '\f' || (c) == '\v')
84
85 /* Return nonzero if S is the EOF string.  */
86 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
87
88 extern char *version_string;
89
90 static FILE *input_stream;
91
92 /* Buffer for reading arguments from input.  */
93 static char *linebuf;
94
95 static int keep_stdin = 0;
96
97 /* Line number in stdin since the last command was executed.  */
98 static size_t lineno = 0;
99
100 static struct buildcmd_state bc_state;
101 static struct buildcmd_control bc_ctl;
102
103 /* Did we already complain about NUL characters in the input? */
104 static int nullwarning_given = 0;
105
106
107 /* If nonzero, when this string is read on stdin it is treated as
108    end of file.
109    IEEE Std 1003.1, 2004 Edition allows this to be NULL.
110    In findutils releases up to and including 4.2.8, this was "_".
111 */
112 static char *eof_str = NULL;
113
114 /* Number of chars in the initial args.  */
115 /* static int initial_argv_chars = 0; */
116
117 /* true when building up initial arguments in `cmd_argv'.  */
118 static bool initial_args = true;
119
120 /* If nonzero, the maximum number of child processes that can be running
121    at once.  */
122 /* TODO: check conversion safety (i.e. range) for -P option. */
123 static volatile sig_atomic_t proc_max = 1;
124
125 /* Did we fork a child yet? */
126 static bool procs_executed = false;
127
128 /* The number of elements in `pids'.  */
129 static unsigned long int procs_executing = 0uL;
130
131 /* List of child processes currently executing.  */
132 static pid_t *pids = NULL;
133
134 /* The number of allocated elements in `pids'. */
135 static size_t pids_alloc = 0u;
136
137 /* Process ID of the parent xargs process. */
138 static pid_t parent;
139
140 /* If nonzero, we've been signaled that we can start more child processes. */
141 static volatile sig_atomic_t stop_waiting = 0;
142
143 /* Exit status; nonzero if any child process exited with a
144    status of 1-125.  */
145 static volatile int child_error = EXIT_SUCCESS;
146
147 static volatile int original_exit_value;
148
149 /* If true, print each command on stderr before executing it.  */
150 static bool print_command = false; /* Option -t */
151
152 /* If true, query the user before executing each command, and only
153    execute the command if the user responds affirmatively.  */
154 static bool query_before_executing = false;
155
156 /* The delimiter for input arguments.   This is only consulted if the
157  * -0 or -d option had been given.
158  */
159 static char input_delimiter = '\0';
160
161
162 /* Name of the environment variable which indicates which 'slot'
163  * the child process is in.   This can be used to do some kind of basic
164  * load distribution.   We guarantee not to allow two processes to run
165  * at the same time with the same value of this variable.
166  */
167 static char* slot_var_name = NULL;
168
169 enum LongOptionIdentifier
170   {
171     PROCESS_SLOT_VAR = CHAR_MAX+1
172   };
173
174 static struct option const longopts[] =
175 {
176   {"null", no_argument, NULL, '0'},
177   {"arg-file", required_argument, NULL, 'a'},
178   {"delimiter", required_argument, NULL, 'd'},
179   {"eof", optional_argument, NULL, 'e'},
180   {"replace", optional_argument, NULL, 'I'},
181   {"max-lines", optional_argument, NULL, 'l'},
182   {"max-args", required_argument, NULL, 'n'},
183   {"interactive", no_argument, NULL, 'p'},
184   {"no-run-if-empty", no_argument, NULL, 'r'},
185   {"max-chars", required_argument, NULL, 's'},
186   {"verbose", no_argument, NULL, 't'},
187   {"show-limits", no_argument, NULL, 'S'},
188   {"exit", no_argument, NULL, 'x'},
189   {"max-procs", required_argument, NULL, 'P'},
190   {"process-slot-var", required_argument, NULL, PROCESS_SLOT_VAR},
191   {"version", no_argument, NULL, 'v'},
192   {"help", no_argument, NULL, 'h'},
193   {NULL, no_argument, NULL, 0}
194 };
195
196 /* xargs exits with status values with specific meanings (this is a POSIX requirement).
197    These are the values.
198 */
199 enum XargsStatusValues {
200   XARGS_EXIT_CLIENT_EXIT_NONZERO = 123, /* utility exited with nonzero status */
201   XARGS_EXIT_CLIENT_EXIT_255 = 124,     /* utility exites with status 255 */
202   XARGS_EXIT_CLIENT_FATAL_SIG = 125,    /* utility died from a fatal signal */
203   XARGS_EXIT_COMMAND_CANNOT_BE_RUN = 126, /* canot run the command */
204   XARGS_EXIT_COMMAND_NOT_FOUND = 127,     /* cannot find the command */
205 };
206 /* Exit status values the child might use. */
207 enum  ClientStatusValues {
208   CHILD_EXIT_PLEASE_STOP_IMMEDIATELY = 255
209 };
210
211 static int read_line (void);
212 static int read_string (void);
213 static bool print_args (bool ask);
214 /* static void do_exec (void); */
215 static int xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv);
216 static void exec_if_possible (void);
217 static unsigned int add_proc (pid_t pid);
218 static void wait_for_proc (bool all, unsigned int minreap);
219 static void wait_for_proc_all (void);
220 static void increment_proc_max (int);
221 static void decrement_proc_max (int);
222 static long parse_num (char *str, int option, long min, long max, int fatal);
223 static void usage (FILE * stream);
224
225
226 static char
227 get_char_oct_or_hex_escape (const char *s)
228 {
229   const char * p;
230   int base = 8;
231   unsigned long val;
232   char *endp;
233
234   assert ('\\' == s[0]);
235
236   if ('x' == s[1])
237     {
238       /* hex */
239       p = s+2;
240       base = 16;
241     }
242   else if (isdigit ((unsigned char) s[1]))
243     {
244       /* octal */
245       p = s+1;
246       base = 8;
247     }
248   else
249     {
250       p = NULL;                 /* Silence compiler warning. */
251       error (EXIT_FAILURE, 0,
252              _("Invalid escape sequence %s in input delimiter specification."),
253              s);
254     }
255   errno = 0;
256   endp = NULL;
257   val = strtoul (p, &endp, base);
258
259   /* This if condition is carefully constructed to do
260    * the right thing if UCHAR_MAX has the same
261    * value as ULONG_MAX.   IF UCHAR_MAX==ULONG_MAX,
262    * then val can never be greater than UCHAR_MAX.
263    */
264   if ((ULONG_MAX == val && ERANGE == errno)
265       || (val > UCHAR_MAX))
266     {
267       if (16 == base)
268         {
269           error (EXIT_FAILURE, 0,
270                  _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
271                  s, (unsigned long)UCHAR_MAX);
272         }
273       else
274         {
275           error (EXIT_FAILURE, 0,
276                  _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
277                  s, (unsigned long)UCHAR_MAX);
278         }
279     }
280
281   /* check for trailing garbage */
282   if (0 != *endp)
283     {
284       error (EXIT_FAILURE, 0,
285              _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
286              s, endp);
287     }
288
289   return (char) val;
290 }
291
292
293 static char
294 get_input_delimiter (const char *s)
295 {
296   if (1 == strlen (s))
297     {
298       return s[0];
299     }
300   else
301     {
302       if ('\\' == s[0])
303         {
304           /* an escape code */
305           switch (s[1])
306             {
307             case 'a':
308               return '\a';
309             case 'b':
310               return '\b';
311             case 'f':
312               return '\f';
313             case 'n':
314               return '\n';
315             case 'r':
316               return '\r';
317             case 't':
318               return'\t';
319             case 'v':
320               return '\v';
321             case '\\':
322               return '\\';
323             default:
324               return get_char_oct_or_hex_escape (s);
325             }
326         }
327       else
328         {
329           error (EXIT_FAILURE, 0,
330                  _("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
331                  s);
332           /*NOTREACHED*/
333           return 0;
334         }
335     }
336 }
337
338 static void
339 noop (void)
340 {
341   /* does nothing. */
342 }
343
344 static void
345 fail_due_to_env_size (void)
346 {
347   error (EXIT_FAILURE, 0, _("environment is too large for exec"));
348 }
349
350 static size_t
351 smaller_of (size_t a, size_t b)
352 {
353   if (a < b)
354     return a;
355   else
356     return b;
357 }
358
359
360
361 int
362 main (int argc, char **argv)
363 {
364   int optc, option_index;
365   int show_limits = 0;                  /* --show-limits */
366   int always_run_command = 1;
367   const char *input_file = "-"; /* "-" is stdin */
368   char default_cmd[] = "echo";
369   char *default_arglist[1];
370   int (*read_args) (void) = read_line;
371   void (*act_on_init_result)(void) = noop;
372   enum BC_INIT_STATUS bcstatus;
373   enum { XARGS_POSIX_HEADROOM = 2048u };
374   struct sigaction sigact;
375
376   if (argv[0])
377     set_program_name (argv[0]);
378   else
379     set_program_name ("xargs");
380
381   parent = getpid ();
382   original_exit_value = EXIT_SUCCESS;
383
384 #ifdef HAVE_SETLOCALE
385   setlocale (LC_ALL, "");
386 #endif
387   bindtextdomain (PACKAGE, LOCALEDIR);
388   textdomain (PACKAGE);
389
390   if (atexit (close_stdin) || atexit (wait_for_proc_all))
391     {
392       error (EXIT_FAILURE, errno, _("The atexit library function failed"));
393     }
394
395   /* xargs is required by POSIX to allow 2048 bytes of headroom
396    * for extra environment variables (that perhaps the utliity might
397    * want to set before execing something else).
398    */
399   bcstatus = bc_init_controlinfo (&bc_ctl, XARGS_POSIX_HEADROOM);
400
401   /* The bc_init_controlinfo call may have determined that the
402    * environment is too big.  In that case, we will fail with
403    * an error message after processing the command-line options,
404    * as "xargs --help" should still work even if the environment is
405    * too big.
406    *
407    * Some of the argument processing depends on the contents of
408    * bc_ctl, which will be in an undefined state if bc_init_controlinfo ()
409    * failed.
410    */
411   if (BC_INIT_ENV_TOO_BIG == bcstatus)
412     {
413       act_on_init_result = fail_due_to_env_size;
414     }
415   else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM == bcstatus)
416     {
417       /* All POSIX systems are required to support ARG_MAX of at least
418        * 4096.  For everything to work the total of (command line +
419        * headroom + environment) must fit into this.  POSIX requires
420        * that we use a headroom of 2048 bytes.  The user is in control
421        * of the size of the environment.
422        *
423        * In general if bc_init_controlinfo () returns
424        * BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
425        * with a smaller headroom.  However, in the case of xargs, this
426        * would not be POSIX-compliant.
427        */
428       act_on_init_result = fail_due_to_env_size;
429     }
430   else
431     {
432       /* IEEE Std 1003.1, 2003 specifies that the combined argument and
433        * environment list shall not exceed {ARG_MAX}-2048 bytes.  It also
434        * specifies that it shall be at least LINE_MAX.
435        */
436       long val;
437 #ifdef _SC_ARG_MAX
438       val = sysconf (_SC_ARG_MAX);
439       if (val > 0)
440         {
441           assert (val > XARGS_POSIX_HEADROOM);
442           /* Note that val can in fact be greater than ARG_MAX
443            * and bc_ctl.arg_max can also be greater than ARG_MAX.
444            */
445           bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
446                                        (size_t)val-XARGS_POSIX_HEADROOM);
447         }
448       else
449         {
450 # if defined ARG_MAX
451           assert (ARG_MAX > XARGS_POSIX_HEADROOM);
452           bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
453                                        (ARG_MAX - XARGS_POSIX_HEADROOM));
454 # endif
455         }
456 #else
457       /* No _SC_ARG_MAX */
458       assert (ARG_MAX > XARGS_POSIX_HEADROOM);
459       bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
460                                    (ARG_MAX - XARGS_POSIX_HEADROOM));
461 #endif
462
463
464 #ifdef LINE_MAX
465       /* This assertion ensures that this xargs implementation
466        * conforms to the POSIX requirement that the default command
467        * line length shall be at least LINE_MAX.
468        */
469       assert (bc_ctl.arg_max >= LINE_MAX);
470 #endif
471
472       bc_ctl.exec_callback = xargs_do_exec;
473
474       /* Start with a reasonable default size, though this can be
475        * adjusted via the -s option.
476        */
477       bc_use_sensible_arg_max (&bc_ctl);
478     }
479
480   while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
481                               longopts, &option_index)) != -1)
482     {
483       switch (optc)
484         {
485         case '0':
486           read_args = read_string;
487           input_delimiter = '\0';
488           break;
489
490         case 'd':
491           read_args = read_string;
492           input_delimiter = get_input_delimiter (optarg);
493           break;
494
495         case 'E':               /* POSIX */
496         case 'e':               /* deprecated */
497           if (optarg && (strlen (optarg) > 0))
498             eof_str = optarg;
499           else
500             eof_str = 0;
501           break;
502
503         case 'h':
504           usage (stdout);
505           return 0;
506
507         case 'I':               /* POSIX */
508         case 'i':               /* deprecated */
509           if (optarg)
510             bc_ctl.replace_pat = optarg;
511           else
512             bc_ctl.replace_pat = "{}";
513           /* -i excludes -n -l.  */
514           bc_ctl.args_per_exec = 0;
515           bc_ctl.lines_per_exec = 0;
516           break;
517
518         case 'L':               /* POSIX */
519           bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
520           /* -L excludes -i -n.  */
521           bc_ctl.args_per_exec = 0;
522           bc_ctl.replace_pat = NULL;
523           break;
524
525         case 'l':               /* deprecated */
526           if (optarg)
527             bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
528           else
529             bc_ctl.lines_per_exec = 1;
530           /* -l excludes -i -n.  */
531           bc_ctl.args_per_exec = 0;
532           bc_ctl.replace_pat = NULL;
533           break;
534
535         case 'n':
536           bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
537           /* -n excludes -i -l.  */
538           bc_ctl.lines_per_exec = 0;
539           if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
540             /* ignore -n1 in '-i -n1' */
541             bc_ctl.args_per_exec = 0;
542           else
543             bc_ctl.replace_pat = NULL;
544           break;
545
546           /* The POSIX standard specifies that it is not an error
547            * for the -s option to specify a size that the implementation
548            * cannot support - in that case, the relevant limit is used.
549            */
550         case 's':
551           {
552             size_t arg_size;
553             act_on_init_result ();
554             arg_size = parse_num (optarg, 's', 1L,
555                                   bc_ctl.posix_arg_size_max, 0);
556             if (arg_size > bc_ctl.posix_arg_size_max)
557               {
558                 error (0, 0,
559                        _("warning: value %ld for -s option is too large, "
560                          "using %ld instead"),
561                        (long) arg_size, (long) bc_ctl.posix_arg_size_max);
562                 arg_size = bc_ctl.posix_arg_size_max;
563               }
564             bc_ctl.arg_max = arg_size;
565           }
566           break;
567
568         case 'S':
569           show_limits = true;
570           break;
571
572         case 't':
573           print_command = true;
574           break;
575
576         case 'x':
577           bc_ctl.exit_if_size_exceeded = true;
578           break;
579
580         case 'p':
581           query_before_executing = true;
582           print_command = true;
583           break;
584
585         case 'r':
586           always_run_command = 0;
587           break;
588
589         case 'P':
590           /* Allow only up to LONG_MAX child processes. */
591           proc_max = parse_num (optarg, 'P', 0L, LONG_MAX, 1);
592           break;
593
594         case 'a':
595           input_file = optarg;
596           break;
597
598         case 'v':
599           display_findutils_version ("xargs");
600           return 0;
601
602         case PROCESS_SLOT_VAR:
603           if (strchr (optarg, '='))
604             {
605               error (EXIT_FAILURE, 0,
606                      _("option --%s may not be set to a value which includes `='"),
607                      longopts[option_index].name);
608             }
609           slot_var_name = optarg;
610           if (0 != unsetenv (slot_var_name))
611             {
612               /* This is a fatal error, otherwise some child process
613                  may not be able to guarantee that no two children
614                  have the same value for this variable; see
615                  set_slot_var.
616               */
617               error (EXIT_FAILURE, errno,
618                      _("failed to unset environment variable %s"),
619                      slot_var_name);
620             }
621           break;
622
623         default:
624           usage (stderr);
625           return 1;
626         }
627     }
628
629   if (eof_str && (read_args == read_string))
630     {
631       error (0, 0,
632              _("warning: the -E option has no effect if -0 or -d is used.\n"));
633     }
634
635   /* If we had deferred failing due to problems in bc_init_controlinfo (),
636    * do it now.
637    *
638    * We issue this error message after processing command line
639    * arguments so that it is possible to use "xargs --help" even if
640    * the environment is too large.
641    */
642   act_on_init_result ();
643   assert (BC_INIT_OK == bcstatus);
644
645 #ifdef SIGUSR1
646 #ifdef SIGUSR2
647   /* Accept signals to increase or decrease the number of running
648      child processes.  Do this as early as possible after setting
649      proc_max.  */
650   sigact.sa_handler = increment_proc_max;
651   sigemptyset(&sigact.sa_mask);
652   sigact.sa_flags = 0;
653   if (0 != sigaction (SIGUSR1, &sigact, (struct sigaction *)NULL))
654           error (0, errno, _("Cannot set SIGUSR1 signal handler"));
655
656   sigact.sa_handler = decrement_proc_max;
657   sigemptyset(&sigact.sa_mask);
658   sigact.sa_flags = 0;
659   if (0 != sigaction (SIGUSR2, &sigact, (struct sigaction *)NULL))
660           error (0, errno, _("Cannot set SIGUSR2 signal handler"));
661 #endif /* SIGUSR2 */
662 #endif /* SIGUSR1 */
663
664
665   if (0 == strcmp (input_file, "-"))
666     {
667       input_stream = stdin;
668     }
669   else
670     {
671       keep_stdin = 1;           /* see prep_child_for_exec () */
672       input_stream = fopen (input_file, "r");
673       if (NULL == input_stream)
674         {
675           error (EXIT_FAILURE, errno,
676                  _("Cannot open input file %s"),
677                  quotearg_n_style (0, locale_quoting_style, input_file));
678         }
679     }
680
681   if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
682     bc_ctl.exit_if_size_exceeded = true;
683
684   if (optind == argc)
685     {
686       optind = 0;
687       argc = 1;
688       default_arglist[0] = default_cmd;
689       argv = default_arglist;
690     }
691
692   if (show_limits)
693     {
694       fprintf (stderr,
695               _("Your environment variables take up %" PRIuMAX " bytes\n"),
696               (uintmax_t)bc_size_of_environment ());
697       fprintf (stderr,
698               _("POSIX upper limit on argument length (this system): %" PRIuMAX "\n"),
699               (uintmax_t)bc_ctl.posix_arg_size_max);
700       fprintf (stderr,
701               _("POSIX smallest allowable upper limit on argument length (all systems): %" PRIuMAX "\n"),
702               (uintmax_t)bc_ctl.posix_arg_size_min);
703       fprintf (stderr,
704               _("Maximum length of command we could actually use: %" PRIuMAX "\n"),
705               (uintmax_t)(bc_ctl.posix_arg_size_max - bc_size_of_environment ()));
706       fprintf (stderr,
707               _("Size of command buffer we are actually using: %" PRIuMAX "\n"),
708               (uintmax_t)bc_ctl.arg_max);
709
710       if (isatty (STDIN_FILENO))
711         {
712           fprintf (stderr,
713                   _("\n"
714                     "Execution of xargs will continue now, and it will "
715                     "try to read its input and run commands; if this is "
716                     "not what you wanted to happen, please type the "
717                     "end-of-file keystroke.\n"));
718           if (always_run_command)
719             {
720               fprintf (stderr,
721                       _("Warning: %s will be run at least once.  "
722                         "If you do not want that to happen, then press "
723                         "the interrupt keystroke.\n"),
724                       argv[optind]);
725             }
726         }
727     }
728
729   linebuf = xmalloc (bc_ctl.arg_max + 1);
730   bc_state.argbuf = xmalloc (bc_ctl.arg_max + 1);
731
732   /* Make sure to listen for the kids.  */
733   signal (SIGCHLD, SIG_DFL);
734
735   if (!bc_ctl.replace_pat)
736     {
737       for (; optind < argc; optind++)
738         bc_push_arg (&bc_ctl, &bc_state,
739                      argv[optind], strlen (argv[optind]) + 1,
740                      NULL, 0,
741                      initial_args);
742       initial_args = false;
743       bc_ctl.initial_argc = bc_state.cmd_argc;
744       bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;
745       bc_ctl.initial_argc = bc_state.cmd_argc;
746       /*fprintf (stderr, "setting initial_argc=%d\n", bc_state.cmd_initial_argc);*/
747
748       while ((*read_args) () != -1)
749         if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
750           {
751             bc_do_exec (&bc_ctl, &bc_state);
752             lineno = 0;
753           }
754
755       /* SYSV xargs seems to do at least one exec, even if the
756          input is empty.  */
757       if (bc_state.cmd_argc != bc_ctl.initial_argc
758           || (always_run_command && procs_executed==0))
759         bc_do_exec (&bc_ctl, &bc_state);
760
761     }
762   else
763     {
764       int i, args;
765       size_t *arglen = xmalloc (sizeof (size_t) * argc);
766
767       for (i = optind; i < argc; i++)
768         arglen[i] = strlen (argv[i]);
769       bc_ctl.rplen = strlen (bc_ctl.replace_pat);
770       while ((args = (*read_args) ()) != -1)
771         {
772           size_t len = (size_t) args;
773           /* Don't do insert on the command name.  */
774           bc_clear_args (&bc_ctl, &bc_state);
775           bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
776
777           bc_push_arg (&bc_ctl, &bc_state,
778                        argv[optind], arglen[optind] + 1,
779                        NULL, 0,
780                        initial_args);
781           len--;
782           initial_args = false;
783
784           for (i = optind + 1; i < argc; i++)
785             bc_do_insert (&bc_ctl, &bc_state,
786                           argv[i], arglen[i],
787                           NULL, 0,
788                           linebuf, len,
789                           initial_args);
790           bc_do_exec (&bc_ctl, &bc_state);
791         }
792     }
793
794   original_exit_value = child_error;
795   return child_error;
796 }
797
798
799 /* Read a line of arguments from the input and add them to the list of
800    arguments to pass to the command.  Ignore blank lines and initial blanks.
801    Single and double quotes and backslashes quote metacharacters and blanks
802    as they do in the shell.
803    Return -1 if eof (either physical or logical) is reached,
804    otherwise the length of the last string read (including the null).  */
805
806 static int
807 read_line (void)
808 {
809 /* States for read_line. */
810   enum read_line_state
811     {
812       NORM = 0,
813       SPACE = 1,
814       QUOTE = 2,
815       BACKSLASH = 3
816     };
817   static bool eof = false;
818   /* Start out in mode SPACE to always strip leading spaces (even with -i).  */
819   enum read_line_state state = SPACE; /* The type of character we last read.  */
820   int prevc;                    /* The previous value of c.  */
821   int quotc = 0;                /* The last quote character read.  */
822   int c = EOF;
823   bool first = true;            /* true if reading first arg on line.  */
824   bool seen_arg = false;      /* true if we have seen any arg (or part of one) yet */
825   int len;
826   char *p = linebuf;
827   /* Including the NUL, the args must not grow past this point.  */
828   char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
829
830   if (eof)
831     return -1;
832   while (1)
833     {
834       prevc = c;
835       c = getc (input_stream);
836
837       if (c == EOF)
838         {
839           /* COMPAT: SYSV seems to ignore stuff on a line that
840              ends without a \n; we don't.  */
841           eof = true;
842           if (p == linebuf)
843             return -1;
844           *p++ = '\0';
845           len = p - linebuf;
846           if (state == QUOTE)
847             {
848               exec_if_possible ();
849               error (EXIT_FAILURE, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
850                      quotc == '"' ? _("double") : _("single"));
851             }
852           if (first && EOF_STR (linebuf))
853             return -1;
854           if (!bc_ctl.replace_pat)
855             bc_push_arg (&bc_ctl, &bc_state,
856                          linebuf, len,
857                          NULL, 0,
858                          initial_args);
859           return len;
860         }
861       switch (state)
862         {
863         case SPACE:
864           if (ISSPACE (c))
865             continue;
866           state = NORM;
867           /* aaahhhh....  */
868
869         case NORM:
870           if (c == '\n')
871             {
872               if (!ISBLANK (prevc))
873                 lineno++;       /* For -l.  */
874               if (p == linebuf)
875                 {
876                   if (seen_arg)
877                     {
878                       /* An empty argument, add it to the list as normal. */
879                     }
880                   else
881                     {
882                       /* Blank line.  */
883                       state = SPACE;
884                       continue;
885                     }
886                 }
887               *p++ = '\0';
888               len = p - linebuf;
889               if (EOF_STR (linebuf))
890                 {
891                   eof = true;
892                   return first ? -1 : len;
893                 }
894               if (!bc_ctl.replace_pat)
895                 bc_push_arg (&bc_ctl, &bc_state,
896                              linebuf, len,
897                              NULL, 0,
898                              initial_args);
899               return len;
900             }
901           seen_arg = true;
902
903           /* POSIX: In the POSIX locale, the separators are <SPC> and
904            * <TAB>, but not <FF> or <VT>.
905            */
906           if (!bc_ctl.replace_pat && ISBLANK (c))
907             {
908               *p++ = '\0';
909               len = p - linebuf;
910               if (EOF_STR (linebuf))
911                 {
912                   eof = true;
913                   return first ? -1 : len;
914                 }
915               bc_push_arg (&bc_ctl, &bc_state,
916                            linebuf, len,
917                            NULL, 0,
918                            initial_args);
919               p = linebuf;
920               state = SPACE;
921               first = false;
922               continue;
923             }
924           switch (c)
925             {
926             case '\\':
927               state = BACKSLASH;
928               continue;
929
930             case '\'':
931             case '"':
932               state = QUOTE;
933               quotc = c;
934               continue;
935             }
936           break;
937
938         case QUOTE:
939           if (c == '\n')
940             {
941               exec_if_possible ();
942               error (EXIT_FAILURE, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
943                      quotc == '"' ? _("double") : _("single"));
944             }
945           if (c == quotc)
946             {
947               state = NORM;
948               seen_arg = true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
949               continue;
950             }
951           break;
952
953         case BACKSLASH:
954           state = NORM;
955           break;
956         }
957
958       if ( (0 == c) && !nullwarning_given )
959         {
960           /* This is just a warning message.  We only issue it once. */
961           error (0, 0,
962                  _("WARNING: a NUL character occurred in the input.  "
963                    "It cannot be passed through in the argument list.  "
964                    "Did you mean to use the --null option?"));
965           nullwarning_given = 1;
966         }
967
968 #if 1
969       if (p >= endbuf)
970         {
971           exec_if_possible ();
972           error (EXIT_FAILURE, 0, _("argument line too long"));
973         }
974       *p++ = c;
975 #else
976       append_char_to_buf (&linebuf, &endbuf, &p, c);
977 #endif
978     }
979 }
980
981 /* Read a string (terminated by the delimiter, which may be NUL) from
982    the input and add it to the list of arguments to pass to the
983    command.
984
985    The return value is the length of the added argument, including its
986    terminating NUL.  The added argument is always terminated by NUL,
987    even if that is not the delimiter.
988
989    If we reach physical EOF before seeing the delimiter, we treat any
990    characters read as the final argument.
991
992    If no argument was read (that is, we reached physical EOF before
993    reading any characters) then -1 is returned. */
994 static int
995 read_string (void)
996 {
997   static bool eof = false;
998   int len;
999   char *p = linebuf;
1000   /* Including the NUL, the args must not grow past this point.  */
1001   char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
1002
1003   if (eof)
1004     return -1;
1005   while (1)
1006     {
1007       int c = getc (input_stream);
1008       if (c == EOF)
1009         {
1010           eof = true;
1011           if (p == linebuf)
1012             return -1;
1013           *p++ = '\0';
1014           len = p - linebuf;
1015           if (!bc_ctl.replace_pat)
1016             bc_push_arg (&bc_ctl, &bc_state,
1017                          linebuf, len,
1018                          NULL, 0,
1019                          initial_args);
1020           return len;
1021         }
1022       if (c == input_delimiter)
1023         {
1024           lineno++;             /* For -l.  */
1025           *p++ = '\0';
1026           len = p - linebuf;
1027           if (!bc_ctl.replace_pat)
1028             bc_push_arg (&bc_ctl, &bc_state,
1029                          linebuf, len,
1030                          NULL, 0,
1031                          initial_args);
1032           return len;
1033         }
1034       if (p >= endbuf)
1035         {
1036           exec_if_possible ();
1037           error (EXIT_FAILURE, 0, _("argument line too long"));
1038         }
1039       *p++ = c;
1040     }
1041 }
1042
1043 /* Print the arguments of the command to execute.
1044    If ASK is nonzero, prompt the user for a response, and
1045    if the user responds affirmatively, return true;
1046    otherwise, return false.  */
1047
1048 static bool
1049 print_args (bool ask)
1050 {
1051   size_t i;
1052
1053   for (i = 0; i < bc_state.cmd_argc - 1; i++)
1054     {
1055       if (fprintf (stderr, "%s ", bc_state.cmd_argv[i]) < 0)
1056         error (EXIT_FAILURE, errno, _("Failed to write to stderr"));
1057     }
1058
1059   if (ask)
1060     {
1061       static FILE *tty_stream;
1062       int c, savec;
1063
1064       if (!tty_stream)
1065         {
1066           tty_stream = fopen ("/dev/tty", "r");
1067           if (!tty_stream)
1068             error (EXIT_FAILURE, errno,
1069                    _("failed to open /dev/tty for reading"));
1070         }
1071       fputs ("?...", stderr);
1072       if (fflush (stderr) != 0)
1073         error (EXIT_FAILURE, errno, _("Failed to write to stderr"));
1074
1075       c = savec = getc (tty_stream);
1076       while (c != EOF && c != '\n')
1077         c = getc (tty_stream);
1078       if (EOF == c)
1079         error (EXIT_FAILURE, errno, _("Failed to read from stdin"));
1080       if (savec == 'y' || savec == 'Y')
1081         return true;
1082     }
1083   else
1084     putc ('\n', stderr);
1085
1086   return false;
1087 }
1088
1089 /* Set SOME_ENVIRONMENT_VARIABLE=n in the environment. */
1090 static void
1091 set_slot_var (unsigned int n)
1092 {
1093   static const char *fmt = "%u";
1094   int size;
1095   char *buf;
1096
1097
1098   /* Determine the length of the buffer we need.
1099
1100      If the result would be zero-length or have length (not value) >
1101      INT_MAX, the assumptions we made about how snprintf behaves (or
1102      what UINT_MAX is) are wrong.  Hence we have a design error (not
1103      an environmental error).
1104   */
1105   size = snprintf (NULL, 0u, fmt, n);
1106   assert (size > 0);
1107
1108
1109   /* Failures here are undesirable but not fatal, since we can still
1110      guarantee that this child does not have a duplicate value of the
1111      indicated environment variable set (since the parent unset it on
1112      startup).
1113   */
1114   if (NULL == (buf = malloc (size+1)))
1115     {
1116       error (0, errno, _("unable to allocate memory"));
1117     }
1118   else
1119     {
1120       snprintf (buf, size+1, fmt, n);
1121
1122       /* If the user doesn't want us to set the variable, there is
1123          nothing to do.  However, we defer the bail-out until this
1124          point in order to get better test coverage.
1125       */
1126       if (slot_var_name)
1127         {
1128           if (setenv (slot_var_name, buf, 1) < 0)
1129             {
1130               error (0, errno,
1131                      _("failed to set environment variable %s"), slot_var_name);
1132             }
1133         }
1134       free (buf);
1135     }
1136 }
1137
1138
1139 /* Close stdin and attach /dev/null to it.
1140  * This resolves Savannah bug #3992.
1141  */
1142 static void
1143 prep_child_for_exec (void)
1144 {
1145   /* The parent will call add_proc to allocate a slot.  We do the same in the
1146      child to make sure we get the same value.
1147
1148      We use 0 here in order to avoid generating a data structure that appears
1149      to indicate that we (the child) have a child. */
1150   unsigned int slot = add_proc (0);
1151   set_slot_var (slot);
1152
1153   if (!keep_stdin)
1154     {
1155       const char inputfile[] = "/dev/null";
1156       /* fprintf (stderr, "attaching stdin to /dev/null\n"); */
1157
1158       close (0);
1159       if (open (inputfile, O_RDONLY) < 0)
1160         {
1161           /* This is not entirely fatal, since
1162            * executing the child with a closed
1163            * stdin is almost as good as executing it
1164            * with its stdin attached to /dev/null.
1165            */
1166           error (0, errno, "%s",
1167                  quotearg_n_style (0, locale_quoting_style, inputfile));
1168         }
1169     }
1170 }
1171
1172
1173 /* Execute the command that has been built in `cmd_argv'.  This may involve
1174    waiting for processes that were previously executed.
1175
1176    There are a number of cases where we want to terminate the current (child)
1177    process.  We do this by calling _exit () rather than exit () in order to
1178    avoid the invocation of wait_for_proc_all (), which was registered by the parent
1179    as an atexit () function.
1180 */
1181 static int
1182 xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
1183 {
1184   pid_t child;
1185   int fd[2];
1186   int buf;
1187   size_t r;
1188
1189   (void) ctl;
1190   (void) argc;
1191   (void) usercontext;
1192
1193   if (proc_max)
1194     {
1195       while (procs_executing >= proc_max)
1196         {
1197           wait_for_proc (false, 1u);
1198         }
1199     }
1200
1201   if (!query_before_executing || print_args (true))
1202     {
1203       if (!query_before_executing && print_command)
1204         print_args (false);
1205
1206       /* Before forking, reap any already-exited child. We do this so
1207          that we don't leave unreaped children around while we build a
1208          new command line.  For example this command will spend most
1209          of its time waiting for sufficient arguments to launch
1210          another command line:
1211
1212          seq 1 1000 | fmt | while read x ; do echo $x; sleep 1 ; done |
1213          ./xargs -P 200 -n 20  sh -c 'echo "$@"; sleep $((1 + $RANDOM % 5))' sleeper
1214       */
1215       wait_for_proc (false, 0u);
1216
1217       if (pipe (fd))
1218         error (EXIT_FAILURE, errno, _("could not create pipe before fork"));
1219       fcntl (fd[1], F_SETFD, FD_CLOEXEC);
1220
1221       /* If we run out of processes, wait for a child to return and
1222          try again.  */
1223       while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1224         wait_for_proc (false, 1u);
1225
1226       switch (child)
1227         {
1228         case -1:
1229           error (EXIT_FAILURE, errno, _("cannot fork"));
1230
1231         case 0:         /* Child.  */
1232           {
1233             close (fd[0]);
1234             child_error = EXIT_SUCCESS;
1235
1236             prep_child_for_exec ();
1237
1238             if (bc_args_exceed_testing_limit (argv))
1239               errno = E2BIG;
1240             else
1241               execvp (argv[0], argv);
1242             if (errno)
1243               {
1244                 /* Write errno value to parent.  We do this even if
1245                  * the error was not E2BIG, because we need to
1246                  * distinguish successful execs from unsuccessful
1247                  * ones.  The reason we need to do this is to know
1248                  * whether to reap the child here (preventing the
1249                  * exit status processing in wait_for_proc () from
1250                  * changing the value of child_error) or leave it
1251                  * for wait_for_proc () to handle.  We need to have
1252                  * wait_for_proc () handle the exit values from the
1253                  * utility if we run it, for POSIX compliance on the
1254                  * handling of exit values.
1255                  */
1256                 write (fd[1], &errno, sizeof (int));
1257               }
1258
1259             close (fd[1]);
1260             if (E2BIG != errno)
1261               {
1262                 error (0, errno, "%s", argv[0]);
1263               }
1264             /* The actual value returned here should be irrelevant,
1265              * because the parent will test our value of errno.
1266              */
1267             _exit (errno == ENOENT ? XARGS_EXIT_COMMAND_NOT_FOUND : XARGS_EXIT_COMMAND_CANNOT_BE_RUN);
1268
1269           /*NOTREACHED*/
1270           } /* child */
1271
1272         default:
1273           {
1274             /* Parent */
1275             close (fd[1]);
1276           }
1277
1278         } /* switch (child) */
1279       /*fprintf (stderr, "forked child (bc_state.cmd_argc=%d) -> ", bc_state.cmd_argc);*/
1280
1281       /* We use safe_read here in order to avoid an error if
1282          SIGUSR[12] is handled during the read system call. */
1283       switch (r = safe_read (fd[0], &buf, sizeof (int)))
1284         {
1285         case SAFE_READ_ERROR:
1286           {
1287             close (fd[0]);
1288             error (0, errno,
1289                    _("errno-buffer safe_read failed in xargs_do_exec "
1290                      "(this is probably a bug, please report it)"));
1291             break;
1292           }
1293
1294         case sizeof (int):
1295           {
1296             /* Failure */
1297             int childstatus;
1298
1299             close (fd[0]);
1300
1301             /* we know the child is about to exit, so wait for that.
1302              * We have to do this so that wait_for_proc () does not
1303              * change the value of child_error on the basis of the
1304              * return value -- since in this case we did not launch
1305              * the utility.
1306              *
1307              * We do the wait before deciding if we failed in order to
1308              * avoid creating a zombie, even briefly.
1309              */
1310             waitpid (child, &childstatus, 0);
1311
1312
1313             if (E2BIG == buf)
1314               {
1315                 return 0; /* Failure; caller should pass fewer args */
1316               }
1317             else if (ENOENT == buf)
1318               {
1319                 exit (XARGS_EXIT_COMMAND_NOT_FOUND); /* command cannot be found */
1320               }
1321             else
1322               {
1323                 exit (XARGS_EXIT_COMMAND_CANNOT_BE_RUN); /* command cannot be run */
1324               }
1325             break;
1326           }
1327
1328         case 0:
1329           {
1330             /* Failed to read data from pipe; the exec must have
1331              * succeeded.  We call add_proc only in this case,
1332              * because it increments procs_executing, and we only
1333              * want to do that if we didn't already wait for the
1334              * child.
1335              */
1336             add_proc (child);
1337             break;
1338           }
1339         default:
1340           {
1341             error (EXIT_FAILURE, errno,
1342                    _("read returned unexpected value %zu; "
1343                      "this is probably a bug, please report it"), r);
1344           }
1345         } /* switch on bytes read */
1346       close (fd[0]);
1347     }
1348   return 1;                     /* Success */
1349 }
1350
1351 /* Execute the command if possible.  */
1352
1353 static void
1354 exec_if_possible (void)
1355 {
1356   if (bc_ctl.replace_pat || initial_args ||
1357       bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
1358     return;
1359   bc_do_exec (&bc_ctl, &bc_state);
1360 }
1361
1362 /* Add the process with id PID to the list of processes that have
1363    been executed.  */
1364
1365 static unsigned int
1366 add_proc (pid_t pid)
1367 {
1368   unsigned int i, j;
1369
1370   /* Find an empty slot.  */
1371   for (i = 0; i < pids_alloc && pids[i]; i++)
1372     ;
1373
1374   /* Extend the array if we failed. */
1375   if (i == pids_alloc)
1376     {
1377       pids = x2nrealloc (pids, &pids_alloc, sizeof *pids);
1378
1379       /* Zero out the new slots. */
1380       for (j=i; j<pids_alloc; ++j)
1381         pids[j] = (pid_t)0;
1382     }
1383   /* Verify that we are not destroying the record of some existing child. */
1384   assert (0 == pids[i]);
1385
1386   /* Remember the child. */
1387   pids[i] = pid;
1388   procs_executing++;
1389   procs_executed = true;
1390   return i;
1391 }
1392
1393
1394 /* If ALL is true, wait for all child processes to finish;
1395    otherwise, wait for one child process to finish, or for another signal
1396    that tells us that we can run more child processes.
1397    Remove the processes that finish from the list of executing processes.  */
1398
1399 static void
1400 wait_for_proc (bool all, unsigned int minreap)
1401 {
1402   unsigned int reaped = 0;
1403
1404   while (procs_executing)
1405     {
1406       unsigned int i;
1407       int status;
1408       pid_t pid;
1409       int wflags = 0;
1410
1411       if (!all)
1412         {
1413           if (reaped >= minreap)
1414             {
1415               /* We already reaped enough children.  To save system
1416                * resources, reap any dead children anyway, but do not
1417                * wait for any currently executing children to exit.
1418
1419                */
1420               wflags = WNOHANG;
1421             }
1422         }
1423
1424       stop_waiting = 0;
1425       do
1426         {
1427           /* Wait for any child.   We used to use wait () here, but it's
1428            * unlikely that that offers any portability advantage over
1429            * wait these days.
1430            */
1431           while ((pid = waitpid (-1, &status, wflags)) == (pid_t) -1)
1432             {
1433               if (errno != EINTR)
1434                 error (EXIT_FAILURE, errno,
1435                        _("error waiting for child process"));
1436
1437               if (stop_waiting && !all)
1438                 {
1439                   /* Receipt of SIGUSR1 gave us an extra slot and we
1440                    * don't need to wait for all processes to finish.
1441                    * We can stop reaping now, but in any case check for
1442                    * further dead children without waiting for another
1443                    * to exit.
1444                    */
1445                   wflags = WNOHANG;
1446                 }
1447             }
1448
1449           /* Find the entry in `pids' for the child process
1450              that exited.  */
1451           if (pid)
1452             {
1453               for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1454                 ;
1455             }
1456         }
1457       while (pid && i == pids_alloc);   /* A child died that we didn't start? */
1458
1459       if (!pid)
1460         {
1461           if (!(wflags & WNOHANG))
1462             {
1463               /* Nothing remained to be reaped.  This should not
1464                * happen, because procs_executing should contain the
1465                * number of child processes still executing, so the
1466                * loop should have terminated.
1467                */
1468               error (0, 0, _("WARNING: Lost track of %lu child processes"),
1469                      procs_executing);
1470             }
1471           else
1472             {
1473               /* Children are (probably) executing but are not ready
1474                * to be reaped at the moment.
1475                */
1476             }
1477           break;
1478         }
1479
1480       /* Remove the child from the list.  */
1481       pids[i] = 0;
1482       procs_executing--;
1483       reaped++;
1484
1485       if (WEXITSTATUS (status) == CHILD_EXIT_PLEASE_STOP_IMMEDIATELY)
1486         error (XARGS_EXIT_CLIENT_EXIT_255, 0,
1487                _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
1488       if (WIFSTOPPED (status))
1489         error (XARGS_EXIT_CLIENT_FATAL_SIG, 0,
1490                _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
1491       if (WIFSIGNALED (status))
1492         error (XARGS_EXIT_CLIENT_FATAL_SIG, 0,
1493                _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
1494       if (WEXITSTATUS (status) != 0)
1495         child_error = XARGS_EXIT_CLIENT_EXIT_NONZERO;
1496     }
1497 }
1498
1499 /* Wait for all child processes to finish.  */
1500
1501 static void
1502 wait_for_proc_all (void)
1503 {
1504   static bool waiting = false;
1505
1506   /* This function was registered by the original, parent, process.
1507    * The child processes must not call exit () to terminate, since this
1508    * will mean that their exit status will be inappropriately changed.
1509    * Instead the child processes should call _exit ().  If someone
1510    * forgets this rule, you may see the following assert () fail.
1511    */
1512   assert (getpid () == parent);
1513
1514   if (waiting)
1515     return;
1516
1517   waiting = true;
1518   wait_for_proc (true, 0u);
1519   waiting = false;
1520
1521   if (original_exit_value != child_error)
1522     {
1523       /* wait_for_proc () changed the value of child_error ().  This
1524        * function is registered via atexit (), and so may have been
1525        * called from exit ().  We now know that the original value
1526        * passed to exit () is no longer the exit status we require.
1527        * The POSIX standard states that the behaviour if exit () is
1528        * called more than once is undefined.  Therefore we now have to
1529        * exit with _exit () instead of exit ().
1530        */
1531       _exit (child_error);
1532     }
1533
1534 }
1535
1536
1537 /* Increment or decrement the number of processes we can start simultaneously,
1538    when we receive a signal from the outside world.
1539
1540    We must take special care around proc_max == 0 (unlimited children),
1541    proc_max == 1 (don't decrement to zero).  */
1542 static void
1543 increment_proc_max (int ignore)
1544 {
1545         (void) ignore;
1546         /* If user increments from 0 to 1, we'll take it and serialize. */
1547         proc_max++;
1548         /* If we're waiting for a process to die before doing something,
1549            no need to wait any more. */
1550         stop_waiting = 1;
1551 }
1552
1553 static void
1554 decrement_proc_max (int ignore)
1555 {
1556         (void) ignore;
1557         if (proc_max > 1)
1558                 proc_max--;
1559 }
1560
1561
1562 /* Return the value of the number represented in STR.
1563    OPTION is the command line option to which STR is the argument.
1564    If the value does not fall within the boundaries MIN and MAX,
1565    Print an error message mentioning OPTION.  If FATAL is true,
1566    we also exit. */
1567
1568 static long
1569 parse_num (char *str, int option, long int min, long int max, int fatal)
1570 {
1571   char *eptr;
1572   long val;
1573
1574   val = strtol (str, &eptr, 10);
1575   if (eptr == str || *eptr)
1576     {
1577       fprintf (stderr, _("%s: invalid number for -%c option\n"),
1578                program_name, option);
1579       usage (stderr);
1580       exit (EXIT_FAILURE);
1581     }
1582   else if (val < min)
1583     {
1584       fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
1585                program_name, option, min);
1586       if (fatal)
1587         {
1588           usage (stderr);
1589           exit (EXIT_FAILURE);
1590         }
1591       else
1592         {
1593           val = min;
1594         }
1595     }
1596   else if (max >= 0 && val > max)
1597     {
1598       fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
1599                program_name, option, max);
1600       if (fatal)
1601         {
1602           usage (stderr);
1603           exit (EXIT_FAILURE);
1604         }
1605       else
1606         {
1607           val = max;
1608         }
1609     }
1610   return val;
1611 }
1612
1613 static void
1614 usage (FILE *stream)
1615 {
1616   fprintf (stream,
1617            _("Usage: %s [OPTION]... COMMAND [INITIAL-ARGS]...\n"),
1618            program_name);
1619
1620 #define HTL(t) fputs (t, stream);
1621
1622   HTL (_("Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.\n"
1623          "\n"));
1624   HTL (_("Mandatory and optional arguments to long options are also\n"
1625          "mandatory or optional for the corresponding short option.\n"));
1626   HTL (_("  -0, --null                   items are separated by a null, not whitespace;\n"
1627          "                                 disables quote and backslash processing and\n"
1628          "                                 logical EOF processing\n"));
1629   HTL (_("  -a, --arg-file=FILE          read arguments from FILE, not standard input\n"));
1630   HTL (_("  -d, --delimiter=CHARACTER    items in input stream are separated by CHARACTER,\n"
1631          "                                 not by whitespace; disables quote and backslash\n"
1632          "                                 processing and logical EOF processing\n"));
1633   HTL (_("  -E END                       set logical EOF string; if END occurs as a line\n"
1634          "                                 of input, the rest of the input is ignored\n"
1635          "                                 (ignored if -0 or -d was specified)\n"));
1636   HTL (_("  -e, --eof[=END]              equivalent to -E END if END is specified;\n"
1637          "                                 otherwise, there is no end-of-file string\n"));
1638   HTL (_("  -I R                         same as --replace=R\n"));
1639   HTL (_("  -i, --replace[=R]            replace R in INITIAL-ARGS with names read\n"
1640          "                                 from standard input; if R is unspecified,\n"
1641          "                                 assume {}\n"));
1642   HTL (_("  -L, --max-lines=MAX-LINES    use at most MAX-LINES non-blank input lines per\n"
1643          "                                 command line\n"));
1644   HTL (_("  -l[MAX-LINES]                similar to -L but defaults to at most one non-\n"
1645          "                                 blank input line if MAX-LINES is not specified\n"));
1646   HTL (_("  -n, --max-args=MAX-ARGS      use at most MAX-ARGS arguments per command line\n"));
1647   HTL (_("  -P, --max-procs=MAX-PROCS    run at most MAX-PROCS processes at a time\n"));
1648   HTL (_("  -p, --interactive            prompt before running commands\n"));
1649   HTL (_("      --process-slot-var=VAR   set environment variable VAR in child processes\n"));
1650   HTL (_("  -r, --no-run-if-empty        if there are no arguments, then do not run COMMAND;\n"
1651          "                                 if this option is not given, COMMAND will be\n"
1652          "                                 run at least once\n"));
1653   HTL (_("  -s, --max-chars=MAX-CHARS    limit length of command line to MAX-CHARS\n"));
1654   HTL (_("      --show-limits            show limits on command-line length\n"));
1655   HTL (_("  -t, --verbose                print commands before executing them\n"));
1656   HTL (_("  -x, --exit                   exit if the size (see -s) is exceeded\n"));
1657
1658   HTL (_("      --help                   display this help and exit\n"));
1659   HTL (_("      --version                output version information and exit\n"));
1660   HTL (_("\n"
1661          "Report bugs to <bug-findutils@gnu.org>.\n"));
1662 }