1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#define FOR_xargs
36#include "toys.h"
37
38GLOBALS(
39 long s, n, P;
40 char *E;
41
42 long entries, bytes, np;
43 char delim;
44 FILE *tty;
45)
46
47
48
49
50
51
52
53
54
55static char *handle_entries(char *data, char **entry)
56{
57 if (TT.delim) {
58 char *save, *ss, *s;
59
60
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
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
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
95static void signal_P(int sig)
96{
97 if (sig == SIGUSR2 && TT.P>1) TT.P--;
98 else TT.P++;
99}
100
101void xargs_main(void)
102{
103 struct double_list *dlist = 0, *dtemp;
104 int entries, bytes, done = 0, status;
105 char *data = 0, **out = 0;
106 pid_t pid = 0;
107
108 xsignal_flags(SIGUSR1, signal_P, SA_RESTART);
109 xsignal_flags(SIGUSR2, signal_P, SA_RESTART);
110
111
112
113
114
115
116 if (!FLAG(s)) TT.s = sysconf(_SC_ARG_MAX) - environ_bytes() - 4096;
117
118 TT.delim = '\n'*!FLAG(0);
119
120
121 if (!toys.optc) {
122 free(toys.optargs);
123 *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
124 toys.optc = 1;
125 }
126
127
128 for (entries = 0, bytes = -1; entries < toys.optc; entries++)
129 bytes += strlen(toys.optargs[entries])+1+sizeof(char *)*!FLAG(s);
130 if (bytes >= TT.s) error_exit("command too long");
131
132
133 while (data || !done) {
134 TT.entries = 0;
135 TT.bytes = bytes;
136
137
138 for (;;) {
139
140
141 if (!data) {
142 size_t l = 0;
143
144 if (getdelim(&data, &l, TT.delim, stdin)<0) {
145 data = 0;
146 done++;
147 break;
148 }
149 }
150 dlist_add(&dlist, data);
151
152 if (!(data = handle_entries(data, 0))) continue;
153 if (data == (char *)2) done++;
154 if ((unsigned long)data <= 2) data = 0;
155 else data = xstrdup(data);
156
157 break;
158 }
159
160 if (!TT.entries) {
161 if (data) error_exit("argument too long");
162 if (pid || FLAG(r)) goto reap_children;
163 }
164
165
166 out = xzalloc((entries+TT.entries+1)*sizeof(char *));
167 memcpy(out, toys.optargs, entries*sizeof(char *));
168 TT.entries = 0;
169 TT.bytes = bytes;
170 if (dlist) dlist->prev->next = 0;
171 for (dtemp = dlist; dtemp; dtemp = dtemp->next)
172 handle_entries(dtemp->data, out+entries);
173
174 if (FLAG(p) || FLAG(t)) {
175 int i;
176
177 for (i = 0; out[i]; ++i) fprintf(stderr, "%s ", out[i]);
178 if (FLAG(p)) {
179 fprintf(stderr, "?");
180 if (!TT.tty) TT.tty = xfopen("/dev/tty", "re");
181 if (!fyesno(TT.tty, 0)) goto reap_children;
182 } else fprintf(stderr, "\n");
183 }
184
185 if (!(pid = XVFORK())) {
186 close(0);
187 xopen_stdio(FLAG(o) ? "/dev/tty" : "/dev/null", O_RDONLY|O_CLOEXEC);
188 xexec(out);
189 }
190 TT.np++;
191
192reap_children:
193 while (TT.np) {
194 int xv = (TT.np == TT.P) || (!data && done);
195
196 if (1>(xv = waitpid(-1, &status, WNOHANG*!xv))) break;
197 TT.np--;
198 xv = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+128;
199 if (xv == 255) {
200 error_msg("%s: exited with status 255; aborting", *out);
201 toys.exitval = 124;
202 break;
203 } else if ((xv|1)==127) toys.exitval = xv;
204 else if (xv>127) xv = 125;
205 else if (xv) toys.exitval = 123;
206 }
207
208
209 llist_traverse(dlist, llist_free_double);
210 dlist = 0;
211 free(out);
212 out = 0;
213 }
214 while (TT.np && -1 != wait(&status)) TT.np--;
215 if (TT.tty) fclose(TT.tty);
216}
217