blob: 029d4077d60e0674237e4a026e31645697ce660b [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00001/* vi: set sw=4 ts=4: */
Eric Andersen5b176932000-09-22 20:22:28 +00002/*
Eric Andersena37d5b72000-09-23 06:10:14 +00003 * Mini xargs implementation for busybox
Glenn L McGrath61796942003-10-10 12:10:18 +00004 * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
Eric Andersen5b176932000-09-22 20:22:28 +00005 *
Glenn L McGrath61796942003-10-10 12:10:18 +00006 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrathf57674e2002-11-10 21:47:17 +00007 *
Glenn L McGrath61796942003-10-10 12:10:18 +00008 * Special thanks
Eric Andersenaff114c2004-04-14 17:51:38 +00009 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
Glenn L McGrath61796942003-10-10 12:10:18 +000010 * - Mike Rendell <michael@cs.mun.ca>
11 * and David MacKenzie <djm@gnu.ai.mit.edu>.
Eric Andersen5b176932000-09-22 20:22:28 +000012 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersena37d5b72000-09-23 06:10:14 +000014 *
Glenn L McGrath40c94892003-10-30 22:51:33 +000015 * xargs is described in the Single Unix Specification v3 at
16 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
17 *
Eric Andersen5b176932000-09-22 20:22:28 +000018 */
Eric Andersen92a61c12000-09-22 20:01:23 +000019
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000020#include "busybox.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000021
Glenn L McGrath61796942003-10-10 12:10:18 +000022/* COMPAT: SYSV version defaults size (and has a max value of) to 470.
23 We try to make it as large as possible. */
24#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
25#define ARG_MAX sysconf (_SC_ARG_MAX)
26#endif
27#ifndef ARG_MAX
28#define ARG_MAX 470
29#endif
30
31
32#ifdef TEST
33# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
34# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
35# endif
36# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
37# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
38# endif
39# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
40# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
41# endif
42# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
43# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
44# endif
45#endif
46
Glenn L McGrathf57674e2002-11-10 21:47:17 +000047/*
Denis Vlasenko6248a732006-09-29 08:20:30 +000048 This function has special algorithm.
49 Don't use fork and include to main!
Glenn L McGrathf57674e2002-11-10 21:47:17 +000050*/
Glenn L McGrath09c295a2003-10-30 22:47:16 +000051static int xargs_exec(char *const *args)
Glenn L McGrathf57674e2002-11-10 21:47:17 +000052{
Glenn L McGrath09c295a2003-10-30 22:47:16 +000053 pid_t p;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +000054 volatile int exec_errno = 0; /* shared vfork stack */
Denis Vlasenko6248a732006-09-29 08:20:30 +000055 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +000056
Denis Vlasenko6248a732006-09-29 08:20:30 +000057 p = vfork();
58 if (p < 0)
Glenn L McGrath09c295a2003-10-30 22:47:16 +000059 bb_perror_msg_and_die("vfork");
Denis Vlasenko6248a732006-09-29 08:20:30 +000060
61 if (p == 0) {
62 /* vfork -- child */
63 execvp(args[0], args);
64 exec_errno = errno; /* set error to shared stack */
65 _exit(1);
Glenn L McGrath61796942003-10-10 12:10:18 +000066 }
Denis Vlasenko6248a732006-09-29 08:20:30 +000067
68 /* vfork -- parent */
69 while (wait(&status) == (pid_t) -1)
70 if (errno != EINTR)
71 break;
72 if (exec_errno) {
73 errno = exec_errno;
74 bb_perror_msg("%s", args[0]);
75 return exec_errno == ENOENT ? 127 : 126;
76 }
77 if (WEXITSTATUS(status) == 255) {
78 bb_error_msg("%s: exited with status 255; aborting", args[0]);
79 return 124;
80 }
81 if (WIFSTOPPED(status)) {
82 bb_error_msg("%s: stopped by signal %d",
83 args[0], WSTOPSIG(status));
84 return 125;
85 }
86 if (WIFSIGNALED(status)) {
87 bb_error_msg("%s: terminated by signal %d",
88 args[0], WTERMSIG(status));
89 return 125;
90 }
91 if (WEXITSTATUS(status))
92 return 123;
93 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +000094}
Glenn L McGrath99825962003-10-09 11:06:45 +000095
Glenn L McGrath61796942003-10-10 12:10:18 +000096
97typedef struct xlist_s {
98 char *data;
99 size_t lenght;
100 struct xlist_s *link;
101} xlist_t;
102
103static int eof_stdin_detected;
104
105#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
Denis Vlasenko6248a732006-09-29 08:20:30 +0000106#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
Glenn L McGrath61796942003-10-10 12:10:18 +0000107 || (c) == '\f' || (c) == '\v')
108
109#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000110static xlist_t *process_stdin(xlist_t * list_arg,
Eric Andersen252183e2003-10-31 08:19:44 +0000111 const char *eof_str, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000112{
113#define NORM 0
114#define QUOTE 1
115#define BACKSLASH 2
116#define SPACE 4
117
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000118 char *s = NULL; /* start word */
119 char *p = NULL; /* pointer to end word */
120 char q = 0; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000121 char state = NORM;
122 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000123 size_t line_l = 0; /* size loaded args line */
124 int c; /* current char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000125 xlist_t *cur;
126 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000127
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000128 for (prev = cur = list_arg; cur; cur = cur->link) {
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000129 line_l += cur->lenght; /* previous allocated */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000130 if (prev != cur)
131 prev = prev->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000132 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000133
134 while (!eof_stdin_detected) {
135 c = getchar();
136 if (c == EOF) {
137 eof_stdin_detected++;
138 if (s)
139 goto unexpected_eof;
140 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000141 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000142 if (eof_str_detected)
143 continue;
144 if (state == BACKSLASH) {
145 state = NORM;
146 goto set;
147 } else if (state == QUOTE) {
148 if (c == q) {
149 q = 0;
150 state = NORM;
151 } else {
152 goto set;
153 }
Eric Andersen252183e2003-10-31 08:19:44 +0000154 } else { /* if(state == NORM) */
Glenn L McGrath61796942003-10-10 12:10:18 +0000155
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000156 if (ISSPACE(c)) {
157 if (s) {
Eric Andersen252183e2003-10-31 08:19:44 +0000158unexpected_eof:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000159 state = SPACE;
160 c = 0;
161 goto set;
162 }
163 } else {
164 if (s == NULL)
165 s = p = buf;
166 if (c == '\\') {
167 state = BACKSLASH;
168 } else if (c == '\'' || c == '"') {
169 q = c;
170 state = QUOTE;
171 } else {
Eric Andersen252183e2003-10-31 08:19:44 +0000172set:
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000173 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000174 bb_error_msg_and_die("argument line too long");
175 *p++ = c;
176 }
177 }
178 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000179 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000180 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000181 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000182 q == '\'' ? "single" : "double");
183 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000184 /* word loaded */
185 if (eof_str) {
186 eof_str_detected = strcmp(s, eof_str) == 0;
187 }
188 if (!eof_str_detected) {
189 size_t lenght = (p - buf);
190
191 cur = xmalloc(sizeof(xlist_t) + lenght);
192 cur->data = memcpy(cur + 1, s, lenght);
193 cur->lenght = lenght;
194 cur->link = NULL;
195 if (prev == NULL) {
196 list_arg = cur;
197 } else {
198 prev->link = cur;
199 }
200 prev = cur;
201 line_l += lenght;
202 if (line_l > mc) {
203 /* stop memory usage :-) */
204 break;
205 }
206 }
207 s = NULL;
208 state = NORM;
209 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000210 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000211 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000212}
Glenn L McGrath61796942003-10-10 12:10:18 +0000213#else
Eric Andersen252183e2003-10-31 08:19:44 +0000214/* The variant does not support single quotes, double quotes or backslash */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000215static xlist_t *process_stdin(xlist_t * list_arg,
Eric Andersen252183e2003-10-31 08:19:44 +0000216 const char *eof_str, size_t mc, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000217{
Eric Andersen92a61c12000-09-22 20:01:23 +0000218
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000219 int c; /* current char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000220 int eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000221 char *s = NULL; /* start word */
222 char *p = NULL; /* pointer to end word */
223 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000224 xlist_t *cur;
225 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000226
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000227 for (prev = cur = list_arg; cur; cur = cur->link) {
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000228 line_l += cur->lenght; /* previous allocated */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000229 if (prev != cur)
230 prev = prev->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000231 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000232
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000233 while (!eof_stdin_detected) {
234 c = getchar();
235 if (c == EOF) {
236 eof_stdin_detected++;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000237 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000238 if (eof_str_detected)
239 continue;
240 if (c == EOF || ISSPACE(c)) {
241 if (s == NULL)
242 continue;
243 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000244 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000245 if (s == NULL)
246 s = p = buf;
247 if ((p - buf) >= mc)
248 bb_error_msg_and_die("argument line too long");
249 *p++ = c == EOF ? 0 : c;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000250 if (c == EOF) { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000251 /* word loaded */
252 if (eof_str) {
253 eof_str_detected = strcmp(s, eof_str) == 0;
254 }
255 if (!eof_str_detected) {
256 size_t lenght = (p - buf);
257
258 cur = xmalloc(sizeof(xlist_t) + lenght);
259 cur->data = memcpy(cur + 1, s, lenght);
260 cur->lenght = lenght;
261 cur->link = NULL;
262 if (prev == NULL) {
263 list_arg = cur;
264 } else {
265 prev->link = cur;
266 }
267 prev = cur;
268 line_l += lenght;
269 if (line_l > mc) {
270 /* stop memory usage :-) */
271 break;
272 }
273 s = NULL;
274 }
275 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000276 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000277 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000278}
Eric Andersen252183e2003-10-31 08:19:44 +0000279#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000280
281
282#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
283/* Prompt the user for a response, and
284 if the user responds affirmatively, return true;
285 otherwise, return false. Used "/dev/tty", not stdin. */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000286static int xargs_ask_confirmation(void)
Glenn L McGrath61796942003-10-10 12:10:18 +0000287{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000288 static FILE *tty_stream;
289 int c, savec;
Glenn L McGrath61796942003-10-10 12:10:18 +0000290
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000291 if (!tty_stream) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000292 tty_stream = xfopen(CURRENT_TTY, "r");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000293 /* pranoidal security by vodz */
294 fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
295 }
296 fputs(" ?...", stderr);
297 fflush(stderr);
298 c = savec = getc(tty_stream);
299 while (c != EOF && c != '\n')
300 c = getc(tty_stream);
301 if (savec == 'y' || savec == 'Y')
302 return 1;
303 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000304}
Glenn L McGrath61796942003-10-10 12:10:18 +0000305#else
Glenn L McGrath61796942003-10-10 12:10:18 +0000306# define xargs_ask_confirmation() 1
Eric Andersen252183e2003-10-31 08:19:44 +0000307#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
Glenn L McGrath61796942003-10-10 12:10:18 +0000308
Glenn L McGrath61796942003-10-10 12:10:18 +0000309#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000310static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED,
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000311 size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000312{
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000313 int c; /* current char */
314 char *s = NULL; /* start word */
315 char *p = NULL; /* pointer to end word */
316 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000317 xlist_t *cur;
318 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000319
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000320 for (prev = cur = list_arg; cur; cur = cur->link) {
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000321 line_l += cur->lenght; /* previous allocated */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000322 if (prev != cur)
323 prev = prev->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000324 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000325
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000326 while (!eof_stdin_detected) {
327 c = getchar();
328 if (c == EOF) {
329 eof_stdin_detected++;
330 if (s == NULL)
331 break;
332 c = 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000333 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000334 if (s == NULL)
335 s = p = buf;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000336 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000337 bb_error_msg_and_die("argument line too long");
338 *p++ = c;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000339 if (c == 0) { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000340 /* word loaded */
341 size_t lenght = (p - buf);
342
343 cur = xmalloc(sizeof(xlist_t) + lenght);
344 cur->data = memcpy(cur + 1, s, lenght);
345 cur->lenght = lenght;
346 cur->link = NULL;
347 if (prev == NULL) {
348 list_arg = cur;
349 } else {
350 prev->link = cur;
351 }
352 prev = cur;
353 line_l += lenght;
354 if (line_l > mc) {
355 /* stop memory usage :-) */
356 break;
357 }
358 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000359 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000360 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000361 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000362}
Eric Andersen252183e2003-10-31 08:19:44 +0000363#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000364
Denis Vlasenko6248a732006-09-29 08:20:30 +0000365/* Correct regardless of combination of CONFIG_xxx */
366enum {
367 OPTBIT_VERBOSE = 0,
368 OPTBIT_NO_EMPTY,
369 OPTBIT_UPTO_NUMBER,
370 OPTBIT_UPTO_SIZE,
371 OPTBIT_EOF_STRING,
372 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
373 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
374 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000375
Denis Vlasenko6248a732006-09-29 08:20:30 +0000376 OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
377 OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
378 OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
379 OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
380 OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
381 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
382 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
383 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
384};
385#define OPTION_STR "+trn:s:e::" \
386 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
387 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
388 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000389
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000390int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000391{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000392 char **args;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000393 int i, n;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000394 xlist_t *list = NULL;
395 xlist_t *cur;
396 int child_error = 0;
397 char *max_args, *max_chars;
398 int n_max_arg;
399 size_t n_chars = 0;
400 long orig_arg_max;
401 const char *eof_str = "_";
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000402 unsigned opt;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000403 size_t n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000404#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
405 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
406#else
407#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000408#endif
409
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000410 opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000411
Denis Vlasenko6248a732006-09-29 08:20:30 +0000412 if (opt & OPT_ZEROTERM)
413 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000414
Denis Vlasenko6248a732006-09-29 08:20:30 +0000415 argc -= optind;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000416 argv += optind;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000417 if (!argc) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000418 /* default behavior is to echo all the filenames */
419 *argv = "echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000420 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000421 }
422
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000423 orig_arg_max = ARG_MAX;
424 if (orig_arg_max == -1)
425 orig_arg_max = LONG_MAX;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000426 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
427
428 if (opt & OPT_UPTO_SIZE) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000429 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000430 for (i = 0; i < argc; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000431 n_chars += strlen(*argv) + 1;
Glenn L McGrath61796942003-10-10 12:10:18 +0000432 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000433 if (n_max_chars < n_chars) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000434 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000435 }
436 n_max_chars -= n_chars;
437 } else {
438 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
439 have it at 1 meg). Things will work fine with a large ARG_MAX but it
440 will probably hurt the system more than it needs to; an array of this
441 size is allocated. */
442 if (orig_arg_max > 20 * 1024)
443 orig_arg_max = 20 * 1024;
444 n_max_chars = orig_arg_max;
Glenn L McGrath61796942003-10-10 12:10:18 +0000445 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000446 max_chars = xmalloc(n_max_chars);
447
Denis Vlasenko6248a732006-09-29 08:20:30 +0000448 if (opt & OPT_UPTO_NUMBER) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000449 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000450 } else {
451 n_max_arg = n_max_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000452 }
453
Denis Vlasenko6248a732006-09-29 08:20:30 +0000454 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
455 !(opt & OPT_NO_EMPTY))
Eric Andersen252183e2003-10-31 08:19:44 +0000456 {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000457 opt |= OPT_NO_EMPTY;
458 n = 0;
459 n_chars = 0;
460#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
461 for (cur = list; cur;) {
462 n_chars += cur->lenght;
463 n++;
464 cur = cur->link;
465 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
466 if (opt & OPT_TERMINATE)
467 bb_error_msg_and_die("argument list too long");
468 break;
469 }
470 }
471#else
472 for (cur = list; cur; cur = cur->link) {
473 n_chars += cur->lenght;
474 n++;
475 if (n_chars > n_max_chars || n == n_max_arg) {
476 break;
477 }
478 }
Eric Andersen252183e2003-10-31 08:19:44 +0000479#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000480
Denis Vlasenko6248a732006-09-29 08:20:30 +0000481 /* allocate pointers for execvp:
482 argc*arg, n*arg from stdin, NULL */
483 args = xzalloc((n + argc + 1) * sizeof(char *));
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000484
Denis Vlasenko6248a732006-09-29 08:20:30 +0000485 /* store the command to be executed
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000486 (taken from the command line) */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000487 for (i = 0; i < argc; i++)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000488 args[i] = argv[i];
489 /* (taken from stdin) */
490 for (cur = list; n; cur = cur->link) {
491 args[i++] = cur->data;
492 n--;
493 }
494
Denis Vlasenko6248a732006-09-29 08:20:30 +0000495 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000496 for (i = 0; args[i]; i++) {
497 if (i)
498 fputc(' ', stderr);
499 fputs(args[i], stderr);
500 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000501 if (!(opt & OPT_INTERACTIVE))
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000502 fputc('\n', stderr);
503 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000504 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000505 child_error = xargs_exec(args);
506 }
507
508 /* clean up */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000509 for (i = argc; args[i]; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000510 cur = list;
511 list = list->link;
512 free(cur);
513 }
514 free(args);
515 if (child_error > 0 && child_error != 123) {
516 break;
517 }
518 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000519 if (ENABLE_FEATURE_CLEAN_UP) free(max_chars);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000520 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000521}
522
523
524#ifdef TEST
525
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000526const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000527
528void bb_show_usage(void)
529{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000531 applet_name);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000532 exit(1);
Glenn L McGrath61796942003-10-10 12:10:18 +0000533}
534
535int main(int argc, char **argv)
536{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000537 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000538}
Eric Andersen252183e2003-10-31 08:19:44 +0000539#endif /* TEST */