1/* xargs -- build and execute command lines from standard input
2   Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3
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 3 of the License, or
7   (at your option) any later version.
8
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.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/* Written by Mike Rendell <michael@cs.mun.ca>
19   and David MacKenzie <djm@gnu.org>.
20   Modifications by
21	James Youngman
22	Dmitry V. Levin
23*/
24
25#include <config.h>
26
27# ifndef PARAMS
28#  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
29#   define PARAMS(Args) Args
30#  else
31#   define PARAMS(Args) ()
32#  endif
33# endif
34
35#include <ctype.h>
36
37#if !defined (isascii) || defined (STDC_HEADERS)
38#ifdef isascii
39#undef isascii
40#endif
41#define isascii(c) 1
42#endif
43
44#ifdef isblank
45#define ISBLANK(c) (isascii (c) && isblank (c))
46#else
47#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
48#endif
49
50#define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
51		    || (c) == '\f' || (c) == '\v')
52
53#include <sys/types.h>
54#include <stdio.h>
55#include <errno.h>
56#include <getopt.h>
57#include <fcntl.h>
58
59#if defined(STDC_HEADERS)
60#include <assert.h>
61#endif
62
63#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
64#include <string.h>
65#if !defined(STDC_HEADERS)
66#include <memory.h>
67#endif
68#else
69#include <strings.h>
70#define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
71#endif
72
73#ifndef _POSIX_SOURCE
74#include <sys/param.h>
75#endif
76
77#ifdef HAVE_LIMITS_H
78#include <limits.h>
79#endif
80
81#ifndef LONG_MAX
82#define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
83#endif
84
85/* The presence of unistd.h is assumed by gnulib these days, so we
86 * might as well assume it too.
87 */
88#include <unistd.h>
89
90#include <signal.h>
91
92#if !defined(SIGCHLD) && defined(SIGCLD)
93#define SIGCHLD SIGCLD
94#endif
95
96#include "wait.h"
97
98/* States for read_line. */
99#define NORM 0
100#define SPACE 1
101#define QUOTE 2
102#define BACKSLASH 3
103
104#ifdef STDC_HEADERS
105#include <stdlib.h>
106#else
107extern int errno;
108#endif
109
110#ifdef HAVE_LOCALE_H
111#include <locale.h>
112#endif
113#if ENABLE_NLS
114# include <libintl.h>
115# define _(Text) gettext (Text)
116#else
117# define _(Text) Text
118#define textdomain(Domain)
119#define bindtextdomain(Package, Directory)
120#endif
121#ifdef gettext_noop
122# define N_(String) gettext_noop (String)
123#else
124/* See locate.c for explanation as to why not use (String) */
125# define N_(String) String
126#endif
127
128#include "buildcmd.h"
129
130
131/* Return nonzero if S is the EOF string.  */
132#define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
133
134/* Do multibyte processing if multibyte characters are supported,
135   unless multibyte sequences are search safe.  Multibyte sequences
136   are search safe if searching for a substring using the byte
137   comparison function 'strstr' gives no false positives.  All 8-bit
138   encodings and the UTF-8 multibyte encoding are search safe, but
139   the EUC encodings are not.
140   BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
141#if defined __BEOS__
142# define MULTIBYTE_IS_SEARCH_SAFE 1
143#endif
144#define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
145
146#if DO_MULTIBYTE
147# if HAVE_MBRLEN
148#  include <wchar.h>
149# else
150   /* Simulate mbrlen with mblen as best we can.  */
151#  define mbstate_t int
152#  define mbrlen(s, n, ps) mblen (s, n)
153# endif
154#endif
155
156/* Not char because of type promotion; NeXT gcc can't handle it.  */
157typedef int boolean;
158#define		true    1
159#define		false	0
160
161#if __STDC__
162#define VOID void
163#else
164#define VOID char
165#endif
166
167#include <xalloc.h>
168#include "closein.h"
169#include "gnulib-version.h"
170
171void error PARAMS ((int status, int errnum, char *message,...));
172
173extern char *version_string;
174
175/* The name this program was run with.  */
176char *program_name;
177
178static FILE *input_stream;
179
180/* Buffer for reading arguments from input.  */
181static char *linebuf;
182
183static int keep_stdin = 0;
184
185/* Line number in stdin since the last command was executed.  */
186static int lineno = 0;
187
188static struct buildcmd_state bc_state;
189static struct buildcmd_control bc_ctl;
190
191
192/* If nonzero, when this string is read on stdin it is treated as
193   end of file.
194   IEEE Std 1003.1, 2004 Edition allows this to be NULL.
195   In findutils releases up to and including 4.2.8, this was "_".
196*/
197static char *eof_str = NULL;
198
199/* Number of chars in the initial args.  */
200/* static int initial_argv_chars = 0; */
201
202/* true when building up initial arguments in `cmd_argv'.  */
203static boolean initial_args = true;
204
205/* If nonzero, the maximum number of child processes that can be running
206   at once.  */
207static int proc_max = 1;
208
209/* Total number of child processes that have been executed.  */
210static int procs_executed = 0;
211
212/* The number of elements in `pids'.  */
213static int procs_executing = 0;
214
215/* List of child processes currently executing.  */
216static pid_t *pids = NULL;
217
218/* The number of allocated elements in `pids'. */
219static int pids_alloc = 0;
220
221/* Exit status; nonzero if any child process exited with a
222   status of 1-125.  */
223static volatile int child_error = 0;
224
225static volatile int original_exit_value;
226
227/* If true, print each command on stderr before executing it.  */
228static boolean print_command = false; /* Option -t */
229
230/* If true, query the user before executing each command, and only
231   execute the command if the user responds affirmatively.  */
232static boolean query_before_executing = false;
233
234/* The delimiter for input arguments.   This is only consulted if the
235 * -0 or -d option had been given.
236 */
237static char input_delimiter = '\0';
238
239
240static struct option const longopts[] =
241{
242  {"null", no_argument, NULL, '0'},
243  {"arg-file", required_argument, NULL, 'a'},
244  {"delimiter", required_argument, NULL, 'd'},
245  {"eof", optional_argument, NULL, 'e'},
246  {"replace", optional_argument, NULL, 'I'},
247  {"max-lines", optional_argument, NULL, 'l'},
248  {"max-args", required_argument, NULL, 'n'},
249  {"interactive", no_argument, NULL, 'p'},
250  {"no-run-if-empty", no_argument, NULL, 'r'},
251  {"max-chars", required_argument, NULL, 's'},
252  {"verbose", no_argument, NULL, 't'},
253  {"show-limits", no_argument, NULL, 'S'},
254  {"exit", no_argument, NULL, 'x'},
255  {"max-procs", required_argument, NULL, 'P'},
256  {"version", no_argument, NULL, 'v'},
257  {"help", no_argument, NULL, 'h'},
258  {NULL, no_argument, NULL, 0}
259};
260
261static int read_line PARAMS ((void));
262static int read_string PARAMS ((void));
263static boolean print_args PARAMS ((boolean ask));
264/* static void do_exec PARAMS ((void)); */
265static int xargs_do_exec (const struct buildcmd_control *cl, struct buildcmd_state *state);
266static void exec_if_possible PARAMS ((void));
267static void add_proc PARAMS ((pid_t pid));
268static void wait_for_proc PARAMS ((boolean all));
269static void wait_for_proc_all PARAMS ((void));
270static long parse_num PARAMS ((char *str, int option, long min, long max, int fatal));
271static void usage PARAMS ((FILE * stream));
272
273
274
275static char
276get_char_oct_or_hex_escape(const char *s)
277{
278  const char * p;
279  int base = 8;
280  unsigned long val;
281  char *endp;
282
283  assert('\\' == s[0]);
284
285  if ('x' == s[1])
286    {
287      /* hex */
288      p = s+2;
289      base = 16;
290    }
291  else if (isdigit(s[1]))
292    {
293      /* octal */
294      p = s+1;
295      base = 8;
296    }
297  else
298    {
299      error(1, 0,
300	    _("Invalid escape sequence %s in input delimiter specification."),
301	    s);
302    }
303  errno = 0;
304  endp = (char*)p;
305  val = strtoul(p, &endp, base);
306
307  /* This if condition is carefully constructed to do
308   * the right thing if UCHAR_MAX has the same
309   * value as ULONG_MAX.   IF UCHAR_MAX==ULONG_MAX,
310   * then val can never be greater than UCHAR_MAX.
311   */
312  if ((ULONG_MAX == val && ERANGE == errno)
313      || (val > UCHAR_MAX))
314    {
315      if (16 == base)
316	{
317	  error(1, 0,
318		_("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
319		s, (unsigned long)UCHAR_MAX);
320	}
321      else
322	{
323	  error(1, 0,
324		_("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
325		s, (unsigned long)UCHAR_MAX);
326	}
327    }
328
329  /* check for trailing garbage */
330  if (0 != *endp)
331    {
332      error(1, 0,
333	    _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
334	    s, endp);
335    }
336
337  return (char) val;
338}
339
340
341static char
342get_input_delimiter(const char *s)
343{
344  if (1 == strlen(s))
345    {
346      return s[0];
347    }
348  else
349    {
350      if ('\\' == s[0])
351	{
352	  /* an escape code */
353	  switch (s[1])
354	    {
355	    case 'a':
356	      return '\a';
357	    case 'b':
358	      return '\b';
359	    case 'f':
360	      return '\f';
361	    case 'n':
362	      return '\n';
363	    case 'r':
364	      return '\r';
365	    case 't':
366	      return'\t';
367	    case 'v':
368	      return '\v';
369	    case '\\':
370	      return '\\';
371	    default:
372	      return get_char_oct_or_hex_escape(s);
373	    }
374	}
375      else
376	{
377	  error(1, 0,
378		_("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
379		s);
380	  abort ();
381	}
382    }
383}
384
385static void
386noop (void)
387{
388  /* does nothing. */
389}
390
391static void
392fail_due_to_env_size (void)
393{
394  error (1, 0, _("environment is too large for exec"));
395}
396
397
398int
399main (int argc, char **argv)
400{
401  int optc;
402  int show_limits = 0;			/* --show-limits */
403  int always_run_command = 1;
404  char *input_file = "-"; /* "-" is stdin */
405  char *default_cmd = "/bin/echo";
406  int (*read_args) PARAMS ((void)) = read_line;
407  void (*act_on_init_result)(void) = noop;
408  enum BC_INIT_STATUS bcstatus;
409
410  program_name = argv[0];
411  original_exit_value = 0;
412
413#ifdef HAVE_SETLOCALE
414  setlocale (LC_ALL, "");
415#endif
416  bindtextdomain (PACKAGE, LOCALEDIR);
417  textdomain (PACKAGE);
418  atexit (close_stdin);
419  atexit (wait_for_proc_all);
420
421  bcstatus = bc_init_controlinfo(&bc_ctl);
422  /* The bc_init_controlinfo call may have determined that the
423   * environment is too big.  In that case, we will fail with
424   * an error message after processing the command-line options,
425   * as "xargs --help" should still work even if the environment is
426   * too big.
427   *
428   * Some of the argument processing depends on the contents of
429   * bc_ctl, which will be in an undefined state if bc_init_controlinfo()
430   * failed.
431   */
432  if (BC_INIT_ENV_TOO_BIG == bcstatus)
433    {
434      act_on_init_result = fail_due_to_env_size;
435    }
436  else
437    {
438      /* IEEE Std 1003.1, 2003 specifies that the combined argument and
439       * environment list shall not exceed {ARG_MAX}-2048 bytes.  It also
440       * specifies that it shall be at least LINE_MAX.
441       */
442#if defined(ARG_MAX)
443      assert(bc_ctl.arg_max <= (ARG_MAX-2048));
444#endif
445#ifdef LINE_MAX
446      assert(bc_ctl.arg_max >= LINE_MAX);
447#endif
448
449      bc_ctl.exec_callback = xargs_do_exec;
450
451      /* Start with a reasonable default size, though this can be
452       * adjusted via the -s option.
453       */
454      bc_use_sensible_arg_max(&bc_ctl);
455    }
456
457  while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
458			      longopts, (int *) 0)) != -1)
459    {
460      switch (optc)
461	{
462	case '0':
463	  read_args = read_string;
464	  input_delimiter = '\0';
465	  break;
466
467	case 'd':
468	  read_args = read_string;
469	  input_delimiter = get_input_delimiter(optarg);
470	  break;
471
472	case 'E':		/* POSIX */
473	case 'e':		/* deprecated */
474	  if (optarg && (strlen(optarg) > 0))
475	    eof_str = optarg;
476	  else
477	    eof_str = 0;
478	  break;
479
480	case 'h':
481	  usage (stdout);
482	  return 0;
483
484	case 'I':		/* POSIX */
485	case 'i':		/* deprecated */
486	  if (optarg)
487	    bc_ctl.replace_pat = optarg;
488	  else
489	    bc_ctl.replace_pat = "{}";
490	  /* -i excludes -n -l.  */
491	  bc_ctl.args_per_exec = 0;
492	  bc_ctl.lines_per_exec = 0;
493	  break;
494
495	case 'L':		/* POSIX */
496	  bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
497	  /* -L excludes -i -n.  */
498	  bc_ctl.args_per_exec = 0;
499	  bc_ctl.replace_pat = NULL;
500	  break;
501
502	case 'l':		/* deprecated */
503	  if (optarg)
504	    bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
505	  else
506	    bc_ctl.lines_per_exec = 1;
507	  /* -l excludes -i -n.  */
508	  bc_ctl.args_per_exec = 0;
509	  bc_ctl.replace_pat = NULL;
510	  break;
511
512	case 'n':
513	  bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
514	  /* -n excludes -i -l.  */
515	  bc_ctl.lines_per_exec = 0;
516	  if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
517	    /* ignore -n1 in '-i -n1' */
518	    bc_ctl.args_per_exec = 0;
519	  else
520	    bc_ctl.replace_pat = NULL;
521	  break;
522
523	  /* The POSIX standard specifies that it is not an error
524	   * for the -s option to specify a size that the implementation
525	   * cannot support - in that case, the relevant limit is used.
526	   */
527	case 's':
528	  {
529	    size_t arg_size;
530	    act_on_init_result();
531	    arg_size = parse_num (optarg, 's', 1L,
532				  bc_ctl.posix_arg_size_max, 0);
533	    if (arg_size > bc_ctl.posix_arg_size_max)
534	      {
535		error (0, 0,
536		       _("warning: value %ld for -s option is too large, "
537			 "using %ld instead"),
538		       arg_size, bc_ctl.posix_arg_size_max);
539		arg_size = bc_ctl.posix_arg_size_max;
540	      }
541	    bc_ctl.arg_max = arg_size;
542	  }
543	  break;
544
545	case 'S':
546	  show_limits = true;
547	  break;
548
549	case 't':
550	  print_command = true;
551	  break;
552
553	case 'x':
554	  bc_ctl.exit_if_size_exceeded = true;
555	  break;
556
557	case 'p':
558	  query_before_executing = true;
559	  print_command = true;
560	  break;
561
562	case 'r':
563	  always_run_command = 0;
564	  break;
565
566	case 'P':
567	  proc_max = parse_num (optarg, 'P', 0L, -1L, 1);
568	  break;
569
570        case 'a':
571          input_file = optarg;
572          break;
573
574	case 'v':
575	  printf (_("GNU xargs version %s\n"), version_string);
576	  printf (_("Built using GNU gnulib version %s\n"), gnulib_version);
577	  return 0;
578
579	default:
580	  usage (stderr);
581	  return 1;
582	}
583    }
584
585  /* If we had deferred failing due to problems in bc_init_controlinfo(),
586   * do it now.
587   *
588   * We issue this error message after processing command line
589   * arguments so that it is possible to use "xargs --help" even if
590   * the environment is too large.
591   */
592  act_on_init_result();
593  assert(BC_INIT_OK == bcstatus);
594
595  if (0 == strcmp (input_file, "-"))
596    {
597      input_stream = stdin;
598    }
599  else
600    {
601      keep_stdin = 1;		/* see prep_child_for_exec() */
602      input_stream = fopen (input_file, "r");
603      if (NULL == input_stream)
604	{
605	  error (1, errno,
606		 _("Cannot open input file `%s'"),
607		 input_file);
608	}
609    }
610
611  if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
612    bc_ctl.exit_if_size_exceeded = true;
613
614  if (optind == argc)
615    {
616      optind = 0;
617      argc = 1;
618      argv = &default_cmd;
619    }
620
621  /* We want to be able to print size_t values as unsigned long, so if
622   * the cast isn't value-preserving, we have a problem.  This isn't a
623   * problem in C89, because size_t was known to be no wider than
624   * unsigned long.  In C99 this is no longer the case, but there are
625   * special C99 ways to print such values.  Unfortunately this
626   * program tries to work on both C89 and C99 systems.
627   */
628#if defined(SIZE_MAX)
629# if SIZE_MAX > ULONG_MAX
630# error "I'm not sure how to print size_t values on your system"
631# endif
632#else
633  /* Without SIZE_MAX (i.e. limits.h) this is probably
634   * close to the best we can do.
635   */
636  assert(sizeof(size_t) <= sizeof(unsigned long));
637#endif
638
639  if (show_limits)
640    {
641      fprintf(stderr,
642	      _("Your environment variables take up %lu bytes\n"),
643	      (unsigned long)bc_size_of_environment());
644      fprintf(stderr,
645	      _("POSIX lower and upper limits on argument length: %lu, %lu\n"),
646	      (unsigned long)bc_ctl.posix_arg_size_min,
647	      (unsigned long)bc_ctl.posix_arg_size_max);
648      fprintf(stderr,
649	      _("Maximum length of command we could actually use: %ld\n"),
650	      (unsigned long)(bc_ctl.posix_arg_size_max -
651			      bc_size_of_environment()));
652      fprintf(stderr,
653	      _("Size of command buffer we are actually using: %lu\n"),
654	      (unsigned long)bc_ctl.arg_max);
655
656      if (isatty(STDIN_FILENO))
657	{
658	  fprintf(stderr,
659		  "\n"
660		  "Execution of xargs will continue now, and it will "
661		  "try to read its input and run commands; if this is "
662		  "not what you wanted to happen, please type the "
663		  "end-of-file keystroke.\n");
664	}
665    }
666
667  linebuf = (char *) xmalloc (bc_ctl.arg_max + 1);
668  bc_state.argbuf = (char *) xmalloc (bc_ctl.arg_max + 1);
669
670  /* Make sure to listen for the kids.  */
671  signal (SIGCHLD, SIG_DFL);
672
673  if (!bc_ctl.replace_pat)
674    {
675      for (; optind < argc; optind++)
676	bc_push_arg (&bc_ctl, &bc_state,
677		     argv[optind], strlen (argv[optind]) + 1,
678		     NULL, 0,
679		     initial_args);
680      initial_args = false;
681      bc_ctl.initial_argc = bc_state.cmd_argc;
682      bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;
683
684      while ((*read_args) () != -1)
685	if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
686	  {
687	    xargs_do_exec (&bc_ctl, &bc_state);
688	    lineno = 0;
689	  }
690
691      /* SYSV xargs seems to do at least one exec, even if the
692         input is empty.  */
693      if (bc_state.cmd_argc != bc_ctl.initial_argc
694	  || (always_run_command && procs_executed == 0))
695	xargs_do_exec (&bc_ctl, &bc_state);
696
697    }
698  else
699    {
700      int i;
701      size_t len;
702      size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
703
704      for (i = optind; i < argc; i++)
705	arglen[i] = strlen(argv[i]);
706      bc_ctl.rplen = strlen (bc_ctl.replace_pat);
707      while ((len = (*read_args) ()) != -1)
708	{
709	  /* Don't do insert on the command name.  */
710	  bc_clear_args(&bc_ctl, &bc_state);
711	  bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
712
713	  bc_push_arg (&bc_ctl, &bc_state,
714		       argv[optind], arglen[optind] + 1,
715		       NULL, 0,
716		       initial_args);
717	  len--;
718	  initial_args = false;
719
720	  for (i = optind + 1; i < argc; i++)
721	    bc_do_insert (&bc_ctl, &bc_state,
722			  argv[i], arglen[i],
723			  NULL, 0,
724			  linebuf, len,
725			  initial_args);
726	  xargs_do_exec (&bc_ctl, &bc_state);
727	}
728    }
729
730  original_exit_value = child_error;
731  return child_error;
732}
733
734
735/* Read a line of arguments from the input and add them to the list of
736   arguments to pass to the command.  Ignore blank lines and initial blanks.
737   Single and double quotes and backslashes quote metacharacters and blanks
738   as they do in the shell.
739   Return -1 if eof (either physical or logical) is reached,
740   otherwise the length of the last string read (including the null).  */
741
742static int
743read_line (void)
744{
745  static boolean eof = false;
746  /* Start out in mode SPACE to always strip leading spaces (even with -i).  */
747  int state = SPACE;		/* The type of character we last read.  */
748  int prevc;			/* The previous value of c.  */
749  int quotc = 0;		/* The last quote character read.  */
750  int c = EOF;
751  boolean first = true;		/* true if reading first arg on line.  */
752  int len;
753  char *p = linebuf;
754  /* Including the NUL, the args must not grow past this point.  */
755  char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
756
757  if (eof)
758    return -1;
759  while (1)
760    {
761      prevc = c;
762      c = getc (input_stream);
763      if (c == EOF)
764	{
765	  /* COMPAT: SYSV seems to ignore stuff on a line that
766	     ends without a \n; we don't.  */
767	  eof = true;
768	  if (p == linebuf)
769	    return -1;
770	  *p++ = '\0';
771	  len = p - linebuf;
772	  if (state == QUOTE)
773	    {
774	      exec_if_possible ();
775	      error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
776		     quotc == '"' ? _("double") : _("single"));
777	    }
778	  if (first && EOF_STR (linebuf))
779	    return -1;
780	  if (!bc_ctl.replace_pat)
781	    bc_push_arg (&bc_ctl, &bc_state,
782			 linebuf, len,
783			 NULL, 0,
784			 initial_args);
785	  return len;
786	}
787      switch (state)
788	{
789	case SPACE:
790	  if (ISSPACE (c))
791	    continue;
792	  state = NORM;
793	  /* aaahhhh....  */
794
795	case NORM:
796	  if (c == '\n')
797	    {
798	      if (!ISBLANK (prevc))
799		lineno++;	/* For -l.  */
800	      if (p == linebuf)
801		{
802		  /* Blank line.  */
803		  state = SPACE;
804		  continue;
805		}
806	      *p++ = '\0';
807	      len = p - linebuf;
808	      if (EOF_STR (linebuf))
809		{
810		  eof = true;
811		  return first ? -1 : len;
812		}
813	      if (!bc_ctl.replace_pat)
814		bc_push_arg (&bc_ctl, &bc_state,
815			     linebuf, len,
816			     NULL, 0,
817			     initial_args);
818	      return len;
819	    }
820	  if (!bc_ctl.replace_pat && ISSPACE (c))
821	    {
822	      *p++ = '\0';
823	      len = p - linebuf;
824	      if (EOF_STR (linebuf))
825		{
826		  eof = true;
827		  return first ? -1 : len;
828		}
829	      bc_push_arg (&bc_ctl, &bc_state,
830			   linebuf, len,
831			   NULL, 0,
832			   initial_args);
833	      p = linebuf;
834	      state = SPACE;
835	      first = false;
836	      continue;
837	    }
838	  switch (c)
839	    {
840	    case '\\':
841	      state = BACKSLASH;
842	      continue;
843
844	    case '\'':
845	    case '"':
846	      state = QUOTE;
847	      quotc = c;
848	      continue;
849	    }
850	  break;
851
852	case QUOTE:
853	  if (c == '\n')
854	    {
855	      exec_if_possible ();
856	      error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
857		     quotc == '"' ? _("double") : _("single"));
858	    }
859	  if (c == quotc)
860	    {
861	      state = NORM;
862	      continue;
863	    }
864	  break;
865
866	case BACKSLASH:
867	  state = NORM;
868	  break;
869	}
870#if 1
871      if (p >= endbuf)
872        {
873	  exec_if_possible ();
874	  error (1, 0, _("argument line too long"));
875	}
876      *p++ = c;
877#else
878      append_char_to_buf(&linebuf, &endbuf, &p, c);
879#endif
880    }
881}
882
883/* Read a null-terminated string from the input and add it to the list of
884   arguments to pass to the command.
885   Return -1 if eof (either physical or logical) is reached,
886   otherwise the length of the string read (including the null).  */
887
888static int
889read_string (void)
890{
891  static boolean eof = false;
892  int len;
893  char *p = linebuf;
894  /* Including the NUL, the args must not grow past this point.  */
895  char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
896
897  if (eof)
898    return -1;
899  while (1)
900    {
901      int c = getc (input_stream);
902      if (c == EOF)
903	{
904	  eof = true;
905	  if (p == linebuf)
906	    return -1;
907	  *p++ = '\0';
908	  len = p - linebuf;
909	  if (!bc_ctl.replace_pat)
910	    bc_push_arg (&bc_ctl, &bc_state,
911			 linebuf, len,
912			 NULL, 0,
913			 initial_args);
914	  return len;
915	}
916      if (c == input_delimiter)
917	{
918	  lineno++;		/* For -l.  */
919	  *p++ = '\0';
920	  len = p - linebuf;
921	  if (!bc_ctl.replace_pat)
922	    bc_push_arg (&bc_ctl, &bc_state,
923			 linebuf, len,
924			 NULL, 0,
925			 initial_args);
926	  return len;
927	}
928      if (p >= endbuf)
929        {
930	  exec_if_possible ();
931	  error (1, 0, _("argument line too long"));
932	}
933      *p++ = c;
934    }
935}
936
937/* Print the arguments of the command to execute.
938   If ASK is nonzero, prompt the user for a response, and
939   if the user responds affirmatively, return true;
940   otherwise, return false.  */
941
942static boolean
943print_args (boolean ask)
944{
945  int i;
946
947  for (i = 0; i < bc_state.cmd_argc - 1; i++)
948    fprintf (stderr, "%s ", bc_state.cmd_argv[i]);
949  if (ask)
950    {
951      static FILE *tty_stream;
952      int c, savec;
953
954      if (!tty_stream)
955	{
956	  tty_stream = fopen ("/dev/tty", "r");
957	  if (!tty_stream)
958	    error (1, errno, "/dev/tty");
959	}
960      fputs ("?...", stderr);
961      fflush (stderr);
962      c = savec = getc (tty_stream);
963      while (c != EOF && c != '\n')
964	c = getc (tty_stream);
965      if (savec == 'y' || savec == 'Y')
966	return true;
967    }
968  else
969    putc ('\n', stderr);
970
971  return false;
972}
973
974
975/* Close stdin and attach /dev/null to it.
976 * This resolves Savannah bug #3992.
977 */
978static void
979prep_child_for_exec (void)
980{
981  if (!keep_stdin)
982    {
983      const char inputfile[] = "/dev/null";
984      /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
985
986      close(0);
987      if (open(inputfile, O_RDONLY) < 0)
988	{
989	  /* This is not entirely fatal, since
990	   * executing the child with a closed
991	   * stdin is almost as good as executing it
992	   * with its stdin attached to /dev/null.
993	   */
994	  error (0, errno, "%s", inputfile);
995	}
996    }
997}
998
999
1000/* Execute the command that has been built in `cmd_argv'.  This may involve
1001   waiting for processes that were previously executed.  */
1002
1003static int
1004xargs_do_exec (const struct buildcmd_control *ctl, struct buildcmd_state *state)
1005{
1006  pid_t child;
1007
1008  bc_push_arg (&bc_ctl, &bc_state,
1009	       (char *) NULL, 0,
1010	       NULL, 0,
1011	       false); /* Null terminate the arg list.  */
1012
1013  if (!query_before_executing || print_args (true))
1014    {
1015      if (proc_max && procs_executing >= proc_max)
1016	wait_for_proc (false);
1017      if (!query_before_executing && print_command)
1018	print_args (false);
1019      /* If we run out of processes, wait for a child to return and
1020         try again.  */
1021      while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1022	wait_for_proc (false);
1023      switch (child)
1024	{
1025	case -1:
1026	  error (1, errno, _("cannot fork"));
1027
1028	case 0:		/* Child.  */
1029	  prep_child_for_exec();
1030	  execvp (bc_state.cmd_argv[0], bc_state.cmd_argv);
1031	  error (0, errno, "%s", bc_state.cmd_argv[0]);
1032	  _exit (errno == ENOENT ? 127 : 126);
1033	  /*NOTREACHED*/
1034	}
1035      add_proc (child);
1036    }
1037
1038  bc_clear_args(&bc_ctl, &bc_state);
1039  return 1;			/* Success */
1040}
1041
1042/* Execute the command if possible.  */
1043
1044static void
1045exec_if_possible (void)
1046{
1047  if (bc_ctl.replace_pat || initial_args ||
1048      bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
1049    return;
1050  xargs_do_exec (&bc_ctl, &bc_state);
1051}
1052
1053/* Add the process with id PID to the list of processes that have
1054   been executed.  */
1055
1056static void
1057add_proc (pid_t pid)
1058{
1059  int i;
1060
1061  /* Find an empty slot.  */
1062  for (i = 0; i < pids_alloc && pids[i]; i++)
1063    ;
1064  if (i == pids_alloc)
1065    {
1066      if (pids_alloc == 0)
1067	{
1068	  pids_alloc = proc_max ? proc_max : 64;
1069	  pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
1070	}
1071      else
1072	{
1073	  pids_alloc *= 2;
1074	  pids = (pid_t *) xrealloc (pids,
1075				     sizeof (pid_t) * pids_alloc);
1076	}
1077      memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
1078    }
1079  pids[i] = pid;
1080  procs_executing++;
1081  procs_executed++;
1082}
1083
1084/* If ALL is true, wait for all child processes to finish;
1085   otherwise, wait for one child process to finish.
1086   Remove the processes that finish from the list of executing processes.  */
1087
1088static void
1089wait_for_proc (boolean all)
1090{
1091  while (procs_executing)
1092    {
1093      int i, status;
1094
1095      do
1096	{
1097	  pid_t pid;
1098
1099	  while ((pid = wait (&status)) == (pid_t) -1)
1100	    if (errno != EINTR)
1101	      error (1, errno, _("error waiting for child process"));
1102
1103	  /* Find the entry in `pids' for the child process
1104	     that exited.  */
1105	  for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1106	    ;
1107	}
1108      while (i == pids_alloc);	/* A child died that we didn't start? */
1109
1110      /* Remove the child from the list.  */
1111      pids[i] = 0;
1112      procs_executing--;
1113
1114      if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
1115	exit (WEXITSTATUS (status));	/* Can't find or run the command.  */
1116      if (WEXITSTATUS (status) == 255)
1117	error (124, 0, _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
1118      if (WIFSTOPPED (status))
1119	error (125, 0, _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
1120      if (WIFSIGNALED (status))
1121	error (125, 0, _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
1122      if (WEXITSTATUS (status) != 0)
1123	child_error = 123;
1124
1125      if (!all)
1126	break;
1127    }
1128}
1129
1130/* Wait for all child processes to finish.  */
1131
1132static void
1133wait_for_proc_all (void)
1134{
1135  static boolean waiting = false;
1136
1137  if (waiting)
1138    return;
1139
1140  waiting = true;
1141  wait_for_proc (true);
1142  waiting = false;
1143
1144  if (original_exit_value != child_error)
1145    {
1146      /* wait_for_proc() changed the value of child_error().  This
1147       * function is registered via atexit(), and so may have been
1148       * called from exit().  We now know that the original value
1149       * passed to exit() is no longer the exit status we require.
1150       * The POSIX standard states that the behaviour if exit() is
1151       * called more than once is undefined.  Therefore we now have to
1152       * exit with _exit() instead of exit().
1153       */
1154      _exit(child_error);
1155    }
1156
1157}
1158
1159/* Return the value of the number represented in STR.
1160   OPTION is the command line option to which STR is the argument.
1161   If the value does not fall within the boundaries MIN and MAX,
1162   Print an error message mentioning OPTION.  If FATAL is true,
1163   we also exit. */
1164
1165static long
1166parse_num (char *str, int option, long int min, long int max, int fatal)
1167{
1168  char *eptr;
1169  long val;
1170
1171  val = strtol (str, &eptr, 10);
1172  if (eptr == str || *eptr)
1173    {
1174      fprintf (stderr, _("%s: invalid number for -%c option\n"),
1175	       program_name, option);
1176      usage (stderr);
1177      exit(1);
1178    }
1179  else if (val < min)
1180    {
1181      fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
1182	       program_name, option, min);
1183      if (fatal)
1184	{
1185	  usage (stderr);
1186	  exit(1);
1187	}
1188      else
1189	{
1190	  val = min;
1191	}
1192    }
1193  else if (max >= 0 && val > max)
1194    {
1195      fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
1196	       program_name, option, max);
1197      if (fatal)
1198	{
1199	  usage (stderr);
1200	  exit(1);
1201	}
1202      else
1203	{
1204	  val = max;
1205	}
1206    }
1207  return val;
1208}
1209
1210/* Return how much of ARG_MAX is used by the environment.  */
1211
1212static void
1213usage (FILE *stream)
1214{
1215  fprintf (stream, _("\
1216Usage: %s [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]\n\
1217       [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]\n\
1218       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]\n\
1219       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
1220       [-n max-args] [--max-args=max-args]\n\
1221       [-s max-chars] [--max-chars=max-chars]\n\
1222       [-P max-procs]  [--max-procs=max-procs] [--show-limits]\n\
1223       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
1224       [--version] [--help] [command [initial-arguments]]\n"),
1225	   program_name);
1226  fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);
1227}
1228