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
36
37
38
39
40#define FOR_xargs
41#include "toys.h"
42
43GLOBALS(
44 long s, n;
45 char *E, *I;
46
47 long entries, bytes;
48 char delim;
49)
50
51
52
53
54
55
56
57
58
59static char *handle_entries(char *data, char **entry)
60{
61 if (TT.delim) {
62 char *s = data;
63
64
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
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
116
117
118
119
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
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
136 while (data || !done) {
137 int doit = 1;
138
139 TT.entries = 0;
140 TT.bytes = bytes;
141
142
143 for (;;) {
144
145
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
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
171
172 if (data && !TT.entries) error_exit("argument too long");
173 out = xzalloc((entries+TT.entries+1)*sizeof(char *));
174
175
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
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