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. */
104 /* Return nonzero if S is the EOF string. */
105 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
107 extern char **environ
;
109 /* Not char because of type promotion; NeXT gcc can't handle it. */
120 VOID
*xmalloc
P_ ((size_t n
));
121 VOID
*xrealloc
P_ ((VOID
* p
, size_t n
));
122 void error
P_ ((int status
, int errnum
, char *message
,...));
124 extern char *version_string
;
126 /* The name this program was run with. */
129 /* Buffer for reading arguments from stdin. */
130 static char *linebuf
;
132 /* Line number in stdin since the last command was executed. */
133 static int lineno
= 0;
135 /* If nonzero, then instead of putting the args from stdin at
136 the end of the command argument list, they are each stuck into the
137 initial args, replacing each occurrence of the `replace_pat' in the
139 static char *replace_pat
= NULL
;
141 /* The length of `replace_pat'. */
142 static size_t rplen
= 0;
144 /* If nonzero, when this string is read on stdin it is treated as
146 I don't like this - it should default to NULL. */
147 static char *eof_str
= "_";
149 /* If nonzero, the maximum number of nonblank lines from stdin to use
151 static long lines_per_exec
= 0;
153 /* The maximum number of arguments to use per command line. */
154 static long args_per_exec
= 1024;
156 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */
157 static boolean exit_if_size_exceeded
= false;
159 /* The maximum number of characters that can be used per command line. */
162 /* Storage for elements of `cmd_argv'. */
165 /* The list of args being built. */
166 static char **cmd_argv
= NULL
;
168 /* Number of elements allocated for `cmd_argv'. */
169 static int cmd_argv_alloc
= 0;
171 /* Number of valid elements in `cmd_argv'. */
172 static int cmd_argc
= 0;
174 /* Number of chars being used in `cmd_argv'. */
175 static int cmd_argv_chars
= 0;
177 /* Number of initial arguments given on the command line. */
178 static int initial_argc
= 0;
180 /* Number of chars in the initial args. */
181 static int initial_argv_chars
= 0;
183 /* true when building up initial arguments in `cmd_argv'. */
184 static boolean initial_args
= true;
186 /* If nonzero, the maximum number of child processes that can be running
188 static int proc_max
= 1;
190 /* Total number of child processes that have been executed. */
191 static int procs_executed
= 0;
193 /* The number of elements in `pids'. */
194 static int procs_executing
= 0;
196 /* List of child processes currently executing. */
197 static pid_t
*pids
= NULL
;
199 /* The number of allocated elements in `pids'. */
200 static int pids_alloc
= 0;
202 /* Exit status; nonzero if any child process exited with a
204 static int child_error
= 0;
206 /* If true, print each command on stderr before executing it. */
207 static boolean print_command
= false;
209 /* If true, query the user before executing each command, and only
210 execute the command if the user responds affirmatively. */
211 static boolean query_before_executing
= false;
213 static struct option
const longopts
[] =
215 {"null", no_argument
, NULL
, '0'},
216 {"eof", optional_argument
, NULL
, 'e'},
217 {"replace", optional_argument
, NULL
, 'i'},
218 {"max-lines", optional_argument
, NULL
, 'l'},
219 {"max-args", required_argument
, NULL
, 'n'},
220 {"interactive", no_argument
, NULL
, 'p'},
221 {"no-run-if-empty", no_argument
, NULL
, 'r'},
222 {"max-chars", required_argument
, NULL
, 's'},
223 {"verbose", no_argument
, NULL
, 't'},
224 {"exit", no_argument
, NULL
, 'x'},
225 {"max-procs", required_argument
, NULL
, 'P'},
226 {"version", no_argument
, NULL
, 'v'},
227 {"help", no_argument
, NULL
, 'h'},
228 {NULL
, no_argument
, NULL
, 0}
231 static int read_line
P_ ((void));
232 static int read_string
P_ ((void));
233 static void do_insert
P_ ((char *arg
, size_t arglen
, size_t lblen
));
234 static void push_arg
P_ ((char *arg
, size_t len
));
235 static boolean print_args
P_ ((boolean ask
));
236 static void do_exec
P_ ((void));
237 static void add_proc
P_ ((pid_t pid
));
238 static void wait_for_proc
P_ ((boolean all
));
239 static long parse_num
P_ ((char *str
, int option
, long min
, long max
));
240 static long env_size
P_ ((char **envp
));
241 static void usage
P_ ((FILE * stream
, int status
));
249 int always_run_command
= 1;
251 char *default_cmd
= "/bin/echo";
252 int (*read_args
) P_ ((void)) = read_line
;
254 program_name
= argv
[0];
256 orig_arg_max
= ARG_MAX
- 2048; /* POSIX.2 requires subtracting 2048. */
257 arg_max
= orig_arg_max
;
259 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
260 have it at 1 meg). Things will work fine with a large ARG_MAX but it
261 will probably hurt the system more than it needs to; an array of this
262 size is allocated. */
263 if (arg_max
> 20 * 1024)
266 /* Take the size of the environment into account. */
267 arg_max
-= env_size (environ
);
269 error (1, 0, "environment is too large for exec");
271 while ((optc
= getopt_long (argc
, argv
, "+0e::i::l::n:prs:txP:",
272 longopts
, (int *) 0)) != -1)
277 read_args
= read_string
;
292 replace_pat
= optarg
;
295 /* -i excludes -n -l. */
302 lines_per_exec
= parse_num (optarg
, 'l', 1L, -1L);
305 /* -l excludes -i -n. */
311 args_per_exec
= parse_num (optarg
, 'n', 1L, -1L);
312 /* -n excludes -i -l. */
318 arg_max
= parse_num (optarg
, 's', 1L, orig_arg_max
);
322 print_command
= true;
326 exit_if_size_exceeded
= true;
330 query_before_executing
= true;
331 print_command
= true;
335 always_run_command
= 0;
339 proc_max
= parse_num (optarg
, 'P', 0L, -1L);
343 printf ("GNU xargs version %s\n", version_string
);
351 if (replace_pat
|| lines_per_exec
)
352 exit_if_size_exceeded
= true;
361 linebuf
= (char *) xmalloc (arg_max
+ 1);
362 argbuf
= (char *) xmalloc (arg_max
+ 1);
364 /* Make sure to listen for the kids. */
365 signal (SIGCHLD
, SIG_DFL
);
369 for (; optind
< argc
; optind
++)
370 push_arg (argv
[optind
], strlen (argv
[optind
]) + 1);
371 initial_args
= false;
372 initial_argc
= cmd_argc
;
373 initial_argv_chars
= cmd_argv_chars
;
375 while ((*read_args
) () != -1)
376 if (lines_per_exec
&& lineno
>= lines_per_exec
)
382 /* SYSV xargs seems to do at least one exec, even if the
384 if (cmd_argc
!= initial_argc
385 || (always_run_command
&& procs_executed
== 0))
392 size_t *arglen
= (size_t *) xmalloc (sizeof (size_t) * argc
);
394 for (i
= optind
; i
< argc
; i
++)
395 arglen
[i
] = strlen(argv
[i
]);
396 rplen
= strlen (replace_pat
);
397 while ((len
= (*read_args
) ()) != -1)
399 /* Don't do insert on the command name. */
400 push_arg (argv
[optind
], arglen
[optind
] + 1);
402 for (i
= optind
+ 1; i
< argc
; i
++)
403 do_insert (argv
[i
], arglen
[i
], len
);
408 wait_for_proc (true);
412 /* Read a line of arguments from stdin and add them to the list of
413 arguments to pass to the command. Ignore blank lines and initial blanks.
414 Single and double quotes and backslashes quote metacharacters and blanks
415 as they do in the shell.
416 Return -1 if eof (either physical or logical) is reached,
417 otherwise the length of the last string read (including the null). */
422 static boolean eof
= false;
423 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
424 int state
= SPACE
; /* The type of character we last read. */
425 int prevc
; /* The previous value of c. */
426 int quotc
= 0; /* The last quote character read. */
428 boolean first
= true; /* true if reading first arg on line. */
431 /* Including the NUL, the args must not grow past this point. */
432 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
442 /* COMPAT: SYSV seems to ignore stuff on a line that
443 ends without a \n; we don't. */
449 /* FIXME we don't check for unterminated quotes here. */
450 if (first
&& EOF_STR (linebuf
))
453 push_arg (linebuf
, len
);
467 if (!ISBLANK (prevc
))
468 lineno
++; /* For -l. */
477 if (EOF_STR (linebuf
))
480 return first
? -1 : len
;
483 push_arg (linebuf
, len
);
486 if (!replace_pat
&& ISSPACE (c
))
490 if (EOF_STR (linebuf
))
493 return first
? -1 : len
;
495 push_arg (linebuf
, len
);
517 error (1, 0, "unmatched %s quote",
518 quotc
== '"' ? "double" : "single");
531 error (1, 0, "argument line too long");
536 /* Read a null-terminated string from stdin and add it to the list of
537 arguments to pass to the command.
538 Return -1 if eof (either physical or logical) is reached,
539 otherwise the length of the string read (including the null). */
544 static boolean eof
= false;
547 /* Including the NUL, the args must not grow past this point. */
548 char *endbuf
= linebuf
+ arg_max
- initial_argv_chars
- 1;
554 int c
= getc (stdin
);
563 push_arg (linebuf
, len
);
568 lineno
++; /* For -l. */
572 push_arg (linebuf
, len
);
576 error (1, 0, "argument line too long");
581 /* Replace all instances of `replace_pat' in ARG with `linebuf',
582 and add the resulting string to the list of arguments for the command
584 ARGLEN is the length of ARG, not including the null.
585 LBLEN is the length of `linebuf', not including the null.
587 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
588 and a max of 5 occurences of replace_pat in the initial-arguments.
589 Those restrictions do not exist here. */
592 do_insert (arg
, arglen
, lblen
)
597 /* Temporary copy of each arg with the replace pattern replaced by the
599 static char *insertbuf
;
601 int bytes_left
= arg_max
- 1; /* Bytes left on the command line. */
604 insertbuf
= (char *) xmalloc (arg_max
+ 1);
609 size_t len
; /* Length in ARG before `replace_pat'. */
610 char *s
= strstr (arg
, replace_pat
);
619 strncpy (p
, arg
, len
);
637 error (1, 0, "command too long");
639 push_arg (insertbuf
, p
- insertbuf
);
642 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
644 LEN is the length of ARG, including the terminating null.
645 If this brings the list up to its maximum size, execute the command. */
654 if (cmd_argv_chars
+ len
> arg_max
)
656 if (initial_args
|| cmd_argc
== initial_argc
)
657 error (1, 0, "can not fit single argument within argument list size limit");
659 || (exit_if_size_exceeded
&&
660 (lines_per_exec
|| args_per_exec
)))
661 error (1, 0, "argument list too long");
664 if (!initial_args
&& args_per_exec
&&
665 cmd_argc
- initial_argc
== args_per_exec
)
669 if (cmd_argc
>= cmd_argv_alloc
)
674 cmd_argv
= (char **) xmalloc (sizeof (char *) * cmd_argv_alloc
);
679 cmd_argv
= (char **) xrealloc (cmd_argv
,
680 sizeof (char *) * cmd_argv_alloc
);
685 cmd_argv
[cmd_argc
++] = NULL
;
688 cmd_argv
[cmd_argc
++] = argbuf
+ cmd_argv_chars
;
689 strcpy (argbuf
+ cmd_argv_chars
, arg
);
690 cmd_argv_chars
+= len
;
694 /* Print the arguments of the command to execute.
695 If ASK is nonzero, prompt the user for a response, and
696 if the user responds affirmatively, return true;
697 otherwise, return false. */
705 for (i
= 0; i
< cmd_argc
- 1; i
++)
706 fprintf (stderr
, "%s ", cmd_argv
[i
]);
709 static FILE *tty_stream
;
714 tty_stream
= fopen ("/dev/tty", "r");
716 error (1, errno
, "/dev/tty");
718 fputs ("?...", stderr
);
720 c
= savec
= getc (tty_stream
);
721 while (c
!= EOF
&& c
!= '\n')
722 c
= getc (tty_stream
);
723 if (savec
== 'y' || savec
== 'Y')
732 /* Execute the command that has been built in `cmd_argv'. This may involve
733 waiting for processes that were previously executed. */
740 push_arg ((char *) NULL
, 0); /* Null terminate the arg list. */
741 if (!query_before_executing
|| print_args (true))
743 if (proc_max
&& procs_executing
>= proc_max
)
744 wait_for_proc (false);
745 if (!query_before_executing
&& print_command
)
747 /* If we run out of processes, wait for a child to return and
749 while ((child
= fork ()) < 0 && errno
== EAGAIN
&& procs_executing
)
750 wait_for_proc (false);
754 error (1, errno
, "cannot fork");
757 execvp (cmd_argv
[0], cmd_argv
);
758 error (0, errno
, "%s", cmd_argv
[0]);
759 _exit (errno
== ENOENT
? 127 : 126);
764 cmd_argc
= initial_argc
;
765 cmd_argv_chars
= initial_argv_chars
;
768 /* Add the process with id PID to the list of processes that have
777 /* Find an empty slot. */
778 for (i
= 0; i
< pids_alloc
&& pids
[i
]; i
++)
784 pids_alloc
= proc_max
? proc_max
: 64;
785 pids
= (pid_t
*) xmalloc (sizeof (pid_t
) * pids_alloc
);
790 pids
= (pid_t
*) xrealloc (pids
,
791 sizeof (pid_t
) * pids_alloc
);
793 memset (&pids
[i
], '\0', sizeof (pid_t
) * (pids_alloc
- i
));
800 /* If ALL is true, wait for all child processes to finish;
801 otherwise, wait for one child process to finish.
802 Remove the processes that finish from the list of executing processes. */
808 while (procs_executing
)
816 pid
= wait (&status
);
818 error (1, errno
, "error waiting for child process");
820 /* Find the entry in `pids' for the child process
822 for (i
= 0; i
< pids_alloc
&& pid
!= pids
[i
]; i
++)
825 while (i
== pids_alloc
); /* A child died that we didn't start? */
827 /* Remove the child from the list. */
831 if (WEXITSTATUS (status
) == 126 || WEXITSTATUS (status
) == 127)
832 exit (WEXITSTATUS (status
)); /* Can't find or run the command. */
833 if (WEXITSTATUS (status
) == 255)
834 error (124, 0, "%s: exited with status 255; aborting", cmd_argv
[0]);
835 if (WIFSTOPPED (status
))
836 error (125, 0, "%s: stopped by signal %d", cmd_argv
[0], WSTOPSIG (status
));
837 if (WIFSIGNALED (status
))
838 error (125, 0, "%s: terminated by signal %d", cmd_argv
[0], WTERMSIG (status
));
839 if (WEXITSTATUS (status
) != 0)
847 /* Return the value of the number represented in STR.
848 OPTION is the command line option to which STR is the argument.
849 If the value does not fall within the boundaries MIN and MAX,
850 Print an error message mentioning OPTION and exit. */
853 parse_num (str
, option
, min
, max
)
862 val
= strtol (str
, &eptr
, 10);
863 if (eptr
== str
|| *eptr
)
865 fprintf (stderr
, "%s: invalid number for -%c option\n",
866 program_name
, option
);
871 fprintf (stderr
, "%s: value for -%c option must be >= %ld\n",
872 program_name
, option
, min
);
875 else if (max
>= 0 && val
> max
)
877 fprintf (stderr
, "%s: value for -%c option must be < %ld\n",
878 program_name
, option
, max
);
884 /* Return how much of ARG_MAX is used by the environment. */
893 len
+= strlen (*envp
++) + 1;
899 usage (stream
, status
)
904 Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
905 [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
906 [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
907 [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
908 [--max-args=max-args] [--no-run-if-empty] [--version] [--help]\n\
909 [command [initial-arguments]]\n",