Refactored arg colleciton in parser and added new test cases, mosly for cases where...
[findutils.git] / xargs / xargs.c
blobc6b016fb37236a6bef6ca2a5093517f40084aea7
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.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
20 /* Written by Mike Rendell <michael@cs.mun.ca>
21 and David MacKenzie <djm@gnu.org>.
22 Modifications by
23 James Youngman
24 Dmitry V. Levin
27 #include <config.h>
29 # ifndef PARAMS
30 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
31 # define PARAMS(Args) Args
32 # else
33 # define PARAMS(Args) ()
34 # endif
35 # endif
37 #include <ctype.h>
39 #if !defined (isascii) || defined (STDC_HEADERS)
40 #ifdef isascii
41 #undef isascii
42 #endif
43 #define isascii(c) 1
44 #endif
46 #ifdef isblank
47 #define ISBLANK(c) (isascii (c) && isblank (c))
48 #else
49 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
50 #endif
52 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
53 || (c) == '\f' || (c) == '\v')
55 #include <sys/types.h>
56 #include <stdio.h>
57 #include <errno.h>
58 #include <getopt.h>
59 #include <fcntl.h>
61 #if defined(STDC_HEADERS)
62 #include <assert.h>
63 #endif
65 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
66 #include <string.h>
67 #if !defined(STDC_HEADERS)
68 #include <memory.h>
69 #endif
70 #else
71 #include <strings.h>
72 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
73 #endif
75 #ifndef _POSIX_SOURCE
76 #include <sys/param.h>
77 #endif
79 #ifdef HAVE_LIMITS_H
80 #include <limits.h>
81 #endif
83 #ifndef LONG_MAX
84 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
85 #endif
87 /* The presence of unistd.h is assumed by gnulib these days, so we
88 * might as well assume it too.
90 #include <unistd.h>
92 #include <signal.h>
94 #if !defined(SIGCHLD) && defined(SIGCLD)
95 #define SIGCHLD SIGCLD
96 #endif
98 #include "wait.h"
99 #include "quotearg.h"
102 #ifdef STDC_HEADERS
103 #include <stdlib.h>
104 #else
105 extern int errno;
106 #endif
108 #ifdef HAVE_LOCALE_H
109 #include <locale.h>
110 #endif
111 #if ENABLE_NLS
112 # include <libintl.h>
113 # define _(Text) gettext (Text)
114 #else
115 # define _(Text) Text
116 #define textdomain(Domain)
117 #define bindtextdomain(Package, Directory)
118 #endif
119 #ifdef gettext_noop
120 # define N_(String) gettext_noop (String)
121 #else
122 /* See locate.c for explanation as to why not use (String) */
123 # define N_(String) String
124 #endif
126 #include "buildcmd.h"
129 /* Return nonzero if S is the EOF string. */
130 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
132 /* Do multibyte processing if multibyte characters are supported,
133 unless multibyte sequences are search safe. Multibyte sequences
134 are search safe if searching for a substring using the byte
135 comparison function 'strstr' gives no false positives. All 8-bit
136 encodings and the UTF-8 multibyte encoding are search safe, but
137 the EUC encodings are not.
138 BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
139 #if defined __BEOS__
140 # define MULTIBYTE_IS_SEARCH_SAFE 1
141 #endif
142 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
144 #if DO_MULTIBYTE
145 # if HAVE_MBRLEN
146 # include <wchar.h>
147 # else
148 /* Simulate mbrlen with mblen as best we can. */
149 # define mbstate_t int
150 # define mbrlen(s, n, ps) mblen (s, n)
151 # endif
152 #endif
154 /* Not char because of type promotion; NeXT gcc can't handle it. */
155 typedef int boolean;
156 #define true 1
157 #define false 0
159 #if __STDC__
160 #define VOID void
161 #else
162 #define VOID char
163 #endif
165 #include <xalloc.h>
166 #include "closeout.h"
167 #include "gnulib-version.h"
169 void error PARAMS ((int status, int errnum, char *message,...));
171 extern char *version_string;
173 /* The name this program was run with. */
174 char *program_name;
176 static FILE *input_stream;
178 /* Buffer for reading arguments from input. */
179 static char *linebuf;
181 static int keep_stdin = 0;
183 /* Line number in stdin since the last command was executed. */
184 static int lineno = 0;
186 static struct buildcmd_state bc_state;
187 static struct buildcmd_control bc_ctl;
189 /* Did we already complain about NUL characters in the input? */
190 static int nullwarning_given = 0;
193 /* If nonzero, when this string is read on stdin it is treated as
194 end of file.
195 IEEE Std 1003.1, 2004 Edition allows this to be NULL.
196 In findutils releases up to and including 4.2.8, this was "_".
198 static char *eof_str = NULL;
200 /* Number of chars in the initial args. */
201 /* static int initial_argv_chars = 0; */
203 /* true when building up initial arguments in `cmd_argv'. */
204 static boolean initial_args = true;
206 /* If nonzero, the maximum number of child processes that can be running
207 at once. */
208 static int proc_max = 1;
210 /* Total number of child processes that have been executed. */
211 static int procs_executed = 0;
213 /* The number of elements in `pids'. */
214 static int procs_executing = 0;
216 /* List of child processes currently executing. */
217 static pid_t *pids = NULL;
219 /* The number of allocated elements in `pids'. */
220 static int pids_alloc = 0;
222 /* Exit status; nonzero if any child process exited with a
223 status of 1-125. */
224 static volatile int child_error = 0;
226 static volatile int original_exit_value;
228 /* If true, print each command on stderr before executing it. */
229 static boolean print_command = false; /* Option -t */
231 /* If true, query the user before executing each command, and only
232 execute the command if the user responds affirmatively. */
233 static boolean query_before_executing = false;
235 /* The delimiter for input arguments. This is only consulted if the
236 * -0 or -d option had been given.
238 static char input_delimiter = '\0';
241 static struct option const longopts[] =
243 {"null", no_argument, NULL, '0'},
244 {"arg-file", required_argument, NULL, 'a'},
245 {"delimiter", required_argument, NULL, 'd'},
246 {"eof", optional_argument, NULL, 'e'},
247 {"replace", optional_argument, NULL, 'I'},
248 {"max-lines", optional_argument, NULL, 'l'},
249 {"max-args", required_argument, NULL, 'n'},
250 {"interactive", no_argument, NULL, 'p'},
251 {"no-run-if-empty", no_argument, NULL, 'r'},
252 {"max-chars", required_argument, NULL, 's'},
253 {"verbose", no_argument, NULL, 't'},
254 {"show-limits", no_argument, NULL, 'S'},
255 {"exit", no_argument, NULL, 'x'},
256 {"max-procs", required_argument, NULL, 'P'},
257 {"version", no_argument, NULL, 'v'},
258 {"help", no_argument, NULL, 'h'},
259 {NULL, no_argument, NULL, 0}
262 static int read_line PARAMS ((void));
263 static int read_string PARAMS ((void));
264 static boolean print_args PARAMS ((boolean ask));
265 /* static void do_exec PARAMS ((void)); */
266 static int xargs_do_exec (const struct buildcmd_control *cl, struct buildcmd_state *state);
267 static void exec_if_possible PARAMS ((void));
268 static void add_proc PARAMS ((pid_t pid));
269 static void wait_for_proc PARAMS ((boolean all));
270 static void wait_for_proc_all PARAMS ((void));
271 static long parse_num PARAMS ((char *str, int option, long min, long max, int fatal));
272 static void usage PARAMS ((FILE * stream));
276 static char
277 get_char_oct_or_hex_escape(const char *s)
279 const char * p;
280 int base = 8;
281 unsigned long val;
282 char *endp;
284 assert('\\' == s[0]);
286 if ('x' == s[1])
288 /* hex */
289 p = s+2;
290 base = 16;
292 else if (isdigit(s[1]))
294 /* octal */
295 p = s+1;
296 base = 8;
298 else
300 p = NULL; /* Silence compiler warning. */
301 error(1, 0,
302 _("Invalid escape sequence %s in input delimiter specification."),
305 errno = 0;
306 endp = (char*)p;
307 val = strtoul(p, &endp, base);
309 /* This if condition is carefully constructed to do
310 * the right thing if UCHAR_MAX has the same
311 * value as ULONG_MAX. IF UCHAR_MAX==ULONG_MAX,
312 * then val can never be greater than UCHAR_MAX.
314 if ((ULONG_MAX == val && ERANGE == errno)
315 || (val > UCHAR_MAX))
317 if (16 == base)
319 error(1, 0,
320 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
321 s, (unsigned long)UCHAR_MAX);
323 else
325 error(1, 0,
326 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
327 s, (unsigned long)UCHAR_MAX);
331 /* check for trailing garbage */
332 if (0 != *endp)
334 error(1, 0,
335 _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
336 s, endp);
339 return (char) val;
343 static char
344 get_input_delimiter(const char *s)
346 if (1 == strlen(s))
348 return s[0];
350 else
352 if ('\\' == s[0])
354 /* an escape code */
355 switch (s[1])
357 case 'a':
358 return '\a';
359 case 'b':
360 return '\b';
361 case 'f':
362 return '\f';
363 case 'n':
364 return '\n';
365 case 'r':
366 return '\r';
367 case 't':
368 return'\t';
369 case 'v':
370 return '\v';
371 case '\\':
372 return '\\';
373 default:
374 return get_char_oct_or_hex_escape(s);
377 else
379 error(1, 0,
380 _("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
382 /*NOTREACHED*/
383 return 0;
388 static void
389 noop (void)
391 /* does nothing. */
394 static void
395 fail_due_to_env_size (void)
397 error (1, 0, _("environment is too large for exec"));
402 main (int argc, char **argv)
404 int optc;
405 int show_limits = 0; /* --show-limits */
406 int always_run_command = 1;
407 char *input_file = "-"; /* "-" is stdin */
408 char *default_cmd = "/bin/echo";
409 int (*read_args) PARAMS ((void)) = read_line;
410 void (*act_on_init_result)(void) = noop;
411 enum BC_INIT_STATUS bcstatus;
412 enum { XARGS_POSIX_HEADROOM = 2048u };
414 program_name = argv[0];
415 original_exit_value = 0;
417 #ifdef HAVE_SETLOCALE
418 setlocale (LC_ALL, "");
419 #endif
420 bindtextdomain (PACKAGE, LOCALEDIR);
421 textdomain (PACKAGE);
422 atexit (close_stdout);
423 atexit (wait_for_proc_all);
425 /* xargs is required by POSIX to allow 2048 bytes of headroom
426 * for extra environment variables (that perhaps the utliity might
427 * want to set before execing something else).
429 bcstatus = bc_init_controlinfo(&bc_ctl, XARGS_POSIX_HEADROOM);
431 /* The bc_init_controlinfo call may have determined that the
432 * environment is too big. In that case, we will fail with
433 * an error message after processing the command-line options,
434 * as "xargs --help" should still work even if the environment is
435 * too big.
437 * Some of the argument processing depends on the contents of
438 * bc_ctl, which will be in an undefined state if bc_init_controlinfo()
439 * failed.
441 if (BC_INIT_ENV_TOO_BIG == bcstatus)
443 act_on_init_result = fail_due_to_env_size;
445 else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM == bcstatus)
447 /* All POSIX systems are required to support ARG_MAX of at least
448 * 4096. For everything to work the total of (command line +
449 * headroom + environment) must fit into this. POSIX requires
450 * that we use a headroom of 2048 bytes. The user is in control
451 * of the size of the environment.
453 * In general if bc_init_controlinfo() returns
454 * BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
455 * with a smaller headroom. However, in the case of xargs, this
456 * would not be POSIX-compliant.
458 act_on_init_result = fail_due_to_env_size;
460 else
462 /* IEEE Std 1003.1, 2003 specifies that the combined argument and
463 * environment list shall not exceed {ARG_MAX}-2048 bytes. It also
464 * specifies that it shall be at least LINE_MAX.
466 #ifdef _SC_ARG_MAX
467 long val = sysconf(_SC_ARG_MAX);
468 if (val > 0)
470 /* Note that val can in fact be greater than ARG_MAX
471 * and bc_ctl.arg_max can also be greater than ARG_MAX.
473 assert(bc_ctl.arg_max <= (val-XARGS_POSIX_HEADROOM));
475 else
477 # if defined(ARG_MAX)
478 assert(bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
479 # endif
481 #else
482 /* No _SC_ARG_MAX */
483 assert(bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
484 #endif
487 #ifdef LINE_MAX
488 /* This assertion ensures that this xargs implementation
489 * conforms to the POSIX requirement that the default command
490 * line length shall be at least LINE_MAX.
492 assert(bc_ctl.arg_max >= LINE_MAX);
493 #endif
495 bc_ctl.exec_callback = xargs_do_exec;
497 /* Start with a reasonable default size, though this can be
498 * adjusted via the -s option.
500 bc_use_sensible_arg_max(&bc_ctl);
503 while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
504 longopts, (int *) 0)) != -1)
506 switch (optc)
508 case '0':
509 read_args = read_string;
510 input_delimiter = '\0';
511 break;
513 case 'd':
514 read_args = read_string;
515 input_delimiter = get_input_delimiter(optarg);
516 break;
518 case 'E': /* POSIX */
519 case 'e': /* deprecated */
520 if (optarg && (strlen(optarg) > 0))
521 eof_str = optarg;
522 else
523 eof_str = 0;
524 break;
526 case 'h':
527 usage (stdout);
528 return 0;
530 case 'I': /* POSIX */
531 case 'i': /* deprecated */
532 if (optarg)
533 bc_ctl.replace_pat = optarg;
534 else
535 bc_ctl.replace_pat = "{}";
536 /* -i excludes -n -l. */
537 bc_ctl.args_per_exec = 0;
538 bc_ctl.lines_per_exec = 0;
539 break;
541 case 'L': /* POSIX */
542 bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
543 /* -L excludes -i -n. */
544 bc_ctl.args_per_exec = 0;
545 bc_ctl.replace_pat = NULL;
546 break;
548 case 'l': /* deprecated */
549 if (optarg)
550 bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
551 else
552 bc_ctl.lines_per_exec = 1;
553 /* -l excludes -i -n. */
554 bc_ctl.args_per_exec = 0;
555 bc_ctl.replace_pat = NULL;
556 break;
558 case 'n':
559 bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
560 /* -n excludes -i -l. */
561 bc_ctl.lines_per_exec = 0;
562 if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
563 /* ignore -n1 in '-i -n1' */
564 bc_ctl.args_per_exec = 0;
565 else
566 bc_ctl.replace_pat = NULL;
567 break;
569 /* The POSIX standard specifies that it is not an error
570 * for the -s option to specify a size that the implementation
571 * cannot support - in that case, the relevant limit is used.
573 case 's':
575 size_t arg_size;
576 act_on_init_result();
577 arg_size = parse_num (optarg, 's', 1L,
578 bc_ctl.posix_arg_size_max, 0);
579 if (arg_size > bc_ctl.posix_arg_size_max)
581 error (0, 0,
582 _("warning: value %ld for -s option is too large, "
583 "using %ld instead"),
584 arg_size, bc_ctl.posix_arg_size_max);
585 arg_size = bc_ctl.posix_arg_size_max;
587 bc_ctl.arg_max = arg_size;
589 break;
591 case 'S':
592 show_limits = true;
593 break;
595 case 't':
596 print_command = true;
597 break;
599 case 'x':
600 bc_ctl.exit_if_size_exceeded = true;
601 break;
603 case 'p':
604 query_before_executing = true;
605 print_command = true;
606 break;
608 case 'r':
609 always_run_command = 0;
610 break;
612 case 'P':
613 proc_max = parse_num (optarg, 'P', 0L, -1L, 1);
614 break;
616 case 'a':
617 input_file = optarg;
618 break;
620 case 'v':
621 printf (_("GNU xargs version %s\n"), version_string);
622 printf (_("Built using GNU gnulib version %s\n"), gnulib_version);
623 return 0;
625 default:
626 usage (stderr);
627 return 1;
631 /* If we had deferred failing due to problems in bc_init_controlinfo(),
632 * do it now.
634 * We issue this error message after processing command line
635 * arguments so that it is possible to use "xargs --help" even if
636 * the environment is too large.
638 act_on_init_result();
639 assert(BC_INIT_OK == bcstatus);
641 if (0 == strcmp (input_file, "-"))
643 input_stream = stdin;
645 else
647 keep_stdin = 1; /* see prep_child_for_exec() */
648 input_stream = fopen (input_file, "r");
649 if (NULL == input_stream)
651 error (1, errno,
652 _("Cannot open input file %s"),
653 quotearg_n_style(0, locale_quoting_style, input_file));
657 if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
658 bc_ctl.exit_if_size_exceeded = true;
660 if (optind == argc)
662 optind = 0;
663 argc = 1;
664 argv = &default_cmd;
667 /* We want to be able to print size_t values as unsigned long, so if
668 * the cast isn't value-preserving, we have a problem. This isn't a
669 * problem in C89, because size_t was known to be no wider than
670 * unsigned long. In C99 this is no longer the case, but there are
671 * special C99 ways to print such values. Unfortunately this
672 * program tries to work on both C89 and C99 systems.
674 #if defined(SIZE_MAX)
675 # if SIZE_MAX > ULONG_MAX
676 # error "I'm not sure how to print size_t values on your system"
677 # endif
678 #else
679 /* Without SIZE_MAX (i.e. limits.h) this is probably
680 * close to the best we can do.
682 assert(sizeof(size_t) <= sizeof(unsigned long));
683 #endif
685 if (show_limits)
687 fprintf(stderr,
688 _("Your environment variables take up %lu bytes\n"),
689 (unsigned long)bc_size_of_environment());
690 fprintf(stderr,
691 _("POSIX upper limit on argument length (this system): %lu\n"),
692 (unsigned long)bc_ctl.posix_arg_size_max);
693 fprintf(stderr,
694 _("POSIX smallest allowable upper limit on argument length (all systems): %lu\n"),
695 (unsigned long)bc_ctl.posix_arg_size_min);
696 fprintf(stderr,
697 _("Maximum length of command we could actually use: %ld\n"),
698 (unsigned long)(bc_ctl.posix_arg_size_max -
699 bc_size_of_environment()));
700 fprintf(stderr,
701 _("Size of command buffer we are actually using: %lu\n"),
702 (unsigned long)bc_ctl.arg_max);
704 if (isatty(STDIN_FILENO))
706 fprintf(stderr,
707 _("\n"
708 "Execution of xargs will continue now, and it will "
709 "try to read its input and run commands; if this is "
710 "not what you wanted to happen, please type the "
711 "end-of-file keystroke.\n"));
712 if (always_run_command)
714 fprintf(stderr,
715 _("Warning: %s will be run at least once. "
716 "If you do not want that to happen, then press "
717 "the interrupt keystroke.\n"),
718 argv[optind]);
723 linebuf = (char *) xmalloc (bc_ctl.arg_max + 1);
724 bc_state.argbuf = (char *) xmalloc (bc_ctl.arg_max + 1);
726 /* Make sure to listen for the kids. */
727 signal (SIGCHLD, SIG_DFL);
729 if (!bc_ctl.replace_pat)
731 for (; optind < argc; optind++)
732 bc_push_arg (&bc_ctl, &bc_state,
733 argv[optind], strlen (argv[optind]) + 1,
734 NULL, 0,
735 initial_args);
736 initial_args = false;
737 bc_ctl.initial_argc = bc_state.cmd_argc;
738 bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;
740 while ((*read_args) () != -1)
741 if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
743 xargs_do_exec (&bc_ctl, &bc_state);
744 lineno = 0;
747 /* SYSV xargs seems to do at least one exec, even if the
748 input is empty. */
749 if (bc_state.cmd_argc != bc_ctl.initial_argc
750 || (always_run_command && procs_executed == 0))
751 xargs_do_exec (&bc_ctl, &bc_state);
754 else
756 int i;
757 size_t len;
758 size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
760 for (i = optind; i < argc; i++)
761 arglen[i] = strlen(argv[i]);
762 bc_ctl.rplen = strlen (bc_ctl.replace_pat);
763 while ((len = (*read_args) ()) != -1)
765 /* Don't do insert on the command name. */
766 bc_clear_args(&bc_ctl, &bc_state);
767 bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
769 bc_push_arg (&bc_ctl, &bc_state,
770 argv[optind], arglen[optind] + 1,
771 NULL, 0,
772 initial_args);
773 len--;
774 initial_args = false;
776 for (i = optind + 1; i < argc; i++)
777 bc_do_insert (&bc_ctl, &bc_state,
778 argv[i], arglen[i],
779 NULL, 0,
780 linebuf, len,
781 initial_args);
782 xargs_do_exec (&bc_ctl, &bc_state);
786 original_exit_value = child_error;
787 return child_error;
791 /* Read a line of arguments from the input and add them to the list of
792 arguments to pass to the command. Ignore blank lines and initial blanks.
793 Single and double quotes and backslashes quote metacharacters and blanks
794 as they do in the shell.
795 Return -1 if eof (either physical or logical) is reached,
796 otherwise the length of the last string read (including the null). */
798 static int
799 read_line (void)
801 /* States for read_line. */
802 enum read_line_state
804 NORM = 0,
805 SPACE = 1,
806 QUOTE = 2,
807 BACKSLASH = 3
809 static boolean eof = false;
810 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
811 enum read_line_state state = SPACE; /* The type of character we last read. */
812 int prevc; /* The previous value of c. */
813 int quotc = 0; /* The last quote character read. */
814 int c = EOF;
815 boolean first = true; /* true if reading first arg on line. */
816 boolean seen_arg = false; /* true if we have seen any arg (or part of one) yet */
817 int len;
818 char *p = linebuf;
819 /* Including the NUL, the args must not grow past this point. */
820 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
822 if (eof)
823 return -1;
824 while (1)
826 prevc = c;
827 c = getc (input_stream);
829 if (c == EOF)
831 /* COMPAT: SYSV seems to ignore stuff on a line that
832 ends without a \n; we don't. */
833 eof = true;
834 if (p == linebuf)
835 return -1;
836 *p++ = '\0';
837 len = p - linebuf;
838 if (state == QUOTE)
840 exec_if_possible ();
841 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
842 quotc == '"' ? _("double") : _("single"));
844 if (first && EOF_STR (linebuf))
845 return -1;
846 if (!bc_ctl.replace_pat)
847 bc_push_arg (&bc_ctl, &bc_state,
848 linebuf, len,
849 NULL, 0,
850 initial_args);
851 return len;
853 switch (state)
855 case SPACE:
856 if (ISSPACE (c))
857 continue;
858 state = NORM;
859 /* aaahhhh.... */
861 case NORM:
862 if (c == '\n')
864 if (!ISBLANK (prevc))
865 lineno++; /* For -l. */
866 if (p == linebuf)
868 if (seen_arg)
870 /* An empty argument, add it to the list as normal. */
872 else
874 /* Blank line. */
875 state = SPACE;
876 continue;
879 *p++ = '\0';
880 len = p - linebuf;
881 if (EOF_STR (linebuf))
883 eof = true;
884 return first ? -1 : len;
886 if (!bc_ctl.replace_pat)
887 bc_push_arg (&bc_ctl, &bc_state,
888 linebuf, len,
889 NULL, 0,
890 initial_args);
891 return len;
893 seen_arg = true;
895 /* POSIX: In the POSIX locale, the separators are <SPC> and
896 * <TAB>, but not <FF> or <VT>.
898 if (!bc_ctl.replace_pat && ISBLANK (c))
900 *p++ = '\0';
901 len = p - linebuf;
902 if (EOF_STR (linebuf))
904 eof = true;
905 return first ? -1 : len;
907 bc_push_arg (&bc_ctl, &bc_state,
908 linebuf, len,
909 NULL, 0,
910 initial_args);
911 p = linebuf;
912 state = SPACE;
913 first = false;
914 continue;
916 switch (c)
918 case '\\':
919 state = BACKSLASH;
920 continue;
922 case '\'':
923 case '"':
924 state = QUOTE;
925 quotc = c;
926 continue;
928 break;
930 case QUOTE:
931 if (c == '\n')
933 exec_if_possible ();
934 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
935 quotc == '"' ? _("double") : _("single"));
937 if (c == quotc)
939 state = NORM;
940 seen_arg = true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
941 continue;
943 break;
945 case BACKSLASH:
946 state = NORM;
947 break;
950 if ( (0 == c) && !nullwarning_given )
952 /* This is just a warning message. We only issue it once. */
953 error (0, 0,
954 _("warning: a NUL character occurred in the input. "
955 "It cannot be passed through in the argument list. "
956 "Did you mean to use the --null option?"));
957 nullwarning_given = 1;
960 #if 1
961 if (p >= endbuf)
963 exec_if_possible ();
964 error (1, 0, _("argument line too long"));
966 *p++ = c;
967 #else
968 append_char_to_buf(&linebuf, &endbuf, &p, c);
969 #endif
973 /* Read a null-terminated string from the input and add it to the list of
974 arguments to pass to the command.
975 Return -1 if eof (either physical or logical) is reached,
976 otherwise the length of the string read (including the null). */
978 static int
979 read_string (void)
981 static boolean eof = false;
982 int len;
983 char *p = linebuf;
984 /* Including the NUL, the args must not grow past this point. */
985 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
987 if (eof)
988 return -1;
989 while (1)
991 int c = getc (input_stream);
992 if (c == EOF)
994 eof = true;
995 if (p == linebuf)
996 return -1;
997 *p++ = '\0';
998 len = p - linebuf;
999 if (!bc_ctl.replace_pat)
1000 bc_push_arg (&bc_ctl, &bc_state,
1001 linebuf, len,
1002 NULL, 0,
1003 initial_args);
1004 return len;
1006 if (c == input_delimiter)
1008 lineno++; /* For -l. */
1009 *p++ = '\0';
1010 len = p - linebuf;
1011 if (!bc_ctl.replace_pat)
1012 bc_push_arg (&bc_ctl, &bc_state,
1013 linebuf, len,
1014 NULL, 0,
1015 initial_args);
1016 return len;
1018 if (p >= endbuf)
1020 exec_if_possible ();
1021 error (1, 0, _("argument line too long"));
1023 *p++ = c;
1027 /* Print the arguments of the command to execute.
1028 If ASK is nonzero, prompt the user for a response, and
1029 if the user responds affirmatively, return true;
1030 otherwise, return false. */
1032 static boolean
1033 print_args (boolean ask)
1035 int i;
1037 for (i = 0; i < bc_state.cmd_argc - 1; i++)
1038 fprintf (stderr, "%s ", bc_state.cmd_argv[i]);
1039 if (ask)
1041 static FILE *tty_stream;
1042 int c, savec;
1044 if (!tty_stream)
1046 tty_stream = fopen ("/dev/tty", "r");
1047 if (!tty_stream)
1048 error (1, errno, "/dev/tty");
1050 fputs ("?...", stderr);
1051 fflush (stderr);
1052 c = savec = getc (tty_stream);
1053 while (c != EOF && c != '\n')
1054 c = getc (tty_stream);
1055 if (savec == 'y' || savec == 'Y')
1056 return true;
1058 else
1059 putc ('\n', stderr);
1061 return false;
1065 /* Close stdin and attach /dev/null to it.
1066 * This resolves Savannah bug #3992.
1068 static void
1069 prep_child_for_exec (void)
1071 if (!keep_stdin)
1073 const char inputfile[] = "/dev/null";
1074 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1076 close(0);
1077 if (open(inputfile, O_RDONLY) < 0)
1079 /* This is not entirely fatal, since
1080 * executing the child with a closed
1081 * stdin is almost as good as executing it
1082 * with its stdin attached to /dev/null.
1084 error (0, errno, "%s", quotearg_n_style(0, locale_quoting_style, inputfile));
1090 /* Execute the command that has been built in `cmd_argv'. This may involve
1091 waiting for processes that were previously executed. */
1093 static int
1094 xargs_do_exec (const struct buildcmd_control *ctl, struct buildcmd_state *state)
1096 pid_t child;
1098 (void) ctl;
1099 (void) state;
1101 bc_push_arg (&bc_ctl, &bc_state,
1102 (char *) NULL, 0,
1103 NULL, 0,
1104 false); /* Null terminate the arg list. */
1106 if (!query_before_executing || print_args (true))
1108 if (proc_max && procs_executing >= proc_max)
1109 wait_for_proc (false);
1110 if (!query_before_executing && print_command)
1111 print_args (false);
1112 /* If we run out of processes, wait for a child to return and
1113 try again. */
1114 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1115 wait_for_proc (false);
1116 switch (child)
1118 case -1:
1119 error (1, errno, _("cannot fork"));
1121 case 0: /* Child. */
1122 prep_child_for_exec();
1123 execvp (bc_state.cmd_argv[0], bc_state.cmd_argv);
1124 error (0, errno, "%s", bc_state.cmd_argv[0]);
1125 _exit (errno == ENOENT ? 127 : 126);
1126 /*NOTREACHED*/
1128 add_proc (child);
1131 bc_clear_args(&bc_ctl, &bc_state);
1132 return 1; /* Success */
1135 /* Execute the command if possible. */
1137 static void
1138 exec_if_possible (void)
1140 if (bc_ctl.replace_pat || initial_args ||
1141 bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
1142 return;
1143 xargs_do_exec (&bc_ctl, &bc_state);
1146 /* Add the process with id PID to the list of processes that have
1147 been executed. */
1149 static void
1150 add_proc (pid_t pid)
1152 int i;
1154 /* Find an empty slot. */
1155 for (i = 0; i < pids_alloc && pids[i]; i++)
1157 if (i == pids_alloc)
1159 if (pids_alloc == 0)
1161 pids_alloc = proc_max ? proc_max : 64;
1162 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
1164 else
1166 pids_alloc *= 2;
1167 pids = (pid_t *) xrealloc (pids,
1168 sizeof (pid_t) * pids_alloc);
1170 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
1172 pids[i] = pid;
1173 procs_executing++;
1174 procs_executed++;
1177 /* If ALL is true, wait for all child processes to finish;
1178 otherwise, wait for one child process to finish.
1179 Remove the processes that finish from the list of executing processes. */
1181 static void
1182 wait_for_proc (boolean all)
1184 while (procs_executing)
1186 int i, status;
1190 pid_t pid;
1192 while ((pid = wait (&status)) == (pid_t) -1)
1193 if (errno != EINTR)
1194 error (1, errno, _("error waiting for child process"));
1196 /* Find the entry in `pids' for the child process
1197 that exited. */
1198 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1201 while (i == pids_alloc); /* A child died that we didn't start? */
1203 /* Remove the child from the list. */
1204 pids[i] = 0;
1205 procs_executing--;
1207 if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
1208 exit (WEXITSTATUS (status)); /* Can't find or run the command. */
1209 if (WEXITSTATUS (status) == 255)
1210 error (124, 0, _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
1211 if (WIFSTOPPED (status))
1212 error (125, 0, _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
1213 if (WIFSIGNALED (status))
1214 error (125, 0, _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
1215 if (WEXITSTATUS (status) != 0)
1216 child_error = 123;
1218 if (!all)
1219 break;
1223 /* Wait for all child processes to finish. */
1225 static void
1226 wait_for_proc_all (void)
1228 static boolean waiting = false;
1230 if (waiting)
1231 return;
1233 waiting = true;
1234 wait_for_proc (true);
1235 waiting = false;
1237 if (original_exit_value != child_error)
1239 /* wait_for_proc() changed the value of child_error(). This
1240 * function is registered via atexit(), and so may have been
1241 * called from exit(). We now know that the original value
1242 * passed to exit() is no longer the exit status we require.
1243 * The POSIX standard states that the behaviour if exit() is
1244 * called more than once is undefined. Therefore we now have to
1245 * exit with _exit() instead of exit().
1247 _exit(child_error);
1252 /* Return the value of the number represented in STR.
1253 OPTION is the command line option to which STR is the argument.
1254 If the value does not fall within the boundaries MIN and MAX,
1255 Print an error message mentioning OPTION. If FATAL is true,
1256 we also exit. */
1258 static long
1259 parse_num (char *str, int option, long int min, long int max, int fatal)
1261 char *eptr;
1262 long val;
1264 val = strtol (str, &eptr, 10);
1265 if (eptr == str || *eptr)
1267 fprintf (stderr, _("%s: invalid number for -%c option\n"),
1268 program_name, option);
1269 usage (stderr);
1270 exit(1);
1272 else if (val < min)
1274 fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
1275 program_name, option, min);
1276 if (fatal)
1278 usage (stderr);
1279 exit(1);
1281 else
1283 val = min;
1286 else if (max >= 0 && val > max)
1288 fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
1289 program_name, option, max);
1290 if (fatal)
1292 usage (stderr);
1293 exit(1);
1295 else
1297 val = max;
1300 return val;
1303 static void
1304 usage (FILE *stream)
1306 fprintf (stream, _("\
1307 Usage: %s [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]\n\
1308 [-E eof-str] [-e[eof-str]] [--eof[=eof-str]]\n\
1309 [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]\n\
1310 [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
1311 [-n max-args] [--max-args=max-args]\n\
1312 [-s max-chars] [--max-chars=max-chars]\n\
1313 [-P max-procs] [--max-procs=max-procs] [--show-limits]\n\
1314 [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
1315 [--version] [--help] [command [initial-arguments]]\n"),
1316 program_name);
1317 fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);