xref: /aosp_15_r20/external/toybox/toys/posix/xargs.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* xargs.c - Run command with arguments taken from stdin.
2  *
3  * Copyright 2011 Rob Landley <[email protected]>
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: -I	Insert mode
9  * TODO: -L	Max number of lines of input per command
10  * TODO: -x	Exit if can't fit everything in one command
11 
12 USE_XARGS(NEWTOY(xargs, "^E:P#<0(null)=1optr(no-run-if-empty)n#<1(max-args)s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
13 
14 config XARGS
15   bool "xargs"
16   default y
17   help
18     usage: xargs [-0Pprt] [-snE STR] COMMAND...
19 
20     Run command line one or more times, appending arguments from stdin.
21 
22     If COMMAND exits with 255, don't launch another even if arguments remain.
23 
24     -0	Each argument is NULL terminated, no whitespace or quote processing
25     -E	Stop at line matching string
26     -n	Max number of arguments per command
27     -o	Open tty for COMMAND's stdin (default /dev/null)
28     -P	Parallel processes (default 1)
29     -p	Prompt for y/n from tty before running each command
30     -r	Don't run with empty input (otherwise always run command once)
31     -s	Size in bytes per command line
32     -t	Trace, print command line to stderr
33 */
34 
35 #define FOR_xargs
36 #include "toys.h"
37 
GLOBALS(long s,n,P;char * E;long entries,bytes,np;char delim;FILE * tty;)38 GLOBALS(
39   long s, n, P;
40   char *E;
41 
42   long entries, bytes, np;
43   char delim;
44   FILE *tty;
45 )
46 
47 // If !entry count TT.bytes and TT.entries, stopping at max.
48 // Otherwise, fill out entry[].
49 
50 // Returning NULL means need more data.
51 // Returning char * means hit data limits, start of data left over
52 // Returning 1 means hit data limits, but consumed all data
53 // Returning 2 means hit -E STR
54 
55 static char *handle_entries(char *data, char **entry)
56 {
57   if (TT.delim) {
58     char *save, *ss, *s;
59 
60     // Chop up whitespace delimited string into args
61     for (s = data; *s; TT.entries++) {
62       while (isspace(*s)) s++;
63       if (TT.n && TT.entries >= TT.n) return *s ? s : (char *)1;
64       if (!*s) break;
65       save = ss = s;
66 
67       // Specifying -s can cause "argument too long" errors.
68       if (!FLAG(s)) TT.bytes += sizeof(void *)+1;
69       for (;;) {
70         if (++TT.bytes >= TT.s) return save;
71         if (!*s || isspace(*s)) break;
72         s++;
73       }
74       if (TT.E && strstart(&ss, TT.E) && ss == s) return (char *)2;
75       if (entry) {
76         entry[TT.entries] = save;
77         if (*s) *s++ = 0;
78       }
79     }
80 
81   // -0 support
82   } else {
83     long bytes = TT.bytes+sizeof(char *)+strlen(data)+1;
84 
85     if (bytes >= TT.s || (TT.n && TT.entries >= TT.n)) return data;
86     TT.bytes = bytes;
87     if (entry) entry[TT.entries] = data;
88     TT.entries++;
89   }
90 
91   return 0;
92 }
93 
94 // Handle SIGUSR1 and SIGUSR2 for -P
signal_P(int sig)95 static void signal_P(int sig)
96 {
97   if (sig == SIGUSR2 && TT.P>1) TT.P--;
98   else TT.P++;
99 }
100 
waitchild(int options)101 static void waitchild(int options)
102 {
103   int ii, status;
104 
105   if (1>waitpid(-1, &status, options)) return;
106   TT.np--;
107   ii = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+128;
108   if (ii == 255) {
109     error_msg("%s: exited with status 255; aborting", *toys.optargs);
110     toys.exitval = 124;
111   } else if ((ii|1)==127) toys.exitval = ii;
112   else if (ii>127) toys.exitval = 125;
113   else if (ii) toys.exitval = 123;
114 }
115 
xargs_main(void)116 void xargs_main(void)
117 {
118   struct double_list *dlist = 0, *dtemp;
119   int entries, bytes, done = 0;
120   char *data = 0, **out = 0;
121   pid_t pid = 0;
122 
123   xsignal_flags(SIGUSR1, signal_P, SA_RESTART);
124   xsignal_flags(SIGUSR2, signal_P, SA_RESTART);
125 
126   // POSIX requires that we never hit the ARG_MAX limit, even if we try to
127   // with -s. POSIX also says we have to reserve 2048 bytes "to guarantee
128   // that the invoked utility has room to modify its environment variables
129   // and command line arguments and still be able to invoke another utility",
130   // though obviously that's not really something you can guarantee.
131   if (!FLAG(s)) TT.s = sysconf(_SC_ARG_MAX) - environ_bytes() - 4096;
132 
133   TT.delim = '\n'*!FLAG(0);
134 
135   // If no optargs, call echo.
136   if (!toys.optc) {
137     free(toys.optargs);
138     *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
139     toys.optc = 1;
140   }
141 
142   // count entries
143   for (entries = 0, bytes = -1; entries < toys.optc; entries++)
144     bytes += strlen(toys.optargs[entries])+1+sizeof(char *)*!FLAG(s);
145   if (bytes >= TT.s) error_exit("command too long");
146 
147   // Loop through exec chunks.
148   while (data || !done) {
149     TT.entries = 0;
150     TT.bytes = bytes;
151     if (TT.np) waitchild(WNOHANG*!(TT.np==TT.P||(!data && done)));
152     if (toys.exitval==124) break;
153 
154     // Arbitrary number of execs, can't just leak memory each time...
155     llist_traverse(dlist, llist_free_double);
156     dlist = 0;
157     free(out);
158     out = 0;
159 
160     // Loop reading input
161     for (;;) {
162       // Read line
163       if (!data) {
164         size_t l = 0;
165 
166         if (getdelim(&data, &l, TT.delim, stdin)<0) {
167           data = 0;
168           done++;
169           break;
170         }
171       }
172       dlist_add(&dlist, data);
173       // Count data used
174       if (!(data = handle_entries(data, 0))) continue;
175       if (data == (char *)2) done++;
176       if ((unsigned long)data <= 2) data = 0;
177       else data = xstrdup(data);
178 
179       break;
180     }
181 
182     if (!TT.entries) {
183       if (data) error_exit("argument too long");
184       if (pid || FLAG(r)) break;
185     }
186 
187     // Fill out command line to exec
188     out = xzalloc((toys.optc+TT.entries+1)*sizeof(char *));
189     memcpy(out, toys.optargs, toys.optc*sizeof(char *));
190     TT.entries = 0;
191     TT.bytes = bytes;
192     dlist_terminate(dlist);
193     for (dtemp = dlist; dtemp; dtemp = dtemp->next)
194       handle_entries(dtemp->data, out+entries);
195 
196     if (FLAG(p) || FLAG(t)) {
197       int i;
198 
199       for (i = 0; out[i]; ++i) fprintf(stderr, "%s ", out[i]);
200       if (FLAG(p)) {
201         fprintf(stderr, "?");
202         if (!TT.tty) TT.tty = xfopen("/dev/tty", "re");
203         if (!fyesno(TT.tty, 0)) continue;
204       } else fprintf(stderr, "\n");
205     }
206 
207     if (!(pid = XVFORK())) {
208       close(0);
209       xopen_stdio(FLAG(o) ? "/dev/tty" : "/dev/null", O_RDONLY|O_CLOEXEC);
210       xexec(out);
211     }
212     TT.np++;
213   }
214   while (TT.np) waitchild(0);
215   if (TT.tty) fclose(TT.tty);
216 }
217