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 max_bytes;
45 long max_entries;
46 long L;
47 char *eofstr;
48 char *I;
49
50 long entries, bytes;
51 char delim;
52)
53
54
55
56
57
58
59
60
61
62static char *handle_entries(char *data, char **entry)
63{
64 if (TT.delim) {
65 char *s = data;
66
67
68 while (*s) {
69 char *save;
70
71 while (isspace(*s)) {
72 if (entry) *s = 0;
73 s++;
74 }
75
76 if (TT.max_entries && TT.entries >= TT.max_entries)
77 return *s ? s : (char *)1;
78
79 if (!*s) break;
80 save = s;
81
82 TT.bytes += sizeof(char *);
83
84 for (;;) {
85 if (++TT.bytes >= TT.max_bytes && TT.max_bytes) return save;
86 if (!*s || isspace(*s)) break;
87 s++;
88 }
89 if (TT.eofstr) {
90 int len = s-save;
91 if (len == strlen(TT.eofstr) && !strncmp(save, TT.eofstr, len))
92 return (char *)2;
93 }
94 if (entry) entry[TT.entries] = save;
95 ++TT.entries;
96 }
97
98
99 } else {
100 TT.bytes += sizeof(char *)+strlen(data)+1;
101 if (TT.max_bytes && TT.bytes >= TT.max_bytes) return data;
102 if (TT.max_entries && TT.entries >= TT.max_entries) return data;
103 if (entry) entry[TT.entries] = data;
104 TT.entries++;
105 }
106
107 return NULL;
108}
109
110void xargs_main(void)
111{
112 struct double_list *dlist = NULL, *dtemp;
113 int entries, bytes, done = 0, status;
114 char *data = NULL, **out;
115 pid_t pid;
116 long posix_max_bytes;
117
118
119
120
121
122
123 posix_max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes() - 2048;
124 if (TT.max_bytes == 0 || TT.max_bytes > posix_max_bytes)
125 TT.max_bytes = posix_max_bytes;
126
127 if (!(toys.optflags & FLAG_0)) TT.delim = '\n';
128
129
130 if (!toys.optc) {
131 free(toys.optargs);
132 *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
133 toys.optc = 1;
134 }
135
136 for (entries = 0, bytes = -1; entries < toys.optc; entries++, bytes++)
137 bytes += strlen(toys.optargs[entries]);
138
139
140 while (data || !done) {
141 TT.entries = 0;
142 TT.bytes = bytes;
143
144
145 for (;;) {
146
147
148 if (!data) {
149 ssize_t l = 0;
150 l = getdelim(&data, (size_t *)&l, TT.delim, stdin);
151
152 if (l<0) {
153 data = 0;
154 done++;
155 break;
156 }
157 }
158 dlist_add(&dlist, data);
159
160
161 data = handle_entries(data, NULL);
162 if (!data) continue;
163 if (data == (char *)2) done++;
164 if ((long)data <= 2) data = 0;
165 else data = xstrdup(data);
166
167 break;
168 }
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 (!(pid = XVFORK())) {
184 xclose(0);
185 open("/dev/null", O_RDONLY);
186 xexec(out);
187 }
188 waitpid(pid, &status, 0);
189 status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
190
191
192 while (dlist) {
193 struct double_list *dtemp = dlist->next;
194
195 free(dlist->data);
196 free(dlist);
197 dlist = dtemp;
198 }
199 free(out);
200 }
201}
202