1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 91, 92, 93, 94 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>. */
32 #if !defined (isascii) || defined (STDC_HEADERS)
40 #define ISBLANK(c) (isascii (c) && isblank (c))
42 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
45 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
46 || (c) == '\f' || (c) == '\v')
48 #include <sys/types.h>
53 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
55 #if !defined(STDC_HEADERS)
60 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
64 #include <sys/param.h>
77 #if !defined(SIGCHLD) && defined(SIGCLD)
78 #define SIGCHLD SIGCLD
81 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
82 We try to make it as large as possible. */
83 #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
84 #define ARG_MAX sysconf (_SC_ARG_MAX)
87 #define ARG_MAX NCARGS
92 /* States for read_line. */
109 /* Return nonzero if S is the EOF string. */
110 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
112 extern char **environ
;
114 /* Not char because of type promotion; NeXT gcc can't handle it. */
125 VOID
*xmalloc
P_ ((size_t n
));
126 VOID
*xrealloc
P_ ((VOID
* p
, size_t n
));
127 void error
P_ ((int status
, int errnum
, char *message
,...));
129 extern char *version_string
;
131 /* The name this program was run with. */
134 /* Buffer for reading arguments from stdin. */
135 static char *linebuf
;
137 /* Line number in stdin since the last command was executed. */
138 static int lineno
= 0;
140 /* If nonzero, then instead of putting the args from stdin at
141 the end of the command argument list, they are each stuck into the
142 initial args, replacing each occurrence of the `replace_pat' in the
144 static char *replace_pat
= NULL
;
146 /* The length of `replace_pat'. */
147 static size_t rplen
= 0;
149 /* If nonzero, when this string is read on stdin it is treated as
151 I don't like this - it should default to NULL. */
152 static char *eof_str
= "_";
154 /* If nonzero, the maximum number of nonblank lines from stdin to use
156 static long lines_per_exec
= 0;
158 /* The maximum number of arguments to use per command line. */
159 static long args_per_exec
= 1024;
161 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
162 static boolean exit_if_size_exceeded
= false;
164 /* The maximum number of characters that can be used per command line. */
167 /* Storage for elements of `cmd_argv'. */
170 /* The list of args being built. */
171 static char **cmd_argv
= NULL
;
173 /* Number of elements allocated for `cmd_argv'. */
174 static int cmd_argv_alloc
= 0;
176 /* Number of valid elements in `cmd_argv'. */
177 static int cmd_argc
= 0;
179 /* Number of chars being used in `cmd_argv'. */
180 static int cmd_argv_chars
= 0;
182 /* Number of initial arguments given on the command line. */
183 static int initial_argc
= 0;
185 /* Number of chars in the initial args. */
186 static int initial_argv_chars
= 0;
188 /* true when building up initial arguments in `cmd_argv'. */
189 static boolean initial_args
= true;
191 /* If nonzero, the maximum number of child processes that can be running
193 static int proc_max
= 1;
195 /* Total number of child processes that have been executed. */
196 static int procs_executed
= 0;
198 /* The number of elements in `pids'. */
199 static int procs_executing
= 0;
201 /* List of child processes currently executing. */
202 static pid_t
*pids
= NULL
;
204 /* The number of allocated elements in `pids'. */
205 static int pids_alloc
= 0;
207 /* Exit status; nonzero if any child process exited with a
209 static int child_error
= 0;
211 /* If true, print each command on stderr before executing it. */
212 static boolean print_command
= false;
214 /* If true, query the user before executing each command, and only
215 execute the command if the user responds affirmatively. */
216 static boolean query_before_executing
= false;
218 static struct option
const longopts
[] =
220 {"null", no_argument
, NULL
, '0'},
221 {"eof", optional_argument
, NULL
, 'e'},
222 {"replace", optional_argument
, NULL
, 'i'},
223 {"max-lines", optional_argument
, NULL
, 'l'},
224 {"max-args", required_argument
, NULL
, 'n'},
225 {"interactive", no_argument
, NULL
, 'p'},
226 {"no-run-if-empty", no_argument
, NULL
, 'r'},
227 {"max-chars", required_argument
, NULL
, 's'},
228 {"verbose", no_argument
, NULL
, 't'},
229 {"exit", no_argument
, NULL
, 'x'},
230 {"max-procs", required_argument
, NULL
, 'P'},
231 {"version", no_argument
, NULL
, 'v'},
232 {"help", no_argument
, NULL
, 'h'},
233 {NULL
, no_argument
, NULL
, 0}
236 static int read_line
P_ ((void));
237 static int read_string
P_ ((void));
238 static void do_insert
P_ ((char *arg
, size_t arglen
, size_t lblen
));
239 static void push_arg
P_ ((char *arg
, size_t len
));
240 static boolean print_args
P_ ((boolean ask
));
241 static void do_exec
P_ ((void));
242 static void add_proc
P_ ((pid_t pid
));
243 static void wait_for_proc
P_ ((boolean all
));
244 static long parse_num
P_ ((char *str
, int option
, long min
, long max
));
245 static long env_size
P_ ((char **envp
));
246 static void usage
P_ ((FILE * stream
, int status
));
254 int always_run_command
= 1;
256 char *default_cmd
= "/bin/echo";
257 int (*read_args
) P_ ((void)) = read_line
;
259 program_name
= argv
[0];
261 orig_arg_max
= ARG_MAX
- 2048; /* POSIX.2 requires subtracting 2048. */
262 arg_max
= orig_arg_max
;
264 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
265 have it at 1 meg). Things will work fine with a large ARG_MAX but it
266 will probably hurt the system more than it needs to; an array of this
267 size is allocated. */
268 if (arg_max
> 20 * 1024)
271 /* Take the size of the environment into account. */
272 arg_max
-= env_size (environ
);
274 error (1, 0, "environment is too large for exec");
276 while ((optc
= getopt_long (argc
, argv
, "+0e::i::l::n:prs:txP:",
277 longopts
, (int *) 0)) != -1)
282 read_args
= read_string
;
297 replace_pat
= optarg
;
300 /* -i excludes -n -l. */
307 lines_per_exec
= parse_num (optarg
, 'l', 1L, -1L);
310 /* -l excludes -i -n. */
316 args_per_exec
= parse_num (optarg
, 'n', 1L, -1L);
317 /* -n excludes -i -l. */
323 arg_max
= parse_num (optarg
, 's', 1L, orig_arg_max
);
327 print_command
= true;
331 exit_if_size_exceeded
= true;
335 query_before_executing
= true;
336 print_command
= true;
340 always_run_command
= 0;
344 proc_max
= parse_num (optarg
, 'P', 0L, -1L);
348 printf ("GNU xargs version %s\n", version_string
);
356 if (replace_pat
|| lines_per_exec
)
357 exit_if_size_exceeded
= true;
366 linebuf
= (char *) xmalloc (arg_max
+ 1);
367 argbuf
= (char *) xmalloc (arg_max
+ 1);
369 /* Make sure to listen for the kids. */
370 signal (SIGCHLD
, SIG_DFL
);
374 for (; optind
< argc
; optind
++)
375 push_arg (argv
[optind
], strlen (argv
[optind
]) + 1);
376 initial_args
= false;
377 initial_argc
= cmd_argc
;
378 initial_argv_chars
= cmd_argv_chars
;
380 while ((*read_args
) () != -1)
381 if (lines_per_exec
&& lineno
>= lines_per_exec
)
387 /* SYSV xargs seems to do at least one exec, even if the
389 if (cmd_argc
!= initial_argc
390 || (always_run_command
&& procs_executed
== 0))
397 size_t *arglen
= (size_t *) xmalloc (sizeof (size_t) * argc
);
399 for (i
= optind
; i
< argc
; i
++)
400 arglen
[i
] = strlen(argv
[i
]);
401 rplen
= strlen (replace_pat
);
402 while ((len
= (*read_args
) ()) != -1)
404 /* Don't do insert on the command name. */
405 push_arg (argv
[optind
], arglen
[optind
] + 1);
407 for (i
= optind
+ 1; i
< argc
; i
++)
408 do_insert (argv
[i
], arglen
[i
], len
);
413 wait_for_proc (true);
417 /* Read a line of arguments from stdin and add them to the list of
418 arguments to pass to the command. Ignore blank lines and initial blanks.
419 Single and double quotes and backslashes quote metacharacters and blanks
420 as they do in the shell.
421 Return -1 if eof (either physical or logical) is reached,
422 otherwise the length of the last string read (including the null). */
427 static boolean eof
= false;
428 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
429 int state
= SPACE
; /* The type of character we last read. */
430 int prevc
; /* The previous value of c. */
431 int quotc
= 0; /* The last quote character read. */
433 boolean first
= true; /* true if reading first arg on line. */
436 /* Including the NUL, the args must not grow past this point. */
437 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
447 /* COMPAT: SYSV seems to ignore stuff on a line that
448 ends without a \n; we don't. */
454 /* FIXME we don't check for unterminated quotes here. */
455 if (first
&& EOF_STR (linebuf
))
458 push_arg (linebuf
, len
);
472 if (!ISBLANK (prevc
))
473 lineno
++; /* For -l. */
482 if (EOF_STR (linebuf
))
485 return first
? -1 : len
;
488 push_arg (linebuf
, len
);
491 if (!replace_pat
&& ISSPACE (c
))
495 if (EOF_STR (linebuf
))
498 return first
? -1 : len
;
500 push_arg (linebuf
, len
);
522 error (1, 0, "unmatched %s quote",
523 quotc
== '"' ? "double" : "single");
536 error (1, 0, "argument line too long");
541 /* Read a null-terminated string from stdin and add it to the list of
542 arguments to pass to the command.
543 Return -1 if eof (either physical or logical) is reached,
544 otherwise the length of the string read (including the null). */
549 static boolean eof
= false;
552 /* Including the NUL, the args must not grow past this point. */
553 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
559 int c
= getc (stdin
);
568 push_arg (linebuf
, len
);
573 lineno
++; /* For -l. */
577 push_arg (linebuf
, len
);
581 error (1, 0, "argument line too long");
586 /* Replace all instances of `replace_pat' in ARG with `linebuf',
587 and add the resulting string to the list of arguments for the command
589 ARGLEN is the length of ARG, not including the null.
590 LBLEN is the length of `linebuf', not including the null.
592 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
593 and a max of 5 occurences of replace_pat in the initial-arguments.
594 Those restrictions do not exist here. */
597 do_insert (arg
, arglen
, lblen
)
602 /* Temporary copy of each arg with the replace pattern replaced by the
604 static char *insertbuf
;
606 int bytes_left
= arg_max
- 1; /* Bytes left on the command line. */
609 insertbuf
= (char *) xmalloc (arg_max
+ 1);
614 size_t len
; /* Length in ARG before `replace_pat'. */
615 char *s
= strstr (arg
, replace_pat
);
624 strncpy (p
, arg
, len
);
642 error (1, 0, "command too long");
644 push_arg (insertbuf
, p
- insertbuf
);
647 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
649 LEN is the length of ARG, including the terminating null.
650 If this brings the list up to its maximum size, execute the command. */
659 if (cmd_argv_chars
+ len
> arg_max
)
661 if (initial_args
|| cmd_argc
== initial_argc
)
662 error (1, 0, "can not fit single argument within argument list size limit");
664 || (exit_if_size_exceeded
&&
665 (lines_per_exec
|| args_per_exec
)))
666 error (1, 0, "argument list too long");
669 if (!initial_args
&& args_per_exec
&&
670 cmd_argc
- initial_argc
== args_per_exec
)
674 if (cmd_argc
>= cmd_argv_alloc
)
679 cmd_argv
= (char **) xmalloc (sizeof (char *) * cmd_argv_alloc
);
684 cmd_argv
= (char **) xrealloc (cmd_argv
,
685 sizeof (char *) * cmd_argv_alloc
);
690 cmd_argv
[cmd_argc
++] = NULL
;
693 cmd_argv
[cmd_argc
++] = argbuf
+ cmd_argv_chars
;
694 strcpy (argbuf
+ cmd_argv_chars
, arg
);
695 cmd_argv_chars
+= len
;
699 /* Print the arguments of the command to execute.
700 If ASK is nonzero, prompt the user for a response, and
701 if the user responds affirmatively, return true;
702 otherwise, return false. */
710 for (i
= 0; i
< cmd_argc
- 1; i
++)
711 fprintf (stderr
, "%s ", cmd_argv
[i
]);
714 static FILE *tty_stream
;
719 tty_stream
= fopen ("/dev/tty", "r");
721 error (1, errno
, "/dev/tty");
723 fputs ("?...", stderr
);
725 c
= savec
= getc (tty_stream
);
726 while (c
!= EOF
&& c
!= '\n')
727 c
= getc (tty_stream
);
728 if (savec
== 'y' || savec
== 'Y')
737 /* Execute the command that has been built in `cmd_argv'. This may involve
738 waiting for processes that were previously executed. */
745 push_arg ((char *) NULL
, 0); /* Null terminate the arg list. */
746 if (!query_before_executing
|| print_args (true))
748 if (proc_max
&& procs_executing
>= proc_max
)
749 wait_for_proc (false);
750 if (!query_before_executing
&& print_command
)
752 /* If we run out of processes, wait for a child to return and
754 while ((child
= fork ()) < 0 && errno
== EAGAIN
&& procs_executing
)
755 wait_for_proc (false);
759 error (1, errno
, "cannot fork");
762 execvp (cmd_argv
[0], cmd_argv
);
763 error (0, errno
, "%s", cmd_argv
[0]);
764 _exit (errno
== ENOENT
? 127 : 126);
769 cmd_argc
= initial_argc
;
770 cmd_argv_chars
= initial_argv_chars
;
773 /* Add the process with id PID to the list of processes that have
782 /* Find an empty slot. */
783 for (i
= 0; i
< pids_alloc
&& pids
[i
]; i
++)
789 pids_alloc
= proc_max
? proc_max
: 64;
790 pids
= (pid_t
*) xmalloc (sizeof (pid_t
) * pids_alloc
);
795 pids
= (pid_t
*) xrealloc (pids
,
796 sizeof (pid_t
) * pids_alloc
);
798 memset (&pids
[i
], '\0', sizeof (pid_t
) * (pids_alloc
- i
));
805 /* If ALL is true, wait for all child processes to finish;
806 otherwise, wait for one child process to finish.
807 Remove the processes that finish from the list of executing processes. */
813 while (procs_executing
)
821 pid
= wait (&status
);
823 error (1, errno
, "error waiting for child process");
825 /* Find the entry in `pids' for the child process
827 for (i
= 0; i
< pids_alloc
&& pid
!= pids
[i
]; i
++)
830 while (i
== pids_alloc
); /* A child died that we didn't start? */
832 /* Remove the child from the list. */
836 if (WEXITSTATUS (status
) == 126 || WEXITSTATUS (status
) == 127)
837 exit (WEXITSTATUS (status
)); /* Can't find or run the command. */
838 if (WEXITSTATUS (status
) == 255)
839 error (124, 0, "%s: exited with status 255; aborting", cmd_argv
[0]);
840 if (WIFSTOPPED (status
))
841 error (125, 0, "%s: stopped by signal %d", cmd_argv
[0], WSTOPSIG (status
));
842 if (WIFSIGNALED (status
))
843 error (125, 0, "%s: terminated by signal %d", cmd_argv
[0], WTERMSIG (status
));
844 if (WEXITSTATUS (status
) != 0)
852 /* Return the value of the number represented in STR.
853 OPTION is the command line option to which STR is the argument.
854 If the value does not fall within the boundaries MIN and MAX,
855 Print an error message mentioning OPTION and exit. */
858 parse_num (str
, option
, min
, max
)
867 val
= strtol (str
, &eptr
, 10);
868 if (eptr
== str
|| *eptr
)
870 fprintf (stderr
, "%s: invalid number for -%c option\n",
871 program_name
, option
);
876 fprintf (stderr
, "%s: value for -%c option must be >= %ld\n",
877 program_name
, option
, min
);
880 else if (max
>= 0 && val
> max
)
882 fprintf (stderr
, "%s: value for -%c option must be < %ld\n",
883 program_name
, option
, max
);
889 /* Return how much of ARG_MAX is used by the environment. */
898 len
+= strlen (*envp
++) + 1;
904 usage (stream
, status
)
909 Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
910 [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
911 [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
912 [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
913 [--max-args=max-args] [--no-run-if-empty] [--version] [--help]\n\
914 [command [initial-arguments]]\n",