toybox/toys/posix/xargs.c
<<
>>
Prefs
   1/* xargs.c - Run command with arguments taken from stdin.
   2 *
   3 * Copyright 2011 Rob Landley <rob@landley.net>
   4 *
   5 * See http://opengroup.org/onlinepubs/9699919799/utilities/xargs.html
   6 *
   7 * TODO: Rich's whitespace objection, env size isn't fixed anymore.
   8
   9USE_XARGS(NEWTOY(xargs, "^I:E:L#ptxrn#<1s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
  10
  11config XARGS
  12  bool "xargs"
  13  default y
  14  help
  15    usage: xargs [-ptxr0] [-s NUM] [-n NUM] [-L NUM] [-E STR] COMMAND...
  16
  17    Run command line one or more times, appending arguments from stdin.
  18
  19    If command exits with 255, don't launch another even if arguments remain.
  20
  21    -s  Size in bytes per command line
  22    -n  Max number of arguments per command
  23    -0  Each argument is NULL terminated, no whitespace or quote processing
  24    #-p Prompt for y/n from tty before running each command
  25    #-t Trace, print command line to stderr
  26    #-x Exit if can't fit everything in one command
  27    #-r Don't run command with empty input
  28    #-L Max number of lines of input per command
  29    -E  stop at line matching string
  30
  31config XARGS_PEDANTIC
  32  bool "TODO xargs pedantic posix compatability"
  33  default n
  34  depends on XARGS
  35  help
  36    This version supports insane posix whitespace handling rendered obsolete
  37    by -0 mode.
  38*/
  39
  40#define FOR_xargs
  41#include "toys.h"
  42
  43GLOBALS(
  44  long s, n, L;
  45  char *E, *I;
  46
  47  long entries, bytes;
  48  char delim;
  49)
  50
  51// If out==NULL count TT.bytes and TT.entries, stopping at max.
  52// Otherwise, fill out out[]
  53
  54// Returning NULL means need more data.
  55// Returning char * means hit data limits, start of data left over
  56// Returning 1 means hit data limits, but consumed all data
  57// Returning 2 means hit -E STR
  58
  59static char *handle_entries(char *data, char **entry)
  60{
  61  if (TT.delim) {
  62    char *s = data;
  63
  64    // Chop up whitespace delimited string into args
  65    while (*s) {
  66      char *save;
  67
  68      while (isspace(*s)) {
  69        if (entry) *s = 0;
  70        s++;
  71      }
  72
  73      if (TT.n && TT.entries >= TT.n)
  74        return *s ? s : (char *)1;
  75
  76      if (!*s) break;
  77      save = s;
  78
  79      TT.bytes += sizeof(char *);
  80
  81      for (;;) {
  82        if (++TT.bytes >= TT.s && TT.s) return save;
  83        if (!*s || isspace(*s)) break;
  84        s++;
  85      }
  86      if (TT.E) {
  87        int len = s-save;
  88        if (len == strlen(TT.E) && !strncmp(save, TT.E, len))
  89          return (char *)2;
  90      }
  91      if (entry) entry[TT.entries] = save;
  92      ++TT.entries;
  93    }
  94
  95  // -0 support
  96  } else {
  97    TT.bytes += sizeof(char *)+strlen(data)+1;
  98    if (TT.s && TT.bytes >= TT.s) return data;
  99    if (TT.n && TT.entries >= TT.n) return data;
 100    if (entry) entry[TT.entries] = data;
 101    TT.entries++;
 102  }
 103
 104  return NULL;
 105}
 106
 107void xargs_main(void)
 108{
 109  struct double_list *dlist = NULL, *dtemp;
 110  int entries, bytes, done = 0, status;
 111  char *data = NULL, **out;
 112  pid_t pid;
 113  long posix_max_bytes;
 114
 115  // POSIX requires that we never hit the ARG_MAX limit, even if we try to
 116  // with -s. POSIX also says we have to reserve 2048 bytes "to guarantee
 117  // that the invoked utility has room to modify its environment variables
 118  // and command line arguments and still be able to invoke another utility",
 119  // though obviously that's not really something you can guarantee.
 120  posix_max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes() - 2048;
 121  if (!TT.s || TT.s > posix_max_bytes) TT.s = posix_max_bytes;
 122
 123  if (!(toys.optflags & FLAG_0)) TT.delim = '\n';
 124
 125  // If no optargs, call echo.
 126  if (!toys.optc) {
 127    free(toys.optargs);
 128    *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
 129    toys.optc = 1;
 130  }
 131
 132  for (entries = 0, bytes = -1; entries < toys.optc; entries++, bytes++)
 133    bytes += strlen(toys.optargs[entries]);
 134
 135  // Loop through exec chunks.
 136  while (data || !done) {
 137    TT.entries = 0;
 138    TT.bytes = bytes;
 139
 140    // Loop reading input
 141    for (;;) {
 142
 143      // Read line
 144      if (!data) {
 145        ssize_t l = 0;
 146        l = getdelim(&data, (size_t *)&l, TT.delim, stdin);
 147
 148        if (l<0) {
 149          data = 0;
 150          done++;
 151          break;
 152        }
 153      }
 154      dlist_add(&dlist, data);
 155
 156      // Count data used
 157      data = handle_entries(data, NULL);
 158      if (!data) continue;
 159      if (data == (char *)2) done++;
 160      if ((long)data <= 2) data = 0;
 161      else data = xstrdup(data);
 162
 163      break;
 164    }
 165
 166    // Accumulate cally thing
 167
 168    if (data && !TT.entries) error_exit("argument too long");
 169    out = xzalloc((entries+TT.entries+1)*sizeof(char *));
 170
 171    // Fill out command line to exec
 172    memcpy(out, toys.optargs, entries*sizeof(char *));
 173    TT.entries = 0;
 174    TT.bytes = bytes;
 175    if (dlist) dlist->prev->next = 0;
 176    for (dtemp = dlist; dtemp; dtemp = dtemp->next)
 177      handle_entries(dtemp->data, out+entries);
 178
 179    if (!(pid = XVFORK())) {
 180      xclose(0);
 181      open("/dev/null", O_RDONLY);
 182      xexec(out);
 183    }
 184    waitpid(pid, &status, 0);
 185    status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
 186
 187    // Abritrary number of execs, can't just leak memory each time...
 188    while (dlist) {
 189      struct double_list *dtemp = dlist->next;
 190
 191      free(dlist->data);
 192      free(dlist);
 193      dlist = dtemp;
 194    }
 195    free(out);
 196  }
 197}
 198