ip: code shrink
[oweals/busybox.git] / findutils / xargs.c
index 73b1a023755b00c32fb944dc656b305878e841a8..0d1bb43fc89a7e05ee06627342762f0149c1b0d2 100644 (file)
-/* xargs for busybox */
-
-/* xargs -- build and execute command lines from standard input
-   Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
-
-/* Written by Mike Rendell <michael@cs.mun.ca>
-   and David MacKenzie <djm@gnu.ai.mit.edu>.  */
-
-#include "internal.h"
-
-#define HAVE_STRING_H 1
-#define HAVE_SYS_WAIT_H 1
-#define HAVE_UNISTD_H 1
-#define HAVE_LIMITS_H 1
-#define STDC_HEADERS 1
-
-#include <sys/types.h>         /* For pid_t. */
-#if HAVE_SYS_WAIT_H
-#include <sys/wait.h>
-#endif
-
-#ifndef WIFSTOPPED
-#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
-#endif
-#ifndef WIFSIGNALED
-#define WIFSIGNALED(w) (((w) & 0xff) != 0x7f && ((w) & 0xff) != 0)
-#endif
-#ifndef WIFEXITED
-#define WIFEXITED(w) (((w) & 0xff) == 0)
-#endif
-
-#ifndef WSTOPSIG
-#define WSTOPSIG(w) (((w) >> 8) & 0xff)
-#endif
-#ifndef WTERMSIG
-#define WTERMSIG(w) ((w) & 0x7f)
-#endif
-#ifndef WEXITSTATUS
-#define WEXITSTATUS(w) (((w) >> 8) & 0xff)
-#endif
-
-#if __STDC__
-#define P_(s) s
-#else
-#define P_(s) ()
-#endif
-
-#include <ctype.h>
-
-#if !defined (isascii) || defined (STDC_HEADERS)
-#ifdef isascii
-#undef isascii
-#endif
-#define isascii(c) 1
-#endif
-
-#ifdef isblank
-#define ISBLANK(c) (isascii (c) && isblank (c))
-#else
-#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
-#endif
-
-#define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
-                   || (c) == '\f' || (c) == '\v')
-
-#include <stdio.h>
-#include <errno.h>
-#include <getopt.h>
-
-#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
-#include <string.h>
-#if !defined(STDC_HEADERS)
-#include <memory.h>
-#endif
-#else
-#include <strings.h>
-#define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
-#endif
-
-#ifndef _POSIX_SOURCE
-#include <sys/param.h>
-#endif
-
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#endif
-
-#ifndef LONG_MAX
-#define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini xargs implementation for busybox
+ *
+ * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
+ *
+ * Special thanks
+ * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
+ * - Mike Rendell <michael@cs.mun.ca>
+ * and David MacKenzie <djm@gnu.ai.mit.edu>.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ *
+ * xargs is described in the Single Unix Specification v3 at
+ * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
+ */
+
+//config:config XARGS
+//config:      bool "xargs"
+//config:      default y
+//config:      help
+//config:        xargs is used to execute a specified command for
+//config:        every item from standard input.
+//config:
+//config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
+//config:      bool "Enable -p: prompt and confirmation"
+//config:      default y
+//config:      depends on XARGS
+//config:      help
+//config:        Support -p: prompt the user whether to run each command
+//config:        line and read a line from the terminal.
+//config:
+//config:config FEATURE_XARGS_SUPPORT_QUOTES
+//config:      bool "Enable single and double quotes and backslash"
+//config:      default y
+//config:      depends on XARGS
+//config:      help
+//config:        Support quoting in the input.
+//config:
+//config:config FEATURE_XARGS_SUPPORT_TERMOPT
+//config:      bool "Enable -x: exit if -s or -n is exceeded"
+//config:      default y
+//config:      depends on XARGS
+//config:      help
+//config:        Support -x: exit if the command size (see the -s or -n option)
+//config:        is exceeded.
+//config:
+//config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
+//config:      bool "Enable -0: NUL-terminated input"
+//config:      default y
+//config:      depends on XARGS
+//config:      help
+//config:        Support -0: input items are terminated by a NUL character
+//config:        instead of whitespace, and the quotes and backslash
+//config:        are not special.
+
+//applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
+
+//kbuild:lib-$(CONFIG_XARGS) += xargs.o
+
+#include "libbb.h"
+
+/* This is a NOEXEC applet. Be very careful! */
+
+
+//#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
+#define dbg_msg(...) ((void)0)
+
+
+#ifdef TEST
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
+#  define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+#  define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
+#  define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+#  define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
+# endif
 #endif
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#include <signal.h>
-
-#if !defined(SIGCHLD) && defined(SIGCLD)
-#define SIGCHLD SIGCLD
-#endif
-
-/* COMPAT:  SYSV version defaults size (and has a max value of) to 470.
-   We try to make it as large as possible. */
-#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
-#define ARG_MAX sysconf (_SC_ARG_MAX)
-#endif
-#ifndef ARG_MAX
-#define ARG_MAX NCARGS
-#endif
-
-/* States for read_line. */
-#define NORM 0
-#define SPACE 1
-#define QUOTE 2
-#define BACKSLASH 3
-
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#else
-extern int errno;
-#endif
-
-/* Return nonzero if S is the EOF string.  */
-#define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
-
-extern char **environ;
-
-/* Not char because of type promotion; NeXT gcc can't handle it.  */
-typedef int boolean;
-#define                true    1
-#define                false   0
-
-#if __STDC__
-#define VOID void
-#else
-#define VOID char
-#endif
-
-VOID *xmalloc P_ ((size_t n));
-VOID *xrealloc P_ ((VOID * p, size_t n));
-
-/* The name this program was run with.  */
-char *program_name;
-
-/* Buffer for reading arguments from stdin.  */
-static char *linebuf;
-
-/* Line number in stdin since the last command was executed.  */
-static int lineno = 0;
-
-/* If nonzero, then instead of putting the args from stdin at
-   the end of the command argument list, they are each stuck into the
-   initial args, replacing each occurrence of the `replace_pat' in the
-   initial args.  */
-static char *replace_pat = NULL;
-
-/* The length of `replace_pat'.  */
-static size_t rplen = 0;
-
-/* If nonzero, when this string is read on stdin it is treated as
-   end of file.
-   I don't like this - it should default to NULL.  */
-static char *eof_str = "_";
-
-/* If nonzero, the maximum number of nonblank lines from stdin to use
-   per command line.  */
-static long lines_per_exec = 0;
-
-/* The maximum number of arguments to use per command line.  */
-static long args_per_exec = 1024;
-
-/* If true, exit if lines_per_exec or args_per_exec is exceeded.  */
-static boolean exit_if_size_exceeded = false;
-
-/* The maximum number of characters that can be used per command line.  */
-static long arg_max;
-
-/* Storage for elements of `cmd_argv'.  */
-static char *argbuf;
-
-/* The list of args being built.  */
-static char **cmd_argv = NULL;
-
-/* Number of elements allocated for `cmd_argv'.  */
-static int cmd_argv_alloc = 0;
-
-/* Number of valid elements in `cmd_argv'.  */
-static int cmd_argc = 0;
-
-/* Number of chars being used in `cmd_argv'.  */
-static int cmd_argv_chars = 0;
-
-/* Number of initial arguments given on the command line.  */
-static int initial_argc = 0;
-
-/* Number of chars in the initial args.  */
-static int initial_argv_chars = 0;
-
-/* true when building up initial arguments in `cmd_argv'.  */
-static boolean initial_args = true;
-
-/* If nonzero, the maximum number of child processes that can be running
-   at once.  */
-static int proc_max = 1;
-
-/* Total number of child processes that have been executed.  */
-static int procs_executed = 0;
 
