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