*** empty log message ***
[findutils.git] / xargs / xargs.c
blobcd2d1a2fa1eb32c7a10fabb48aba958d4af53f06
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)
7 any later version.
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>. */
21 #include <config.h>
23 # ifndef PARAMS
24 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
25 # define PARAMS(Args) Args
26 # else
27 # define PARAMS(Args) ()
28 # endif
29 # endif
31 #define _GNU_SOURCE
32 #include <ctype.h>
34 #if !defined (isascii) || defined (STDC_HEADERS)
35 #ifdef isascii
36 #undef isascii
37 #endif
38 #define isascii(c) 1
39 #endif
41 #ifdef isblank
42 #define ISBLANK(c) (isascii (c) && isblank (c))
43 #else
44 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
45 #endif
47 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
48 || (c) == '\f' || (c) == '\v')
50 #include <sys/types.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <getopt.h>
55 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
56 #include <string.h>
57 #if !defined(STDC_HEADERS)
58 #include <memory.h>
59 #endif
60 #else
61 #include <strings.h>
62 #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
63 #endif
65 #ifndef _POSIX_SOURCE
66 #include <sys/param.h>
67 #endif
69 #ifdef HAVE_LIMITS_H
70 #include <limits.h>
71 #endif
73 #ifndef LONG_MAX
74 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
75 #endif
77 #ifdef HAVE_UNISTD_H
78 #include <unistd.h>
79 #endif
81 #include <signal.h>
83 #if !defined(SIGCHLD) && defined(SIGCLD)
84 #define SIGCHLD SIGCLD
85 #endif
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)
91 #endif
92 #ifndef ARG_MAX
93 #define ARG_MAX NCARGS
94 #endif
96 #include "wait.h"
98 /* States for read_line. */
99 #define NORM 0
100 #define SPACE 1
101 #define QUOTE 2
102 #define BACKSLASH 3
104 #ifdef STDC_HEADERS
105 #include <stdlib.h>
106 #else
107 extern int errno;
108 #endif
110 #ifdef HAVE_LOCALE_H
111 #include <locale.h>
112 #endif
113 #if ENABLE_NLS
114 # include <libintl.h>
115 # define _(Text) gettext (Text)
116 #else
117 # define _(Text) Text
118 #define textdomain(Domain)
119 #define bindtextdomain(Package, Directory)
120 #endif
121 #ifdef gettext_noop
122 # define N_(String) gettext_noop (String)
123 #else
124 # define N_(String) (String)
125 #endif
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. */
133 typedef int boolean;
134 #define true 1
135 #define false 0
137 #if __STDC__
138 #define VOID void
139 #else
140 #define VOID char
141 #endif
143 #include <xalloc.h>
144 void error PARAMS ((int status, int errnum, char *message,...));
146 extern char *version_string;
148 /* The name this program was run with. */
149 char *program_name;
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
160 initial args. */
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
167 end of file.
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
172 per command line. */
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. */
182 static long arg_max;
184 /* Storage for elements of `cmd_argv'. */
185 static char *argbuf;
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
209 at once. */
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
225 status of 1-125. */
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)
268 int optc;
269 int always_run_command = 1;
270 long orig_arg_max;
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, "");
278 #endif
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)
293 arg_max = 20 * 1024;
295 /* Take the size of the environment into account. */
296 arg_max -= env_size (environ);
297 if (arg_max <= 0)
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)
303 switch (optc)
305 case '0':
306 read_args = read_string;
307 break;
309 case 'e':
310 if (optarg)
311 eof_str = optarg;
312 else
313 eof_str = 0;
314 break;
316 case 'h':
317 usage (stdout, 0);
319 case 'i':
320 if (optarg)
321 replace_pat = optarg;
322 else
323 replace_pat = "{}";
324 /* -i excludes -n -l. */
325 args_per_exec = 0;
326 lines_per_exec = 0;
327 break;
329 case 'l':
330 if (optarg)
331 lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
332 else
333 lines_per_exec = 1;
334 /* -l excludes -i -n. */
335 args_per_exec = 0;
336 replace_pat = NULL;
337 break;
339 case 'n':
340 args_per_exec = parse_num (optarg, 'n', 1L, -1L);
341 /* -n excludes -i -l. */
342 lines_per_exec = 0;
343 replace_pat = NULL;
344 break;
346 case 's':
347 arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
348 break;
350 case 't':
351 print_command = true;
352 break;
354 case 'x':
355 exit_if_size_exceeded = true;
356 break;
358 case 'p':
359 query_before_executing = true;
360 print_command = true;
361 break;
363 case 'r':
364 always_run_command = 0;
365 break;
367 case 'P':
368 proc_max = parse_num (optarg, 'P', 0L, -1L);
369 break;
371 case 'v':
372 printf (_("GNU xargs version %s\n"), version_string);
373 exit (0);
375 default:
376 usage (stderr, 1);
380 if (replace_pat || lines_per_exec)
381 exit_if_size_exceeded = true;
383 if (optind == argc)
385 optind = 0;
386 argc = 1;
387 argv = &default_cmd;
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);
396 if (!replace_pat)
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)
407 do_exec ();
408 lineno = 0;
411 /* SYSV xargs seems to do at least one exec, even if the
412 input is empty. */
413 if (cmd_argc != initial_argc
414 || (always_run_command && procs_executed == 0))
415 do_exec ();
417 else
419 int i;
420 size_t len;
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);
430 len--;
431 for (i = optind + 1; i < argc; i++)
432 do_insert (argv[i], arglen[i], len);
433 do_exec ();
437 wait_for_proc (true);
438 exit (child_error);
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). */
448 static int
449 read_line (void)
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. */
456 int c = EOF;
457 boolean first = true; /* true if reading first arg on line. */
458 int len;
459 char *p = linebuf;
460 /* Including the NUL, the args must not grow past this point. */
461 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
463 if (eof)
464 return -1;
465 while (1)
467 prevc = c;
468 c = getc (stdin);
469 if (c == EOF)
471 /* COMPAT: SYSV seems to ignore stuff on a line that
472 ends without a \n; we don't. */
473 eof = true;
474 if (p == linebuf)
475 return -1;
476 *p++ = '\0';
477 len = p - linebuf;
478 /* FIXME we don't check for unterminated quotes here. */
479 if (first && EOF_STR (linebuf))
480 return -1;
481 if (!replace_pat)
482 push_arg (linebuf, len);
483 return len;
485 switch (state)
487 case SPACE:
488 if (ISSPACE (c))
489 continue;
490 state = NORM;
491 /* aaahhhh.... */
493 case NORM:
494 if (c == '\n')
496 if (!ISBLANK (prevc))
497 lineno++; /* For -l. */
498 if (p == linebuf)
500 /* Blank line. */
501 state = SPACE;
502 continue;
504 *p++ = '\0';
505 len = p - linebuf;
506 if (EOF_STR (linebuf))
508 eof = true;
509 return first ? -1 : len;
511 if (!replace_pat)
512 push_arg (linebuf, len);
513 return len;
515 if (!replace_pat && ISSPACE (c))
517 *p++ = '\0';
518 len = p - linebuf;
519 if (EOF_STR (linebuf))
521 eof = true;
522 return first ? -1 : len;
524 push_arg (linebuf, len);
525 p = linebuf;
526 state = SPACE;
527 first = false;
528 continue;
530 switch (c)
532 case '\\':
533 state = BACKSLASH;
534 continue;
536 case '\'':
537 case '"':
538 state = QUOTE;
539 quotc = c;
540 continue;
542 break;
544 case QUOTE:
545 if (c == '\n')
546 error (1, 0, _("unmatched %s quote"),
547 quotc == '"' ? _("double") : _("single"));
548 if (c == quotc)
550 state = NORM;
551 continue;
553 break;
555 case BACKSLASH:
556 state = NORM;
557 break;
559 if (p >= endbuf)
560 error (1, 0, _("argument line too long"));
561 *p++ = c;
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). */
570 static int
571 read_string (void)
573 static boolean eof = false;
574 int len;
575 char *p = linebuf;
576 /* Including the NUL, the args must not grow past this point. */
577 char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
579 if (eof)
580 return -1;
581 while (1)
583 int c = getc (stdin);
584 if (c == EOF)
586 eof = true;
587 if (p == linebuf)
588 return -1;
589 *p++ = '\0';
590 len = p - linebuf;
591 if (!replace_pat)
592 push_arg (linebuf, len);
593 return len;
595 if (c == '\0')
597 lineno++; /* For -l. */
598 *p++ = '\0';
599 len = p - linebuf;
600 if (!replace_pat)
601 push_arg (linebuf, len);
602 return len;
604 if (p >= endbuf)
605 error (1, 0, _("argument line too long"));
606 *p++ = c;
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
612 to execute.
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 occurences of replace_pat in the initial-arguments.
618 Those restrictions do not exist here. */
620 static void
621 do_insert (char *arg, size_t arglen, size_t lblen)
623 /* Temporary copy of each arg with the replace pattern replaced by the
624 real arg. */
625 static char *insertbuf;
626 char *p;
627 int bytes_left = arg_max - 1; /* Bytes left on the command line. */
629 if (!insertbuf)
630 insertbuf = (char *) xmalloc (arg_max + 1);
631 p = insertbuf;
635 size_t len; /* Length in ARG before `replace_pat'. */
636 char *s = strstr (arg, replace_pat);
637 if (s)
638 len = s - arg;
639 else
640 len = arglen;
641 bytes_left -= len;
642 if (bytes_left <= 0)
643 break;
645 strncpy (p, arg, len);
646 p += len;
647 arg += len;
648 arglen -= len;
650 if (s)
652 bytes_left -= lblen;
653 if (bytes_left <= 0)
654 break;
655 strcpy (p, linebuf);
656 arg += rplen;
657 arglen -= rplen;
658 p += lblen;
661 while (*arg);
662 if (*arg)
663 error (1, 0, _("command too long"));
664 *p++ = '\0';
665 push_arg (insertbuf, p - insertbuf);
668 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
669 to the command.
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. */
673 static void
674 push_arg (char *arg, size_t len)
676 if (arg)
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"));
682 if (replace_pat
683 || (exit_if_size_exceeded &&
684 (lines_per_exec || args_per_exec)))
685 error (1, 0, _("argument list too long"));
686 do_exec ();
688 if (!initial_args && args_per_exec &&
689 cmd_argc - initial_argc == args_per_exec)
690 do_exec ();
693 if (cmd_argc >= cmd_argv_alloc)
695 if (!cmd_argv)
697 cmd_argv_alloc = 64;
698 cmd_argv = (char **) xmalloc (sizeof (char *) * cmd_argv_alloc);
700 else
702 cmd_argv_alloc *= 2;
703 cmd_argv = (char **) xrealloc (cmd_argv,
704 sizeof (char *) * cmd_argv_alloc);
708 if (!arg)
709 cmd_argv[cmd_argc++] = NULL;
710 else
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. */
723 static boolean
724 print_args (boolean ask)
726 int i;
728 for (i = 0; i < cmd_argc - 1; i++)
729 fprintf (stderr, "%s ", cmd_argv[i]);
730 if (ask)
732 static FILE *tty_stream;
733 int c, savec;
735 if (!tty_stream)
737 tty_stream = fopen ("/dev/tty", "r");
738 if (!tty_stream)
739 error (1, errno, "/dev/tty");
741 fputs ("?...", stderr);
742 fflush (stderr);
743 c = savec = getc (tty_stream);
744 while (c != EOF && c != '\n')
745 c = getc (tty_stream);
746 if (savec == 'y' || savec == 'Y')
747 return true;
749 else
750 putc ('\n', stderr);
752 return false;
755 /* Execute the command that has been built in `cmd_argv'. This may involve
756 waiting for processes that were previously executed. */
758 static void
759 do_exec (void)
761 pid_t child;
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)
769 print_args (false);
770 /* If we run out of processes, wait for a child to return and
771 try again. */
772 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
773 wait_for_proc (false);
774 switch (child)
776 case -1:
777 error (1, errno, _("cannot fork"));
779 case 0: /* Child. */
780 execvp (cmd_argv[0], cmd_argv);
781 error (0, errno, "%s", cmd_argv[0]);
782 _exit (errno == ENOENT ? 127 : 126);
784 add_proc (child);
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
792 been executed. */
794 static void
795 add_proc (pid_t pid)
797 int i;
799 /* Find an empty slot. */
800 for (i = 0; i < pids_alloc && pids[i]; i++)
802 if (i == pids_alloc)
804 if (pids_alloc == 0)
806 pids_alloc = proc_max ? proc_max : 64;
807 pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
809 else
811 pids_alloc *= 2;
812 pids = (pid_t *) xrealloc (pids,
813 sizeof (pid_t) * pids_alloc);
815 memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
817 pids[i] = pid;
818 procs_executing++;
819 procs_executed++;
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. */
826 static void
827 wait_for_proc (boolean all)
829 while (procs_executing)
831 int i, status;
835 pid_t pid;
837 while ((pid = wait (&status)) == (pid_t) -1)
838 if (errno != EINTR)
839 error (1, errno, _("error waiting for child process"));
841 /* Find the entry in `pids' for the child process
842 that exited. */
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. */
849 pids[i] = 0;
850 procs_executing--;
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)
861 child_error = 123;
863 if (!all)
864 break;
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. */
873 static long
874 parse_num (char *str, int option, long int min, long int max)
876 char *eptr;
877 long val;
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);
884 usage (stderr, 1);
886 else if (val < min)
888 fprintf (stderr, _("%s: value for -%c option must be >= %ld\n"),
889 program_name, option, min);
890 usage (stderr, 1);
892 else if (max >= 0 && val > max)
894 fprintf (stderr, _("%s: value for -%c option must be < %ld\n"),
895 program_name, option, max);
896 usage (stderr, 1);
898 return val;
901 /* Return how much of ARG_MAX is used by the environment. */
903 static long
904 env_size (char **envp)
906 long len = 0;
908 while (*envp)
909 len += strlen (*envp++) + 1;
911 return len;
914 static void
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"),
924 program_name);
925 exit (status);