blob: e2e6d3abdbb43406c8ac4c5a6bbf0ffbf9bed94e [file] [log] [blame]
Eric Andersen5b176932000-09-22 20:22:28 +00001/*
Eric Andersena37d5b72000-09-23 06:10:14 +00002 * Mini xargs implementation for busybox
Glenn L McGrath61796942003-10-10 12:10:18 +00003 * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
Eric Andersen5b176932000-09-22 20:22:28 +00004 *
Glenn L McGrath61796942003-10-10 12:10:18 +00005 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrathf57674e2002-11-10 21:47:17 +00006 *
Glenn L McGrath61796942003-10-10 12:10:18 +00007 * Special thanks
8 * - Mark Whitley and Glenn McGrath for stimul to rewrote :)
9 * - Mike Rendell <michael@cs.mun.ca>
10 * and David MacKenzie <djm@gnu.ai.mit.edu>.
Eric Andersen5b176932000-09-22 20:22:28 +000011 *
Eric Andersena37d5b72000-09-23 06:10:14 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
Eric Andersen5b176932000-09-22 20:22:28 +000016 *
Eric Andersena37d5b72000-09-23 06:10:14 +000017 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
Eric Andersen5b176932000-09-22 20:22:28 +000021 *
Eric Andersena37d5b72000-09-23 06:10:14 +000022 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
Eric Andersen5b176932000-09-22 20:22:28 +000026 */
Eric Andersen92a61c12000-09-22 20:01:23 +000027
Eric Andersen5b176932000-09-22 20:22:28 +000028#include <stdio.h>
Mark Whitleye2e2c292000-11-14 22:43:21 +000029#include <stdlib.h>
Glenn L McGrathadd3ead2003-10-04 14:44:27 +000030#include <string.h>
Glenn L McGrathf57674e2002-11-10 21:47:17 +000031#include <unistd.h>
32#include <getopt.h>
33#include <errno.h>
Glenn L McGrath61796942003-10-10 12:10:18 +000034#include <fcntl.h>
Glenn L McGrathf57674e2002-11-10 21:47:17 +000035#include <sys/types.h>
36#include <sys/wait.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000037#include "busybox.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000038
Glenn L McGrath61796942003-10-10 12:10:18 +000039/* COMPAT: SYSV version defaults size (and has a max value of) to 470.
40 We try to make it as large as possible. */
41#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
42#define ARG_MAX sysconf (_SC_ARG_MAX)
43#endif
44#ifndef ARG_MAX
45#define ARG_MAX 470
46#endif
47
48
49#ifdef TEST
50# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
51# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
52# endif
53# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
54# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
55# endif
56# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
57# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
58# endif
59# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
60# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
61# endif
62#endif
63
Glenn L McGrathf57674e2002-11-10 21:47:17 +000064/*
65 This function have special algorithm.
66 Don`t use fork and include to main!
67*/
Glenn L McGrath09c295a2003-10-30 22:47:16 +000068static int xargs_exec(char *const *args)
Glenn L McGrathf57674e2002-11-10 21:47:17 +000069{
Glenn L McGrath09c295a2003-10-30 22:47:16 +000070 pid_t p;
71 volatile int exec_errno = 0; /* shared vfork stack */
Glenn L McGrath61796942003-10-10 12:10:18 +000072
Glenn L McGrath09c295a2003-10-30 22:47:16 +000073 if ((p = vfork()) >= 0) {
74 if (p == 0) {
75 /* vfork -- child */
76 execvp(args[0], args);
77 exec_errno = errno; /* set error to shared stack */
78 _exit(1);
79 } else {
80 /* vfork -- parent */
81 int status;
82
83 while (wait(&status) == (pid_t) - 1)
84 if (errno != EINTR)
85 break;
86 if (exec_errno) {
87 errno = exec_errno;
88 bb_perror_msg("%s", args[0]);
89 return exec_errno == ENOENT ? 127 : 126;
90 } else {
91 if (WEXITSTATUS(status) == 255) {
92 bb_error_msg("%s: exited with status 255; aborting",
93 args[0]);
94 return 124;
95 }
96 if (WIFSTOPPED(status)) {
97 bb_error_msg("%s: stopped by signal %d", args[0],
98 WSTOPSIG(status));
99 return 125;
100 }
101 if (WIFSIGNALED(status)) {
102 bb_error_msg("%s: terminated by signal %d", args[0],
103 WTERMSIG(status));
104 return 125;
105 }
106 if (WEXITSTATUS(status) != 0)
107 return 123;
108 return 0;
109 }
110 }
Glenn L McGrath99825962003-10-09 11:06:45 +0000111 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000112 bb_perror_msg_and_die("vfork");
Glenn L McGrath61796942003-10-10 12:10:18 +0000113 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000114}
Glenn L McGrath99825962003-10-09 11:06:45 +0000115
Glenn L McGrath61796942003-10-10 12:10:18 +0000116
117typedef struct xlist_s {
118 char *data;
119 size_t lenght;
120 struct xlist_s *link;
121} xlist_t;
122
123static int eof_stdin_detected;
124
125#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
126#define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
127 || (c) == '\f' || (c) == '\v')
128
129#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000130static xlist_t *process_stdin(xlist_t * list_arg, const char *eof_str,
131 size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000132{
133#define NORM 0
134#define QUOTE 1
135#define BACKSLASH 2
136#define SPACE 4
137
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000138 char *s = NULL; /* start word */
139 char *p = NULL; /* pointer to end word */
140 char q = 0; /* quote char */
141 char state = NORM;
142 char eof_str_detected = 0;
143 size_t line_l = 0; /* size loaded args line */
144 int c; /* current char */
145 xlist_t *cur;
146 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000147
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000148 for (prev = cur = list_arg; cur; cur = cur->link) {
149 line_l += cur->lenght; /* previous allocated */
150 if (prev != cur)
151 prev = prev->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000152 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000153
154 while (!eof_stdin_detected) {
155 c = getchar();
156 if (c == EOF) {
157 eof_stdin_detected++;
158 if (s)
159 goto unexpected_eof;
160 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000161 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000162 if (eof_str_detected)
163 continue;
164 if (state == BACKSLASH) {
165 state = NORM;
166 goto set;
167 } else if (state == QUOTE) {
168 if (c == q) {
169 q = 0;
170 state = NORM;
171 } else {
172 goto set;
173 }
174 } else { /* if(state == NORM) */
Glenn L McGrath61796942003-10-10 12:10:18 +0000175
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000176 if (ISSPACE(c)) {
177 if (s) {
178 unexpected_eof:
179 state = SPACE;
180 c = 0;
181 goto set;
182 }
183 } else {
184 if (s == NULL)
185 s = p = buf;
186 if (c == '\\') {
187 state = BACKSLASH;
188 } else if (c == '\'' || c == '"') {
189 q = c;
190 state = QUOTE;
191 } else {
192 set:
193 if ((p - buf) >= mc)
194 bb_error_msg_and_die("argument line too long");
195 *p++ = c;
196 }
197 }
198 }
199 if (state == SPACE) { /* word's delimiter or EOF detected */
200 if (q)
201 bb_error_msg_and_die("unmatched %s quote",
202 q == '\'' ? "single" : "double");
203 /* word loaded */
204 if (eof_str) {
205 eof_str_detected = strcmp(s, eof_str) == 0;
206 }
207 if (!eof_str_detected) {
208 size_t lenght = (p - buf);
209
210 cur = xmalloc(sizeof(xlist_t) + lenght);
211 cur->data = memcpy(cur + 1, s, lenght);
212 cur->lenght = lenght;
213 cur->link = NULL;
214 if (prev == NULL) {
215 list_arg = cur;
216 } else {
217 prev->link = cur;
218 }
219 prev = cur;
220 line_l += lenght;
221 if (line_l > mc) {
222 /* stop memory usage :-) */
223 break;
224 }
225 }
226 s = NULL;
227 state = NORM;
228 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000229 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000230 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000231}
Glenn L McGrath61796942003-10-10 12:10:18 +0000232#else
233/* The variant is unsupport single, double quotes and backslash */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000234static xlist_t *process_stdin(xlist_t * list_arg, const char *eof_str,
235 size_t mc, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000236{
Eric Andersen92a61c12000-09-22 20:01:23 +0000237
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000238 int c; /* current char */
239 int eof_str_detected = 0;
240 char *s = NULL; /* start word */
241 char *p = NULL; /* pointer to end word */
242 size_t line_l = 0; /* size loaded args line */
243 xlist_t *cur;
244 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000245
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000246 for (prev = cur = list_arg; cur; cur = cur->link) {
247 line_l += cur->lenght; /* previous allocated */
248 if (prev != cur)
249 prev = prev->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000250 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000251
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000252 while (!eof_stdin_detected) {
253 c = getchar();
254 if (c == EOF) {
255 eof_stdin_detected++;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000256 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000257 if (eof_str_detected)
258 continue;
259 if (c == EOF || ISSPACE(c)) {
260 if (s == NULL)
261 continue;
262 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000263 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000264 if (s == NULL)
265 s = p = buf;
266 if ((p - buf) >= mc)
267 bb_error_msg_and_die("argument line too long");
268 *p++ = c == EOF ? 0 : c;
269 if (c == EOF) { /* word's delimiter or EOF detected */
270 /* word loaded */
271 if (eof_str) {
272 eof_str_detected = strcmp(s, eof_str) == 0;
273 }
274 if (!eof_str_detected) {
275 size_t lenght = (p - buf);
276
277 cur = xmalloc(sizeof(xlist_t) + lenght);
278 cur->data = memcpy(cur + 1, s, lenght);
279 cur->lenght = lenght;
280 cur->link = NULL;
281 if (prev == NULL) {
282 list_arg = cur;
283 } else {
284 prev->link = cur;
285 }
286 prev = cur;
287 line_l += lenght;
288 if (line_l > mc) {
289 /* stop memory usage :-) */
290 break;
291 }
292 s = NULL;
293 }
294 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000295 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000296 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000297}
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000298#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000299
300
301#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
302/* Prompt the user for a response, and
303 if the user responds affirmatively, return true;
304 otherwise, return false. Used "/dev/tty", not stdin. */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000305static int xargs_ask_confirmation(void)
Glenn L McGrath61796942003-10-10 12:10:18 +0000306{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000307 static FILE *tty_stream;
308 int c, savec;
Glenn L McGrath61796942003-10-10 12:10:18 +0000309
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000310 if (!tty_stream) {
311 tty_stream = fopen("/dev/tty", "r");
312 if (!tty_stream)
313 bb_perror_msg_and_die("/dev/tty");
314 /* pranoidal security by vodz */
315 fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
316 }
317 fputs(" ?...", stderr);
318 fflush(stderr);
319 c = savec = getc(tty_stream);
320 while (c != EOF && c != '\n')
321 c = getc(tty_stream);
322 if (savec == 'y' || savec == 'Y')
323 return 1;
324 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000325}
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000326
Glenn L McGrath61796942003-10-10 12:10:18 +0000327# define OPT_INC_P 1
328#else
329# define OPT_INC_P 0
330# define xargs_ask_confirmation() 1
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000331#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
Glenn L McGrath61796942003-10-10 12:10:18 +0000332
333#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
334# define OPT_INC_X 1
335#else
336# define OPT_INC_X 0
337#endif
338
339#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000340static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str,
341 size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000342{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000343 int c; /* current char */
344 char *s = NULL; /* start word */
345 char *p = NULL; /* pointer to end word */
346 size_t line_l = 0; /* size loaded args line */
347 xlist_t *cur;
348 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000349
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000350 for (prev = cur = list_arg; cur; cur = cur->link) {
351 line_l += cur->lenght; /* previous allocated */
352 if (prev != cur)
353 prev = prev->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000354 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000355
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000356 while (!eof_stdin_detected) {
357 c = getchar();
358 if (c == EOF) {
359 eof_stdin_detected++;
360 if (s == NULL)
361 break;
362 c = 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000363 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000364 if (s == NULL)
365 s = p = buf;
366 if ((p - buf) >= mc)
367 bb_error_msg_and_die("argument line too long");
368 *p++ = c;
369 if (c == 0) { /* word's delimiter or EOF detected */
370 /* word loaded */
371 size_t lenght = (p - buf);
372
373 cur = xmalloc(sizeof(xlist_t) + lenght);
374 cur->data = memcpy(cur + 1, s, lenght);
375 cur->lenght = lenght;
376 cur->link = NULL;
377 if (prev == NULL) {
378 list_arg = cur;
379 } else {
380 prev->link = cur;
381 }
382 prev = cur;
383 line_l += lenght;
384 if (line_l > mc) {
385 /* stop memory usage :-) */
386 break;
387 }
388 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000389 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000390 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000391 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000392}
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000393
Glenn L McGrath61796942003-10-10 12:10:18 +0000394# define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000395# define OPT_INC_0 1 /* future use */
Glenn L McGrath61796942003-10-10 12:10:18 +0000396#else
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000397# define OPT_INC_0 0 /* future use */
Glenn L McGrath61796942003-10-10 12:10:18 +0000398# define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000399#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000400
401
402#define OPT_VERBOSE (1<<0)
403#define OPT_NO_EMPTY (1<<1)
404#define OPT_UPTO_NUMBER (1<<2)
405#define OPT_UPTO_SIZE (1<<3)
406#define OPT_EOF_STRING (1<<4)
407#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
408#define OPT_INTERACTIVE (1<<5)
409#else
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000410#define OPT_INTERACTIVE (0) /* require for algorithm &| */
Glenn L McGrath61796942003-10-10 12:10:18 +0000411#endif
412#define OPT_TERMINATE (1<<(5+OPT_INC_P))
413#define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X))
414/* next future
415#define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0))
416*/
417
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000418int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000419{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000420 char **args;
421 int i, a, n;
422 xlist_t *list = NULL;
423 xlist_t *cur;
424 int child_error = 0;
425 char *max_args, *max_chars;
426 int n_max_arg;
427 size_t n_chars = 0;
428 long orig_arg_max;
429 const char *eof_str = "_";
430 unsigned long opt;
431 size_t n_max_chars;
432
Glenn L McGrath61796942003-10-10 12:10:18 +0000433#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000434 xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) =
435 process_stdin;
Glenn L McGrath61796942003-10-10 12:10:18 +0000436#endif
437
438#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000439 bb_opt_complementaly = "pt";
Glenn L McGrath61796942003-10-10 12:10:18 +0000440#endif
441
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000442 opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::"
Glenn L McGrath61796942003-10-10 12:10:18 +0000443#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000444 "p"
Glenn L McGrath61796942003-10-10 12:10:18 +0000445#endif
446#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000447 "x"
Glenn L McGrath61796942003-10-10 12:10:18 +0000448#endif
449#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000450 "0"
Glenn L McGrath61796942003-10-10 12:10:18 +0000451#endif
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000452 , &max_args, &max_chars, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000453
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000454 a = argc - optind;
455 argv += optind;
456 if (a == 0) {
457 /* default behavior is to echo all the filenames */
458 *argv = "echo";
459 a++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000460 }
461
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000462 orig_arg_max = ARG_MAX;
463 if (orig_arg_max == -1)
464 orig_arg_max = LONG_MAX;
465 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
466 if ((opt & OPT_UPTO_SIZE)) {
467 n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max);
468 for (i = 0; i < a; i++) {
469 n_chars += strlen(*argv) + 1;
Glenn L McGrath61796942003-10-10 12:10:18 +0000470 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000471 if (n_max_chars < n_chars) {
472 bb_error_msg_and_die
473 ("can not fit single argument within argument list size limit");
474 }
475 n_max_chars -= n_chars;
476 } else {
477 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
478 have it at 1 meg). Things will work fine with a large ARG_MAX but it
479 will probably hurt the system more than it needs to; an array of this
480 size is allocated. */
481 if (orig_arg_max > 20 * 1024)
482 orig_arg_max = 20 * 1024;
483 n_max_chars = orig_arg_max;
Glenn L McGrath61796942003-10-10 12:10:18 +0000484 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000485 max_chars = xmalloc(n_max_chars);
486
487 if ((opt & OPT_UPTO_NUMBER)) {
488 n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX);
489 } else {
490 n_max_arg = n_max_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000491 }
492
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000493#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
494 if (opt & OPT_ZEROTERM)
495 read_args = process0_stdin;
Glenn L McGrath61796942003-10-10 12:10:18 +0000496#endif
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000497
498 while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL
499 || (opt & OPT_NO_EMPTY) == 0) {
500 opt |= OPT_NO_EMPTY;
501 n = 0;
502 n_chars = 0;
503#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
504 for (cur = list; cur;) {
505 n_chars += cur->lenght;
506 n++;
507 cur = cur->link;
508 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
509 if (opt & OPT_TERMINATE)
510 bb_error_msg_and_die("argument list too long");
511 break;
512 }
513 }
514#else
515 for (cur = list; cur; cur = cur->link) {
516 n_chars += cur->lenght;
517 n++;
518 if (n_chars > n_max_chars || n == n_max_arg) {
519 break;
520 }
521 }
522#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
523
524 /* allocating pointers for execvp:
525 a*arg, n*arg from stdin, NULL */
526 args = xcalloc(n + a + 1, sizeof(char *));
527
528 /* Store the command to be executed
529 (taken from the command line) */
530 for (i = 0; i < a; i++)
531 args[i] = argv[i];
532 /* (taken from stdin) */
533 for (cur = list; n; cur = cur->link) {
534 args[i++] = cur->data;
535 n--;
536 }
537
538 if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) {
539 for (i = 0; args[i]; i++) {
540 if (i)
541 fputc(' ', stderr);
542 fputs(args[i], stderr);
543 }
544 if ((opt & OPT_INTERACTIVE) == 0)
545 fputc('\n', stderr);
546 }
547 if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) {
548 child_error = xargs_exec(args);
549 }
550
551 /* clean up */
552 for (i = a; args[i]; i++) {
553 cur = list;
554 list = list->link;
555 free(cur);
556 }
557 free(args);
558 if (child_error > 0 && child_error != 123) {
559 break;
560 }
561 }
562#ifdef CONFIG_FEATURE_CLEAN_UP
563 free(max_chars);
564#endif
565 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000566}
567
568
569#ifdef TEST
570
571const char *bb_applet_name = "debug stuff usage";
572
573void bb_show_usage(void)
574{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000575 fprintf(stderr,
576 "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
577 bb_applet_name);
578 exit(1);
Glenn L McGrath61796942003-10-10 12:10:18 +0000579}
580
581int main(int argc, char **argv)
582{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000583 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000584}
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000585#endif /* TEST */