• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * TODO: -x	Exit if can't fit everything in one command
9  * TODO: -L	Max number of lines of input per command
10 
11 USE_XARGS(NEWTOY(xargs, "^I:E:ptrn#<1s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
12 
13 config XARGS
14   bool "xargs"
15   default y
16   help
17     usage: xargs [-0prt] [-s NUM] [-n NUM] [-E STR] COMMAND...
18 
19     Run command line one or more times, appending arguments from stdin.
20 
21     If command exits with 255, don't launch another even if arguments remain.
22 
23     -0	Each argument is NULL terminated, no whitespace or quote processing
24     -E	Stop at line matching string
25     -n	Max number of arguments per command
26     -p	Prompt for y/n from tty before running each command
27     -r	Don't run command with empty input
28     -s	Size in bytes per command line
29     -t	Trace, print command line to stderr
30 
31 config 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 
43 GLOBALS(
44   long s, n;
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 
handle_entries(char * data,char ** entry)59 static 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 
xargs_main(void)107 void 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 (!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     int doit = 1;
138 
139     TT.entries = 0;
140     TT.bytes = bytes;
141 
142     // Loop reading input
143     for (;;) {
144 
145       // Read line
146       if (!data) {
147         ssize_t l = 0;
148         l = getdelim(&data, (size_t *)&l, TT.delim, stdin);
149 
150         if (l<0) {
151           data = 0;
152           done++;
153           break;
154         }
155       }
156       dlist_add(&dlist, data);
157 
158       // Count data used
159       data = handle_entries(data, NULL);
160       if (!data) continue;
161       if (data == (char *)2) done++;
162       if ((long)data <= 2) data = 0;
163       else data = xstrdup(data);
164 
165       break;
166     }
167 
168     if (TT.entries == 0 && FLAG(r)) continue;
169 
170     // Accumulate cally thing
171 
172     if (data && !TT.entries) error_exit("argument too long");
173     out = xzalloc((entries+TT.entries+1)*sizeof(char *));
174 
175     // Fill out command line to exec
176     memcpy(out, toys.optargs, entries*sizeof(char *));
177     TT.entries = 0;
178     TT.bytes = bytes;
179     if (dlist) dlist->prev->next = 0;
180     for (dtemp = dlist; dtemp; dtemp = dtemp->next)
181       handle_entries(dtemp->data, out+entries);
182 
183     if (FLAG(p) || FLAG(t)) {
184       int i;
185 
186       for (i = 0; out[i]; ++i) fprintf(stderr, "%s ", out[i]);
187       if (FLAG(p)) {
188         fprintf(stderr, "?");
189         doit = yesno(0);
190       } else fprintf(stderr, "\n");
191     }
192 
193     if (doit) {
194       if (!(pid = XVFORK())) {
195         xclose(0);
196         open("/dev/null", O_RDONLY);
197         xexec(out);
198       }
199       waitpid(pid, &status, 0);
200       status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
201     }
202 
203     // Abritrary number of execs, can't just leak memory each time...
204     while (dlist) {
205       struct double_list *dtemp = dlist->next;
206 
207       free(dlist->data);
208       free(dlist);
209       dlist = dtemp;
210     }
211     free(out);
212   }
213 }
214