1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005, 2006,
3 2007, 2008 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* Written by Mike Rendell <michael@cs.mun.ca>
20 and David MacKenzie <djm@gnu.org>.
29 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
30 # define PARAMS(Args) Args
32 # define PARAMS(Args) ()
38 #if !defined (isascii) || defined (STDC_HEADERS)
46 #define ISBLANK(c) (isascii (c) && isblank (c))
48 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
51 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
52 || (c) == '\f' || (c) == '\v')
54 #include <sys/types.h>
60 #if defined STDC_HEADERS
64 #if defined HAVE_STRING_H || defined STDC_HEADERS
66 #if !defined STDC_HEADERS
71 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
75 #include <sys/param.h>
83 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
86 /* The presence of unistd.h is assumed by gnulib these days, so we
87 * might as well assume it too.
93 #if !defined(SIGCHLD) && defined(SIGCLD)
94 #define SIGCHLD SIGCLD
100 #include "findutils-version.h"
113 # include <libintl.h>
114 # define _(Text) gettext (Text)
116 # define _(Text) Text
117 #define textdomain(Domain)
118 #define bindtextdomain(Package, Directory)
121 # define N_(String) gettext_noop (String)
123 /* See locate.c for explanation as to why not use (String) */
124 # define N_(String) String
127 #include "buildcmd.h"
130 /* Return nonzero if S is the EOF string. */
131 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
133 /* Do multibyte processing if multibyte characters are supported,
134 unless multibyte sequences are search safe. Multibyte sequences
135 are search safe if searching for a substring using the byte
136 comparison function 'strstr' gives no false positives. All 8-bit
137 encodings and the UTF-8 multibyte encoding are search safe, but
138 the EUC encodings are not.
139 BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
141 # define MULTIBYTE_IS_SEARCH_SAFE 1
143 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
149 /* Simulate mbrlen with mblen as best we can. */
150 # define mbstate_t int
151 # define mbrlen(s, n, ps) mblen (s, n)
155 /* Not char because of type promotion; NeXT gcc can't handle it. */
168 #include "gnulib-version.h"
170 void error
PARAMS ((int status
, int errnum
, char *message
,...));
172 extern char *version_string
;
174 /* The name this program was run with. */
177 static FILE *input_stream
;
179 /* Buffer for reading arguments from input. */
180 static char *linebuf
;
182 static int keep_stdin
= 0;
184 /* Line number in stdin since the last command was executed. */
185 static int lineno
= 0;
187 static struct buildcmd_state bc_state
;
188 static struct buildcmd_control bc_ctl
;
190 /* Did we already complain about NUL characters in the input? */
191 static int nullwarning_given
= 0;
194 /* If nonzero, when this string is read on stdin it is treated as
196 IEEE Std 1003.1, 2004 Edition allows this to be NULL.
197 In findutils releases up to and including 4.2.8, this was "_".
199 static char *eof_str
= NULL
;
201 /* Number of chars in the initial args. */
202 /* static int initial_argv_chars = 0; */
204 /* true when building up initial arguments in `cmd_argv'. */
205 static boolean initial_args
= true;
207 /* If nonzero, the maximum number of child processes that can be running
209 static unsigned long int proc_max
= 1uL;
211 /* Did we fork a child yet? */
212 static boolean procs_executed
= false;
214 /* The number of elements in `pids'. */
215 static unsigned long int procs_executing
= 0uL;
217 /* List of child processes currently executing. */
218 static pid_t
*pids
= NULL
;
220 /* The number of allocated elements in `pids'. */
221 static size_t pids_alloc
= 0u;
223 /* Exit status; nonzero if any child process exited with a
225 static volatile int child_error
= 0;
227 static volatile int original_exit_value
;
229 /* If true, print each command on stderr before executing it. */
230 static boolean print_command
= false; /* Option -t */
232 /* If true, query the user before executing each command, and only
233 execute the command if the user responds affirmatively. */
234 static boolean query_before_executing
= false;
236 /* The delimiter for input arguments. This is only consulted if the
237 * -0 or -d option had been given.
239 static char input_delimiter
= '\0';
242 static struct option
const longopts
[] =
244 {"null", no_argument
, NULL
, '0'},
245 {"arg-file", required_argument
, NULL
, 'a'},
246 {"delimiter", required_argument
, NULL
, 'd'},
247 {"eof", optional_argument
, NULL
, 'e'},
248 {"replace", optional_argument
, NULL
, 'I'},
249 {"max-lines", optional_argument
, NULL
, 'l'},
250 {"max-args", required_argument
, NULL
, 'n'},
251 {"interactive", no_argument
, NULL
, 'p'},
252 {"no-run-if-empty", no_argument
, NULL
, 'r'},
253 {"max-chars", required_argument
, NULL
, 's'},
254 {"verbose", no_argument
, NULL
, 't'},
255 {"show-limits", no_argument
, NULL
, 'S'},
256 {"exit", no_argument
, NULL
, 'x'},
257 {"max-procs", required_argument
, NULL
, 'P'},
258 {"version", no_argument
, NULL
, 'v'},
259 {"help", no_argument
, NULL
, 'h'},
260 {NULL
, no_argument
, NULL
, 0}
263 static int read_line
PARAMS ((void));
264 static int read_string
PARAMS ((void));
265 static boolean print_args
PARAMS ((boolean ask
));
266 /* static void do_exec PARAMS ((void)); */
267 static int xargs_do_exec (const struct buildcmd_control
*cl
, struct buildcmd_state
*state
);
268 static void exec_if_possible
PARAMS ((void));
269 static void add_proc
PARAMS ((pid_t pid
));
270 static void wait_for_proc
PARAMS ((boolean all
, unsigned int minreap
));
271 static void wait_for_proc_all
PARAMS ((void));
272 static long parse_num
PARAMS ((char *str
, int option
, long min
, long max
, int fatal
));
273 static void usage
PARAMS ((FILE * stream
));
278 get_char_oct_or_hex_escape(const char *s
)
285 assert ('\\' == s
[0]);
293 else if (isdigit(s
[1]))
301 p
= NULL
; /* Silence compiler warning. */
303 _("Invalid escape sequence %s in input delimiter specification."),
308 val
= strtoul(p
, &endp
, base
);
310 /* This if condition is carefully constructed to do
311 * the right thing if UCHAR_MAX has the same
312 * value as ULONG_MAX. IF UCHAR_MAX==ULONG_MAX,
313 * then val can never be greater than UCHAR_MAX.
315 if ((ULONG_MAX
== val
&& ERANGE
== errno
)
316 || (val
> UCHAR_MAX
))
321 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
322 s
, (unsigned long)UCHAR_MAX
);
327 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
328 s
, (unsigned long)UCHAR_MAX
);
332 /* check for trailing garbage */
336 _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
345 get_input_delimiter(const char *s
)
375 return get_char_oct_or_hex_escape(s
);
381 _("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
396 fail_due_to_env_size (void)
398 error (1, 0, _("environment is too large for exec"));
403 main (int argc
, char **argv
)
406 int show_limits
= 0; /* --show-limits */
407 int always_run_command
= 1;
408 char *input_file
= "-"; /* "-" is stdin */
409 char *default_cmd
= "/bin/echo";
410 int (*read_args
) PARAMS ((void)) = read_line
;
411 void (*act_on_init_result
)(void) = noop
;
412 enum BC_INIT_STATUS bcstatus
;
413 enum { XARGS_POSIX_HEADROOM
= 2048u };
415 program_name
= argv
[0];
416 original_exit_value
= 0;
418 #ifdef HAVE_SETLOCALE
419 setlocale (LC_ALL
, "");
421 bindtextdomain (PACKAGE
, LOCALEDIR
);
422 textdomain (PACKAGE
);
423 atexit (close_stdin
);
424 atexit (wait_for_proc_all
);
426 /* xargs is required by POSIX to allow 2048 bytes of headroom
427 * for extra environment variables (that perhaps the utliity might
428 * want to set before execing something else).
430 bcstatus
= bc_init_controlinfo(&bc_ctl
, XARGS_POSIX_HEADROOM
);
432 /* The bc_init_controlinfo call may have determined that the
433 * environment is too big. In that case, we will fail with
434 * an error message after processing the command-line options,
435 * as "xargs --help" should still work even if the environment is
438 * Some of the argument processing depends on the contents of
439 * bc_ctl, which will be in an undefined state if bc_init_controlinfo()
442 if (BC_INIT_ENV_TOO_BIG
== bcstatus
)
444 act_on_init_result
= fail_due_to_env_size
;
446 else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM
== bcstatus
)
448 /* All POSIX systems are required to support ARG_MAX of at least
449 * 4096. For everything to work the total of (command line +
450 * headroom + environment) must fit into this. POSIX requires
451 * that we use a headroom of 2048 bytes. The user is in control
452 * of the size of the environment.
454 * In general if bc_init_controlinfo() returns
455 * BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
456 * with a smaller headroom. However, in the case of xargs, this
457 * would not be POSIX-compliant.
459 act_on_init_result
= fail_due_to_env_size
;
463 /* IEEE Std 1003.1, 2003 specifies that the combined argument and
464 * environment list shall not exceed {ARG_MAX}-2048 bytes. It also
465 * specifies that it shall be at least LINE_MAX.
468 long val
= sysconf(_SC_ARG_MAX
);
471 /* Note that val can in fact be greater than ARG_MAX
472 * and bc_ctl.arg_max can also be greater than ARG_MAX.
474 assert (bc_ctl
.arg_max
<= (val
-XARGS_POSIX_HEADROOM
));
479 assert (bc_ctl
.arg_max
<= (ARG_MAX
-XARGS_POSIX_HEADROOM
));
484 assert (bc_ctl
.arg_max
<= (ARG_MAX
-XARGS_POSIX_HEADROOM
));
489 /* This assertion ensures that this xargs implementation
490 * conforms to the POSIX requirement that the default command
491 * line length shall be at least LINE_MAX.
493 assert (bc_ctl
.arg_max
>= LINE_MAX
);
496 bc_ctl
.exec_callback
= xargs_do_exec
;
498 /* Start with a reasonable default size, though this can be
499 * adjusted via the -s option.
501 bc_use_sensible_arg_max(&bc_ctl
);
504 while ((optc
= getopt_long (argc
, argv
, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
505 longopts
, (int *) 0)) != -1)
510 read_args
= read_string
;
511 input_delimiter
= '\0';
515 read_args
= read_string
;
516 input_delimiter
= get_input_delimiter(optarg
);
519 case 'E': /* POSIX */
520 case 'e': /* deprecated */
521 if (optarg
&& (strlen(optarg
) > 0))
531 case 'I': /* POSIX */
532 case 'i': /* deprecated */
534 bc_ctl
.replace_pat
= optarg
;
536 bc_ctl
.replace_pat
= "{}";
537 /* -i excludes -n -l. */
538 bc_ctl
.args_per_exec
= 0;
539 bc_ctl
.lines_per_exec
= 0;
542 case 'L': /* POSIX */
543 bc_ctl
.lines_per_exec
= parse_num (optarg
, 'L', 1L, -1L, 1);
544 /* -L excludes -i -n. */
545 bc_ctl
.args_per_exec
= 0;
546 bc_ctl
.replace_pat
= NULL
;
549 case 'l': /* deprecated */
551 bc_ctl
.lines_per_exec
= parse_num (optarg
, 'l', 1L, -1L, 1);
553 bc_ctl
.lines_per_exec
= 1;
554 /* -l excludes -i -n. */
555 bc_ctl
.args_per_exec
= 0;
556 bc_ctl
.replace_pat
= NULL
;
560 bc_ctl
.args_per_exec
= parse_num (optarg
, 'n', 1L, -1L, 1);
561 /* -n excludes -i -l. */
562 bc_ctl
.lines_per_exec
= 0;
563 if (bc_ctl
.args_per_exec
== 1 && bc_ctl
.replace_pat
)
564 /* ignore -n1 in '-i -n1' */
565 bc_ctl
.args_per_exec
= 0;
567 bc_ctl
.replace_pat
= NULL
;
570 /* The POSIX standard specifies that it is not an error
571 * for the -s option to specify a size that the implementation
572 * cannot support - in that case, the relevant limit is used.
577 act_on_init_result();
578 arg_size
= parse_num (optarg
, 's', 1L,
579 bc_ctl
.posix_arg_size_max
, 0);
580 if (arg_size
> bc_ctl
.posix_arg_size_max
)
583 _("Warning: value %ld for -s option is too large, "
584 "using %ld instead"),
585 arg_size
, bc_ctl
.posix_arg_size_max
);
586 arg_size
= bc_ctl
.posix_arg_size_max
;
588 bc_ctl
.arg_max
= arg_size
;
597 print_command
= true;
601 bc_ctl
.exit_if_size_exceeded
= true;
605 query_before_executing
= true;
606 print_command
= true;
610 always_run_command
= 0;
614 /* Allow only up to LONG_MAX child processes. */
615 proc_max
= parse_num (optarg
, 'P', 0L, LONG_MAX
, 1);
623 display_findutils_version("xargs");
632 /* If we had deferred failing due to problems in bc_init_controlinfo(),
635 * We issue this error message after processing command line
636 * arguments so that it is possible to use "xargs --help" even if
637 * the environment is too large.
639 act_on_init_result();
640 assert (BC_INIT_OK
== bcstatus
);
642 if (0 == strcmp (input_file
, "-"))
644 input_stream
= stdin
;
648 keep_stdin
= 1; /* see prep_child_for_exec() */
649 input_stream
= fopen (input_file
, "r");
650 if (NULL
== input_stream
)
653 _("Cannot open input file %s"),
654 quotearg_n_style(0, locale_quoting_style
, input_file
));
658 if (bc_ctl
.replace_pat
|| bc_ctl
.lines_per_exec
)
659 bc_ctl
.exit_if_size_exceeded
= true;
668 /* We want to be able to print size_t values as unsigned long, so if
669 * the cast isn't value-preserving, we have a problem. This isn't a
670 * problem in C89, because size_t was known to be no wider than
671 * unsigned long. In C99 this is no longer the case, but there are
672 * special C99 ways to print such values. Unfortunately this
673 * program tries to work on both C89 and C99 systems.
676 # if SIZE_MAX > ULONG_MAX
677 # error "I'm not sure how to print size_t values on your system"
680 /* Without SIZE_MAX (i.e. limits.h) this is probably
681 * close to the best we can do.
683 verify_true (sizeof(size_t) <= sizeof(unsigned long));
689 _("Your environment variables take up %lu bytes\n"),
690 (unsigned long)bc_size_of_environment());
692 _("POSIX upper limit on argument length (this system): %lu\n"),
693 (unsigned long)bc_ctl
.posix_arg_size_max
);
695 _("POSIX smallest allowable upper limit on argument length (all systems): %lu\n"),
696 (unsigned long)bc_ctl
.posix_arg_size_min
);
698 _("Maximum length of command we could actually use: %ld\n"),
699 (unsigned long)(bc_ctl
.posix_arg_size_max
-
700 bc_size_of_environment()));
702 _("Size of command buffer we are actually using: %lu\n"),
703 (unsigned long)bc_ctl
.arg_max
);
705 if (isatty(STDIN_FILENO
))
709 "Execution of xargs will continue now, and it will "
710 "try to read its input and run commands; if this is "
711 "not what you wanted to happen, please type the "
712 "end-of-file keystroke.\n"));
713 if (always_run_command
)
716 _("Warning: %s will be run at least once. "
717 "If you do not want that to happen, then press "
718 "the interrupt keystroke.\n"),
724 linebuf
= xmalloc (bc_ctl
.arg_max
+ 1);
725 bc_state
.argbuf
= xmalloc (bc_ctl
.arg_max
+ 1);
727 /* Make sure to listen for the kids. */
728 signal (SIGCHLD
, SIG_DFL
);
730 if (!bc_ctl
.replace_pat
)
732 for (; optind
< argc
; optind
++)
733 bc_push_arg (&bc_ctl
, &bc_state
,
734 argv
[optind
], strlen (argv
[optind
]) + 1,
737 initial_args
= false;
738 bc_ctl
.initial_argc
= bc_state
.cmd_argc
;
739 bc_state
.cmd_initial_argv_chars
= bc_state
.cmd_argv_chars
;
741 while ((*read_args
) () != -1)
742 if (bc_ctl
.lines_per_exec
&& lineno
>= bc_ctl
.lines_per_exec
)
744 xargs_do_exec (&bc_ctl
, &bc_state
);
748 /* SYSV xargs seems to do at least one exec, even if the
750 if (bc_state
.cmd_argc
!= bc_ctl
.initial_argc
751 || (always_run_command
&& !procs_executed
))
752 xargs_do_exec (&bc_ctl
, &bc_state
);
759 size_t *arglen
= xmalloc (sizeof (size_t) * argc
);
761 for (i
= optind
; i
< argc
; i
++)
762 arglen
[i
] = strlen(argv
[i
]);
763 bc_ctl
.rplen
= strlen (bc_ctl
.replace_pat
);
764 while ((len
= (*read_args
) ()) != -1)
766 /* Don't do insert on the command name. */
767 bc_clear_args(&bc_ctl
, &bc_state
);
768 bc_state
.cmd_argv_chars
= 0; /* begin at start of buffer */
770 bc_push_arg (&bc_ctl
, &bc_state
,
771 argv
[optind
], arglen
[optind
] + 1,
775 initial_args
= false;
777 for (i
= optind
+ 1; i
< argc
; i
++)
778 bc_do_insert (&bc_ctl
, &bc_state
,
783 xargs_do_exec (&bc_ctl
, &bc_state
);
787 original_exit_value
= child_error
;
792 /* Read a line of arguments from the input and add them to the list of
793 arguments to pass to the command. Ignore blank lines and initial blanks.
794 Single and double quotes and backslashes quote metacharacters and blanks
795 as they do in the shell.
796 Return -1 if eof (either physical or logical) is reached,
797 otherwise the length of the last string read (including the null). */
802 /* States for read_line. */
810 static boolean eof
= false;
811 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
812 enum read_line_state state
= SPACE
; /* The type of character we last read. */
813 int prevc
; /* The previous value of c. */
814 int quotc
= 0; /* The last quote character read. */
816 boolean first
= true; /* true if reading first arg on line. */
817 boolean seen_arg
= false; /* true if we have seen any arg (or part of one) yet */
820 /* Including the NUL, the args must not grow past this point. */
821 char *endbuf
= linebuf
+ bc_ctl
.arg_max
- bc_state
.cmd_initial_argv_chars
- 1;
828 c
= getc (input_stream
);
832 /* COMPAT: SYSV seems to ignore stuff on a line that
833 ends without a \n; we don't. */
842 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
843 quotc
== '"' ? _("double") : _("single"));
845 if (first
&& EOF_STR (linebuf
))
847 if (!bc_ctl
.replace_pat
)
848 bc_push_arg (&bc_ctl
, &bc_state
,
865 if (!ISBLANK (prevc
))
866 lineno
++; /* For -l. */
871 /* An empty argument, add it to the list as normal. */
882 if (EOF_STR (linebuf
))
885 return first
? -1 : len
;
887 if (!bc_ctl
.replace_pat
)
888 bc_push_arg (&bc_ctl
, &bc_state
,
896 /* POSIX: In the POSIX locale, the separators are <SPC> and
897 * <TAB>, but not <FF> or <VT>.
899 if (!bc_ctl
.replace_pat
&& ISBLANK (c
))
903 if (EOF_STR (linebuf
))
906 return first
? -1 : len
;
908 bc_push_arg (&bc_ctl
, &bc_state
,
935 error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
936 quotc
== '"' ? _("double") : _("single"));
941 seen_arg
= true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
951 if ( (0 == c
) && !nullwarning_given
)
953 /* This is just a warning message. We only issue it once. */
955 _("Warning: a NUL character occurred in the input. "
956 "It cannot be passed through in the argument list. "
957 "Did you mean to use the --null option?"));
958 nullwarning_given
= 1;
965 error (1, 0, _("argument line too long"));
969 append_char_to_buf(&linebuf
, &endbuf
, &p
, c
);
974 /* Read a null-terminated string from the input and add it to the list of
975 arguments to pass to the command.
976 Return -1 if eof (either physical or logical) is reached,
977 otherwise the length of the string read (including the null). */
982 static boolean eof
= false;
985 /* Including the NUL, the args must not grow past this point. */
986 char *endbuf
= linebuf
+ bc_ctl
.arg_max
- bc_state
.cmd_initial_argv_chars
- 1;
992 int c
= getc (input_stream
);
1000 if (!bc_ctl
.replace_pat
)
1001 bc_push_arg (&bc_ctl
, &bc_state
,
1007 if (c
== input_delimiter
)
1009 lineno
++; /* For -l. */
1012 if (!bc_ctl
.replace_pat
)
1013 bc_push_arg (&bc_ctl
, &bc_state
,
1021 exec_if_possible ();
1022 error (1, 0, _("argument line too long"));
1028 /* Print the arguments of the command to execute.
1029 If ASK is nonzero, prompt the user for a response, and
1030 if the user responds affirmatively, return true;
1031 otherwise, return false. */
1034 print_args (boolean ask
)
1038 for (i
= 0; i
< bc_state
.cmd_argc
- 1; i
++)
1039 fprintf (stderr
, "%s ", bc_state
.cmd_argv
[i
]);
1042 static FILE *tty_stream
;
1047 tty_stream
= fopen ("/dev/tty", "r");
1049 error (1, errno
, "/dev/tty");
1051 fputs ("?...", stderr
);
1053 c
= savec
= getc (tty_stream
);
1054 while (c
!= EOF
&& c
!= '\n')
1055 c
= getc (tty_stream
);
1056 if (savec
== 'y' || savec
== 'Y')
1060 putc ('\n', stderr
);
1066 /* Close stdin and attach /dev/null to it.
1067 * This resolves Savannah bug #3992.
1070 prep_child_for_exec (void)
1074 const char inputfile
[] = "/dev/null";
1075 /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
1078 if (open(inputfile
, O_RDONLY
) < 0)
1080 /* This is not entirely fatal, since
1081 * executing the child with a closed
1082 * stdin is almost as good as executing it
1083 * with its stdin attached to /dev/null.
1085 error (0, errno
, "%s", quotearg_n_style(0, locale_quoting_style
, inputfile
));
1091 /* Execute the command that has been built in `cmd_argv'. This may involve
1092 waiting for processes that were previously executed. */
1095 xargs_do_exec (const struct buildcmd_control
*ctl
, struct buildcmd_state
*state
)
1102 bc_push_arg (&bc_ctl
, &bc_state
,
1105 false); /* Null terminate the arg list. */
1107 if (!query_before_executing
|| print_args (true))
1111 if (procs_executing
>= proc_max
)
1112 wait_for_proc (false, proc_max
- procs_executing
+ 1);
1113 assert (procs_executing
< proc_max
);
1115 if (!query_before_executing
&& print_command
)
1118 /* Before forking, reap any already-exited child. We do this so
1119 that we don't leave unreaped children around while we build a
1120 new command line. For example this command will spend most
1121 of its time waiting for sufficient arguments to launch
1122 another command line:
1124 seq 1 1000 | fmt | while read x ; do echo $x; sleep 1 ; done |
1125 ./xargs -P 200 -n 20 sh -c 'echo "$@"; sleep $((1 + $RANDOM % 5))' sleeper
1127 wait_for_proc (false, 0u);
1129 /* If we run out of processes, wait for a child to return and
1131 while ((child
= fork ()) < 0 && errno
== EAGAIN
&& procs_executing
)
1132 wait_for_proc (false, 1u);
1137 error (1, errno
, _("cannot fork"));
1139 case 0: /* Child. */
1140 prep_child_for_exec();
1141 execvp (bc_state
.cmd_argv
[0], bc_state
.cmd_argv
);
1142 error (0, errno
, "%s", bc_state
.cmd_argv
[0]);
1143 _exit (errno
== ENOENT
? 127 : 126);
1149 bc_clear_args(&bc_ctl
, &bc_state
);
1150 return 1; /* Success */
1153 /* Execute the command if possible. */
1156 exec_if_possible (void)
1158 if (bc_ctl
.replace_pat
|| initial_args
||
1159 bc_state
.cmd_argc
== bc_ctl
.initial_argc
|| bc_ctl
.exit_if_size_exceeded
)
1161 xargs_do_exec (&bc_ctl
, &bc_state
);
1164 /* Add the process with id PID to the list of processes that have
1168 add_proc (pid_t pid
)
1172 /* Find an empty slot. */
1173 for (i
= 0; i
< pids_alloc
&& pids
[i
]; i
++)
1176 /* Extend the array if we failed. */
1177 if (i
== pids_alloc
)
1179 pids
= x2nrealloc (pids
, &pids_alloc
, sizeof *pids
);
1181 /* Zero out the new slots. */
1182 for (j
=i
; j
<pids_alloc
; ++j
)
1185 /* Verify that we are not destroying the record of some existing child. */
1186 assert (0 == pids
[i
]);
1188 /* Remember the child. */
1191 procs_executed
= true;
1195 /* If ALL is true, wait for all child processes to finish;
1196 otherwise, wait for at least MINREAP child processes to finish.
1197 Remove the processes that finish from the list of executing processes. */
1200 wait_for_proc (boolean all
, unsigned int minreap
)
1202 unsigned int reaped
= 0;
1204 while (procs_executing
)
1213 if (reaped
>= minreap
)
1215 /* We already reaped enough children. To save system
1216 * resources, reap any dead children anyway, but do not
1217 * wait for any currently executing children to exit.
1226 /* Wait for any child. We used to use wait() here, but it's
1227 * unlikely that that offers any portability advantage over
1230 while ((pid
= waitpid (-1, &status
, wflags
)) == (pid_t
) -1)
1233 error (1, errno
, _("error waiting for child process"));
1236 /* Find the entry in `pids' for the child process
1240 for (i
= 0; i
< pids_alloc
&& pid
!= pids
[i
]; i
++)
1244 while (pid
&& i
== pids_alloc
); /* A child died that we didn't start? */
1248 if (!(wflags
& WNOHANG
))
1250 /* Nothing remained to be reaped. This should not
1251 * happen, because procs_executing should contain the
1252 * number of child processes still executing, so the
1253 * loop should have terminated.
1255 error (0, 0, _("Warning: Lost track of %d child processes"),
1260 /* Children are (probably) executing but are not ready
1261 * to be reaped at the moment.
1267 /* Remove the child from the list. */
1272 if (WEXITSTATUS (status
) == 126 || WEXITSTATUS (status
) == 127)
1273 exit (WEXITSTATUS (status
)); /* Can't find or run the command. */
1274 if (WEXITSTATUS (status
) == 255)
1275 error (124, 0, _("%s: exited with status 255; aborting"), bc_state
.cmd_argv
[0]);
1276 if (WIFSTOPPED (status
))
1277 error (125, 0, _("%s: stopped by signal %d"), bc_state
.cmd_argv
[0], WSTOPSIG (status
));
1278 if (WIFSIGNALED (status
))
1279 error (125, 0, _("%s: terminated by signal %d"), bc_state
.cmd_argv
[0], WTERMSIG (status
));
1280 if (WEXITSTATUS (status
) != 0)
1285 /* Wait for all child processes to finish. */
1288 wait_for_proc_all (void)
1290 static boolean waiting
= false;
1296 wait_for_proc (true, 0u);
1299 if (original_exit_value
!= child_error
)
1301 /* wait_for_proc() changed the value of child_error(). This
1302 * function is registered via atexit(), and so may have been
1303 * called from exit(). We now know that the original value
1304 * passed to exit() is no longer the exit status we require.
1305 * The POSIX standard states that the behaviour if exit() is
1306 * called more than once is undefined. Therefore we now have to
1307 * exit with _exit() instead of exit().
1314 /* Return the value of the number represented in STR.
1315 OPTION is the command line option to which STR is the argument.
1316 If the value does not fall within the boundaries MIN and MAX,
1317 Print an error message mentioning OPTION. If FATAL is true,
1321 parse_num (char *str
, int option
, long int min
, long int max
, int fatal
)
1326 val
= strtol (str
, &eptr
, 10);
1327 if (eptr
== str
|| *eptr
)
1329 fprintf (stderr
, _("%s: invalid number for -%c option\n"),
1330 program_name
, option
);
1336 fprintf (stderr
, _("%s: value for -%c option should be >= %ld\n"),
1337 program_name
, option
, min
);
1348 else if (max
>= 0 && val
> max
)
1350 fprintf (stderr
, _("%s: value for -%c option should be < %ld\n"),
1351 program_name
, option
, max
);
1366 usage (FILE *stream
)
1368 fprintf (stream
, _("\
1369 Usage: %s [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]\n\
1370 [-E eof-str] [-e[eof-str]] [--eof[=eof-str]]\n\
1371 [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]\n\
1372 [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
1373 [-n max-args] [--max-args=max-args]\n\
1374 [-s max-chars] [--max-chars=max-chars]\n\
1375 [-P max-procs] [--max-procs=max-procs] [--show-limits]\n\
1376 [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
1377 [--version] [--help] [command [initial-arguments]]\n"),
1379 fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream
);