1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 91, 92, 93, 94, 2000 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Mike Rendell <michael@cs.mun.ca>
19 and David MacKenzie <djm@gnu.ai.mit.edu>. */
24 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
25 # define PARAMS(Args) Args
27 # define PARAMS(Args) ()
34 #if !defined (isascii) || defined (STDC_HEADERS)
42 #define ISBLANK(c) (isascii (c) && isblank (c))
44 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
47 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
48 || (c) == '\f' || (c) == '\v')
50 #include <sys/types.h>
55 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
57 #if !defined(STDC_HEADERS)
62 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
66 #include <sys/param.h>
74 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
83 #if !defined(SIGCHLD) && defined(SIGCLD)
84 #define SIGCHLD SIGCLD
87 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
88 We try to make it as large as possible. */
89 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
90 #define ARG_MAX sysconf (_SC_ARG_MAX)
93 #define ARG_MAX NCARGS
98 /* States for read_line. */
114 # include <libintl.h>
115 # define _(Text) gettext (Text)
117 # define _(Text) Text
118 #define textdomain(Domain)
119 #define bindtextdomain(Package, Directory)
122 # define N_(String) gettext_noop (String)
124 # define N_(String) (String)
127 /* Return nonzero if S is the EOF string. */
128 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
130 extern char **environ
;
132 /* Not char because of type promotion; NeXT gcc can't handle it. */
144 void error
PARAMS ((int status
, int errnum
, char *message
,...));
146 extern char *version_string
;
148 /* The name this program was run with. */
151 /* Buffer for reading arguments from stdin. */
152 static char *linebuf
;
154 /* Line number in stdin since the last command was executed. */
155 static int lineno
= 0;
157 /* If nonzero, then instead of putting the args from stdin at
158 the end of the command argument list, they are each stuck into the
159 initial args, replacing each occurrence of the `replace_pat' in the
161 static char *replace_pat
= NULL
;
163 /* The length of `replace_pat'. */
164 static size_t rplen
= 0;
166 /* If nonzero, when this string is read on stdin it is treated as
168 I don't like this - it should default to NULL. */
169 static char *eof_str
= "_";
171 /* If nonzero, the maximum number of nonblank lines from stdin to use
173 static long lines_per_exec
= 0;
175 /* The maximum number of arguments to use per command line. */
176 static long args_per_exec
= 1024;
178 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
179 static boolean exit_if_size_exceeded
= false;
181 /* The maximum number of characters that can be used per command line. */
184 /* Storage for elements of `cmd_argv'. */
187 /* The list of args being built. */
188 static char **cmd_argv
= NULL
;
190 /* Number of elements allocated for `cmd_argv'. */
191 static int cmd_argv_alloc
= 0;
193 /* Number of valid elements in `cmd_argv'. */
194 static int cmd_argc
= 0;
196 /* Number of chars being used in `cmd_argv'. */
197 static int cmd_argv_chars
= 0;
199 /* Number of initial arguments given on the command line. */
200 static int initial_argc
= 0;
202 /* Number of chars in the initial args. */
203 static int initial_argv_chars
= 0;
205 /* true when building up initial arguments in `cmd_argv'. */
206 static boolean initial_args
= true;
208 /* If nonzero, the maximum number of child processes that can be running
210 static int proc_max
= 1;
212 /* Total number of child processes that have been executed. */
213 static int procs_executed
= 0;
215 /* The number of elements in `pids'. */
216 static int procs_executing
= 0;
218 /* List of child processes currently executing. */
219 static pid_t
*pids
= NULL
;
221 /* The number of allocated elements in `pids'. */
222 static int pids_alloc
= 0;
224 /* Exit status; nonzero if any child process exited with a
226 static int child_error
= 0;
228 /* If true, print each command on stderr before executing it. */
229 static boolean print_command
= false;
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 static struct option
const longopts
[] =
237 {"null", no_argument
, NULL
, '0'},
238 {"eof", optional_argument
, NULL
, 'e'},
239 {"replace", optional_argument
, NULL
, 'i'},
240 {"max-lines", optional_argument
, NULL
, 'l'},
241 {"max-args", required_argument
, NULL
, 'n'},
242 {"interactive", no_argument
, NULL
, 'p'},
243 {"no-run-if-empty", no_argument
, NULL
, 'r'},
244 {"max-chars", required_argument
, NULL
, 's'},
245 {"verbose", no_argument
, NULL
, 't'},
246 {"exit", no_argument
, NULL
, 'x'},
247 {"max-procs", required_argument
, NULL
, 'P'},
248 {"version", no_argument
, NULL
, 'v'},
249 {"help", no_argument
, NULL
, 'h'},
250 {NULL
, no_argument
, NULL
, 0}
253 static int read_line
PARAMS ((void));
254 static int read_string
PARAMS ((void));
255 static void do_insert
PARAMS ((char *arg
, size_t arglen
, size_t lblen
));
256 static void push_arg
PARAMS ((char *arg
, size_t len
));
257 static boolean print_args
PARAMS ((boolean ask
));
258 static void do_exec
PARAMS ((void));
259 static void add_proc
PARAMS ((pid_t pid
));
260 static void wait_for_proc
PARAMS ((boolean all
));
261 static long parse_num
PARAMS ((char *str
, int option
, long min
, long max
));
262 static long env_size
PARAMS ((char **envp
));
263 static void usage
PARAMS ((FILE * stream
, int status
));
266 main (int argc
, char **argv
)
269 int always_run_command
= 1;
271 char *default_cmd
= "/bin/echo";
272 int (*read_args
) PARAMS ((void)) = read_line
;
274 program_name
= argv
[0];
276 #ifdef HAVE_SETLOCALE
277 setlocale (LC_ALL
, "");
279 bindtextdomain (PACKAGE
, LOCALEDIR
);
280 textdomain (PACKAGE
);
282 orig_arg_max
= ARG_MAX
;
283 if (orig_arg_max
== -1)
284 orig_arg_max
= LONG_MAX
;
285 orig_arg_max
-= 2048; /* POSIX.2 requires subtracting 2048. */
286 arg_max
= orig_arg_max
;
288 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
289 have it at 1 meg). Things will work fine with a large ARG_MAX but it
290 will probably hurt the system more than it needs to; an array of this
291 size is allocated. */
292 if (arg_max
> 20 * 1024)
295 /* Take the size of the environment into account. */
296 arg_max
-= env_size (environ
);
298 error (1, 0, _("environment is too large for exec"));
300 while ((optc
= getopt_long (argc
, argv
, "+0e::i::l::n:prs:txP:",
301 longopts
, (int *) 0)) != -1)
306 read_args
= read_string
;
321 replace_pat
= optarg
;
324 /* -i excludes -n -l. */
331 lines_per_exec
= parse_num (optarg
, 'l', 1L, -1L);
334 /* -l excludes -i -n. */
340 args_per_exec
= parse_num (optarg
, 'n', 1L, -1L);
341 /* -n excludes -i -l. */
347 arg_max
= parse_num (optarg
, 's', 1L, orig_arg_max
);
351 print_command
= true;
355 exit_if_size_exceeded
= true;
359 query_before_executing
= true;
360 print_command
= true;
364 always_run_command
= 0;
368 proc_max
= parse_num (optarg
, 'P', 0L, -1L);
372 printf (_("GNU xargs version %s\n"), version_string
);
380 if (replace_pat
|| lines_per_exec
)
381 exit_if_size_exceeded
= true;
390 linebuf
= (char *) xmalloc (arg_max
+ 1);
391 argbuf
= (char *) xmalloc (arg_max
+ 1);
393 /* Make sure to listen for the kids. */
394 signal (SIGCHLD
, SIG_DFL
);
398 for (; optind
< argc
; optind
++)
399 push_arg (argv
[optind
], strlen (argv
[optind
]) + 1);
400 initial_args
= false;
401 initial_argc
= cmd_argc
;
402 initial_argv_chars
= cmd_argv_chars
;
404 while ((*read_args
) () != -1)
405 if (lines_per_exec
&& lineno
>= lines_per_exec
)
411 /* SYSV xargs seems to do at least one exec, even if the
413 if (cmd_argc
!= initial_argc
414 || (always_run_command
&& procs_executed
== 0))
421 size_t *arglen
= (size_t *) xmalloc (sizeof (size_t) * argc
);
423 for (i
= optind
; i
< argc
; i
++)
424 arglen
[i
] = strlen(argv
[i
]);
425 rplen
= strlen (replace_pat
);
426 while ((len
= (*read_args
) ()) != -1)
428 /* Don't do insert on the command name. */
429 push_arg (argv
[optind
], arglen
[optind
] + 1);
431 for (i
= optind
+ 1; i
< argc
; i
++)
432 do_insert (argv
[i
], arglen
[i
], len
);
437 wait_for_proc (true);
441 /* Read a line of arguments from stdin and add them to the list of
442 arguments to pass to the command. Ignore blank lines and initial blanks.
443 Single and double quotes and backslashes quote metacharacters and blanks
444 as they do in the shell.
445 Return -1 if eof (either physical or logical) is reached,
446 otherwise the length of the last string read (including the null). */
451 static boolean eof
= false;
452 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
453 int state
= SPACE
; /* The type of character we last read. */
454 int prevc
; /* The previous value of c. */
455 int quotc
= 0; /* The last quote character read. */
457 boolean first
= true; /* true if reading first arg on line. */
460 /* Including the NUL, the args must not grow past this point. */
461 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
471 /* COMPAT: SYSV seems to ignore stuff on a line that
472 ends without a \n; we don't. */
478 /* FIXME we don't check for unterminated quotes here. */
479 if (first
&& EOF_STR (linebuf
))
482 push_arg (linebuf
, len
);
496 if (!ISBLANK (prevc
))
497 lineno
++; /* For -l. */
506 if (EOF_STR (linebuf
))
509 return first
? -1 : len
;
512 push_arg (linebuf
, len
);
515 if (!replace_pat
&& ISSPACE (c
))
519 if (EOF_STR (linebuf
))
522 return first
? -1 : len
;
524 push_arg (linebuf
, len
);
546 error (1, 0, _("unmatched %s quote"),
547 quotc
== '"' ? _("double") : _("single"));
560 error (1, 0, _("argument line too long"));
565 /* Read a null-terminated string from stdin and add it to the list of
566 arguments to pass to the command.
567 Return -1 if eof (either physical or logical) is reached,
568 otherwise the length of the string read (including the null). */
573 static boolean eof
= false;
576 /* Including the NUL, the args must not grow past this point. */
577 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
583 int c
= getc (stdin
);
592 push_arg (linebuf
, len
);
597 lineno
++; /* For -l. */
601 push_arg (linebuf
, len
);
605 error (1, 0, _("argument line too long"));
610 /* Replace all instances of `replace_pat' in ARG with `linebuf',
611 and add the resulting string to the list of arguments for the command
613 ARGLEN is the length of ARG, not including the null.
614 LBLEN is the length of `linebuf', not including the null.
616 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
617 and a max of 5 occurrences of replace_pat in the initial-arguments.
618 Those restrictions do not exist here. */
621 do_insert (char *arg
, size_t arglen
, size_t lblen
)
623 /* Temporary copy of each arg with the replace pattern replaced by the
625 static char *insertbuf
;
627 int bytes_left
= arg_max
- 1; /* Bytes left on the command line. */
630 insertbuf
= (char *) xmalloc (arg_max
+ 1);
635 size_t len
; /* Length in ARG before `replace_pat'. */
636 char *s
= strstr (arg
, replace_pat
);
645 strncpy (p
, arg
, len
);
663 error (1, 0, _("command too long"));
665 push_arg (insertbuf
, p
- insertbuf
);
668 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
670 LEN is the length of ARG, including the terminating null.
671 If this brings the list up to its maximum size, execute the command. */
674 push_arg (char *arg
, size_t len
)
678 if (cmd_argv_chars
+ len
> arg_max
)
680 if (initial_args
|| cmd_argc
== initial_argc
)
681 error (1, 0, _("can not fit single argument within argument list size limit"));
683 || (exit_if_size_exceeded
&&
684 (lines_per_exec
|| args_per_exec
)))
685 error (1, 0, _("argument list too long"));
688 if (!initial_args
&& args_per_exec
&&
689 cmd_argc
- initial_argc
== args_per_exec
)
693 if (cmd_argc
>= cmd_argv_alloc
)
698 cmd_argv
= (char **) xmalloc (sizeof (char *) * cmd_argv_alloc
);
703 cmd_argv
= (char **) xrealloc (cmd_argv
,
704 sizeof (char *) * cmd_argv_alloc
);
709 cmd_argv
[cmd_argc
++] = NULL
;
712 cmd_argv
[cmd_argc
++] = argbuf
+ cmd_argv_chars
;
713 strcpy (argbuf
+ cmd_argv_chars
, arg
);
714 cmd_argv_chars
+= len
;
718 /* Print the arguments of the command to execute.
719 If ASK is nonzero, prompt the user for a response, and
720 if the user responds affirmatively, return true;
721 otherwise, return false. */
724 print_args (boolean ask
)
728 for (i
= 0; i
< cmd_argc
- 1; i
++)
729 fprintf (stderr
, "%s ", cmd_argv
[i
]);
732 static FILE *tty_stream
;
737 tty_stream
= fopen ("/dev/tty", "r");
739 error (1, errno
, "/dev/tty");
741 fputs ("?...", stderr
);
743 c
= savec
= getc (tty_stream
);
744 while (c
!= EOF
&& c
!= '\n')
745 c
= getc (tty_stream
);
746 if (savec
== 'y' || savec
== 'Y')
755 /* Execute the command that has been built in `cmd_argv'. This may involve
756 waiting for processes that were previously executed. */
763 push_arg ((char *) NULL
, 0); /* Null terminate the arg list. */
764 if (!query_before_executing
|| print_args (true))
766 if (proc_max
&& procs_executing
>= proc_max
)
767 wait_for_proc (false);
768 if (!query_before_executing
&& print_command
)
770 /* If we run out of processes, wait for a child to return and
772 while ((child
= fork ()) < 0 && errno
== EAGAIN
&& procs_executing
)
773 wait_for_proc (false);
777 error (1, errno
, _("cannot fork"));
780 execvp (cmd_argv
[0], cmd_argv
);
781 error (0, errno
, "%s", cmd_argv
[0]);
782 _exit (errno
== ENOENT
? 127 : 126);
787 cmd_argc
= initial_argc
;
788 cmd_argv_chars
= initial_argv_chars
;
791 /* Add the process with id PID to the list of processes that have
799 /* Find an empty slot. */
800 for (i
= 0; i
< pids_alloc
&& pids
[i
]; i
++)
806 pids_alloc
= proc_max
? proc_max
: 64;
807 pids
= (pid_t
*) xmalloc (sizeof (pid_t
) * pids_alloc
);
812 pids
= (pid_t
*) xrealloc (pids
,
813 sizeof (pid_t
) * pids_alloc
);
815 memset (&pids
[i
], '\0', sizeof (pid_t
) * (pids_alloc
- i
));
822 /* If ALL is true, wait for all child processes to finish;
823 otherwise, wait for one child process to finish.
824 Remove the processes that finish from the list of executing processes. */
827 wait_for_proc (boolean all
)
829 while (procs_executing
)
837 while ((pid
= wait (&status
)) == (pid_t
) -1)
839 error (1, errno
, _("error waiting for child process"));
841 /* Find the entry in `pids' for the child process
843 for (i
= 0; i
< pids_alloc
&& pid
!= pids
[i
]; i
++)
846 while (i
== pids_alloc
); /* A child died that we didn't start? */
848 /* Remove the child from the list. */
852 if (WEXITSTATUS (status
) == 126 || WEXITSTATUS (status
) == 127)
853 exit (WEXITSTATUS (status
)); /* Can't find or run the command. */
854 if (WEXITSTATUS (status
) == 255)
855 error (124, 0, _("%s: exited with status 255; aborting"), cmd_argv
[0]);
856 if (WIFSTOPPED (status
))
857 error (125, 0, _("%s: stopped by signal %d"), cmd_argv
[0], WSTOPSIG (status
));
858 if (WIFSIGNALED (status
))
859 error (125, 0, _("%s: terminated by signal %d"), cmd_argv
[0], WTERMSIG (status
));
860 if (WEXITSTATUS (status
) != 0)
868 /* Return the value of the number represented in STR.
869 OPTION is the command line option to which STR is the argument.
870 If the value does not fall within the boundaries MIN and MAX,
871 Print an error message mentioning OPTION and exit. */
874 parse_num (char *str
, int option
, long int min
, long int max
)
879 val
= strtol (str
, &eptr
, 10);
880 if (eptr
== str
|| *eptr
)
882 fprintf (stderr
, _("%s: invalid number for -%c option\n"),
883 program_name
, option
);
888 fprintf (stderr
, _("%s: value for -%c option must be >= %ld\n"),
889 program_name
, option
, min
);
892 else if (max
>= 0 && val
> max
)
894 fprintf (stderr
, _("%s: value for -%c option must be < %ld\n"),
895 program_name
, option
, max
);
901 /* Return how much of ARG_MAX is used by the environment. */
904 env_size (char **envp
)
909 len
+= strlen (*envp
++) + 1;
915 usage (FILE *stream
, int status
)
917 fprintf (stream
, _("\
918 Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
919 [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
920 [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
921 [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
922 [--max-args=max-args] [--no-run-if-empty] [--version] [--help]\n\
923 [command [initial-arguments]]\n"),
925 fputs (_("\nReport bugs to <bug-findutils@gnu.org>."), stream
);