home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / gnu / findutils-4.1-src.lha / findutils-4.1 / xargs / xargs.c < prev   
C/C++ Source or Header  |  1994-11-12  |  22KB  |  919 lines

  1. /* xargs -- build and execute command lines from standard input
  2.    Copyright (C) 1990, 91, 92, 93, 94 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. /* Written by Mike Rendell <michael@cs.mun.ca>
  19.    and David MacKenzie <djm@gnu.ai.mit.edu>.  */
  20.  
  21. #include <config.h>
  22.  
  23. #if __STDC__
  24. #define P_(s) s
  25. #else
  26. #define P_(s) ()
  27. #endif
  28.  
  29. #define _GNU_SOURCE
  30. #include <ctype.h>
  31.  
  32. #if !defined (isascii) || defined (STDC_HEADERS)
  33. #ifdef isascii
  34. #undef isascii
  35. #endif
  36. #define isascii(c) 1
  37. #endif
  38.  
  39. #ifdef isblank
  40. #define ISBLANK(c) (isascii (c) && isblank (c))
  41. #else
  42. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  43. #endif
  44.  
  45. #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
  46.             || (c) == '\f' || (c) == '\v')
  47.  
  48. #include <sys/types.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <getopt.h>
  52.  
  53. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  54. #include <string.h>
  55. #if !defined(STDC_HEADERS)
  56. #include <memory.h>
  57. #endif
  58. #else
  59. #include <strings.h>
  60. #define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
  61. #endif
  62.  
  63. char *strstr ();
  64. char *strdup ();
  65.  
  66. #ifndef _POSIX_SOURCE
  67. #include <sys/param.h>
  68. #endif
  69.  
  70. #ifdef HAVE_LIMITS_H
  71. #include <limits.h>
  72. #endif
  73.  
  74. #ifdef HAVE_UNISTD_H
  75. #include <unistd.h>
  76. #endif
  77.  
  78. #include <signal.h>
  79.  
  80. #if !defined(SIGCHLD) && defined(SIGCLD)
  81. #define SIGCHLD SIGCLD
  82. #endif
  83.  
  84. /* COMPAT:  SYSV version defaults size (and has a max value of) to 470.
  85.    We try to make it as large as possible. */
  86. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  87. #define ARG_MAX sysconf (_SC_ARG_MAX)
  88. #endif
  89. #ifndef ARG_MAX
  90. #define ARG_MAX NCARGS
  91. #endif
  92.  
  93. #include "wait.h"
  94.  
  95. /* States for read_line. */
  96. #define NORM 0
  97. #define SPACE 1
  98. #define QUOTE 2
  99. #define BACKSLASH 3
  100.  
  101. #ifdef STDC_HEADERS
  102. #include <stdlib.h>
  103. #else
  104. char *malloc ();
  105. void exit ();
  106. void free ();
  107. long strtol ();
  108.  
  109. extern int errno;
  110. #endif
  111.  
  112. /* Return nonzero if S is the EOF string.  */
  113. #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
  114.  
  115. extern char **environ;
  116.  
  117. /* Not char because of type promotion; NeXT gcc can't handle it.  */
  118. typedef int boolean;
  119. #define        true    1
  120. #define        false    0
  121.  
  122. #if __STDC__
  123. #define VOID void
  124. #else
  125. #define VOID char
  126. #endif
  127.  
  128. VOID *xmalloc P_ ((size_t n));
  129. VOID *xrealloc P_ ((VOID * p, size_t n));
  130. void error P_ ((int status, int errnum, char *message,...));
  131.  
  132. extern char *version_string;
  133.  
  134. /* The name this program was run with.  */
  135. char *program_name;
  136.  
  137. /* Buffer for reading arguments from stdin.  */
  138. static char *linebuf;
  139.  
  140. /* Line number in stdin since the last command was executed.  */
  141. static int lineno = 0;
  142.  
  143. /* If nonzero, then instead of putting the args from stdin at
  144.    the end of the command argument list, they are each stuck into the
  145.    initial args, replacing each occurrence of the `replace_pat' in the
  146.    initial args.  */
  147. static char *replace_pat = NULL;
  148.  
  149. /* The length of `replace_pat'.  */
  150. static size_t rplen = 0;
  151.  
  152. /* If nonzero, when this string is read on stdin it is treated as
  153.    end of file.
  154.    I don't like this - it should default to NULL.  */
  155. static char *eof_str = "_";
  156.  
  157. /* If nonzero, the maximum number of nonblank lines from stdin to use
  158.    per command line.  */
  159. static long lines_per_exec = 0;
  160.  
  161. /* The maximum number of arguments to use per command line.  */
  162. static long args_per_exec = 1024;
  163.  
  164. /* If true, exit if lines_per_exec or args_per_exec is exceeded.  */
  165. static boolean exit_if_size_exceeded = false;
  166.  
  167. /* The maximum number of characters that can be used per command line.  */
  168. static long arg_max;
  169.  
  170. /* Storage for elements of `cmd_argv'.  */
  171. static char *argbuf;
  172.  
  173. /* The list of args being built.  */
  174. static char **cmd_argv = NULL;
  175.  
  176. /* Number of elements allocated for `cmd_argv'.  */
  177. static int cmd_argv_alloc = 0;
  178.  
  179. /* Number of valid elements in `cmd_argv'.  */
  180. static int cmd_argc = 0;
  181.  
  182. /* Number of chars being used in `cmd_argv'.  */
  183. static int cmd_argv_chars = 0;
  184.  
  185. /* Number of initial arguments given on the command line.  */
  186. static int initial_argc = 0;
  187.  
  188. /* Number of chars in the initial args.  */
  189. static int initial_argv_chars = 0;
  190.  
  191. /* true when building up initial arguments in `cmd_argv'.  */
  192. static boolean initial_args = true;
  193.  
  194. /* If nonzero, the maximum number of child processes that can be running
  195.    at once.  */
  196. static int proc_max = 1;
  197.  
  198. /* Total number of child processes that have been executed.  */
  199. static int procs_executed = 0;
  200.  
  201. /* The number of elements in `pids'.  */
  202. static int procs_executing = 0;
  203.  
  204. /* List of child processes currently executing.  */
  205. static pid_t *pids = NULL;
  206.  
  207. /* The number of allocated elements in `pids'. */
  208. static int pids_alloc = 0;
  209.  
  210. /* Exit status; nonzero if any child process exited with a
  211.    status of 1-125.  */
  212. static int child_error = 0;
  213.  
  214. /* If true, print each command on stderr before executing it.  */
  215. static boolean print_command = false;
  216.  
  217. /* If true, query the user before executing each command, and only
  218.    execute the command if the user responds affirmatively.  */
  219. static boolean query_before_executing = false;
  220.  
  221. static struct option const longopts[] =
  222. {
  223.   {"null", no_argument, NULL, '0'},
  224.   {"eof", optional_argument, NULL, 'e'},
  225.   {"replace", optional_argument, NULL, 'i'},
  226.   {"max-lines", optional_argument, NULL, 'l'},
  227.   {"max-args", required_argument, NULL, 'n'},
  228.   {"interactive", no_argument, NULL, 'p'},
  229.   {"no-run-if-empty", no_argument, NULL, 'r'},
  230.   {"max-chars", required_argument, NULL, 's'},
  231.   {"verbose", no_argument, NULL, 't'},
  232.   {"exit", no_argument, NULL, 'x'},
  233.   {"max-procs", required_argument, NULL, 'P'},
  234.   {"version", no_argument, NULL, 'v'},
  235.   {"help", no_argument, NULL, 'h'},
  236.   {NULL, no_argument, NULL, 0}
  237. };
  238.  
  239. static int read_line P_ ((void));
  240. static int read_string P_ ((void));
  241. static void do_insert P_ ((char *arg, size_t arglen, size_t lblen));
  242. static void push_arg P_ ((char *arg, size_t len));
  243. static boolean print_args P_ ((boolean ask));
  244. static void do_exec P_ ((void));
  245. static void add_proc P_ ((pid_t pid));
  246. static void wait_for_proc P_ ((boolean all));
  247. static long parse_num P_ ((char *str, int option, long min, long max));
  248. static long env_size P_ ((char **envp));
  249. static void usage P_ ((FILE * stream, int status));
  250.  
  251. int
  252. main (argc, argv)
  253.      int argc;
  254.      char **argv;
  255. {
  256.   int optc;
  257.   int always_run_command = 1;
  258.   long orig_arg_max;
  259.   char *default_cmd = "/bin/echo";
  260.   int (*read_args) P_ ((void)) = read_line;
  261.  
  262.   program_name = argv[0];
  263.  
  264.   orig_arg_max = ARG_MAX - 2048; /* POSIX.2 requires subtracting 2048.  */
  265.   arg_max = orig_arg_max;
  266.  
  267.   /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  268.      have it at 1 meg).  Things will work fine with a large ARG_MAX but it
  269.      will probably hurt the system more than it needs to; an array of this
  270.      size is allocated.  */
  271.   if (arg_max > 20 * 1024)
  272.     arg_max = 20 * 1024;
  273.  
  274.   /* Take the size of the environment into account.  */
  275.   arg_max -= env_size (environ);
  276.   if (arg_max <= 0)
  277.     error (1, 0, "environment is too large for exec");
  278.  
  279.   while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
  280.                   longopts, (int *) 0)) != -1)
  281.     {
  282.       switch (optc)
  283.     {
  284.     case '0':
  285.       read_args = read_string;
  286.       break;
  287.  
  288.     case 'e':
  289.       if (optarg)
  290.         eof_str = optarg;
  291.       else
  292.         eof_str = 0;
  293.       break;
  294.  
  295.     case 'h':
  296.       usage (stdout, 0);
  297.  
  298.     case 'i':
  299.       if (optarg)
  300.         replace_pat = optarg;
  301.       else
  302.         replace_pat = "{}";
  303.       /* -i excludes -n -l.  */
  304.       args_per_exec = 0;
  305.       lines_per_exec = 0;
  306.       break;
  307.  
  308.     case 'l':
  309.       if (optarg)
  310.         lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
  311.       else
  312.         lines_per_exec = 1;
  313.       /* -l excludes -i -n.  */
  314.       args_per_exec = 0;
  315.       r