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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61#include "libbb.h"
62
63
64
65
66
67#define dbg_msg(...) ((void)0)
68
69
70#ifdef TEST
71# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
72# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
73# endif
74# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
75# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
76# endif
77# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
78# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
79# endif
80# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
81# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
82# endif
83#endif
84
85
86struct globals {
87 char **args;
88 const char *eof_str;
89 int idx;
90} FIX_ALIASING;
91#define G (*(struct globals*)&bb_common_bufsiz1)
92#define INIT_G() do { } while (0)
93
94
95
96
97
98
99static int xargs_exec(void)
100{
101 int status;
102
103 status = spawn_and_wait(G.args);
104 if (status < 0) {
105 bb_simple_perror_msg(G.args[0]);
106 return errno == ENOENT ? 127 : 126;
107 }
108 if (status == 255) {
109 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
110 return 124;
111 }
112 if (status >= 0x180) {
113 bb_error_msg("%s: terminated by signal %d",
114 G.args[0], status - 0x180);
115 return 125;
116 }
117 if (status)
118 return 123;
119 return 0;
120}
121
122
123
124
125#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
126
127static void store_param(char *s)
128{
129
130 if (!(G.idx & 0xff)) {
131
132 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
133 }
134 G.args[G.idx++] = s;
135}
136
137
138
139
140
141
142
143
144
145
146
147
148
149#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
150static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
151{
152#define NORM 0
153#define QUOTE 1
154#define BACKSLASH 2
155#define SPACE 4
156 char q = '\0';
157 char state = NORM;
158 char *s = buf;
159 char *p = s + strlen(buf);
160
161 buf += n_max_chars;
162
163
164
165
166 while (1) {
167 int c = getchar();
168 if (c == EOF) {
169 if (p != s)
170 goto close_word;
171 goto ret;
172 }
173 if (state == BACKSLASH) {
174 state = NORM;
175 goto set;
176 }
177 if (state == QUOTE) {
178 if (c != q)
179 goto set;
180 q = '\0';
181 state = NORM;
182 } else {
183 if (ISSPACE(c)) {
184 if (p != s) {
185 close_word:
186 state = SPACE;
187 c = '\0';
188 goto set;
189 }
190 } else {
191 if (c == '\\') {
192 state = BACKSLASH;
193 } else if (c == '\'' || c == '"') {
194 q = c;
195 state = QUOTE;
196 } else {
197 set:
198 *p++ = c;
199 }
200 }
201 }
202 if (state == SPACE) {
203 if (q) {
204 bb_error_msg_and_die("unmatched %s quote",
205 q == '\'' ? "single" : "double");
206 }
207
208 if (G.eof_str) {
209 if (strcmp(s, G.eof_str) == 0) {
210 while (getchar() != EOF)
211 continue;
212 p = s;
213 goto ret;
214 }
215 }
216 store_param(s);
217 dbg_msg("args[]:'%s'", s);
218 s = p;
219 n_max_arg--;
220 if (n_max_arg == 0) {
221 goto ret;
222 }
223 state = NORM;
224 }
225 if (p == buf) {
226 goto ret;
227 }
228 }
229 ret:
230 *p = '\0';
231
232 dbg_msg("return:'%s'", s);
233 return s;
234}
235#else
236
237static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
238{
239 char *s = buf;
240 char *p = s + strlen(buf);
241
242 buf += n_max_chars;
243
244 while (1) {
245 int c = getchar();
246 if (c == EOF) {
247 if (p == s)
248 goto ret;
249 }
250 if (c == EOF || ISSPACE(c)) {
251 if (p == s)
252 continue;
253 c = EOF;
254 }
255 *p++ = (c == EOF ? '\0' : c);
256 if (c == EOF) {
257
258 if (G.eof_str) {
259 if (strcmp(s, G.eof_str) == 0) {
260 while (getchar() != EOF)
261 continue;
262 p = s;
263 goto ret;
264 }
265 }
266 store_param(s);
267 dbg_msg("args[]:'%s'", s);
268 s = p;
269 n_max_arg--;
270 if (n_max_arg == 0) {
271 goto ret;
272 }
273 }
274 if (p == buf) {
275 goto ret;
276 }
277 }
278 ret:
279 *p = '\0';
280
281 dbg_msg("return:'%s'", s);
282 return s;
283}
284#endif
285
286#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
287static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
288{
289 char *s = buf;
290 char *p = s + strlen(buf);
291
292 buf += n_max_chars;
293
294 while (1) {
295 int c = getchar();
296 if (c == EOF) {
297 if (p == s)
298 goto ret;
299 c = '\0';
300 }
301 *p++ = c;
302 if (c == '\0') {
303
304 store_param(s);
305 dbg_msg("args[]:'%s'", s);
306 s = p;
307 n_max_arg--;
308 if (n_max_arg == 0) {
309 goto ret;
310 }
311 }
312 if (p == buf) {
313 goto ret;
314 }
315 }
316 ret:
317 *p = '\0';
318
319 dbg_msg("return:'%s'", s);
320 return s;
321}
322#endif
323
324#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
325
326
327
328static int xargs_ask_confirmation(void)
329{
330 FILE *tty_stream;
331 int c, savec;
332
333 tty_stream = xfopen_for_read(CURRENT_TTY);
334 fputs(" ?...", stderr);
335 fflush_all();
336 c = savec = getc(tty_stream);
337 while (c != EOF && c != '\n')
338 c = getc(tty_stream);
339 fclose(tty_stream);
340 return (savec == 'y' || savec == 'Y');
341}
342#else
343# define xargs_ask_confirmation() 1
344#endif
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370enum {
371 OPTBIT_VERBOSE = 0,
372 OPTBIT_NO_EMPTY,
373 OPTBIT_UPTO_NUMBER,
374 OPTBIT_UPTO_SIZE,
375 OPTBIT_EOF_STRING,
376 OPTBIT_EOF_STRING1,
377 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
378 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
379 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
380
381 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
382 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
383 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
384 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
385 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING ,
386 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1,
387 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
388 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
389 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
390};
391#define OPTION_STR "+trn:s:e::E:" \
392 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
393 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
394 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
395
396int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
397int xargs_main(int argc, char **argv)
398{
399 int i;
400 int child_error = 0;
401 char *max_args;
402 char *max_chars;
403 char *buf;
404 unsigned opt;
405 int n_max_chars;
406 int n_max_arg;
407#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
408 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
409#else
410#define read_args process_stdin
411#endif
412
413 INIT_G();
414
415 G.eof_str = NULL;
416 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &G.eof_str, &G.eof_str);
417
418
419
420
421 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
422 G.eof_str = NULL;
423
424 if (opt & OPT_ZEROTERM)
425 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
426
427 argv += optind;
428 argc -= optind;
429 if (!argv[0]) {
430
431 *--argv = (char*)"echo";
432 argc++;
433 }
434
435
436
437
438
439 n_max_chars = 32 * 1024;
440
441
442
443
444
445
446
447
448 {
449 long arg_max = 0;
450#if defined _SC_ARG_MAX
451 arg_max = sysconf(_SC_ARG_MAX) - 2048;
452#elif defined ARG_MAX
453 arg_max = ARG_MAX - 2048;
454#endif
455 if (arg_max > 0 && n_max_chars > arg_max)
456 n_max_chars = arg_max;
457 }
458 if (opt & OPT_UPTO_SIZE) {
459 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
460 }
461
462 {
463 size_t n_chars = 0;
464 for (i = 0; argv[i]; i++) {
465 n_chars += strlen(argv[i]) + 1;
466 }
467 n_max_chars -= n_chars;
468 }
469
470 if (n_max_chars <= 0) {
471 bb_error_msg_and_die("can't fit single argument within argument list size limit");
472 }
473
474 buf = xzalloc(n_max_chars + 1);
475
476 n_max_arg = n_max_chars;
477 if (opt & OPT_UPTO_NUMBER) {
478 n_max_arg = xatou_range(max_args, 1, INT_MAX);
479
480
481 }
482
483
484
485
486
487
488
489
490
491
492 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
493
494
495 for (i = 0; argv[i]; i++)
496 G.args[i] = argv[i];
497
498 while (1) {
499 char *rem;
500
501 G.idx = argc;
502 rem = read_args(n_max_chars, n_max_arg, buf);
503 store_param(NULL);
504
505 if (!G.args[argc]) {
506 if (*rem != '\0')
507 bb_error_msg_and_die("argument line too long");
508 if (opt & OPT_NO_EMPTY)
509 break;
510 }
511 opt |= OPT_NO_EMPTY;
512
513 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
514 const char *fmt = " %s" + 1;
515 char **args = G.args;
516 for (i = 0; args[i]; i++) {
517 fprintf(stderr, fmt, args[i]);
518 fmt = " %s";
519 }
520 if (!(opt & OPT_INTERACTIVE))
521 bb_putchar_stderr('\n');
522 }
523
524 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
525 child_error = xargs_exec();
526 }
527
528 if (child_error > 0 && child_error != 123) {
529 break;
530 }
531
532 overlapping_strcpy(buf, rem);
533 }
534
535 if (ENABLE_FEATURE_CLEAN_UP) {
536 free(G.args);
537 free(buf);
538 }
539
540 return child_error;
541}
542
543
544#ifdef TEST
545
546const char *applet_name = "debug stuff usage";
547
548void bb_show_usage(void)
549{
550 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
551 applet_name);
552 exit(EXIT_FAILURE);
553}
554
555int main(int argc, char **argv)
556{
557 return xargs_main(argc, argv);
558}
559#endif
560