-/* The number of elements in `pids'.  */
-static int procs_executing = 0;
+struct globals {
+       char **args;
+       const char *eof_str;
+       int idx;
+} FIX_ALIASING;
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { \
+       G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
+} while (0)
 
-/* List of child processes currently executing.  */
-static pid_t *pids = NULL;
 
-/* The number of allocated elements in `pids'. */
-static int pids_alloc = 0;
-
-/* Exit status; nonzero if any child process exited with a
-   status of 1-125.  */
-static int child_error = 0;
-
-/* If true, print each command on stderr before executing it.  */
-static boolean print_command = false;
-
-/* If true, query the user before executing each command, and only
-   execute the command if the user responds affirmatively.  */
-static boolean query_before_executing = false;
-
-static struct option const longopts[] =
+/*
+ * This function has special algorithm.
+ * Don't use fork and include to main!
+ */
+static int xargs_exec(void)
 {
-  {"null", no_argument, NULL, '0'},
-  {"eof", optional_argument, NULL, 'e'},
-  {"replace", optional_argument, NULL, 'i'},
-  {"max-lines", optional_argument, NULL, 'l'},
-  {"max-args", required_argument, NULL, 'n'},
-  {"interactive", no_argument, NULL, 'p'},
-  {"no-run-if-empty", no_argument, NULL, 'r'},
-  {"max-chars", required_argument, NULL, 's'},
-  {"verbose", no_argument, NULL, 't'},
-  {"exit", no_argument, NULL, 'x'},
-  {"max-procs", required_argument, NULL, 'P'},
-  {"help", no_argument, NULL, 'h'},
-  {NULL, no_argument, NULL, 0}
-};
+       int status;
 
