1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 91, 92, 93, 94, 2000,2003 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)
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., 9 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Written by Mike Rendell <michael@cs.mun.ca>
21 and David MacKenzie <djm@gnu.ai.mit.edu>. */
23 #include <gnulib/config.h>
25 #undef PACKAGE_VERSION
26 #undef PACKAGE_TARNAME
33 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
34 # define PARAMS(Args) Args
36 # define PARAMS(Args) ()
45 #if !defined (isascii) || defined (STDC_HEADERS)
53 #define ISBLANK(c) (isascii (c) && isblank (c))
55 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
58 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
59 || (c) == '\f' || (c) == '\v')
61 #include <sys/types.h>
66 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
68 #if !defined(STDC_HEADERS)
73 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
77 #include <sys/param.h>
85 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
94 #if !defined(SIGCHLD) && defined(SIGCLD)
95 #define SIGCHLD SIGCLD
98 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
99 We try to make it as large as possible. */
100 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
101 #define ARG_MAX sysconf (_SC_ARG_MAX)
104 #define ARG_MAX NCARGS
109 /* States for read_line. */
125 # include <libintl.h>
126 # define _(Text) gettext (Text)
128 # define _(Text) Text
129 #define textdomain(Domain)
130 #define bindtextdomain(Package, Directory)
133 # define N_(String) gettext_noop (String)
135 # define N_(String) (String)
138 /* Return nonzero if S is the EOF string. */
139 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
141 extern char **environ
;
143 /* Do multibyte processing if multibyte characters are supported,
144 unless multibyte sequences are search safe. Multibyte sequences
145 are search safe if searching for a substring using the byte
146 comparison function 'strstr' gives no false positives. All 8-bit
147 encodings and the UTF-8 multibyte encoding are search safe, but
148 the EUC encodings are not.
149 BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
151 # define MULTIBYTE_IS_SEARCH_SAFE 1
153 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)
159 /* Simulate mbrlen with mblen as best we can. */
160 # define mbstate_t int
161 # define mbrlen(s, n, ps) mblen (s, n)
165 /* Not char because of type promotion; NeXT gcc can't handle it. */
177 void error
PARAMS ((int status
, int errnum
, char *message
,...));
179 extern char *version_string
;
181 /* The name this program was run with. */
184 /* Buffer for reading arguments from stdin. */
185 static char *linebuf
;
187 /* Line number in stdin since the last command was executed. */
188 static int lineno
= 0;
190 /* If nonzero, then instead of putting the args from stdin at
191 the end of the command argument list, they are each stuck into the
192 initial args, replacing each occurrence of the `replace_pat' in the
194 static char *replace_pat
= NULL
;
196 /* The length of `replace_pat'. */
197 static size_t rplen
= 0;
199 /* If nonzero, when this string is read on stdin it is treated as
201 I don't like this - it should default to NULL. */
202 static char *eof_str
= "_";
204 /* If nonzero, the maximum number of nonblank lines from stdin to use
206 static long lines_per_exec
= 0;
208 /* The maximum number of arguments to use per command line. */
209 static long args_per_exec
= 1024;
211 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
212 static boolean exit_if_size_exceeded
= false;
214 /* The maximum number of characters that can be used per command line. */
217 /* Storage for elements of `cmd_argv'. */
220 /* The list of args being built. */
221 static char **cmd_argv
= NULL
;
223 /* Number of elements allocated for `cmd_argv'. */
224 static int cmd_argv_alloc
= 0;
226 /* Number of valid elements in `cmd_argv'. */
227 static int cmd_argc
= 0;
229 /* Number of chars being used in `cmd_argv'. */
230 static int cmd_argv_chars
= 0;
232 /* Number of initial arguments given on the command line. */
233 static int initial_argc
= 0;
235 /* Number of chars in the initial args. */
236 static int initial_argv_chars
= 0;
238 /* true when building up initial arguments in `cmd_argv'. */
239 static boolean initial_args
= true;
241 /* If nonzero, the maximum number of child processes that can be running
243 static int proc_max
= 1;
245 /* Total number of child processes that have been executed. */
246 static int procs_executed
= 0;
248 /* The number of elements in `pids'. */
249 static int procs_executing
= 0;
251 /* List of child processes currently executing. */
252 static pid_t
*pids
= NULL
;
254 /* The number of allocated elements in `pids'. */
255 static int pids_alloc
= 0;
257 /* Exit status; nonzero if any child process exited with a
259 static int child_error
= 0;
261 /* If true, print each command on stderr before executing it. */
262 static boolean print_command
= false;
264 /* If true, query the user before executing each command, and only
265 execute the command if the user responds affirmatively. */
266 static boolean query_before_executing
= false;
268 static struct option
const longopts
[] =
270 {"null", no_argument
, NULL
, '0'},
271 {"eof", optional_argument
, NULL
, 'e'},
272 {"replace", optional_argument
, NULL
, 'i'},
273 {"max-lines", optional_argument
, NULL
, 'l'},
274 {"max-args", required_argument
, NULL
, 'n'},
275 {"interactive", no_argument
, NULL
, 'p'},
276 {"no-run-if-empty", no_argument
, NULL
, 'r'},
277 {"max-chars", required_argument
, NULL
, 's'},
278 {"verbose", no_argument
, NULL
, 't'},
279 {"exit", no_argument
, NULL
, 'x'},
280 {"max-procs", required_argument
, NULL
, 'P'},
281 {"version", no_argument
, NULL
, 'v'},
282 {"help", no_argument
, NULL
, 'h'},
283 {NULL
, no_argument
, NULL
, 0}
286 static int read_line
PARAMS ((void));
287 static int read_string
PARAMS ((void));
288 static char *mbstrstr
PARAMS ((const char *haystack
, const char *needle
));
289 static void do_insert
PARAMS ((char *arg
, size_t arglen
, size_t lblen
));
290 static void push_arg
PARAMS ((char *arg
, size_t len
));
291 static boolean print_args
PARAMS ((boolean ask
));
292 static void do_exec
PARAMS ((void));
293 static void add_proc
PARAMS ((pid_t pid
));
294 static void wait_for_proc
PARAMS ((boolean all
));
295 static long parse_num
PARAMS ((char *str
, int option
, long min
, long max
));
296 static long env_size
PARAMS ((char **envp
));
297 static void usage
PARAMS ((FILE * stream
, int status
));
300 main (int argc
, char **argv
)
303 int always_run_command
= 1;
305 char *default_cmd
= "/bin/echo";
306 int (*read_args
) PARAMS ((void)) = read_line
;
308 program_name
= argv
[0];
310 #ifdef HAVE_SETLOCALE
311 setlocale (LC_ALL
, "");
313 bindtextdomain (PACKAGE
, LOCALEDIR
);
314 textdomain (PACKAGE
);
316 orig_arg_max
= ARG_MAX
;
317 if (orig_arg_max
== -1)
318 orig_arg_max
= LONG_MAX
;
319 orig_arg_max
-= 2048; /* POSIX.2 requires subtracting 2048. */
320 arg_max
= orig_arg_max
;
322 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
323 have it at 1 meg). Things will work fine with a large ARG_MAX but it
324 will probably hurt the system more than it needs to; an array of this
325 size is allocated. */
326 if (arg_max
> 20 * 1024)
329 /* Take the size of the environment into account. */
330 arg_max
-= env_size (environ
);
332 error (1, 0, _("environment is too large for exec"));
334 while ((optc
= getopt_long (argc
, argv
, "+0e::i::l::n:prs:txP:",
335 longopts
, (int *) 0)) != -1)
340 read_args
= read_string
;
355 replace_pat
= optarg
;
358 /* -i excludes -n -l. */
365 lines_per_exec
= parse_num (optarg
, 'l', 1L, -1L);
368 /* -l excludes -i -n. */
374 args_per_exec
= parse_num (optarg
, 'n', 1L, -1L);
375 /* -n excludes -i -l. */
381 arg_max
= parse_num (optarg
, 's', 1L, orig_arg_max
);
385 print_command
= true;
389 exit_if_size_exceeded
= true;
393 query_before_executing
= true;
394 print_command
= true;
398 always_run_command
= 0;
402 proc_max
= parse_num (optarg
, 'P', 0L, -1L);
406 printf (_("GNU xargs version %s\n"), version_string
);
414 if (replace_pat
|| lines_per_exec
)
415 exit_if_size_exceeded
= true;
424 linebuf
= (char *) xmalloc (arg_max
+ 1);
425 argbuf
= (char *) xmalloc (arg_max
+ 1);
427 /* Make sure to listen for the kids. */
428 signal (SIGCHLD
, SIG_DFL
);
432 for (; optind
< argc
; optind
++)
433 push_arg (argv
[optind
], strlen (argv
[optind
]) + 1);
434 initial_args
= false;
435 initial_argc
= cmd_argc
;
436 initial_argv_chars
= cmd_argv_chars
;
438 while ((*read_args
) () != -1)
439 if (lines_per_exec
&& lineno
>= lines_per_exec
)
445 /* SYSV xargs seems to do at least one exec, even if the
447 if (cmd_argc
!= initial_argc
448 || (always_run_command
&& procs_executed
== 0))
455 size_t *arglen
= (size_t *) xmalloc (sizeof (size_t) * argc
);
457 for (i
= optind
; i
< argc
; i
++)
458 arglen
[i
] = strlen(argv
[i
]);
459 rplen
= strlen (replace_pat
);
460 while ((len
= (*read_args
) ()) != -1)
462 /* Don't do insert on the command name. */
463 push_arg (argv
[optind
], arglen
[optind
] + 1);
465 for (i
= optind
+ 1; i
< argc
; i
++)
466 do_insert (argv
[i
], arglen
[i
], len
);
471 wait_for_proc (true);
475 /* Read a line of arguments from stdin and add them to the list of
476 arguments to pass to the command. Ignore blank lines and initial blanks.
477 Single and double quotes and backslashes quote metacharacters and blanks
478 as they do in the shell.
479 Return -1 if eof (either physical or logical) is reached,
480 otherwise the length of the last string read (including the null). */
485 static boolean eof
= false;
486 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
487 int state
= SPACE
; /* The type of character we last read. */
488 int prevc
; /* The previous value of c. */
489 int quotc
= 0; /* The last quote character read. */
491 boolean first
= true; /* true if reading first arg on line. */
494 /* Including the NUL, the args must not grow past this point. */
495 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
505 /* COMPAT: SYSV seems to ignore stuff on a line that
506 ends without a \n; we don't. */
512 /* FIXME we don't check for unterminated quotes here. */
513 if (first
&& EOF_STR (linebuf
))
516 push_arg (linebuf
, len
);
530 if (!ISBLANK (prevc
))
531 lineno
++; /* For -l. */
540 if (EOF_STR (linebuf
))
543 return first
? -1 : len
;
546 push_arg (linebuf
, len
);
549 if (!replace_pat
&& ISSPACE (c
))
553 if (EOF_STR (linebuf
))
556 return first
? -1 : len
;
558 push_arg (linebuf
, len
);
580 error (1, 0, _("unmatched %s quote"),
581 quotc
== '"' ? _("double") : _("single"));
594 error (1, 0, _("argument line too long"));
599 /* Read a null-terminated string from stdin and add it to the list of
600 arguments to pass to the command.
601 Return -1 if eof (either physical or logical) is reached,
602 otherwise the length of the string read (including the null). */
607 static boolean eof
= false;
610 /* Including the NUL, the args must not grow past this point. */
611 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
617 int c
= getc (stdin
);
626 push_arg (linebuf
, len
);
631 lineno
++; /* For -l. */
635 push_arg (linebuf
, len
);
639 error (1, 0, _("argument line too long"));
644 /* Finds the first occurrence of the substring NEEDLE in the string
645 HAYSTACK. Both strings can be multibyte strings. */
648 mbstrstr (const char *haystack
, const char *needle
)
653 size_t hlen
= strlen (haystack
);
654 size_t nlen
= strlen (needle
);
658 memset (&mbstate
, 0, sizeof (mbstate_t));
661 if (memcmp (haystack
, needle
, nlen
) == 0)
662 return (char *) haystack
;
663 step
= mbrlen (haystack
, hlen
, &mbstate
);
672 return strstr (haystack
, needle
);
675 /* Replace all instances of `replace_pat' in ARG with `linebuf',
676 and add the resulting string to the list of arguments for the command
678 ARGLEN is the length of ARG, not including the null.
679 LBLEN is the length of `linebuf', not including the null.
681 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
682 and a max of 5 occurrences of replace_pat in the initial-arguments.
683 Those restrictions do not exist here. */
686 do_insert (char *arg
, size_t arglen
, size_t lblen
)
688 /* Temporary copy of each arg with the replace pattern replaced by the
690 static char *insertbuf
;
692 int bytes_left
= arg_max
- 1; /* Bytes left on the command line. */
695 insertbuf
= (char *) xmalloc (arg_max
+ 1);
700 size_t len
; /* Length in ARG before `replace_pat'. */
701 char *s
= mbstrstr (arg
, replace_pat
);
710 strncpy (p
, arg
, len
);
728 error (1, 0, _("command too long"));
730 push_arg (insertbuf
, p
- insertbuf
);
733 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
735 LEN is the length of ARG, including the terminating null.
736 If this brings the list up to its maximum size, execute the command. */
739 push_arg (char *arg
, size_t len
)
743 if (cmd_argv_chars
+ len
> arg_max
)
745 if (initial_args
|| cmd_argc
== initial_argc
)
746 error (1, 0, _("can not fit single argument within argument list size limit"));
748 || (exit_if_size_exceeded
&&
749 (lines_per_exec
|| args_per_exec
)))
750 error (1, 0, _("argument list too long"));
753 if (!initial_args
&& args_per_exec
&&
754 cmd_argc
- initial_argc
== args_per_exec
)
758 if (cmd_argc
>= cmd_argv_alloc
)
763 cmd_argv
= (char **) xmalloc (sizeof (char *) * cmd_argv_alloc
);
768 cmd_argv
= (char **) xrealloc (cmd_argv
,
769 sizeof (char *) * cmd_argv_alloc
);
774 cmd_argv
[cmd_argc
++] = NULL
;
777 cmd_argv
[cmd_argc
++] = argbuf
+ cmd_argv_chars
;
778 strcpy (argbuf
+ cmd_argv_chars
, arg
);
779 cmd_argv_chars
+= len
;
783 /* Print the arguments of the command to execute.
784 If ASK is nonzero, prompt the user for a response, and
785 if the user responds affirmatively, return true;
786 otherwise, return false. */
789 print_args (boolean ask
)
793 for (i
= 0; i
< cmd_argc
- 1; i
++)
794 fprintf (stderr
, "%s ", cmd_argv
[i
]);
797 static FILE *tty_stream
;
802 tty_stream
= fopen ("/dev/tty", "r");
804 error (1, errno
, "/dev/tty");
806 fputs ("?...", stderr
);
808 c
= savec
= getc (tty_stream
);
809 while (c
!= EOF
&& c
!= '\n')
810 c
= getc (tty_stream
);
811 if (savec
== 'y' || savec
== 'Y')
820 /* Execute the command that has been built in `cmd_argv'. This may involve
821 waiting for processes that were previously executed. */
828 push_arg ((char *) NULL
, 0); /* Null terminate the arg list. */
829 if (!query_before_executing
|| print_args (true))
831 if (proc_max
&& procs_executing
>= proc_max
)
832 wait_for_proc (false);
833 if (!query_before_executing
&& print_command
)
835 /* If we run out of processes, wait for a child to return and
837 while ((child
= fork ()) < 0 && errno
== EAGAIN
&& procs_executing
)
838 wait_for_proc (false);
842 error (1, errno
, _("cannot fork"));
845 execvp (cmd_argv
[0], cmd_argv
);
846 error (0, errno
, "%s", cmd_argv
[0]);
847 _exit (errno
== ENOENT
? 127 : 126);
852 cmd_argc
= initial_argc
;
853 cmd_argv_chars
= initial_argv_chars
;
856 /* Add the process with id PID to the list of processes that have
864 /* Find an empty slot. */
865 for (i
= 0; i
< pids_alloc
&& pids
[i
]; i
++)
871 pids_alloc
= proc_max
? proc_max
: 64;
872 pids
= (pid_t
*) xmalloc (sizeof (pid_t
) * pids_alloc
);
877 pids
= (pid_t
*) xrealloc (pids
,
878 sizeof (pid_t
) * pids_alloc
);
880 memset (&pids
[i
], '\0', sizeof (pid_t
) * (pids_alloc
- i
));
887 /* If ALL is true, wait for all child processes to finish;
888 otherwise, wait for one child process to finish.
889 Remove the processes that finish from the list of executing processes. */
892 wait_for_proc (boolean all
)
894 while (procs_executing
)
902 while ((pid
= wait (&status
)) == (pid_t
) -1)
904 error (1, errno
, _("error waiting for child process"));
906 /* Find the entry in `pids' for the child process
908 for (i
= 0; i
< pids_alloc
&& pid
!= pids
[i
]; i
++)
911 while (i
== pids_alloc
); /* A child died that we didn't start? */
913 /* Remove the child from the list. */
917 if (WEXITSTATUS (status
) == 126 || WEXITSTATUS (status
) == 127)
918 exit (WEXITSTATUS (status
)); /* Can't find or run the command. */
919 if (WEXITSTATUS (status
) == 255)
920 error (124, 0, _("%s: exited with status 255; aborting"), cmd_argv
[0]);
921 if (WIFSTOPPED (status
))
922 error (125, 0, _("%s: stopped by signal %d"), cmd_argv
[0], WSTOPSIG (status
));
923 if (WIFSIGNALED (status
))
924 error (125, 0, _("%s: terminated by signal %d"), cmd_argv
[0], WTERMSIG (status
));
925 if (WEXITSTATUS (status
) != 0)
933 /* Return the value of the number represented in STR.
934 OPTION is the command line option to which STR is the argument.
935 If the value does not fall within the boundaries MIN and MAX,
936 Print an error message mentioning OPTION and exit. */
939 parse_num (char *str
, int option
, long int min
, long int max
)
944 val
= strtol (str
, &eptr
, 10);
945 if (eptr
== str
|| *eptr
)
947 fprintf (stderr
, _("%s: invalid number for -%c option\n"),
948 program_name
, option
);
953 fprintf (stderr
, _("%s: value for -%c option must be >= %ld\n"),
954 program_name
, option
, min
);
957 else if (max
>= 0 && val
> max
)
959 fprintf (stderr
, _("%s: value for -%c option must be < %ld\n"),
960 program_name
, option
, max
);
966 /* Return how much of ARG_MAX is used by the environment. */
969 env_size (char **envp
)
974 len
+= strlen (*envp
++) + 1;
980 usage (FILE *stream
, int status
)
982 fprintf (stream
, _("\
983 Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
984 [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
985 [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
986 [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
987 [--max-args=max-args] [--no-run-if-empty] [--version] [--help]\n\
988 [command [initial-arguments]]\n"),
990 fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream
);