home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / find-3.8-src.lha / src / amiga / find-3.8 / xargs / xargs.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-23  |  19.7 KB  |  865 lines

  1. /* xargs -- build and execute command lines from standard input
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  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.
  8.  
  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.
  13.  
  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.  */
  17.  
  18. /* Author:
  19.     Mike Rendell            Department of Computer Science
  20.     michael@garfield.mun.edu    Memorial University of Newfoundland
  21.     ..!uunet!garfield!michael    St. John's, Nfld., Canada
  22.     (709) 737-4550            A1C 5S7  */
  23.  
  24. #define _GNU_SOURCE
  25. #include <ctype.h>
  26.  
  27. #ifndef isascii
  28. #define isascii(c) 1
  29. #endif
  30.  
  31. #ifdef isblank
  32. #define ISBLANK(c) (isascii (c) && isblank (c))
  33. #else
  34. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  35. #endif
  36.  
  37. #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
  38.             || (c) == '\f' || (c) == '\v')
  39.  
  40. #include <stdio.h>
  41. #include <errno.h>
  42. #include <getopt.h>
  43.  
  44. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  45. #include <string.h>
  46. #if !defined(STDC_HEADERS)
  47. #include <memory.h>
  48. #endif
  49. #define bcopy(source, dest, count) (memcpy((dest), (source), (count)))
  50. #else
  51. #include <strings.h>
  52. #endif
  53.  
  54. char *strstr ();
  55.  
  56. #ifndef _POSIX_SOURCE
  57. #include <sys/param.h>
  58. #endif
  59.  
  60. #ifdef HAVE_LIMITS_H
  61. #include <limits.h>
  62. #endif
  63.  
  64. #ifdef HAVE_UNISTD_H
  65. #include <sys/types.h>
  66. #include <unistd.h>
  67. #endif
  68.  
  69. /* COMPAT:  SYSV version defaults size (and has a max value of) to 470. We
  70.    try to make it as large as possible. */
  71.  
  72. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  73. #define ARG_MAX sysconf (_SC_ARG_MAX)
  74. #endif
  75.  
  76. #ifndef ARG_MAX
  77. #define ARG_MAX NCARGS
  78. #endif
  79.  
  80. #include "wait.h"
  81.  
  82. /* States for read_line. */
  83. #define NORM 0
  84. #define SPACE 1
  85. #define QUOTE 2
  86. #define BACKSLASH 3
  87.  
  88. /* A linked list of processes that we have forked off. */
  89. struct pid_list
  90. {
  91.   int pl_pid;
  92.   struct pid_list *pl_next;
  93. };
  94.  
  95. #ifdef STDC_HEADERS
  96. #include <stdlib.h>
  97. #else
  98. char *malloc ();
  99. void exit ();
  100. void free ();
  101. long strtol ();
  102.  
  103. extern int errno;
  104. #endif
  105.  
  106. char *strdup ();
  107. char *xmalloc ();
  108. void error ();
  109.  
  110. static int read_string ();
  111. static int read_line ();
  112. static int print_args ();
  113. static long env_size ();
  114. static long parse_num ();
  115. static void add_proc ();
  116. static void do_exec ();
  117. static void do_insert ();
  118. static void push_arg ();
  119. static void usage ();
  120. static void wait_for_proc ();
  121.  
  122. extern char **environ;
  123.  
  124. /* The name this program was run with. */
  125. char *program_name;
  126.  
  127. /* If nonzero, then instead of putting the args from stdin
  128.    at the end of the command argument list, they are each stuck into the
  129.    initial args, replacing each occurrence of the `replace_pat' in the
  130.    initial args. */
  131. static char *replace_pat = 0;
  132.  
  133. /* If nonzero, when this string is read on stdin it is treated as end of file.
  134.    I don't like this - it should default to NULL. */
  135. static char *eof_str = "_";
  136.  
  137. /* If nonzero, the maximum number of nonblank lines from stdin to use
  138.    per command line. */
  139. static long lines_per_exec = 0;
  140.  
  141. /* The maximum number of arguments to use per command line.
  142.    The default is specified by POSIX. */
  143. static long nargs_per_exec = 255;
  144.  
  145. /* The maximum number of characters that can be used per command line. */
  146. static long arg_max;
  147.  
  148. /* If nonzero, print each command on stderr before executing it. */
  149. static int print_command = 0;
  150.  
  151. /* If nonzero, exit if lines_per_exec or nargs_per_exec is exceeded. */
  152. static int exit_if_size_exceeded = 0;
  153.  
  154. /* If nonzero, query the user before executing each command, and only
  155.    execute the command if the user responds affirmatively. */
  156. static int query_before_executing = 0;
  157.  
  158. /* If nonzero, run the command at least once, even if there is no input. */
  159. static int always_run_command = 1;
  160.  
  161. /* If nonzero, the maximum number of child processes that can be running
  162.    at once. */
  163. static int proc_max = 1;
  164.  
  165. /* Total number of child processes that have been executed. */
  166. static int processes_executed = 0;
  167.  
  168. /* List of child processes currently executing. */
  169. static struct pid_list *pid_list = 0;
  170.  
  171. /* The number of elements in `pid_list'. */
  172. static int procs_executing = 0;
  173.  
  174. /* Default program to run. */
  175. static char def_prog[] = "/bin/echo";
  176.  
  177. /* Buffer for reading arguments from stdin. */
  178. static char *linebuf;
  179.  
  180. /* Temporary copy of each arg with the replace pattern replaced by the
  181.    real arg. */
  182. static char *insertbuf;
  183.  
  184. /* Line number in stdin since the last command was executed. */
  185. static int lineno = 0;
  186.  
  187. /* The list of args being built. */
  188. static char **cmd_argv = 0;
  189.  
  190. /* Number of elements allocated for `cmd_argv'. */
  191. static int cmd_nargv = 0;
  192.  
  193. /* Number of valid elements in `cmd_argv'. */
  194. static int cmd_argc = 0;
  195.  
  196. /* Number of chars in `cmd_argv'. */
  197. static int cmd_ncargs = 0;
  198.  
  199. /* Number of initial arguments given on the command line. */
  200. static int cmd_initial_argc = 0;
  201.  
  202. /* Number of chars in the initial args. */
  203. static int cmd_initial_ncargs = 0;
  204.  
  205. /* Nonzero when building up initial arguments in `cmd_argv'. */
  206. static int static_args = 1;
  207.  
  208. /* Nonzero (exit status) if any child process exited with a status of 1-125. */
  209. static int child_error = 0;
  210.  
  211. /* For reading user response to prompting from /dev/tty. */
  212. static FILE *tty_stream;
  213.  
  214. static struct option const longopts[] =
  215. {
  216.   {"null", 0, NULL, '0'},
  217.   {"eof", 2, NULL, 'e'},
  218.   {"replace", 2, NULL, 'i'},
  219.   {"max-lines", 2, NULL, 'l'},
  220.   {"max-args", 1, NULL, 'n'},
  221.   {"interactive", 0, NULL, 'p'},
  222.   {"no-run-if-empty", 0, NULL, 'r'},
  223.   {"max-chars", 1, NULL, 's'},
  224.   {"verbose", 0, NULL, 't'},
  225.   {"exit", 0, NULL, 'x'},
  226.   {"max-procs", 1, NULL, 'P'},
  227.   {NULL, 0, NULL, 0}
  228. };
  229.  
  230. void
  231. main (argc, argv)
  232.      int argc;
  233.      char **argv;
  234. {
  235.   int optc;
  236.   long orig_arg_max;
  237.   char *dummy;
  238.   int (*read_args) () = read_line;
  239.  
  240.   program_name = argv[0];
  241.  
  242.   orig_arg_max = arg_max = ARG_MAX;
  243.  
  244.   /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  245.      have it at 1 meg).  Things will work fine with a large ARG_MAX but it
  246.      will probably hurt the system more than it needs to; an array of this
  247.      size is allocated.  */
  248.   if (arg_max > 20 * 1024)
  249.     arg_max = 20 * 1024;
  250.  
  251.   /* Adjust arg_max to take the size of the environment into account. */
  252.   arg_max -= env_size (environ);
  253.   if (arg_max <= 0)
  254.     error (1, 0, "environment is too large for exec");
  255.  
  256.   while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
  257.                   longopts, (int *) 0)) != -1)
  258.     {
  259.       switch (optc)
  260.     {
  261.     case '0':
  262.       read_args = read_string;
  263.       break;
  264.  
  265.     case 'e':
  266.       if (optarg)
  267.         eof_str = optarg;
  268.       else
  269.         eof_str = 0;
  270.       break;
  271.  
  272.     case 'i':
  273.       if (optarg)
  274.         replace_pat = optarg;
  275.       else
  276.         replace_pat = "{}";
  277.       /* -i excludes -n -l. */
  278.       nargs_per_exec = 0;
  279.       lines_per_exec = 0;
  280.       break;
  281.  
  282.     case 'l':
  283.       if (optarg)
  284.         lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
  285.       else
  286.         lines_per_exec = 1;
  287.       /* -l excludes -i -n. */
  288.       nargs_per_exec = 0;
  289.       replace_pat = 0;
  290.       break;
  291.       
  292.     case 'n':
  293.       nargs_per_exec = parse_num (optarg, 'n', 1L, -1L);
  294.       /* -n excludes -i -l. */
  295.       lines_per_exec = 0;
  296.       replace_pat = 0;
  297.       break;
  298.       
  299.     case 's':
  300.       arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
  301.       break;
  302.       
  303.     case 't':
  304.       print_command++;
  305.       break;
  306.       
  307.     case 'x':
  308.       exit_if_size_exceeded++;
  309.       break;
  310.       
  311.     case 'p':
  312.       query_before_executing++;
  313.       break;
  314.       
  315.     case 'r':
  316.       always_run_command = 0;
  317.       break;
  318.  
  319.     case 'P':
  320.       proc_max = parse_num (optarg, 'P', 0L, -1L);
  321.       break;
  322.       
  323.     default:
  324.       usage ();
  325.     }
  326.     }
  327.  
  328.   if (query_before_executing)
  329.     {
  330.       print_command++;
  331.       tty_stream = fopen ("/dev/tty", "r");
  332.       if (!tty_stream)
  333.     error (1, errno, "/dev/tty");
  334.     }
  335.   if (replace_pat || lines_per_exec)
  336.     exit_if_size_exceeded++;
  337.  
  338.   linebuf = xmalloc ((unsigned) arg_max + 1);
  339.  
  340.   if (optind == argc)
  341.     {
  342.       optind = 0;
  343.       argc = 1;
  344.       dummy = def_prog;
  345.       argv = &dummy;
  346.     }
  347.  
  348.   if (!replace_pat)
  349.     {
  350.       while (optind < argc)
  351.     push_arg (argv[optind++]);
  352.       static_args = 0;
  353.  
  354.       cmd_initial_argc = cmd_argc;
  355.       cmd_initial_ncargs = cmd_ncargs;
  356.  
  357.       while ((*read_args) () == 0)
  358.     if (lines_per_exec && lineno >= lines_per_exec)
  359.       {
  360.         do_exec ();
  361.         lineno = 0;
  362.       }
  363.  
  364.       /* SYSV xargs seems to do at least one exec, even if the
  365.          input is empty. */
  366.       if (cmd_argc != cmd_initial_argc
  367.       || (always_run_command && processes_executed == 0))
  368.     do_exec ();
  369.     }
  370.   else
  371.     {
  372.       int ac;
  373.       char **av;
  374.  
  375.       insertbuf = xmalloc ((unsigned) arg_max + 1);
  376.       while ((*read_args) () == 0)
  377.     {
  378.       push_arg (argv[optind]);    /* Don't do insert on command name. */
  379.       for (ac = argc - optind, av = argv + optind; --ac > 0;)
  380.         do_insert (*++av, linebuf);
  381.       do_exec ();
  382.     }
  383.     }
  384.   wait_for_proc (1);
  385.   exit (child_error);
  386. }
  387.  
  388. /* Read a line of arguments from stdin and add them to the list of
  389.    arguments to pass to the command.  Ignore blank lines and initial blanks.
  390.    Single and double quotes and backslashes quote metacharacters and blanks
  391.    as they do in the shell.
  392.    Return -1 if eof (either physical or logical) is reached, 0 otherwise. */
  393.  
  394. static int
  395. read_line ()
  396. {
  397.   static int eof = 0;
  398.   /* Start out in mode SPACE to always strip leading spaces (even with -i). */
  399.   int state = SPACE;        /* The type of character we last read. */
  400.   int lastc;            /* The previous value of c. */
  401.   int quotc;            /* The last quote character read. */
  402.   int c = EOF;
  403.   int first = 1;        /* Nonzero if reading first arg on the line. */
  404.   char *p = linebuf;
  405.   char *endbuf = linebuf + arg_max - 1;
  406.  
  407.   if (eof)
  408.     return -1;
  409.   while (1)
  410.     {
  411.       lastc = c;
  412.       c = getc (stdin);
  413.       if (c == EOF)
  414.     {
  415.       /* COMPAT: SYSV seems to ignore stuff on a line that
  416.          ends without a \n; we don't.  */
  417.       eof = 1;
  418.       if (p == linebuf)
  419.         return -1;
  420.       *p = '\0';
  421.       if (eof_str && first && !strcmp (eof_str, linebuf))
  422.         return -1;
  423.       if (!replace_pat)
  424.         push_arg (linebuf);
  425.       return 0;
  426.     }
  427.       switch (state)
  428.     {
  429.     case SPACE:
  430.       if (ISSPACE (c))
  431.         continue;
  432.       state = NORM;
  433.       /* aaahhhh.... */
  434.  
  435.     case NORM:
  436.       if (c == '\n')
  437.         {
  438.           if (!ISBLANK (lastc))
  439.         lineno++;    /* For -l. */
  440.           if (p == linebuf)
  441.         {
  442.           state = SPACE;
  443.           continue;
  444.         }
  445.           *p = '\0';
  446.           if (eof_str && !strcmp (eof_str, linebuf))
  447.         {
  448.           eof = 1;
  449.           return first ? -1 : 0;
  450.         }
  451.           if (!replace_pat)
  452.         push_arg (linebuf);
  453.           return 0;
  454.         }
  455.       if (!replace_pat && ISSPACE (c))
  456.         {
  457.           *p = '\0';
  458.           if (eof_str && !strcmp (eof_str, linebuf))
  459.         {
  460.           eof = 1;
  461.           return first ? -1 : 0;
  462.         }
  463.           p = linebuf;
  464.           push_arg (p);
  465.           state = SPACE;
  466.           first = 0;
  467.           continue;
  468.         }
  469.       switch (c)
  470.         {
  471.         case '\\':
  472.           state = BACKSLASH;
  473.           continue;
  474.           
  475.         case '\'':
  476.         case '"':
  477.           state = QUOTE;
  478.           quotc = c;
  479.           continue;
  480.         }
  481.       break;
  482.  
  483.     case QUOTE:
  484.       if (c == '\n')
  485.         error (1, 0, "%s quote did not end before line did",
  486.            quotc == '"' ? "double" : "single");
  487.       if (c == quotc)
  488.         {
  489.           state = NORM;
  490.           continue;
  491.         }
  492.       break;
  493.  
  494.     case BACKSLASH:
  495.       state = NORM;
  496.       break;
  497.     }
  498.       if (p >= endbuf)
  499.     error (1, 0, "argument line too long");
  500.       *p++ = c;
  501.     }
  502. }
  503.  
  504. /* Read a null-terminated string from stdin and add it to the list of
  505.    arguments to pass to the command.
  506.    Return -1 if eof (either physical or logical) is reached, 0 otherwise. */
  507.  
  508. static int
  509. read_string ()
  510. {
  511.   static int eof = 0;
  512.   int c;
  513.   char *p = linebuf;
  514.   char *endbuf = linebuf + arg_max - 1;
  515.  
  516.   if (eof)
  517.     return -1;
  518.   while (1)
  519.     {
  520.       c = getc (stdin);
  521.       if (c == EOF)
  522.     {
  523.       eof = 1;
  524.       if (p == linebuf)
  525.         return -1;
  526.       *p = '\0';
  527.       if (eof_str && !strcmp (eof_str, linebuf))
  528.         return -1;
  529.       if (!replace_pat)
  530.         push_arg (linebuf);
  531.       return 0;
  532.     }
  533.       if (c == '\0')
  534.     {
  535.       lineno++;    /* For -l. */
  536.       *p = '\0';
  537.       if (eof_str && !strcmp (eof_str, linebuf))
  538.         {
  539.           eof = 1;
  540.           return -1;
  541.         }
  542.       if (!replace_pat)
  543.         push_arg (linebuf);
  544.       return 0;
  545.     }
  546.       if (p >= endbuf)
  547.     error (1, 0, "argument line too long");
  548.       *p++ = c;
  549.     }
  550. }
  551.  
  552. /* Print the arguments of the command to execute.
  553.    If ASK is nonzero, prompt the user for a response, and
  554.    if the user responds affirmatively, return 1.
  555.    Otherwise, return 0. */
  556.  
  557. static int
  558. print_args (ask)
  559.      int ask;
  560. {
  561.   int i;
  562.  
  563.   for (i = 0; i < cmd_argc - 1; i++)
  564.     fprintf (stderr, "%s ", cmd_argv[i]);
  565.   if (ask)
  566.     {
  567.       int c, savec;
  568.  
  569.       fputs ("?...", stderr);
  570.       fflush (stderr);
  571.       c = savec = getc (tty_stream);
  572.       while (c != EOF && c != '\n')
  573.     c = getc (tty_stream);
  574.       if (savec == 'y' || savec == 'Y')
  575.     return 1;
  576.     }
  577.   else
  578.     putc ('\n', stderr);
  579.  
  580.   return 0;
  581. }
  582.  
  583. /* Return the value of the number represented in STR.
  584.    OPTION is the command line option to which STR is the argument.
  585.    If the value does not fall within the boundaries MIN and MAX,
  586.    Print an error message mentioning OPTION and exit. */
  587.  
  588. static long
  589. parse_num (str, option, min, max)
  590.      char *str;
  591.      int option;
  592.      long min;
  593.      long max;
  594. {
  595.   char *eptr;
  596.   long val;
  597.  
  598.   val = strtol (str, &eptr, 10);
  599.   if (eptr == str || *eptr)
  600.     {
  601.       fprintf (stderr, "%s: invalid number for -%c option\n",
  602.            program_name, option);
  603.       usage ();
  604.     }
  605.   else if (val < min)
  606.     {
  607.       fprintf (stderr, "%s: value for -%c option must be >= %d\n",
  608.            program_name, option, min);
  609.       usage ();
  610.     }
  611.   else if (max >= 0 && val > max)
  612.     {
  613.       fprintf (stderr, "%s: value for -%c option must be < %ld\n",
  614.            program_name, option, max);
  615.       usage ();
  616.     }
  617.   return val;
  618. }
  619.  
  620. /* Add ARG to the end of the list of arguments `cmd_argv' to pass
  621.    to the command.
  622.    If this brings the list up to its maximum size, execute the command. */
  623.  
  624. static void
  625. push_arg (arg)
  626.      char *arg;
  627. {
  628.   if (arg)
  629.     {
  630.       int len = strlen (arg) + 1;
  631.  
  632.       if (cmd_ncargs + len > arg_max)
  633.     {
  634.       if (static_args || cmd_argc == cmd_initial_argc)
  635.         error (1, 0, "can not fit single argument within argument list size limit");
  636.       if (replace_pat
  637.           || (exit_if_size_exceeded && (lines_per_exec || nargs_per_exec)))
  638.         error (1, 0, "argument list too long");
  639.       do_exec ();
  640.     }
  641.       if (!static_args && nargs_per_exec &&
  642.       cmd_argc - cmd_initial_argc == nargs_per_exec)
  643.     do_exec ();
  644.       cmd_ncargs += len;
  645.     }
  646.   if (cmd_argc >= cmd_nargv)
  647.     {
  648.       char **new_argv;
  649.  
  650.       if (!cmd_argv)
  651.     cmd_nargv = 50;
  652.       new_argv = (char **)
  653.         xmalloc ((unsigned) (sizeof (char *) * cmd_nargv * 2));
  654.       cmd_nargv *= 2;
  655.       if (cmd_argv)
  656.     {
  657.       bcopy ((char *) cmd_argv, (char *) new_argv,
  658.          sizeof (char *) * cmd_argc);
  659.       free ((char *) cmd_argv);
  660.     }
  661.       cmd_argv = new_argv;
  662.     }
  663.   if (!arg)
  664.     cmd_argv[cmd_argc++] = 0;
  665.   else
  666.     {
  667.       cmd_argv[cmd_argc] = strdup (arg);
  668.       if (!cmd_argv[cmd_argc++])
  669.     error (1, 0, "virtual memory exhausted");
  670.     }
  671. }
  672.  
  673. /* Execute the command that has been built in `cmd_argv'.  This may involve
  674.    waiting for processes that were previously executed.
  675.    Also free the memory used by the command's arguments.
  676.  
  677.    COMPAT: since we don't wait for the process to complete before returning,
  678.    it is possible that we won't notice a command failing until the next
  679.    time do_exec is called.  This is only noticeable when xargs is run
  680.    interactivly.  It can be fixed by adding
  681.    if (proc_max == 1)
  682.      wait_for_proc (0)
  683.    after the add_proc call. */
  684.  
  685. static void
  686. do_exec ()
  687. {
  688.   int child;
  689.  
  690.   push_arg ((char *) 0);    /* Null terminate the arg list. */
  691.   if (!query_before_executing || print_args (1))
  692.     {
  693.       if (proc_max && procs_executing >= proc_max)
  694.     wait_for_proc (0);
  695.       if (!query_before_executing && print_command)
  696.     print_args (0);
  697.       /* If we run out of processes, wait for a child to return and
  698.          try again.  */
  699.       while ((child = vfork ()) < 0 && errno == EAGAIN && procs_executing)
  700.     wait_for_proc (0);
  701.       switch (child)
  702.     {
  703.     case -1:
  704.       error (1, errno, "cannot fork");
  705.  
  706.     case 0:            /* Child. */
  707.       execvp (cmd_argv[0], cmd_argv);
  708.       error (0, errno, "%s", cmd_argv[0]);
  709.       _exit (errno == ENOENT ? 127 : 126);
  710.     }
  711.       add_proc (child);
  712.       processes_executed++;
  713.     }
  714.  
  715.   --cmd_argc;            /* For the trailing null. */
  716.   while (--cmd_argc > cmd_initial_argc)
  717.     free (cmd_argv[cmd_argc]);
  718.   cmd_ncargs = cmd_initial_ncargs;
  719. }
  720.  
  721. /* If ALL is nonzero, wait for all child processes to finish;
  722.    otherwise, wait for one child process to finish.
  723.    Remove the processes that finish from the list of executing processes. */
  724.  
  725. static void
  726. wait_for_proc (all)
  727.      int all;
  728. {
  729.   struct pid_list **pl_prev;
  730.   struct pid_list *pl;
  731.   int pid;
  732.   int status;
  733.  
  734.   while (procs_executing)
  735.     {
  736.       do
  737.     {
  738.       pid = wait (&status);
  739.       if (pid < 0)
  740.         error (1, 0, "error waiting for child process");
  741.  
  742.       /* Find the entry in `pid_list' for the child process that exited. */
  743.       pl_prev = &pid_list;
  744.       pl = pid_list;
  745.       while (pl != 0 && pl->pl_pid != pid)
  746.         {
  747.           pl_prev = &pl->pl_next;
  748.           pl = pl->pl_next;
  749.         }
  750.     }
  751.       while (pl == 0);        /* A child died that we didn't start? */
  752.  
  753.       /* Remove the child from the list. */
  754.       *pl_prev = pl->pl_next;
  755.       free ((char *) pl);
  756.       procs_executing--;
  757.  
  758.       if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
  759.     exit (WEXITSTATUS (status)); /* Can't find or run the command. */
  760.       if (WEXITSTATUS (status) == 255)
  761.     error (124, 0, "%s: exited with status 255; aborting", cmd_argv[0]);
  762.       if (WIFSTOPPED (status))
  763.     error (125, 0, "%s: stopped by signal %d", cmd_argv[0], WSTOPSIG (status));
  764.       if (WIFSIGNALED (status))
  765.     error (125, 0, "%s: terminated by signal %d", cmd_argv[0], WTERMSIG (status));
  766.       if (WEXITSTATUS (status) != 0)
  767.     child_error = 123;
  768.  
  769.       if (!all)
  770.     break;
  771.     }
  772. }
  773.  
  774. /* Add the process with id PID to the list of processes that have
  775.    been executed. */
  776.  
  777. static void
  778. add_proc (pid)
  779.      int pid;
  780. {
  781.   struct pid_list *pl;
  782.  
  783.   pl = (struct pid_list *) xmalloc (sizeof (struct pid_list));
  784.   pl->pl_pid = pid;
  785.   pl->pl_next = pid_list;
  786.   pid_list = pl;
  787.   procs_executing++;
  788. }
  789.  
  790. /* Replace all instances of `replace_pat' in ARG with LINE, and add the
  791.    resulting string to the list of arguments for the command to execute.
  792.  
  793.    COMPAT: insertions on the SYSV version are limited to 255 chars per line,
  794.    and a max of 5 occurences of replace_pat in the initial-arguments.  These
  795.    restristrions do not exist here.  */
  796.  
  797. static void
  798. do_insert (arg, line)
  799.      char *arg;
  800.      char *line;
  801. {
  802.   char *p = insertbuf;
  803.   char *s;
  804.   int cnt = arg_max - 1;    /* Bytes left on the command line. */
  805.   int len;            /* Length of ARG before `replace_pat'. */
  806.   int ilen = strlen (replace_pat);
  807.   int llen = strlen (line);
  808.  
  809.   do
  810.     {
  811.       s = strstr (arg, replace_pat);
  812.       if (s)
  813.     len = s - arg;
  814.       else
  815.     len = strlen (arg);
  816.       cnt -= len;
  817.       if (cnt <= 0)
  818.     break;
  819.       strncpy (p, arg, len);
  820.       p += len;
  821.       arg += len;
  822.       if (s)
  823.     {
  824.       cnt -= llen;
  825.       if (cnt <= 0)
  826.         break;
  827.       strcpy (p, line);
  828.       arg += ilen;
  829.       p += llen;
  830.     }
  831.     }
  832.   while (*arg);
  833.   if (*arg)
  834.     error (1, 0, "command too long");
  835.   *p = '\0';
  836.   push_arg (insertbuf);
  837. }
  838.  
  839. /* Return how much of ARG_MAX is used by the environment. */
  840.  
  841. static long
  842. env_size (envp)
  843.      char **envp;
  844. {
  845.   long len = 0;
  846.  
  847.   while (*envp)
  848.     len += strlen (*envp++) + 1;
  849.  
  850.   return len;
  851. }
  852.  
  853. static void
  854. usage ()
  855. {
  856.   fprintf (stderr, "\
  857. Usage: %s [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]\n\
  858.        [-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]\n\
  859.        [--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]\n\
  860.        [--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]\n\
  861.        [--max-args=max-args] [--no-run-if-empty] [command [initial-arguments]]\n",
  862.        program_name);
  863.   exit (1);
  864. }
  865.