blob: c369bdaf5014ac264d5f0e7fcfeda9dfec7cef29 [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
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
Eric Andersenaff114c2004-04-14 17:51:38 +00008 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
Glenn L McGrath61796942003-10-10 12:10:18 +00009 * - Mike Rendell <michael@cs.mun.ca>
10 * and David MacKenzie <djm@gnu.ai.mit.edu>.
Eric Andersen5b176932000-09-22 20:22:28 +000011 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersena37d5b72000-09-23 06:10:14 +000013 *
Glenn L McGrath40c94892003-10-30 22:51:33 +000014 * xargs is described in the Single Unix Specification v3 at
15 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
Eric Andersen5b176932000-09-22 20:22:28 +000016 */
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020017//config:config XARGS
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020018//config: bool "xargs (6.7 kb)"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020019//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020020//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: xargs is used to execute a specified command for
22//config: every item from standard input.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020023//config:
24//config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
25//config: bool "Enable -p: prompt and confirmation"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020026//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020027//config: depends on XARGS
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: Support -p: prompt the user whether to run each command
30//config: line and read a line from the terminal.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020031//config:
32//config:config FEATURE_XARGS_SUPPORT_QUOTES
33//config: bool "Enable single and double quotes and backslash"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020034//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020035//config: depends on XARGS
36//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: Support quoting in the input.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020038//config:
39//config:config FEATURE_XARGS_SUPPORT_TERMOPT
40//config: bool "Enable -x: exit if -s or -n is exceeded"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020041//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020042//config: depends on XARGS
43//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020044//config: Support -x: exit if the command size (see the -s or -n option)
45//config: is exceeded.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020046//config:
47//config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
48//config: bool "Enable -0: NUL-terminated input"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020049//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020050//config: depends on XARGS
51//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020052//config: Support -0: input items are terminated by a NUL character
53//config: instead of whitespace, and the quotes and backslash
54//config: are not special.
Denys Vlasenko6f068902014-02-27 11:17:06 +010055//config:
56//config:config FEATURE_XARGS_SUPPORT_REPL_STR
57//config: bool "Enable -I STR: string to replace"
58//config: default y
59//config: depends on XARGS
60//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020061//config: Support -I STR and -i[STR] options.
Denys Vlasenko14551b72017-08-24 13:23:54 +020062//config:
63//config:config FEATURE_XARGS_SUPPORT_PARALLEL
64//config: bool "Enable -P N: processes to run in parallel"
65//config: default y
66//config: depends on XARGS
Johannes Schindelinf8ee8492017-08-25 22:42:05 +020067//config:
68//config:config FEATURE_XARGS_SUPPORT_ARGS_FILE
69//config: bool "Enable -a FILE: use FILE instead of stdin"
70//config: default y
71//config: depends on XARGS
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020072
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +010073//applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
74
75//kbuild:lib-$(CONFIG_XARGS) += xargs.o
76
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000077#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020078#include "common_bufsiz.h"
Glenn L McGrath61796942003-10-10 12:10:18 +000079
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020080/* This is a NOEXEC applet. Be very careful! */
81
82
83//#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
84#define dbg_msg(...) ((void)0)
85
Glenn L McGrath61796942003-10-10 12:10:18 +000086
87#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000088# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
89# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000090# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000091# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
92# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000093# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000094# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
95# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000096# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000097# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
98# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000099# endif
100#endif
101
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200102
103struct globals {
104 char **args;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100105#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
106 char **argv;
107 const char *repl_str;
108 char eol_ch;
109#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200110 const char *eof_str;
111 int idx;
Denys Vlasenko14551b72017-08-24 13:23:54 +0200112#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
113 int running_procs;
114 int max_procs;
115#endif
116 smalluint xargs_exitcode;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200117} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200118#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100119#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200120 setup_common_bufsiz(); \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100121 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
Denys Vlasenkob270be32017-08-24 11:21:27 +0200122 G.idx = 0; \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200123 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
124 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
125 G.xargs_exitcode = 0; \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100126 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
127 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100128} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200129
130
Denys Vlasenko14551b72017-08-24 13:23:54 +0200131/*
132 * Returns 0 if xargs should continue (but may set G.xargs_exitcode to 123).
133 * Else sets G.xargs_exitcode to error code and returns nonzero.
134 *
135 * If G.max_procs == 0, performs final waitpid() loop for all children.
136 */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200137static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000138{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000139 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000140
Denys Vlasenko14551b72017-08-24 13:23:54 +0200141#if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200142 status = spawn_and_wait(G.args);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200143#else
144 if (G.max_procs == 1) {
145 status = spawn_and_wait(G.args);
146 } else {
147 pid_t pid;
148 int wstat;
149 again:
150 if (G.running_procs >= G.max_procs)
151 pid = safe_waitpid(-1, &wstat, 0);
152 else
153 pid = wait_any_nohang(&wstat);
154 if (pid > 0) {
155 /* We may have children we don't know about:
156 * sh -c 'sleep 1 & exec xargs ...'
157 * Do not make G.running_procs go negative.
158 */
159 if (G.running_procs != 0)
160 G.running_procs--;
161 status = WIFSIGNALED(wstat)
162 ? 0x180 + WTERMSIG(wstat)
163 : WEXITSTATUS(wstat);
164 if (status > 0 && status < 255) {
165 /* See below why 123 does not abort */
166 G.xargs_exitcode = 123;
167 status = 0;
168 }
169 if (status == 0)
170 goto again; /* maybe we have more children? */
171 /* else: "bad" status, will bail out */
172 } else if (G.max_procs != 0) {
173 /* Not in final waitpid() loop,
174 * and G.running_procs < G.max_procs: start more procs
175 */
176 status = spawn(G.args);
177 /* here "status" actually holds pid, or -1 */
178 if (status > 0) {
179 G.running_procs++;
180 status = 0;
181 }
182 /* else: status == -1 (failed to fork or exec) */
183 } else {
184 /* final waitpid() loop: must be ECHILD "no more children" */
185 status = 0;
186 }
187 }
188#endif
189 /* Manpage:
190 * """xargs exits with the following status:
191 * 0 if it succeeds
192 * 123 if any invocation of the command exited with status 1-125
193 * 124 if the command exited with status 255
194 * ("""If any invocation of the command exits with a status of 255,
195 * xargs will stop immediately without reading any further input.
196 * An error message is issued on stderr when this happens.""")
197 * 125 if the command is killed by a signal
198 * 126 if the command cannot be run
199 * 127 if the command is not found
200 * 1 if some other error occurred."""
201 */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000202 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200203 bb_simple_perror_msg(G.args[0]);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200204 status = (errno == ENOENT) ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000205 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200206 else if (status >= 0x180) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200207 bb_error_msg("'%s' terminated by signal %d",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200208 G.args[0], status - 0x180);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200209 status = 125;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000210 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200211 else if (status != 0) {
212 if (status == 255) {
213 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
214 return 124;
215 }
216 /* "123 if any invocation of the command exited with status 1-125"
217 * This implies that nonzero exit code is remembered,
218 * but does not cause xargs to stop: we return 0.
219 */
220 G.xargs_exitcode = 123;
221 status = 0;
222 }
223
224 if (status != 0)
225 G.xargs_exitcode = status;
226 return status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000227}
Glenn L McGrath99825962003-10-09 11:06:45 +0000228
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200229/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
230 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
231 */
232#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000233
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200234static void store_param(char *s)
235{
236 /* Grow by 256 elements at once */
Denys Vlasenkob270be32017-08-24 11:21:27 +0200237 if (!(G.idx & 0xff)) { /* G.idx == N*256? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200238 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
239 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
240 }
241 G.args[G.idx++] = s;
242}
243
244/* process[0]_stdin:
245 * Read characters into buf[n_max_chars+1], and when parameter delimiter
246 * is seen, store the address of a new parameter to args[].
247 * If reading discovers that last chars do not form the complete
248 * parameter, the pointer to the first such "tail character" is returned.
Maninder Singh97c64912015-05-25 13:46:36 +0200249 * (buf has extra byte at the end to accommodate terminating NUL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200250 * of "tail characters" string).
251 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200252 * On entry, buf[] may contain some "seed chars" which are to become
253 * the beginning of the first parameter.
254 */
255
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000256#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200257static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000258{
259#define NORM 0
260#define QUOTE 1
261#define BACKSLASH 2
262#define SPACE 4
Denys Vlasenko237aece2010-06-15 10:18:01 +0200263 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000264 char state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200265 char *s = buf; /* start of the word */
266 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000267
Denys Vlasenko237aece2010-06-15 10:18:01 +0200268 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200269
270 /* "goto ret" is used instead of "break" to make control flow
271 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000272
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200273 while (1) {
274 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000275 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200276 if (p != s)
277 goto close_word;
278 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000279 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000280 if (state == BACKSLASH) {
281 state = NORM;
282 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200283 }
284 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000285 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000286 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000287 q = '\0';
288 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000289 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000290 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200291 if (p != s) {
292 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000293 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000294 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000295 goto set;
296 }
297 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000298 if (c == '\\') {
299 state = BACKSLASH;
300 } else if (c == '\'' || c == '"') {
301 q = c;
302 state = QUOTE;
303 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000304 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000305 *p++ = c;
306 }
307 }
308 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000309 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000310 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000312 q == '\'' ? "single" : "double");
313 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200314 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200315 if (G.eof_str) {
316 if (strcmp(s, G.eof_str) == 0) {
317 while (getchar() != EOF)
318 continue;
319 p = s;
320 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000321 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000322 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200323 store_param(s);
324 dbg_msg("args[]:'%s'", s);
325 s = p;
326 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200327 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200328 goto ret;
329 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000330 state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200331 }
332 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200333 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000334 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000335 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200336 ret:
337 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200338 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200339 dbg_msg("return:'%s'", s);
340 return s;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000341}
Glenn L McGrath61796942003-10-10 12:10:18 +0000342#else
Eric Andersen252183e2003-10-31 08:19:44 +0000343/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200344static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000345{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200346 char *s = buf; /* start of the word */
347 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000348
Denys Vlasenko237aece2010-06-15 10:18:01 +0200349 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000350
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200351 while (1) {
352 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000353 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200354 if (p == s)
355 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200356 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000357 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200358 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000359 continue;
360 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000361 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000362 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000363 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200364 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200365 if (G.eof_str) {
366 if (strcmp(s, G.eof_str) == 0) {
367 while (getchar() != EOF)
368 continue;
369 p = s;
370 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000371 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000372 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200373 store_param(s);
374 dbg_msg("args[]:'%s'", s);
375 s = p;
376 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200377 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200378 goto ret;
379 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200380 }
381 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200382 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000383 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000384 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200385 ret:
386 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200387 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200388 dbg_msg("return:'%s'", s);
389 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000390}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000391#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000392
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000393#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200394static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000395{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200396 char *s = buf; /* start of the word */
397 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000398
Denys Vlasenko237aece2010-06-15 10:18:01 +0200399 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000400
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200401 while (1) {
402 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000403 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200404 if (p == s)
405 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000406 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000407 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000408 *p++ = c;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100409 if (c == '\0') { /* NUL or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200410 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200411 store_param(s);
412 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200413 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200414 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200415 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200416 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000417 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200418 }
419 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200420 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000421 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000422 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200423 ret:
424 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200425 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200426 dbg_msg("return:'%s'", s);
427 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000428}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000429#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000430
Denys Vlasenko6f068902014-02-27 11:17:06 +0100431#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
432/*
433 * Used if -I<repl> was specified.
434 * In this mode, words aren't appended to PROG ARGS.
435 * Instead, entire input line is read, then <repl> string
436 * in every PROG and ARG is replaced with the line:
437 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
438 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
439 * -n MAX_ARGS seems to be ignored.
440 * Tested with GNU findutils 4.5.10.
441 */
442//FIXME: n_max_chars is not handled the same way as in GNU findutils.
443//FIXME: quoting is not implemented.
444static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
445{
446 int i;
447 char *end, *p;
448
449 /* Free strings from last invocation, if any */
450 for (i = 0; G.args && G.args[i]; i++)
451 if (G.args[i] != G.argv[i])
452 free(G.args[i]);
453
454 end = buf + n_max_chars;
455 p = buf;
456
457 while (1) {
458 int c = getchar();
459 if (c == EOF || c == G.eol_ch) {
460 if (p == buf)
461 goto ret; /* empty line */
462 c = '\0';
463 }
464 *p++ = c;
465 if (c == '\0') { /* EOL or EOF detected */
466 i = 0;
467 while (G.argv[i]) {
468 char *arg = G.argv[i];
469 int count = count_strstr(arg, G.repl_str);
470 if (count != 0)
Denys Vlasenko37952662014-02-27 15:02:36 +0100471 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100472 store_param(arg);
473 dbg_msg("args[]:'%s'", arg);
474 i++;
475 }
476 p = buf;
477 goto ret;
478 }
479 if (p == end) {
480 goto ret;
481 }
482 }
483 ret:
484 *p = '\0';
485 /* store_param(NULL) - caller will do it */
486 dbg_msg("return:'%s'", buf);
487 return buf;
488}
489#endif
490
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200491#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
492/* Prompt the user for a response, and
Denys Vlasenko6f068902014-02-27 11:17:06 +0100493 * if user responds affirmatively, return true;
494 * otherwise, return false. Uses "/dev/tty", not stdin.
495 */
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200496static int xargs_ask_confirmation(void)
497{
498 FILE *tty_stream;
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200499 int r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200500
501 tty_stream = xfopen_for_read(CURRENT_TTY);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200502
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200503 fputs(" ?...", stderr);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200504 r = bb_ask_y_confirmation_FILE(tty_stream);
505
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200506 fclose(tty_stream);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200507
508 return r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200509}
510#else
511# define xargs_ask_confirmation() 1
512#endif
513
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200514//usage:#define xargs_trivial_usage
515//usage: "[OPTIONS] [PROG ARGS]"
516//usage:#define xargs_full_usage "\n\n"
517//usage: "Run PROG on every item given by stdin\n"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200518//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
519//usage: "\n -p Ask user whether to run each command"
520//usage: )
521//usage: "\n -r Don't run command if input is empty"
522//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
Denys Vlasenko4d0e2d52018-07-06 20:49:08 +0200523//usage: "\n -0 Input is separated by NULs"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200524//usage: )
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200525//usage: IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
526//usage: "\n -a FILE Read from FILE instead of stdin"
527//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200528//usage: "\n -t Print the command on stderr before execution"
529//usage: "\n -e[STR] STR stops input processing"
530//usage: "\n -n N Pass no more than N args to PROG"
531//usage: "\n -s N Pass command line of no more than N bytes"
Denys Vlasenko6f068902014-02-27 11:17:06 +0100532//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
533//usage: "\n -I STR Replace STR within PROG ARGS with input line"
534//usage: )
Denys Vlasenko14551b72017-08-24 13:23:54 +0200535//usage: IF_FEATURE_XARGS_SUPPORT_PARALLEL(
536//usage: "\n -P N Run up to N PROGs in parallel"
537//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200538//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
539//usage: "\n -x Exit if size is exceeded"
540//usage: )
541//usage:#define xargs_example_usage
542//usage: "$ ls | xargs gzip\n"
543//usage: "$ find . -name '*.c' -print | xargs rm\n"
544
Denis Vlasenko6248a732006-09-29 08:20:30 +0000545/* Correct regardless of combination of CONFIG_xxx */
546enum {
547 OPTBIT_VERBOSE = 0,
548 OPTBIT_NO_EMPTY,
549 OPTBIT_UPTO_NUMBER,
550 OPTBIT_UPTO_SIZE,
551 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000552 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000553 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
554 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
555 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100556 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
557 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000558
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000559 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
560 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
561 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
562 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
563 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
564 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000565 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
566 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
567 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denys Vlasenko6f068902014-02-27 11:17:06 +0100568 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
569 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000570};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000571#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000572 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
573 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100574 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200575 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200576 IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \
577 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:")
Glenn L McGrath61796942003-10-10 12:10:18 +0000578
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000579int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200580int xargs_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000581{
Denys Vlasenkob270be32017-08-24 11:21:27 +0200582 int initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200583 int i;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200584 char *max_args;
585 char *max_chars;
586 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000587 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200588 int n_max_chars;
589 int n_max_arg;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100590#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
591 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200592 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000593#else
594#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000595#endif
Denys Vlasenko58bf9022017-08-30 13:44:27 +0200596 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
Glenn L McGrath61796942003-10-10 12:10:18 +0000597
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200598 INIT_G();
599
Denys Vlasenko036585a2017-08-08 16:38:18 +0200600 opt = getopt32long(argv, OPTION_STR,
601 "no-run-if-empty\0" No_argument "r",
Denys Vlasenko6f068902014-02-27 11:17:06 +0100602 &max_args, &max_chars, &G.eof_str, &G.eof_str
603 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
Denys Vlasenko14551b72017-08-24 13:23:54 +0200604 IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200605 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100606 );
Glenn L McGrath61796942003-10-10 12:10:18 +0000607
Denys Vlasenko14551b72017-08-24 13:23:54 +0200608#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
609 if (G.max_procs <= 0) /* -P0 means "run lots of them" */
610 G.max_procs = 100; /* let's not go crazy high */
611#endif
612
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200613#if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
614 if (opt_a)
615 xmove_fd(xopen(opt_a, O_RDONLY), 0);
616#endif
617
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000618 /* -E ""? You may wonder why not just omit -E?
619 * This is used for portability:
620 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200621 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
622 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000623
Denys Vlasenko6f068902014-02-27 11:17:06 +0100624 if (opt & OPT_ZEROTERM) {
625 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
626 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
627 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000628
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000629 argv += optind;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200630 //argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200631 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000632 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200633 *--argv = (char*)"echo";
Denys Vlasenkob270be32017-08-24 11:21:27 +0200634 //argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000635 }
636
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200637 /*
Denys Vlasenko4a966172010-06-19 21:44:01 +0200638 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200639 * "The xargs utility shall limit the command line length such that
640 * when the command line is invoked, the combined argument
641 * and environment lists (see the exec family of functions
642 * in the System Interfaces volume of IEEE Std 1003.1-2001)
643 * shall not exceed {ARG_MAX}-2048 bytes".
644 */
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200645 n_max_chars = bb_arg_max();
646 if (n_max_chars > 32 * 1024)
647 n_max_chars = 32 * 1024;
648 /*
649 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
650 * so that the process may safely modify its environment.
651 */
652 n_max_chars -= 2048;
653
Denis Vlasenko6248a732006-09-29 08:20:30 +0000654 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200655 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200656 }
657 /* Account for prepended fixed arguments */
658 {
659 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200660 for (i = 0; argv[i]; i++) {
661 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000662 }
663 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200664 }
665 /* Sanity check */
666 if (n_max_chars <= 0) {
667 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000668 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200669
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200670 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000671
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200672 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000673 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200674 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200675 /* Not necessary, we use growable args[]: */
676 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000677 }
678
Denys Vlasenko6f068902014-02-27 11:17:06 +0100679#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
680 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
681 /*
682 * -I<str>:
683 * Unmodified args are kept in G.argv[i],
684 * G.args[i] receives malloced G.argv[i] with <str> replaced
685 * with input line. Setting this up:
686 */
687 G.args = NULL;
688 G.argv = argv;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100689 read_args = process_stdin_with_replace;
Aaro Koskinen6d777b72015-10-24 22:01:29 +0200690 /* Make -I imply -r. GNU findutils seems to do the same: */
691 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
692 opt |= OPT_NO_EMPTY;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100693 } else
694#endif
695 {
Denys Vlasenkob270be32017-08-24 11:21:27 +0200696 /* Store the command to be executed, part 1.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100697 * We can statically allocate (argc + n_max_arg + 1) elements
698 * and do not bother with resizing args[], but on 64-bit machines
699 * this results in args[] vector which is ~8 times bigger
700 * than n_max_chars! That is, with n_max_chars == 20k,
701 * args[] will take 160k (!), which will most likely be
702 * almost entirely unused.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100703 */
Denys Vlasenko6f068902014-02-27 11:17:06 +0100704 for (i = 0; argv[i]; i++)
Denys Vlasenkob270be32017-08-24 11:21:27 +0200705 store_param(argv[i]);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100706 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200707
Denys Vlasenkob270be32017-08-24 11:21:27 +0200708 initial_idx = G.idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200709 while (1) {
710 char *rem;
711
Denys Vlasenkob270be32017-08-24 11:21:27 +0200712 G.idx = initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200713 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200714 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200715
Denys Vlasenkob270be32017-08-24 11:21:27 +0200716 if (!G.args[initial_idx]) { /* not even one ARG was added? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200717 if (*rem != '\0')
718 bb_error_msg_and_die("argument line too long");
719 if (opt & OPT_NO_EMPTY)
720 break;
721 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000722 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200723
Denis Vlasenko6248a732006-09-29 08:20:30 +0000724 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200725 const char *fmt = " %s" + 1;
726 char **args = G.args;
727 for (i = 0; args[i]; i++) {
728 fprintf(stderr, fmt, args[i]);
729 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000730 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000731 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200732 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000733 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200734
Denis Vlasenko6248a732006-09-29 08:20:30 +0000735 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko14551b72017-08-24 13:23:54 +0200736 if (xargs_exec() != 0)
737 break; /* G.xargs_exitcode is set by xargs_exec() */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000738 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200739
740 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000741 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200742
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200743 if (ENABLE_FEATURE_CLEAN_UP) {
744 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200745 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200746 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200747
Denys Vlasenko14551b72017-08-24 13:23:54 +0200748#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
749 G.max_procs = 0;
750 xargs_exec(); /* final waitpid() loop */
751#endif
752
753 return G.xargs_exitcode;
Glenn L McGrath61796942003-10-10 12:10:18 +0000754}
755
756
757#ifdef TEST
758
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000759const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000760
761void bb_show_usage(void)
762{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000763 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000764 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000765 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000766}
767
768int main(int argc, char **argv)
769{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000770 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000771}
Eric Andersen252183e2003-10-31 08:19:44 +0000772#endif /* TEST */