blob: ff04bfe7c967d88f93cc8023e5765ee665088a17 [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 Vlasenkob097a842018-12-28 03:20:17 +010018//config: bool "xargs (7.2 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;
Ron Yorston1ff70022020-01-24 13:16:45 +0000117#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
118#define NORM 0
119#define QUOTE 1
120#define BACKSLASH 2
121#define SPACE 4
122 smalluint process_stdin__state;
123 char process_stdin__q;
124#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200125} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200126#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100127#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200128 setup_common_bufsiz(); \
Ron Yorston1ff70022020-01-24 13:16:45 +0000129 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
130 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
131 /* Even zero values are set because we are NOEXEC applet */ \
132 G.eof_str = NULL; \
Denys Vlasenkob270be32017-08-24 11:21:27 +0200133 G.idx = 0; \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200134 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
135 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
136 G.xargs_exitcode = 0; \
Ron Yorston1ff70022020-01-24 13:16:45 +0000137 IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__state = NORM;) \
138 IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__q = '\0';) \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100139} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200140
141
Denys Vlasenko14551b72017-08-24 13:23:54 +0200142/*
143 * Returns 0 if xargs should continue (but may set G.xargs_exitcode to 123).
144 * Else sets G.xargs_exitcode to error code and returns nonzero.
145 *
146 * If G.max_procs == 0, performs final waitpid() loop for all children.
147 */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200148static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000149{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000150 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000151
Denys Vlasenko14551b72017-08-24 13:23:54 +0200152#if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200153 status = spawn_and_wait(G.args);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200154#else
155 if (G.max_procs == 1) {
156 status = spawn_and_wait(G.args);
157 } else {
158 pid_t pid;
159 int wstat;
160 again:
161 if (G.running_procs >= G.max_procs)
162 pid = safe_waitpid(-1, &wstat, 0);
163 else
164 pid = wait_any_nohang(&wstat);
165 if (pid > 0) {
166 /* We may have children we don't know about:
167 * sh -c 'sleep 1 & exec xargs ...'
168 * Do not make G.running_procs go negative.
169 */
170 if (G.running_procs != 0)
171 G.running_procs--;
172 status = WIFSIGNALED(wstat)
173 ? 0x180 + WTERMSIG(wstat)
174 : WEXITSTATUS(wstat);
175 if (status > 0 && status < 255) {
176 /* See below why 123 does not abort */
177 G.xargs_exitcode = 123;
178 status = 0;
179 }
180 if (status == 0)
181 goto again; /* maybe we have more children? */
182 /* else: "bad" status, will bail out */
183 } else if (G.max_procs != 0) {
184 /* Not in final waitpid() loop,
185 * and G.running_procs < G.max_procs: start more procs
186 */
187 status = spawn(G.args);
188 /* here "status" actually holds pid, or -1 */
189 if (status > 0) {
190 G.running_procs++;
191 status = 0;
192 }
193 /* else: status == -1 (failed to fork or exec) */
194 } else {
195 /* final waitpid() loop: must be ECHILD "no more children" */
196 status = 0;
197 }
198 }
199#endif
200 /* Manpage:
201 * """xargs exits with the following status:
202 * 0 if it succeeds
203 * 123 if any invocation of the command exited with status 1-125
204 * 124 if the command exited with status 255
205 * ("""If any invocation of the command exits with a status of 255,
206 * xargs will stop immediately without reading any further input.
207 * An error message is issued on stderr when this happens.""")
208 * 125 if the command is killed by a signal
209 * 126 if the command cannot be run
210 * 127 if the command is not found
211 * 1 if some other error occurred."""
212 */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000213 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200214 bb_simple_perror_msg(G.args[0]);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200215 status = (errno == ENOENT) ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000216 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200217 else if (status >= 0x180) {
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100218 bb_error_msg("'%s' terminated by signal %u",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200219 G.args[0], status - 0x180);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200220 status = 125;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000221 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200222 else if (status != 0) {
223 if (status == 255) {
224 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100225 status = 124;
226 goto ret;
Denys Vlasenko14551b72017-08-24 13:23:54 +0200227 }
228 /* "123 if any invocation of the command exited with status 1-125"
229 * This implies that nonzero exit code is remembered,
230 * but does not cause xargs to stop: we return 0.
231 */
232 G.xargs_exitcode = 123;
233 status = 0;
234 }
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100235 ret:
Denys Vlasenko14551b72017-08-24 13:23:54 +0200236 if (status != 0)
237 G.xargs_exitcode = status;
238 return status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000239}
Glenn L McGrath99825962003-10-09 11:06:45 +0000240
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200241/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
242 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
243 */
244#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000245
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200246static void store_param(char *s)
247{
248 /* Grow by 256 elements at once */
Denys Vlasenkob270be32017-08-24 11:21:27 +0200249 if (!(G.idx & 0xff)) { /* G.idx == N*256? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200250 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
251 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
252 }
253 G.args[G.idx++] = s;
254}
255
256/* process[0]_stdin:
257 * Read characters into buf[n_max_chars+1], and when parameter delimiter
258 * is seen, store the address of a new parameter to args[].
259 * If reading discovers that last chars do not form the complete
260 * parameter, the pointer to the first such "tail character" is returned.
Maninder Singh97c64912015-05-25 13:46:36 +0200261 * (buf has extra byte at the end to accommodate terminating NUL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200262 * of "tail characters" string).
263 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200264 * On entry, buf[] may contain some "seed chars" which are to become
265 * the beginning of the first parameter.
266 */
267
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000268#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200269static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000270{
Ron Yorston1ff70022020-01-24 13:16:45 +0000271#define q G.process_stdin__q
272#define state G.process_stdin__state
Denys Vlasenko237aece2010-06-15 10:18:01 +0200273 char *s = buf; /* start of the word */
274 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000275
Denys Vlasenko237aece2010-06-15 10:18:01 +0200276 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200277
278 /* "goto ret" is used instead of "break" to make control flow
279 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000280
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200281 while (1) {
282 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000283 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200284 if (p != s)
285 goto close_word;
286 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000287 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000288 if (state == BACKSLASH) {
289 state = NORM;
290 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200291 }
292 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000293 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000294 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000295 q = '\0';
296 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000297 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000298 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200299 if (p != s) {
300 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000301 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000302 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000303 goto set;
304 }
305 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000306 if (c == '\\') {
307 state = BACKSLASH;
308 } else if (c == '\'' || c == '"') {
309 q = c;
310 state = QUOTE;
311 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000312 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000313 *p++ = c;
314 }
315 }
316 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000317 if (state == SPACE) { /* word's delimiter or EOF detected */
Ron Yorston1c462d42020-02-19 11:01:39 +0000318 state = NORM;
Eric Andersen252183e2003-10-31 08:19:44 +0000319 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000320 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000321 q == '\'' ? "single" : "double");
322 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200323 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200324 if (G.eof_str) {
325 if (strcmp(s, G.eof_str) == 0) {
326 while (getchar() != EOF)
327 continue;
328 p = s;
329 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000330 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000331 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200332 store_param(s);
333 dbg_msg("args[]:'%s'", s);
334 s = p;
335 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200336 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200337 goto ret;
338 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200339 }
340 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200341 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000342 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000343 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200344 ret:
345 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200346 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200347 dbg_msg("return:'%s'", s);
348 return s;
Ron Yorston1ff70022020-01-24 13:16:45 +0000349#undef q
350#undef state
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000351}
Glenn L McGrath61796942003-10-10 12:10:18 +0000352#else
Eric Andersen252183e2003-10-31 08:19:44 +0000353/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200354static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000355{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200356 char *s = buf; /* start of the word */
357 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000358
Denys Vlasenko237aece2010-06-15 10:18:01 +0200359 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000360
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200361 while (1) {
362 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000363 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200364 if (p == s)
365 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200366 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000367 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200368 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000369 continue;
370 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000371 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000372 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000373 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200374 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200375 if (G.eof_str) {
376 if (strcmp(s, G.eof_str) == 0) {
377 while (getchar() != EOF)
378 continue;
379 p = s;
380 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000381 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000382 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200383 store_param(s);
384 dbg_msg("args[]:'%s'", s);
385 s = p;
386 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200387 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200388 goto ret;
389 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200390 }
391 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200392 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000393 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000394 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200395 ret:
396 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200397 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200398 dbg_msg("return:'%s'", s);
399 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000400}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000401#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000402
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000403#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200404static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000405{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200406 char *s = buf; /* start of the word */
407 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000408
Denys Vlasenko237aece2010-06-15 10:18:01 +0200409 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000410
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200411 while (1) {
412 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000413 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200414 if (p == s)
415 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000416 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000417 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000418 *p++ = c;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100419 if (c == '\0') { /* NUL or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200420 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200421 store_param(s);
422 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200423 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200424 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200425 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200426 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000427 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200428 }
429 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200430 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000431 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000432 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200433 ret:
434 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200435 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200436 dbg_msg("return:'%s'", s);
437 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000438}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000439#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000440
Denys Vlasenko6f068902014-02-27 11:17:06 +0100441#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
442/*
443 * Used if -I<repl> was specified.
444 * In this mode, words aren't appended to PROG ARGS.
445 * Instead, entire input line is read, then <repl> string
446 * in every PROG and ARG is replaced with the line:
447 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
448 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
449 * -n MAX_ARGS seems to be ignored.
450 * Tested with GNU findutils 4.5.10.
451 */
452//FIXME: n_max_chars is not handled the same way as in GNU findutils.
453//FIXME: quoting is not implemented.
454static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
455{
456 int i;
457 char *end, *p;
458
459 /* Free strings from last invocation, if any */
460 for (i = 0; G.args && G.args[i]; i++)
461 if (G.args[i] != G.argv[i])
462 free(G.args[i]);
463
464 end = buf + n_max_chars;
465 p = buf;
466
467 while (1) {
468 int c = getchar();
469 if (c == EOF || c == G.eol_ch) {
470 if (p == buf)
471 goto ret; /* empty line */
472 c = '\0';
473 }
474 *p++ = c;
475 if (c == '\0') { /* EOL or EOF detected */
476 i = 0;
477 while (G.argv[i]) {
478 char *arg = G.argv[i];
479 int count = count_strstr(arg, G.repl_str);
480 if (count != 0)
Denys Vlasenko37952662014-02-27 15:02:36 +0100481 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100482 store_param(arg);
483 dbg_msg("args[]:'%s'", arg);
484 i++;
485 }
486 p = buf;
487 goto ret;
488 }
489 if (p == end) {
490 goto ret;
491 }
492 }
493 ret:
494 *p = '\0';
495 /* store_param(NULL) - caller will do it */
496 dbg_msg("return:'%s'", buf);
497 return buf;
498}
499#endif
500
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200501#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
502/* Prompt the user for a response, and
Denys Vlasenko6f068902014-02-27 11:17:06 +0100503 * if user responds affirmatively, return true;
504 * otherwise, return false. Uses "/dev/tty", not stdin.
505 */
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200506static int xargs_ask_confirmation(void)
507{
508 FILE *tty_stream;
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200509 int r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200510
511 tty_stream = xfopen_for_read(CURRENT_TTY);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200512
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200513 fputs(" ?...", stderr);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200514 r = bb_ask_y_confirmation_FILE(tty_stream);
515
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200516 fclose(tty_stream);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200517
518 return r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200519}
520#else
521# define xargs_ask_confirmation() 1
522#endif
523
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200524//usage:#define xargs_trivial_usage
525//usage: "[OPTIONS] [PROG ARGS]"
526//usage:#define xargs_full_usage "\n\n"
527//usage: "Run PROG on every item given by stdin\n"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200528//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
Denys Vlasenko4d0e2d52018-07-06 20:49:08 +0200529//usage: "\n -0 Input is separated by NULs"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200530//usage: )
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200531//usage: IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
532//usage: "\n -a FILE Read from FILE instead of stdin"
533//usage: )
Denys Vlasenko24522472019-03-10 14:29:03 +0100534//usage: "\n -r Don't run command if input is empty"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200535//usage: "\n -t Print the command on stderr before execution"
Denys Vlasenko24522472019-03-10 14:29:03 +0100536//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
537//usage: "\n -p Ask user whether to run each command"
538//usage: )
539//usage: "\n -E STR,-e[STR] STR stops input processing"
Denys Vlasenko6f068902014-02-27 11:17:06 +0100540//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
541//usage: "\n -I STR Replace STR within PROG ARGS with input line"
542//usage: )
Denys Vlasenko24522472019-03-10 14:29:03 +0100543//usage: "\n -n N Pass no more than N args to PROG"
544//usage: "\n -s N Pass command line of no more than N bytes"
Denys Vlasenko14551b72017-08-24 13:23:54 +0200545//usage: IF_FEATURE_XARGS_SUPPORT_PARALLEL(
546//usage: "\n -P N Run up to N PROGs in parallel"
547//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200548//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
549//usage: "\n -x Exit if size is exceeded"
550//usage: )
551//usage:#define xargs_example_usage
552//usage: "$ ls | xargs gzip\n"
553//usage: "$ find . -name '*.c' -print | xargs rm\n"
554
Denis Vlasenko6248a732006-09-29 08:20:30 +0000555/* Correct regardless of combination of CONFIG_xxx */
556enum {
557 OPTBIT_VERBOSE = 0,
558 OPTBIT_NO_EMPTY,
559 OPTBIT_UPTO_NUMBER,
560 OPTBIT_UPTO_SIZE,
561 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000562 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000563 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
564 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
565 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100566 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
567 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000568
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000569 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
570 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
571 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
572 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
573 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
574 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000575 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
576 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
577 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denys Vlasenko6f068902014-02-27 11:17:06 +0100578 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
579 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000580};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000581#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000582 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
583 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100584 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200585 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200586 IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \
587 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:")
Glenn L McGrath61796942003-10-10 12:10:18 +0000588
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000589int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200590int xargs_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000591{
Denys Vlasenkob270be32017-08-24 11:21:27 +0200592 int initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200593 int i;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200594 char *max_args;
595 char *max_chars;
596 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000597 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200598 int n_max_chars;
599 int n_max_arg;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100600#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
601 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200602 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000603#else
604#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000605#endif
Denys Vlasenko58bf9022017-08-30 13:44:27 +0200606 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
Glenn L McGrath61796942003-10-10 12:10:18 +0000607
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200608 INIT_G();
609
Denys Vlasenko036585a2017-08-08 16:38:18 +0200610 opt = getopt32long(argv, OPTION_STR,
611 "no-run-if-empty\0" No_argument "r",
Denys Vlasenko6f068902014-02-27 11:17:06 +0100612 &max_args, &max_chars, &G.eof_str, &G.eof_str
613 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
Denys Vlasenko14551b72017-08-24 13:23:54 +0200614 IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200615 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100616 );
Glenn L McGrath61796942003-10-10 12:10:18 +0000617
Denys Vlasenko14551b72017-08-24 13:23:54 +0200618#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
619 if (G.max_procs <= 0) /* -P0 means "run lots of them" */
620 G.max_procs = 100; /* let's not go crazy high */
621#endif
622
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200623#if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
624 if (opt_a)
625 xmove_fd(xopen(opt_a, O_RDONLY), 0);
626#endif
627
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000628 /* -E ""? You may wonder why not just omit -E?
629 * This is used for portability:
630 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200631 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
632 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000633
Denys Vlasenko6f068902014-02-27 11:17:06 +0100634 if (opt & OPT_ZEROTERM) {
635 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
636 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
637 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000638
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000639 argv += optind;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200640 //argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200641 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000642 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200643 *--argv = (char*)"echo";
Denys Vlasenkob270be32017-08-24 11:21:27 +0200644 //argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000645 }
646
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200647 /*
Denys Vlasenko4a966172010-06-19 21:44:01 +0200648 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200649 * "The xargs utility shall limit the command line length such that
650 * when the command line is invoked, the combined argument
651 * and environment lists (see the exec family of functions
652 * in the System Interfaces volume of IEEE Std 1003.1-2001)
653 * shall not exceed {ARG_MAX}-2048 bytes".
654 */
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200655 n_max_chars = bb_arg_max();
656 if (n_max_chars > 32 * 1024)
657 n_max_chars = 32 * 1024;
658 /*
659 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
660 * so that the process may safely modify its environment.
661 */
662 n_max_chars -= 2048;
663
Denis Vlasenko6248a732006-09-29 08:20:30 +0000664 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200665 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200666 }
667 /* Account for prepended fixed arguments */
668 {
669 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200670 for (i = 0; argv[i]; i++) {
671 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000672 }
673 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200674 }
675 /* Sanity check */
676 if (n_max_chars <= 0) {
James Byrne69374872019-07-02 11:35:03 +0200677 bb_simple_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000678 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200679
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200680 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000681
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200682 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000683 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200684 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200685 /* Not necessary, we use growable args[]: */
686 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000687 }
688
Denys Vlasenko6f068902014-02-27 11:17:06 +0100689#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
690 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
691 /*
692 * -I<str>:
693 * Unmodified args are kept in G.argv[i],
694 * G.args[i] receives malloced G.argv[i] with <str> replaced
695 * with input line. Setting this up:
696 */
697 G.args = NULL;
698 G.argv = argv;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100699 read_args = process_stdin_with_replace;
Aaro Koskinen6d777b72015-10-24 22:01:29 +0200700 /* Make -I imply -r. GNU findutils seems to do the same: */
701 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
702 opt |= OPT_NO_EMPTY;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100703 } else
704#endif
705 {
Denys Vlasenkob270be32017-08-24 11:21:27 +0200706 /* Store the command to be executed, part 1.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100707 * We can statically allocate (argc + n_max_arg + 1) elements
708 * and do not bother with resizing args[], but on 64-bit machines
709 * this results in args[] vector which is ~8 times bigger
710 * than n_max_chars! That is, with n_max_chars == 20k,
711 * args[] will take 160k (!), which will most likely be
712 * almost entirely unused.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100713 */
Denys Vlasenko6f068902014-02-27 11:17:06 +0100714 for (i = 0; argv[i]; i++)
Denys Vlasenkob270be32017-08-24 11:21:27 +0200715 store_param(argv[i]);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100716 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200717
Denys Vlasenkob270be32017-08-24 11:21:27 +0200718 initial_idx = G.idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200719 while (1) {
720 char *rem;
721
Denys Vlasenkob270be32017-08-24 11:21:27 +0200722 G.idx = initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200723 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200724 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200725
Denys Vlasenkob270be32017-08-24 11:21:27 +0200726 if (!G.args[initial_idx]) { /* not even one ARG was added? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200727 if (*rem != '\0')
James Byrne69374872019-07-02 11:35:03 +0200728 bb_simple_error_msg_and_die("argument line too long");
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200729 if (opt & OPT_NO_EMPTY)
730 break;
731 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000732 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200733
Denis Vlasenko6248a732006-09-29 08:20:30 +0000734 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200735 const char *fmt = " %s" + 1;
736 char **args = G.args;
737 for (i = 0; args[i]; i++) {
738 fprintf(stderr, fmt, args[i]);
739 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000740 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000741 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200742 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000743 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200744
Denis Vlasenko6248a732006-09-29 08:20:30 +0000745 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko14551b72017-08-24 13:23:54 +0200746 if (xargs_exec() != 0)
747 break; /* G.xargs_exitcode is set by xargs_exec() */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000748 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200749
750 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000751 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200752
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200753 if (ENABLE_FEATURE_CLEAN_UP) {
754 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200755 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200756 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200757
Denys Vlasenko14551b72017-08-24 13:23:54 +0200758#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
759 G.max_procs = 0;
760 xargs_exec(); /* final waitpid() loop */
761#endif
762
763 return G.xargs_exitcode;
Glenn L McGrath61796942003-10-10 12:10:18 +0000764}
765
766
767#ifdef TEST
768
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000769const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000770
771void bb_show_usage(void)
772{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000773 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000774 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000775 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000776}
777
778int main(int argc, char **argv)
779{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000780 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000781}
Eric Andersen252183e2003-10-31 08:19:44 +0000782#endif /* TEST */