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;
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, *s = data;
59
60
61 while (*s) {
62 while (isspace(*s)) {
63 if (entry) *s = 0;
64 s++;
65 }
66
67 if (TT.n && TT.entries >= TT.n)
68 return *s ? s : (char *)1;
69
70 if (!*s) break;
71 save = s;
72
73
74
75
76 for (;;) {
77 if (++TT.bytes >= TT.s && TT.s) return save;
78 if (!*s || isspace(*s)) break;
79 s++;
80 }
81 if (TT.E && strstart(&save, TT.E)) return (char *)2;
82 if (entry) entry[TT.entries] = save;
83 ++TT.entries;
84 }
85
86
87 } else {
88 TT.bytes += sizeof(char *)+strlen(data)+1;
89 if ((TT.s && TT.bytes >= TT.s) || (TT.n && TT.entries >= TT.n)) return data;
90 if (entry) entry[TT.entries] = data;
91 TT.entries++;
92 }
93
94 return 0;
95}
96
97void xargs_main(void)
98{
99 struct double_list *dlist = 0, *dtemp;
100 int entries, bytes, done = 0, ran_once = 0, status;
101 char *data = 0, **out;
102 pid_t pid;
103
104
105
106
107
108
109 bytes = sysconf(_SC_ARG_MAX) - environ_bytes() - 2048;
110 if (!TT.s || TT.s > bytes) TT.s = bytes;
111
112 TT.delim = '\n'*!FLAG(0);
113
114
115 if (!toys.optc) {
116 free(toys.optargs);
117 *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
118 toys.optc = 1;
119 }
120
121
122 for (entries = 0, bytes = -1; entries < toys.optc; entries++, bytes++)
123 bytes += strlen(toys.optargs[entries]);
124 if (bytes >= TT.s) error_exit("argument too long");
125
126
127 while (data || !done) {
128 TT.entries = 0;
129 TT.bytes = bytes;
130
131
132 for (;;) {
133
134
135 if (!data) {
136 ssize_t l = 0;
137 if (getdelim(&data, (size_t *)&l, TT.delim, stdin)<0) {
138 data = 0;
139 done++;
140
141 break;
142 }
143 }
144 dlist_add(&dlist, data);
145
146
147 if (!(data = handle_entries(data, 0))) continue;
148 if (data == (char *)2) done++;
149 if ((unsigned long)data <= 2) data = 0;
150 else data = xstrdup(data);
151
152 break;
153 }
154
155 if (!TT.entries) {
156 if (data) error_exit("argument too long");
157 else if (ran_once) return;
158 else if (FLAG(r)) continue;
159 }
160
161
162 out = xzalloc((entries+TT.entries+1)*sizeof(char *));
163 memcpy(out, toys.optargs, entries*sizeof(char *));
164 TT.entries = 0;
165 TT.bytes = bytes;
166 if (dlist) dlist->prev->next = 0;
167 for (dtemp = dlist; dtemp; dtemp = dtemp->next)
168 handle_entries(dtemp->data, out+entries);
169
170 if (FLAG(p) || FLAG(t)) {
171 int i;
172
173 for (i = 0; out[i]; ++i) fprintf(stderr, "%s ", out[i]);
174 if (FLAG(p)) {
175 fprintf(stderr, "?");
176 if (!TT.tty) TT.tty = xfopen("/dev/tty", "re");
177 if (!fyesno(TT.tty, 0)) goto skip;
178 } else fprintf(stderr, "\n");
179 }
180
181 if (!(pid = XVFORK())) {
182 close(0);
183 xopen_stdio(FLAG(o) ? "/dev/tty" : "/dev/null", O_RDONLY);
184 xexec(out);
185 }
186 waitpid(pid, &status, 0);
187
188
189
190 if (WIFEXITED(status)) {
191 if (WEXITSTATUS(status) == 126 || WEXITSTATUS(status) == 127) {
192 toys.exitval = WEXITSTATUS(status);
193 return;
194 } else if (WEXITSTATUS(status) >= 1 && WEXITSTATUS(status) <= 125) {
195 toys.exitval = 123;
196 } else if (WEXITSTATUS(status) == 255) {
197 error_msg("%s: exited with status 255; aborting", out[0]);
198 toys.exitval = 124;
199 return;
200 }
201 } else toys.exitval = 127;
202
203
204skip:
205 ran_once = 1;
206 while (dlist) {
207 struct double_list *dtemp = dlist->next;
208
209 free(dlist->data);
210 free(dlist);
211 dlist = dtemp;
212 }
213 free(out);
214 }
215 if (TT.tty) fclose(TT.tty);
216}
217