xargs: correctly handle quoted empty arguments occurring first or last on a line...
[findutils.git] / xargs / xargs.c
blobf4284b4fe6a983bde2685b1bc86a7a6b6d09e035
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"
100 /* States for read_line. */
101 #define NORM 0
102 #define SPACE 1
103 #define QUOTE 2
104 #define BACKSLASH 3
106 #ifdef STDC_HEADERS
107 #include <stdlib.h>
108 #else
109 extern int errno;
110 #endif
112 #ifdef HAVE_LOCALE_H
113 #include <locale.h>
114 #endif
115 #if ENABLE_NLS
116 # include <libintl.h>
117 # define _(Text) gettext (Text)
118 #else
119 # define _(Text) Text
120 #define textdomain(Domain)
121 #define bindtextdomain(Package, Directory)
122 #endif
123 #ifdef gettext_noop
124 # define N_(String) gettext_noop (String)
125 #else
126 /* See locate.c for explanation as to why not use (String) */
127 # define N_(String) String
128 #endif
130 #include "buildcmd.h"
133 /* Return nonzero if S is the EOF string. */
134 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
136 /* Do multibyte processing if multibyte characters are supported,
137 unless multibyte sequences are search safe. Multibyte sequences
138 are search safe if searching for a substring using the byte
139 comparison function 'strstr' gives no false positives. All 8-bit
140 encodings and the UTF-8 multibyte encoding are search safe, but
141 the EUC encodings are not.
142 BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
143 #if defined __BEOS__
144 # define MULTIBYTE_IS_SEARCH_SAFE 1
145 #endif
146 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
148 #if DO_MULTIBYTE
149 # if HAVE_MBRLEN
150 # include <wchar.h>
151 # else
152 /* Simulate mbrlen with mblen as best we can. */
153 # define mbstate_t int
154 # define mbrlen(s, n, ps) mblen (s, n)
155 # endif
156 #endif
158 /* Not char because of type promotion; NeXT gcc can't handle it. */
159 typedef int boolean;
160 #define true 1
161 #define false 0
163 #if __STDC__
164 #define VOID void
165 #else
166 #define VOID char
167 #endif
169 #include <xalloc.h>
170 #include "closeout.h"
172 void error PARAMS ((int status, int errnum, char *message,...));
174 extern char *version_string;
176 /* The name this program was run with. */
177 char *program_name;
179 static FILE *input_stream;
181 /* Buffer for reading arguments from input. */
182 static char *linebuf;
184 static int keep_stdin = 0;
186 /* Line number in stdin since the last command was executed. */
187 static int lineno = 0;
189 static struct buildcmd_state bc_state;
190 static struct buildcmd_control bc_ctl;
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 error(1, 0,
301 _("Invalid escape sequence %s in input delimiter specification."),
304 errno = 0;
305 endp = (char*)p;
306 val = strtoul(p, &endp, base);
308 /* This if condition is carefully constructed to do
309 * the right thing if UCHAR_MAX has the same
310 * value as ULONG_MAX. IF UCHAR_MAX==ULONG_MAX,
311 * then val can never be greater than UCHAR_MAX.
313 if ((ULONG_MAX == val && ERANGE == errno)
314 || (val > UCHAR_MAX))
316 if (16 == base)
318 error(1, 0,
319 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
320 s, (unsigned long)UCHAR_MAX);
322 else
324 error(1, 0,
325 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
326 s, (unsigned long)UCHAR_MAX);
330 /* check for trailing garbage */
331 if (0 != *endp)
333 error(1, 0,
334 _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
335 s, endp);
338 return (char) val;
342 static char
343 get_input_delimiter(const char *s)
345 if (1 == strlen(s))
347 return s[0];
349 else
351 if ('\\' == s[0])
353 /* an escape code */
354 switch (s[1])
356 case 'a':
357 return '\a';
358 case 'b':
359 return '\b';
360 case 'f':
361 return '\f';
362 case 'n':
363 return '\n';
364 case 'r':
365 return '\r';
366 case 't':
367 return'\t';
368 case 'v':
369 return '\v';
370 case '\\':
371 return '\\';
372 default:
373 return get_char_oct_or_hex_escape(s);
376 else
378 error(1, 0,
379 _("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
381 /*NOTREACHED*/
382 return 0;
387 static void
388 noop (void)
390 /* does nothing. */
393 static void
394 fail_due_to_env_size (void)
396 error (1, 0, _("environment is too large for exec"));
401 main (int argc, char **argv)
403 int optc;
404 int show_limits = 0; /* --show-limits */
405 int always_run_command = 1;
406 char *input_file = "-"; /* "-" is stdin */
407 char *default_cmd = "/bin/echo";
408 int (*read_args) PARAMS ((void)) = read_line;
409 void (*act_on_init_result)(void) = noop;
410 int env_too_big = 0;
411 enum BC_INIT_STATUS bcstatus;
413 program_name = argv[0];
414 original_exit_value = 0;
416 #ifdef HAVE_SETLOCALE
417 setlocale (LC_ALL, "");
418 #endif
419 bindtextdomain (PACKAGE, LOCALEDIR);
420 textdomain (PACKAGE);
421 atexit (close_stdout);
422 atexit (wait_for_proc_all);
424 bcstatus = bc_init_controlinfo(&bc_ctl);
426 /* The bc_init_controlinfo call may have determined that the
427 * environment is too big. In that case, we will fail with
428 * an error message after processing the command-line options,
429 * as "xargs --help" should still work even if the environment is
430 * too big.
432 * Some of the argument processing depends on the contents of
433 * bc_ctl, which will be in an undefined state if bc_init_controlinfo()
434 * failed.
436 if (BC_INIT_ENV_TOO_BIG == bcstatus)
438 act_on_init_result = fail_due_to_env_size;
440 else
442 /* IEEE Std 1003.1, 2003 specifies that the combined argument and
443 * environment list shall not exceed {ARG_MAX}-2048 bytes. It also
444 * specifies that it shall be at least LINE_MAX.
446 #if defined(ARG_MAX)
447 assert(bc_ctl.arg_max <= (ARG_MAX-2048));
448 #endif
449 assert(bc_ctl.arg_max >= LINE_MAX);
451 bc_ctl.exec_callback = xargs_do_exec;
453 /* Start with a reasonable default size, though this can be
454 * adjusted via the -s option.
456 bc_use_sensible_arg_max(&bc_ctl);
459 while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
460 longopts, (int *) 0)) != -1)
462 switch (optc)
464 case '0':
465 read_args = read_string;
466 input_delimiter = '\0';
467 break;
469 case 'd':
470 read_args = read_string;
471 input_delimiter = get_input_delimiter(optarg);
472 break;
474 case 'E': /* POSIX */
475 case 'e': /* deprecated */
476 if (optarg && (strlen(optarg) > 0))
477 eof_str = optarg;
478 else
479 eof_str = 0;
480 break;
482 case 'h':
483 usage (stdout);
484 return 0;
486 case 'I': /* POSIX */
487 case 'i': /* deprecated */
488 if (optarg)
489 bc_ctl.replace_pat = optarg;
490 else
491 bc_ctl.replace_pat = "{}";
492 /* -i excludes -n -l. */
493 bc_ctl.args_per_exec = 0;
494 bc_ctl.lines_per_exec = 0;
495 break;
497 case 'L': /* POSIX */
498 bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
499 /* -L excludes -i -n. */
500 bc_ctl.args_per_exec = 0;
501 bc_ctl.replace_pat = NULL;
502 break;
504 case 'l': /* deprecated */
505 if (optarg)
506 bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
507 else
508 bc_ctl.lines_per_exec = 1;
509 /* -l excludes -i -n. */
510 bc_ctl.args_per_exec = 0;
511 bc_ctl.replace_pat = NULL;
512 break;
514 case 'n':
515 bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
516 /* -n excludes -i -l. */
517 bc_ctl.lines_per_exec = 0;
518 if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
519 /* ignore -n1 in '-i -n1' */
520 bc_ctl.args_per_exec = 0;
521 else
522 bc_ctl.replace_pat = NULL;
523 break;
525 /* The POSIX standard specifies that it is not an error
526 * for the -s option to specify a size that the implementation
527 * cannot support - in that case, the relevant limit is used.
529 case 's':
531 size_t arg_size;
532 act_on_init_result();
533 arg_size = parse_num (optarg, 's', 1L,
534 bc_ctl.posix_arg_size_max, 0);
535 if (arg_size > bc_ctl.posix_arg_size_max)
537 error (0, 0,
538 _("warning: value %ld for -s option is too large, "
539 "using %ld instead"),
540 arg_size, bc_ctl.posix_arg_size_max);
541 arg_size = bc_ctl.posix_arg_size_max;
543 bc_ctl.arg_max = arg_size;
545 break;
547 case 'S':
548 show_limits = true;
549 break;
551 case 't':
552 print_command = true;
553 break;
555 case 'x':
556 bc_ctl.exit_if_size_exceeded = true;
557 break;
559 case 'p':
560 query_before_executing = true;
561 print_command = true;
562 break;
564 case 'r':
565 always_run_command = 0;
566 break;
568 case 'P':
569 proc_max = parse_num (optarg, 'P', 0L, -1L, 1);
570 break;
572 case 'a':
573 input_file = optarg;
574 break;
576 case 'v':
577 printf (_("GNU xargs version %s\n"), version_string);
578 return 0;
580 default:
581 usage (stderr);
582 return 1;
586 /* If we had deferred failing due to problems in bc_init_controlinfo(),
587 * do it now.
589 * We issue this error message after processing command line
590 * arguments so that it is possible to use "xargs --help" even if
591 * the environment is too large.
593 act_on_init_result();
594 assert(BC_INIT_OK == bcstatus);
596 if (0 == strcmp (input_file, "-"))
598 input_stream = stdin;
600 else
602 keep_stdin = 1; /* see prep_child_for_exec() */
603 input_stream = fopen (input_file, "r");
604 if (NULL == input_stream)
606 error (1, errno,
607 _("Cannot open input file `%s'"),
608 input_file);
612 if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
613 bc_ctl.exit_if_size_exceeded = true;
615 if (optind == argc)
617 optind = 0;
618 argc = 1;
619 argv = &default_cmd;
622 /* We want to be able to print size_t values as unsigned long, so if
623 * the cast isn't value-preserving, we have a problem. This isn't a
624 * problem in C89, because size_t was known to be no wider than
625 * unsigned long. In C99 this is no longer the case, but there are
626 * special C99 ways to print such values. Unfortunately this
627 * program tries to work on both C89 and C99 systems.
629 #if defined(SIZE_MAX)
630 # if SIZE_MAX > ULONG_MAX
631 # error "I'm not sure how to print size_t values on your system"
632 # endif
633 #else
634 /* Without SIZE_MAX (i.e. limits.h) this is probably
635 * close to the best we can do.
637 assert(sizeof(size_t) <= sizeof(unsigned long));
638 #endif
640 if (show_limits)
642 fprintf(stderr,
643 _("Your environment variables take up %lu bytes\n"),
644 (unsigned long)bc_size_of_environment());
645 fprintf(stderr,
646 _("POSIX lower and upper limits on argument length: %lu, %lu\n"),
647 (unsigned long)bc_ctl.posix_arg_size_min,
648 (unsigned long)bc_ctl.posix_arg_size_max);
649 fprintf(stderr,
650 _("Maximum length of command we could actually use: %ld\n"),
651 (unsigned long)(bc_ctl.posix_arg_size_max -
652 bc_size_of_environment()));
653 fprintf(stderr,
654 _("Size of command buffer we are actually using: %lu\n"),
655 (unsigned long)bc_ctl.arg_max);
657 if (isatty(STDIN_FILENO))
659 fprintf(stderr,
660 "\n"
661 "Execution of xargs will continue now, and it will "
662 "try to read its input and run commands; if this is "
663 "not what you wanted to happen, please type the "
664 "end-of-file keystroke.\n");
668 linebuf = (char *) xmalloc (bc_ctl.arg_max + 1);
669 bc_state.argbuf = (char *) xmalloc (bc_ctl.arg_max + 1);
671 /* Make sure to listen for the kids. */
672 signal (SIGCHLD, SIG_DFL);
674 if (!bc_ctl.replace_pat)
676 for (; optind < argc; optind++)
677 bc_push_arg (&bc_ctl, &bc_state,
678 argv[optind], strlen (argv[optind]) + 1,
679 NULL, 0,
680 initial_args);
681 initial_args = false;
682 bc_ctl.initial_argc = bc_state.cmd_argc;
683 bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;
685 while ((*read_args) () != -1)
686 if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
688 xargs_do_exec (&bc_ctl, &bc_state);
689 lineno = 0;
692 /* SYSV xargs seems to do at least one exec, even if the
693 input is empty. */
694 if (bc_state.cmd_argc != bc_ctl.initial_argc
695 || (always_run_command && procs_executed == 0))
696 xargs_do_exec (&bc_ctl, &bc_state);
699 else
701 int i;
702 size_t len;
703 size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
705 for (i = optind; i < argc; i++)
706 arglen[i] = strlen(argv[i]);
707 bc_ctl.rplen = strlen (bc_ctl.replace_pat);
708 while ((len = (*read_args) ()) != -1)
710 /* Don't do insert on the command name. */
711 bc_clear_args(&bc_ctl, &bc_state);
712 bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
714 bc_push_arg (&bc_ctl, &bc_state,
715 argv[optind], arglen[optind] + 1,
716 NULL, 0,
717 initial_args);
718 len--;
719 initial_args = false;
721 for (i = optind + 1; i < argc; i++)
722 bc_do_insert (&bc_ctl, &bc_state,
723 argv[i], arglen[i],
724 NULL, 0,
725 linebuf, len,
726 initial_args);
727 xargs_do_exec (&bc_ctl, &bc_state);
731 original_exit_value = child_error;
732 return child_error;
736 /* Read a line of arguments from the input and add them to the list of
737 arguments to pass to the command. Ignore blank lines and initial blanks.
738 Single and double quotes and backslashes quote metacharacters and blanks
739 as they do in the shell.
740 Return -1 if eof (either physical or logical) is reached,
741 otherwise the length of the last string read (including the null). */
743 static int
744 read_line (void)
746 static boolean eof = false;
747 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
748 int state = SPACE; /* The type of character we last read. */
749 int prevc; /* The previous value of c. */
750 int quotc = 0; /* The last quote character read. */
751 int c = EOF;
752 boolean first = true; /* true if reading first arg on line. */
753 boolean seen_arg = false; /* true if we have seen any arg (or part of one) yet */
754 int len;
755 char *p = linebuf;
756 /* Including the NUL, the args must not grow past this point. */
757 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
759 if (eof)
760 return -1;
761 while (1)
763 prevc = c;
764 c = getc (input_stream);
766 if (c == EOF)
768 /* COMPAT: SYSV seems to ignore stuff on a line that
769 ends without a \n; we don't. */
770 eof = true;
771 if (p == linebuf)
772 return -1;
773 *p++ = '\0';
774 len = p - linebuf;
775 if (state == QUOTE)
777 exec_if_possible ();
778 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
779 quotc == '"' ? _("double") : _("single"));
781 if (first && EOF_STR (linebuf))
782 return -1;
783 if (!bc_ctl.replace_pat)
784 bc_push_arg (&bc_ctl, &bc_state,
785 linebuf, len,
786 NULL, 0,
787 initial_args);
788 return len;
790 switch (state)
792 case SPACE:
793 if (ISSPACE (c))
794 continue;
795 state = NORM;
796 /* aaahhhh.... */
798 case NORM:
799 if (c == '\n')
801 if (!ISBLANK (prevc))
802 lineno++; /* For -l. */
803 if (p == linebuf)
805 if (seen_arg)
807 /* An empty argument, add it to the list as normal. */
809 else
811 /* Blank line. */
812 state = SPACE;
813 continue;
816 *p++ = '\0';
817 len = p - linebuf;
818 if (EOF_STR (linebuf))
820 eof = true;
821 return first ? -1 : len;
823 if (!bc_ctl.replace_pat)
824 bc_push_arg (&bc_ctl, &bc_state,
825 linebuf, len,
826 NULL, 0,
827 initial_args);
828 return len;
830 seen_arg = true;
831 if (!bc_ctl.replace_pat && ISSPACE (c))
833 *p++ = '\0';
834 len = p - linebuf;
835 if (EOF_STR (linebuf))
837 eof = true;
838 return first ? -1 : len;
840 bc_push_arg (&bc_ctl, &bc_state,
841 linebuf, len,
842 NULL, 0,
843 initial_args);
844 p = linebuf;
845 state = SPACE;
846 first = false;
847 continue;
849 switch (c)
851 case '\\':
852 state = BACKSLASH;
853 continue;
855 case '\'':
856 case '"':
857 state = QUOTE;
858 quotc = c;
859 continue;
861 break;
863 case QUOTE:
864 if (c == '\n')
866 exec_if_possible ();
867 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
868 quotc == '"' ? _("double") : _("single"));
870 if (c == quotc)
872 state = NORM;
873 seen_arg = true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
874 continue;
876 break;
878 case BACKSLASH:
879 state = NORM;
880 break;
882 #if 1
883 if (p >= endbuf)
885 exec_if_possible ();
886 error (1, 0, _("argument line too long"));
888 *p++ = c;
889 #else
890 append_char_to_buf(&linebuf, &endbuf, &p, c);
891 #endif
895 /* Read a null-terminated string from the input and add it to the list of
896 arguments to pass to the command.
897 Return -1 if eof (either physical or logical) is reached,
898 otherwise the length of the string read (including the null). */
900 static int
901 read_string (void)
903 static boolean eof = false;
904 int len;
905 char *p = linebuf;
906 /* Including the NUL, the args must not grow past this point. */
907 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
909 if (eof)
910 return -1;
911 while (1)
913 int c = getc (input_stream);
914 if (c == EOF)
916 eof = true;
917 if (p == linebuf)
918 return -1;
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;
928 if (c == input_delimiter)
930 lineno++; /* For -l. */
931 *p++ = '\0';
932 len = p - linebuf;
933 if (!bc_ctl.replace_pat)
934 bc_push_arg (&bc_ctl, &bc_state,
935 linebuf, len,
936 NULL, 0,
937 initial_args);
938 return len;
940 if (p >= endbuf)
942 exec_if_possible ();
943 error (1, 0, _("argument line too long"));
945 *p++ = c;
949 /* Print the arguments of the command to execute.
950 If ASK is nonzero, prompt the user for a response, and
951 if the user responds affirmatively, return true;
952 otherwise, return false. */
954 static boolean
955 print_args (boolean ask)
957 int i;
959 for (i = 0; i < bc_state.cmd_argc - 1; i++)
960 fprintf (stderr, "%s ", bc_state.cmd_argv[i]);
961 if (ask)
963 static FILE *tty_stream;
964 int c, savec;
966 if (!tty_stream)
968 tty_stream = fopen ("/dev/tty", "r");
969 if (!tty_stream)
970 error (1, errno, "/dev/tty");
972 fputs ("?...", stderr);
973 fflush (stderr);
974 c = savec = getc (tty_stream);
975 while (c != EOF && c != '\n')
976 c = getc (tty_stream);
977 if (savec == 'y' || savec == 'Y')
978 return true;
980 else
981 putc ('\n', stderr);
983 return false;
987 /* Close stdin and attach /dev/null to it.
988 * This resolves Savannah bug #3992.
990 static void
991 prep_child_for_exec (void)
993 if (!keep_stdin)
995 const char inputfile[] = "/dev/null";
996 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
998 close(0);
999 if (open(inputfile, O_RDONLY) < 0)
1001 /* This is not entirely fatal, since
1002 * executing the child with a closed
1003 * stdin is almost as good as executing it
1004 * with its stdin attached to /dev/null.
1006 error (0, errno, "%s", inputfile);
1012 /* Execute the command that has been built in `cmd_argv'. This may involve
1013 waiting for processes that were previously executed. */
1015 static int
1016 xargs_do_exec (const struct buildcmd_control *ctl, struct buildcmd_state *state)
1018 pid_t child;
1020 (void) ctl;
1021 (void) state;
1023 bc_push_arg (&bc_ctl, &bc_state,
1024 (char *) NULL, 0,
1025 NULL, 0,
1026 false); /* Null terminate the arg list. */
1028 if (!query_before_executing || print_args (true))
1030 if (proc_max && procs_executing >= proc_max)
1031 wait_for_proc (false);
1032 if (!query_before_executing && print_command)
1033 print_args (false);
1034 /* If we run out of processes, wait for a child to return and
1035 try again. */
1036 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1037 wait_for_proc (false);
1038 switch (child)
1040 case -1:
1041 error (1, errno, _("cannot fork"));
1043 case 0: /* Child. */
1044 prep_child_for_exec();
1045 execvp (bc_state.cmd_argv[0], bc_state.cmd_argv);
1046 error (0, errno, "%s", bc_state.cmd_argv[0]);
1047 _exit (errno == ENOENT ? 127 : 126);
1048 /*NOTREACHED*/
1050 add_proc (child);
1053 bc_clear_args(&bc_ctl, &bc_state);
1054 return 1; /* Success */
1057 /* Execute the command if possible. */
1059 static void
1060 exec_if_possible (void)
1062 if (bc_ctl.replace_pat || initial_args ||
1063 bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
1064 return;
1065 xargs_do_exec (&bc_ctl, &bc_state);
1068 /* Add the process with id PID to the list of processes that have
1069 been executed. */
1071 static void
1072 add_proc (pid_t pid)
1074 int i;
1076 /* Find an empty slot. */
1077 for (i = 0; i < pids_alloc && pids[i]; i++)
1079 if (i == pids_alloc)
1081 if (pids_alloc == 0)
1083 pids_alloc = proc_max ? proc_max : 64;
1084 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
1086 else
1088 pids_alloc *= 2;
1089 pids = (pid_t *) xrealloc (pids,
1090 sizeof (pid_t) * pids_alloc);
1092 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
1094 pids[i] = pid;
1095 procs_executing++;
1096 procs_executed++;
1099 /* If ALL is true, wait for all child processes to finish;
1100 otherwise, wait for one child process to finish.
1101 Remove the processes that finish from the list of executing processes. */
1103 static void
1104 wait_for_proc (boolean all)
1106 while (procs_executing)
1108 int i, status;
1112 pid_t pid;
1114 while ((pid = wait (&status)) == (pid_t) -1)
1115 if (errno != EINTR)
1116 error (1, errno, _("error waiting for child process"));
1118 /* Find the entry in `pids' for the child process
1119 that exited. */
1120 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1123 while (i == pids_alloc); /* A child died that we didn't start? */
1125 /* Remove the child from the list. */
1126 pids[i] = 0;
1127 procs_executing--;
1129 if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
1130 exit (WEXITSTATUS (status)); /* Can't find or run the command. */
1131 if (WEXITSTATUS (status) == 255)
1132 error (124, 0, _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
1133 if (WIFSTOPPED (status))
1134 error (125, 0, _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
1135 if (WIFSIGNALED (status))
1136 error (125, 0, _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
1137 if (WEXITSTATUS (status) != 0)
1138 child_error = 123;
1140 if (!all)
1141 break;
1145 /* Wait for all child processes to finish. */
1147 static void
1148 wait_for_proc_all (void)
1150 static boolean waiting = false;
1152 if (waiting)
1153 return;
1155 waiting = true;
1156 wait_for_proc (true);
1157 waiting = false;
1159 if (original_exit_value != child_error)
1161 /* wait_for_proc() changed the value of child_error(). This
1162 * function is registered via atexit(), and so may have been
1163 * called from exit(). We now know that the original value
1164 * passed to exit() is no longer the exit status we require.
1165 * The POSIX standard states that the behaviour if exit() is
1166 * called more than once is undefined. Therefore we now have to
1167 * exit with _exit() instead of exit().
1169 _exit(child_error);
1174 /* Return the value of the number represented in STR.
1175 OPTION is the command line option to which STR is the argument.
1176 If the value does not fall within the boundaries MIN and MAX,
1177 Print an error message mentioning OPTION. If FATAL is true,
1178 we also exit. */
1180 static long
1181 parse_num (char *str, int option, long int min, long int max, int fatal)
1183 char *eptr;
1184 long val;
1186 val = strtol (str, &eptr, 10);
1187 if (eptr == str || *eptr)
1189 fprintf (stderr, _("%s: invalid number for -%c option\n"),
1190 program_name, option);
1191 usage (stderr);
1192 exit(1);
1194 else if (val < min)
1196 fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
1197 program_name, option, min);
1198 if (fatal)
1200 usage (stderr);
1201 exit(1);
1203 else
1205 val = min;
1208 else if (max >= 0 && val > max)
1210 fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
1211 program_name, option, max);
1212 if (fatal)
1214 usage (stderr);
1215 exit(1);
1217 else
1219 val = max;
1222 return val;
1225 static void
1226 usage (FILE *stream)
1228 fprintf (stream, _("\
1229 Usage: %s [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]\n\
1230 [-E eof-str] [-e[eof-str]] [--eof[=eof-str]]\n\
1231 [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]\n\
1232 [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
1233 [-n max-args] [--max-args=max-args]\n\
1234 [-s max-chars] [--max-chars=max-chars]\n\
1235 [-P max-procs] [--max-procs=max-procs] [[--show-limits]\n\
1236 [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
1237 [--version] [--help] [command [initial-arguments]]\n"),
1238 program_name);
1239 fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);