-static int read_line P_ ((void));
-static int read_string P_ ((void));
-static void do_insert P_ ((char *arg, size_t arglen, size_t lblen));
-static void push_arg P_ ((char *arg, size_t len));
-static boolean print_args P_ ((boolean ask));
-static void do_exec P_ ((void));
-static void add_proc P_ ((pid_t pid));
-static void wait_for_proc P_ ((boolean all));
-static long parse_num P_ ((char *str, int option, long min, long max));
-static long env_size P_ ((char **envp));
-
-int xargs_main (argc, argv)
-     int argc;
-     char **argv;
-{
-  int optc;
-  int always_run_command = 1;
-  long orig_arg_max;
-  char *default_cmd = "/bin/echo";
-  int (*read_args) P_ ((void)) = read_line;
-
-  program_name = argv[0];
-
-  orig_arg_max = ARG_MAX;
-  if (orig_arg_max == -1)
-    orig_arg_max = LONG_MAX;
-  orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048.  */
-  arg_max = orig_arg_max;
-
-  /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
-     have it at 1 meg).  Things will work fine with a large ARG_MAX but it
-     will probably hurt the system more than it needs to; an array of this
-     size is allocated.  */
-  if (arg_max > 20 * 1024)
-    arg_max = 20 * 1024;
-
-  /* Take the size of the environment into account.  */
-  arg_max -= env_size (environ);
-  if (arg_max <= 0)
-    fatalError("environment is too large for exec");
-
-  while ((optc = getopt_long (argc, argv, "+0e::i::l::n:prs:txP:",
-                             longopts, (int *) 0)) != -1)
-    {
-      switch (optc)
-       {
-       case '0':
-         read_args = read_string;
-         break;
-
-       case 'e':
-         if (optarg)
-           eof_str = optarg;
-         else
-           eof_str = 0;
-         break;
-
-       case 'h':
-         usage (xargs_usage);
-
-       case 'i':
-         if (optarg)
-           replace_pat = optarg;
-         else
-           replace_pat = "{}";
-         /* -i excludes -n -l.  */
-         args_per_exec = 0;
-         lines_per_exec = 0;
-         break;
-
-       case 'l':
-         if (optarg)
-           lines_per_exec = parse_num (optarg, 'l', 1L, -1L);
-         else
-           lines_per_exec = 1;
-         /* -l excludes -i -n.  */
-         args_per_exec = 0;
-         replace_pat = NULL;
-         break;
-
-       case 'n':
-         args_per_exec = parse_num (optarg, 'n', 1L, -1L);
-         /* -n excludes -i -l.  */
-         lines_per_exec = 0;
-         replace_pat = NULL;
-         break;
-
-       case 's':
-         arg_max = parse_num (optarg, 's', 1L, orig_arg_max);
-         break;
-
-       case 't':
-         print_command = true;
-         break;
-
-       case 'x':
-         exit_if_size_exceeded = true;
-         break;
-
-       case 'p':
-         query_before_executing = true;
-         print_command = true;
-         break;
-
-       case 'r':
-         always_run_command = 0;
-         break;
-
-       case 'P':
-         proc_max = parse_num (optarg, 'P', 0L, -1L);
-         break;
-
-       default:
-         usage (xargs_usage);
+       status = spawn_and_wait(G.args);
+       if (status < 0) {
+               bb_simple_perror_msg(G.args[0]);
+               return errno == ENOENT ? 127 : 126;
        }
-    }
-
-  if (replace_pat || lines_per_exec)
-    exit_if_size_exceeded = true;
-
-  if (optind == argc)
-    {
-      optind = 0;
-      argc = 1;
-      argv = &default_cmd;
-    }
-
-  linebuf = (char *) xmalloc (arg_max + 1);
-  argbuf = (char *) xmalloc (arg_max + 1);
-
-  /* Make sure to listen for the kids.  */
-  signal (SIGCHLD, SIG_DFL);
-
-  if (!replace_pat)
-    {
-      for (; optind < argc; optind++)
-       push_arg (argv[optind], strlen (argv[optind]) + 1);
-      initial_args = false;
-      initial_argc = cmd_argc;
-      initial_argv_chars = cmd_argv_chars;
-
-      while ((*read_args) () != -1)
-       if (lines_per_exec && lineno >= lines_per_exec)
-         {
-           do_exec ();
-           lineno = 0;
-         }
-
-      /* SYSV xargs seems to do at least one exec, even if the
-         input is empty.  */
-      if (cmd_argc != initial_argc
-         || (always_run_command && procs_executed == 0))
-       do_exec ();
-    }
-  else
-    {
-      int i;
-      size_t len;
-      size_t *arglen = (size_t *) xmalloc (sizeof (size_t) * argc);
-
-      for (i = optind; i < argc; i++)
-       arglen[i] = strlen(argv[i]);
-      rplen = strlen (replace_pat);
-      while ((len = (*read_args) ()) != -1)
-       {
-         /* Don't do insert on the command name.  */
-         push_arg (argv[optind], arglen[optind] + 1);
-         len--;
-         for (i = optind + 1; i < argc; i++)
-           do_insert (argv[i], arglen[i], len);
-         do_exec ();
+       if (status == 255) {
+               bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
+               return 124;
        }
-    }
-
-  wait_for_proc (true);
-  exit (child_error);
+       if (status >= 0x180) {
+               bb_error_msg("%s: terminated by signal %d",
+                       G.args[0], status - 0x180);
+               return 125;
+       }
+       if (status)
+               return 123;
+       return 0;
 }
 
-/* Read a line of arguments from stdin and add them to the list of
-   arguments to pass to the command.  Ignore blank lines and initial blanks.
-   Single and double quotes and backslashes quote metacharacters and blanks
-   as they do in the shell.
-   Return -1 if eof (either physical or logical) is reached,
-   otherwise the length of the last string read (including the null).  */
+/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
+ * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
+ */
+#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
 
-static int
-read_line ()
+static void store_param(char *s)
 {
-  static boolean eof = false;
-  /* Start out in mode SPACE to always strip leading spaces (even with -i).  */
-  int state = SPACE;           /* The type of character we last read.  */
-  int prevc;                   /* The previous value of c.  */
-  int quotc = 0;               /* The last quote character read.  */
-  int c = EOF;
-  boolean first = true;                /* true if reading first arg on line.  */
-  int len;
-  char *p = linebuf;
-  /* Including the NUL, the args must not grow past this point.  */
-  char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
-
-  if (eof)
-    return -1;
-  while (1)
-    {
-      prevc = c;
-      c = getc (stdin);
-      if (c == EOF)
-       {
-         /* COMPAT: SYSV seems to ignore stuff on a line that
-            ends without a \n; we don't.  */
-         eof = true;
-         if (p == linebuf)
-           return -1;
-         *p++ = '\0';
-         len = p - linebuf;
-         /* FIXME we don't check for unterminated quotes here.  */
-         if (first && EOF_STR (linebuf))
-           return -1;
-         if (!replace_pat)
-           push_arg (linebuf, len);
-         return len;
+       /* Grow by 256 elements at once */
+       if (!(G.idx & 0xff)) { /* G.idx == N*256 */
+               /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
+               G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
        }
-      switch (state)
-       {
-       case SPACE:
-         if (ISSPACE (c))
-           continue;
-         state = NORM;
-         /* aaahhhh....  */
-
-       case NORM:
-         if (c == '\n')
-           {
-             if (!ISBLANK (prevc))
-               lineno++;       /* For -l.  */
-             if (p == linebuf)
-               {
-                 /* Blank line.  */
-                 state = SPACE;
-                 continue;
+       G.args[G.idx++] = s;
+}
+
+/* process[0]_stdin:
+ * Read characters into buf[n_max_chars+1], and when parameter delimiter
+ * is seen, store the address of a new parameter to args[].
+ * If reading discovers that last chars do not form the complete
+ * parameter, the pointer to the first such "tail character" is returned.
+ * (buf has extra byte at the end to accomodate terminating NUL
+ * of "tail characters" string).
+ * Otherwise, the returned pointer points to NUL byte.
+ * On entry, buf[] may contain some "seed chars" which are to become
+ * the beginning of the first parameter.
+ */
+
+#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
+{
+#define NORM      0
+#define QUOTE     1
+#define BACKSLASH 2
+#define SPACE     4
+       char q = '\0';             /* quote char */
+       char state = NORM;
+       char *s = buf;             /* start of the word */
+       char *p = s + strlen(buf); /* end of the word */
+
+       buf += n_max_chars;        /* past buffer's end */
+
+       /* "goto ret" is used instead of "break" to make control flow
+        * more obvious: */
+
+       while (1) {
+               int c = getchar();
+               if (c == EOF) {
+                       if (p != s)
+                               goto close_word;
+                       goto ret;
                }
-             *p++ = '\0';
-             len = p - linebuf;
-             if (EOF_STR (linebuf))
-               {
-                 eof = true;
-                 return first ? -1 : len;
+               if (state == BACKSLASH) {
+                       state = NORM;
+                       goto set;
                }
-             if (!replace_pat)
-               push_arg (linebuf, len);
-             return len;
-           }
-         if (!replace_pat && ISSPACE (c))
-           {
-             *p++ = '\0';
-             len = p - linebuf;
-             if (EOF_STR (linebuf))
-               {
-                 eof = true;
-                 return first ? -1 : len;
+               if (state == QUOTE) {
+                       if (c != q)
+                               goto set;
+                       q = '\0';
+                       state = NORM;
+               } else { /* if (state == NORM) */
+                       if (ISSPACE(c)) {
+                               if (p != s) {
+ close_word:
+                                       state = SPACE;
+                                       c = '\0';
+                                       goto set;
+                               }
+                       } else {
+                               if (c == '\\') {
+                                       state = BACKSLASH;
+                               } else if (c == '\'' || c == '"') {
+                                       q = c;
+                                       state = QUOTE;
+                               } else {
+ set:
+                                       *p++ = c;
+                               }
+                       }
+               }
+               if (state == SPACE) {   /* word's delimiter or EOF detected */
+                       if (q) {
+                               bb_error_msg_and_die("unmatched %s quote",
+                                       q == '\'' ? "single" : "double");
+                       }
+                       /* A full word is loaded */
+                       if (G.eof_str) {
+                               if (strcmp(s, G.eof_str) == 0) {
+                                       while (getchar() != EOF)
+                                               continue;
+                                       p = s;
+                                       goto ret;
+                               }
+                       }
+                       store_param(s);
+                       dbg_msg("args[]:'%s'", s);
+                       s = p;
+                       n_max_arg--;
+                       if (n_max_arg == 0) {
+                               goto ret;
+                       }
+                       state = NORM;
+               }
+               if (p == buf) {
+                       goto ret;
                }
-             push_arg (linebuf, len);
-             p = linebuf;
-             state = SPACE;
-             first = false;
-             continue;
-           }
-         switch (c)
-           {
-           case '\\':
-             state = BACKSLASH;
-             continue;
-
-           case '\'':
-           case '"':
-             state = QUOTE;
-             quotc = c;
-             continue;
-           }
-         break;
-
-       case QUOTE:
-         if (c == '\n')
-           fatalError ("unmatched %s quote", quotc == '"' ? "double" : "single");
-         if (c == quotc)
-           {
-             state = NORM;
-             continue;
-           }
-         break;
-
-       case BACKSLASH:
-         state = NORM;
-         break;
        }
-      if (p >= endbuf)
-       fatalError ("argument line too long");
-      *p++ = c;
-    }
+ ret:
+       *p = '\0';
+       /* store_param(NULL) - caller will do it */
+       dbg_msg("return:'%s'", s);
+       return s;
 }
+#else
+/* The variant does not support single quotes, double quotes or backslash */
+static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
+{
+       char *s = buf;             /* start of the word */
+       char *p = s + strlen(buf); /* end of the word */
 
-/* Read a null-terminated string from stdin and add it to the list of
-   arguments to pass to the command.
-   Return -1 if eof (either physical or logical) is reached,
-   otherwise the length of the string read (including the null).  */
+       buf += n_max_chars;        /* past buffer's end */
 
-static int
-read_string ()
-{
-  static boolean eof = false;
-  int len;
-  char *p = linebuf;
-  /* Including the NUL, the args must not grow past this point.  */
-  char *endbuf = linebuf + arg_max - initial_argv_chars - 1;
-
-  if (eof)
-    return -1;
-  while (1)
-    {
-      int c = getc (stdin);
-      if (c == EOF)
-       {
-         eof = true;
-         if (p == linebuf)
-           return -1;
-         *p++ = '\0';
-         len = p - linebuf;
-         if (!replace_pat)
-           push_arg (linebuf, len);
-         return len;
-       }
-      if (c == '\0')
-       {
-         lineno++;             /* For -l.  */
-         *p++ = '\0';
-         len = p - linebuf;
-         if (!replace_pat)
-           push_arg (linebuf, len);
-         return len;
+       while (1) {
+               int c = getchar();
+               if (c == EOF) {
+                       if (p == s)
+                               goto ret;
+               }
+               if (c == EOF || ISSPACE(c)) {
+                       if (p == s)
+                               continue;
+                       c = EOF;
+               }
+               *p++ = (c == EOF ? '\0' : c);
+               if (c == EOF) { /* word's delimiter or EOF detected */
+                       /* A full word is loaded */
+                       if (G.eof_str) {
+                               if (strcmp(s, G.eof_str) == 0) {
+                                       while (getchar() != EOF)
+                                               continue;
+                                       p = s;
+                                       goto ret;
+                               }
+                       }
+                       store_param(s);
+                       dbg_msg("args[]:'%s'", s);
+                       s = p;
+                       n_max_arg--;
+                       if (n_max_arg == 0) {
+                               goto ret;
+                       }
+               }
+               if (p == buf) {
+                       goto ret;
+               }
        }
-      if (p >= endbuf)
-       fatalError ("argument line too long");
-      *p++ = c;
-    }
+ ret:
+       *p = '\0';
+       /* store_param(NULL) - caller will do it */
+       dbg_msg("return:'%s'", s);
+       return s;
 }
+#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
 
-/* Replace all instances of `replace_pat' in ARG with `linebuf',
-   and add the resulting string to the list of arguments for the command
-   to execute.
-   ARGLEN is the length of ARG, not including the null.
-   LBLEN is the length of `linebuf', not including the null.
-
-   COMPAT: insertions on the SYSV version are limited to 255 chars per line,
-   and a max of 5 occurences of replace_pat in the initial-arguments.
-   Those restrictions do not exist here.  */
-
-static void
-do_insert (arg, arglen, lblen)
-     char *arg;
-     size_t arglen;
-     size_t lblen;
+#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
 {
-  /* Temporary copy of each arg with the replace pattern replaced by the
-     real arg.  */
-  static char *insertbuf;
-  char *p;
-  int bytes_left = arg_max - 1;        /* Bytes left on the command line.  */
-
-  if (!insertbuf)
-    insertbuf = (char *) xmalloc (arg_max + 1);
-  p = insertbuf;
-
-  do
-    {
-      size_t len;              /* Length in ARG before `replace_pat'.  */
-      char *s = strstr (arg, replace_pat);
-      if (s)
-       len = s - arg;
-      else
-       len = arglen;
-      bytes_left -= len;
-      if (bytes_left <= 0)
-       break;
-
-      strncpy (p, arg, len);
-      p += len;
-      arg += len;
-      arglen -= len;
-
-      if (s)
-       {
-         bytes_left -= lblen;
-         if (bytes_left <= 0)
-           break;
-         strcpy (p, linebuf);
-         arg += rplen;
-         arglen -= rplen;
-         p += lblen;
-       }
-    }
-  while (*arg);
-  if (*arg)
-    fatalError ("command too long");
-  *p++ = '\0';
-  push_arg (insertbuf, p - insertbuf);
-}
+       char *s = buf;             /* start of the word */
+       char *p = s + strlen(buf); /* end of the word */
 
-/* Add ARG to the end of the list of arguments `cmd_argv' to pass
-   to the command.
-   LEN is the length of ARG, including the terminating null.
-   If this brings the list up to its maximum size, execute the command.  */
+       buf += n_max_chars;        /* past buffer's end */
 
-static void
-push_arg (arg, len)
-     char *arg;
-     size_t len;
-{
-  if (arg)
-    {
-      if (cmd_argv_chars + len > arg_max)
-       {
-         if (initial_args || cmd_argc == initial_argc)
-           fatalError ("can not fit single argument within argument list size limit");
-         if (replace_pat
-             || (exit_if_size_exceeded &&
-                 (lines_per_exec || args_per_exec)))
-           fatalError ("argument list too long");
-         do_exec ();
-       }
-      if (!initial_args && args_per_exec &&
-         cmd_argc - initial_argc == args_per_exec)
-       do_exec ();
-    }
-
-  if (cmd_argc >= cmd_argv_alloc)
-    {
-      if (!cmd_argv)
-       {
-         cmd_argv_alloc = 64;
-         cmd_argv = (char **) xmalloc (sizeof (char *) * cmd_argv_alloc);
-       }
-      else
-       {
-         cmd_argv_alloc *= 2;
-         cmd_argv = (char **) xrealloc (cmd_argv,
-                                        sizeof (char *) * cmd_argv_alloc);
+       while (1) {
+               int c = getchar();
+               if (c == EOF) {
+                       if (p == s)
+                               goto ret;
+                       c = '\0';
+               }
+               *p++ = c;
+               if (c == '\0') {   /* word's delimiter or EOF detected */
+                       /* A full word is loaded */
+                       store_param(s);
+                       dbg_msg("args[]:'%s'", s);
+                       s = p;
+                       n_max_arg--;
+                       if (n_max_arg == 0) {
+                               goto ret;
+                       }
+               }
+               if (p == buf) {
+                       goto ret;
+               }
        }
-    }
-
-  if (!arg)
-    cmd_argv[cmd_argc++] = NULL;
-  else
-    {
-      cmd_argv[cmd_argc++] = argbuf + cmd_argv_chars;
-      strcpy (argbuf + cmd_argv_chars, arg);
-      cmd_argv_chars += len;
-    }
+ ret:
+       *p = '\0';
+       /* store_param(NULL) - caller will do it */
+       dbg_msg("return:'%s'", s);
+       return s;
 }
+#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
 
-/* Print the arguments of the command to execute.
-   If ASK is nonzero, prompt the user for a response, and
+#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
+/* Prompt the user for a response, and
    if the user responds affirmatively, return true;
-   otherwise, return false.  */
-
-static boolean
-print_args (ask)
-     boolean ask;
+   otherwise, return false. Uses "/dev/tty", not stdin. */
+static int xargs_ask_confirmation(void)
 {
-  int i;
-
-  for (i = 0; i < cmd_argc - 1; i++)
-    fprintf (stderr, "%s ", cmd_argv[i]);
-  if (ask)
-    {
-      static FILE *tty_stream;
-      int c, savec;
-
-      if (!tty_stream)
-       {
-         tty_stream = fopen ("/dev/tty", "r");
-         if (!tty_stream)
-           fatalError (" Could not open /dev/tty");
-       }
-      fputs ("?...", stderr);
-      fflush (stderr);
-      c = savec = getc (tty_stream);
-      while (c != EOF && c != '\n')
-       c = getc (tty_stream);
-      if (savec == 'y' || savec == 'Y')
-       return true;
-    }
-  else
-    putc ('\n', stderr);
-
-  return false;
+       FILE *tty_stream;
+       int c, savec;
+
+       tty_stream = xfopen_for_read(CURRENT_TTY);
+       fputs(" ?...", stderr);
+       fflush_all();
+       c = savec = getc(tty_stream);
+       while (c != EOF && c != '\n')
+               c = getc(tty_stream);
+       fclose(tty_stream);
+       return (savec == 'y' || savec == 'Y');
 }
+#else
+# define xargs_ask_confirmation() 1
+#endif
 
-/* Execute the command that has been built in `cmd_argv'.  This may involve
-   waiting for processes that were previously executed.  */
+//usage:#define xargs_trivial_usage
+//usage:       "[OPTIONS] [PROG ARGS]"
+//usage:#define xargs_full_usage "\n\n"
+//usage:       "Run PROG on every item given by stdin\n"
+//usage:       IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
+//usage:     "\n       -p      Ask user whether to run each command"
+//usage:       )
+//usage:     "\n       -r      Don't run command if input is empty"
+//usage:       IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
+//usage:     "\n       -0      Input is separated by NUL characters"
+//usage:       )
+//usage:     "\n       -t      Print the command on stderr before execution"
+//usage:     "\n       -e[STR] STR stops input processing"
+//usage:     "\n       -n N    Pass no more than N args to PROG"
+//usage:     "\n       -s N    Pass command line of no more than N bytes"
+//usage:       IF_FEATURE_XARGS_SUPPORT_TERMOPT(
+//usage:     "\n       -x      Exit if size is exceeded"
+//usage:       )
+//usage:#define xargs_example_usage
+//usage:       "$ ls | xargs gzip\n"
+//usage:       "$ find . -name '*.c' -print | xargs rm\n"
+
+/* Correct regardless of combination of CONFIG_xxx */
+enum {
+       OPTBIT_VERBOSE = 0,
+       OPTBIT_NO_EMPTY,
+       OPTBIT_UPTO_NUMBER,
+       OPTBIT_UPTO_SIZE,
+       OPTBIT_EOF_STRING,
+       OPTBIT_EOF_STRING1,
+       IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
+       IF_FEATURE_XARGS_SUPPORT_TERMOPT(     OPTBIT_TERMINATE  ,)
+       IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(   OPTBIT_ZEROTERM   ,)
+
+       OPT_VERBOSE     = 1 << OPTBIT_VERBOSE    ,
+       OPT_NO_EMPTY    = 1 << OPTBIT_NO_EMPTY   ,
+       OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
+       OPT_UPTO_SIZE   = 1 << OPTBIT_UPTO_SIZE  ,
+       OPT_EOF_STRING  = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
+       OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
+       OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
+       OPT_TERMINATE   = IF_FEATURE_XARGS_SUPPORT_TERMOPT(     (1 << OPTBIT_TERMINATE  )) + 0,
+       OPT_ZEROTERM    = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(   (1 << OPTBIT_ZEROTERM   )) + 0,
+};
+#define OPTION_STR "+trn:s:e::E:" \
+       IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
+       IF_FEATURE_XARGS_SUPPORT_TERMOPT(     "x") \
+       IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(   "0")
 
-static void
-do_exec ()
+int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int xargs_main(int argc, char **argv)
 {
-  pid_t child;
-
-  push_arg ((char *) NULL, 0); /* Null terminate the arg list.  */
-  if (!query_before_executing || print_args (true))
-    {
-      if (proc_max && procs_executing >= proc_max)
-       wait_for_proc (false);
-      if (!query_before_executing && print_command)
-       print_args (false);
-      /* If we run out of processes, wait for a child to return and
-         try again.  */
-      while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
-       wait_for_proc (false);
-      switch (child)
-       {
-       case -1:
-         fatalError ("cannot fork");
-
-       case 0:         /* Child.  */
-         execvp (cmd_argv[0], cmd_argv);
-         errorMsg ("failed to exec '%s'", cmd_argv[0]);
-         _exit (errno == ENOENT ? 127 : 126);
-       }
-      add_proc (child);
-    }
+       int i;
+       int child_error = 0;
+       char *max_args;
+       char *max_chars;
+       char *buf;
+       unsigned opt;
+       int n_max_chars;
+       int n_max_arg;
+#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+       char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
+#else
+#define read_args process_stdin
+#endif
 
-  cmd_argc = initial_argc;
-  cmd_argv_chars = initial_argv_chars;
-}
+       INIT_G();
 
-/* Add the process with id PID to the list of processes that have
-   been executed.  */
+#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
+       /* For example, Fedora's build system uses --no-run-if-empty */
+       applet_long_options =
+               "no-run-if-empty\0" No_argument "r"
+               ;
+#endif
+       opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &G.eof_str, &G.eof_str);
+
+       /* -E ""? You may wonder why not just omit -E?
+        * This is used for portability:
+        * old xargs was using "_" as default for -E / -e */
+       if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
+               G.eof_str = NULL;
+
+       if (opt & OPT_ZEROTERM)
+               IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
+
+       argv += optind;
+       argc -= optind;
+       if (!argv[0]) {
+               /* default behavior is to echo all the filenames */
+               *--argv = (char*)"echo";
+               argc++;
+       }
 
-static void
-add_proc (pid)
-     pid_t pid;
-{
-  int i;
-
-  /* Find an empty slot.  */
-  for (i = 0; i < pids_alloc && pids[i]; i++)
-    ;
-  if (i == pids_alloc)
-    {
-      if (pids_alloc == 0)
+       /* -s NUM default. fileutils-4.4.2 uses 128k, but I heasitate
+        * to use such a big value - first need to change code to use
+        * growable buffer instead of fixed one.
+        */
+       n_max_chars = 32 * 1024;
+       /* Make smaller if system does not allow our default value.
+        * The Open Group Base Specifications Issue 6:
+        * "The xargs utility shall limit the command line length such that
+        * when the command line is invoked, the combined argument
+        * and environment lists (see the exec family of functions
+        * in the System Interfaces volume of IEEE Std 1003.1-2001)
+        * shall not exceed {ARG_MAX}-2048 bytes".
+        */
        {
-         pids_alloc = proc_max ? proc_max : 64;
-         pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
+               long arg_max = 0;
+#if defined _SC_ARG_MAX
+               arg_max = sysconf(_SC_ARG_MAX) - 2048;
+#elif defined ARG_MAX
+               arg_max = ARG_MAX - 2048;
+#endif
+               if (arg_max > 0 && n_max_chars > arg_max)
+                       n_max_chars = arg_max;
        }
-      else
+       if (opt & OPT_UPTO_SIZE) {
+               n_max_chars = xatou_range(max_chars, 1, INT_MAX);
+       }
+       /* Account for prepended fixed arguments */
        {
-         pids_alloc *= 2;
-         pids = (pid_t *) xrealloc (pids,
-                                    sizeof (pid_t) * pids_alloc);
+               size_t n_chars = 0;
+               for (i = 0; argv[i]; i++) {
+                       n_chars += strlen(argv[i]) + 1;
+               }
+               n_max_chars -= n_chars;
+       }
+       /* Sanity check */
+       if (n_max_chars <= 0) {
+               bb_error_msg_and_die("can't fit single argument within argument list size limit");
        }
-      memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
-    }
-  pids[i] = pid;
-  procs_executing++;
-  procs_executed++;
-}
 
-/* If ALL is true, wait for all child processes to finish;
-   otherwise, wait for one child process to finish.
-   Remove the processes that finish from the list of executing processes.  */
+       buf = xzalloc(n_max_chars + 1);
 
-static void
-wait_for_proc (all)
-     boolean all;
-{
-  while (procs_executing)
-    {
-      int i, status;
+       n_max_arg = n_max_chars;
+       if (opt & OPT_UPTO_NUMBER) {
+               n_max_arg = xatou_range(max_args, 1, INT_MAX);
+               /* Not necessary, we use growable args[]: */
+               /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
+       }
 
-      do
-       {
-         pid_t pid;
+       /* Allocate pointers for execvp */
+       /* We can statically allocate (argc + n_max_arg + 1) elements
+        * and do not bother with resizing args[], but on 64-bit machines
+        * this results in args[] vector which is ~8 times bigger
+        * than n_max_chars! That is, with n_max_chars == 20k,
+        * args[] will take 160k (!), which will most likely be
+        * almost entirely unused.
+        */
+       /* See store_param() for matching 256-step growth logic */
+       G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
+
+       /* Store the command to be executed, part 1 */
+       for (i = 0; argv[i]; i++)
+               G.args[i] = argv[i];
+
+       while (1) {
+               char *rem;
+
+               G.idx = argc;
+               rem = read_args(n_max_chars, n_max_arg, buf);
+               store_param(NULL);
+
+               if (!G.args[argc]) {
+                       if (*rem != '\0')
+                               bb_error_msg_and_die("argument line too long");
+                       if (opt & OPT_NO_EMPTY)
+                               break;
+               }
+               opt |= OPT_NO_EMPTY;
+
+               if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
+                       const char *fmt = " %s" + 1;
+                       char **args = G.args;
+                       for (i = 0; args[i]; i++) {
+                               fprintf(stderr, fmt, args[i]);
+                               fmt = " %s";
+                       }
+                       if (!(opt & OPT_INTERACTIVE))
+                               bb_putchar_stderr('\n');
+               }
 
-         pid = wait (&status);
-         if (pid < 0)
-           fatalError ("error waiting for child process");
+               if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
+                       child_error = xargs_exec();
+               }
 
-         /* Find the entry in `pids' for the child process
-            that exited.  */
-         for (i = 0; i < pids_alloc && pid != pids[i]; i++)
-           ;
+               if (child_error > 0 && child_error != 123) {
+                       break;
+               }
+
+               overlapping_strcpy(buf, rem);
+       } /* while */
+
+       if (ENABLE_FEATURE_CLEAN_UP) {
+               free(G.args);
+               free(buf);
        }
-      while (i == pids_alloc); /* A child died that we didn't start? */
-
-      /* Remove the child from the list.  */
-      pids[i] = 0;
-      procs_executing--;
-
-      if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
-       exit (WEXITSTATUS (status));    /* Can't find or run the command.  */
-      if (WEXITSTATUS (status) == 255)
-       fatalError ( "%s: exited with status 255; aborting", cmd_argv[0]);
-      if (WIFSTOPPED (status))
-       fatalError ( "%s: stopped by signal %d", cmd_argv[0], WSTOPSIG (status));
-      if (WIFSIGNALED (status))
-       fatalError ("%s: terminated by signal %d", cmd_argv[0], WTERMSIG (status));
-      if (WEXITSTATUS (status) != 0)
-       child_error = 123;
-
-      if (!all)
-       break;
-    }
-}
 
-/* Return the value of the number represented in STR.
-   OPTION is the command line option to which STR is the argument.
-   If the value does not fall within the boundaries MIN and MAX,
-   Print an error message mentioning OPTION and exit.  */
-
-static long
-parse_num (str, option, min, max)
-     char *str;
-     int option;
-     long min;
-     long max;
-{
-  char *eptr;
-  long val;
-
-  val = strtol (str, &eptr, 10);
-  if (eptr == str || *eptr)
-    {
-      fprintf (stderr, "%s: invalid number for -%c option\n",
-              program_name, option);
-      usage (xargs_usage);
-    }
-  else if (val < min)
-    {
-      fprintf (stderr, "%s: value for -%c option must be >= %ld\n",
-              program_name, option, min);
-      usage (xargs_usage);
-    }
-  else if (max >= 0 && val > max)
-    {
-      fprintf (stderr, "%s: value for -%c option must be < %ld\n",
-              program_name, option, max);
-      usage (xargs_usage);
-    }
-  return val;
+       return child_error;
 }
 
-/* Return how much of ARG_MAX is used by the environment.  */
 
-static long
-env_size (envp)
-     char **envp;
-{
-  long len = 0;
+#ifdef TEST
 
-  while (*envp)
-    len += strlen (*envp++) + 1;
+const char *applet_name = "debug stuff usage";
 
-  return len;
+void bb_show_usage(void)
+{
+       fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
+               applet_name);
+       exit(EXIT_FAILURE);
 }
 
+int main(int argc, char **argv)
+{
+       return xargs_main(argc, argv);
+}
+#endif /* TEST */