blob: 90ff059864bc229c10cd5bfaeef9529892016c39 [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
Denys Vlasenkobd202a52020-09-30 00:00:43 +020083#if 0
84# define dbg_msg(...) bb_error_msg(__VA_ARGS__)
85#else
86# define dbg_msg(...) ((void)0)
87#endif
Glenn L McGrath61796942003-10-10 12:10:18 +000088
89#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000090# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
91# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000092# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000093# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
94# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000095# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000096# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
97# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000098# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000099# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
100# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +0000101# endif
102#endif
103
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200104
105struct globals {
106 char **args;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100107#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
108 char **argv;
109 const char *repl_str;
110 char eol_ch;
111#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200112 const char *eof_str;
113 int idx;
Denys Vlasenko14551b72017-08-24 13:23:54 +0200114#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
115 int running_procs;
116 int max_procs;
117#endif
118 smalluint xargs_exitcode;
Ron Yorston1ff70022020-01-24 13:16:45 +0000119#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
120#define NORM 0
121#define QUOTE 1
122#define BACKSLASH 2
123#define SPACE 4
124 smalluint process_stdin__state;
125 char process_stdin__q;
126#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200127} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200128#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100129#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200130 setup_common_bufsiz(); \
Ron Yorston1ff70022020-01-24 13:16:45 +0000131 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
132 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
133 /* Even zero values are set because we are NOEXEC applet */ \
134 G.eof_str = NULL; \
Denys Vlasenkob270be32017-08-24 11:21:27 +0200135 G.idx = 0; \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200136 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
137 IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
138 G.xargs_exitcode = 0; \
Ron Yorston1ff70022020-01-24 13:16:45 +0000139 IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__state = NORM;) \
140 IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__q = '\0';) \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100141} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200142
143
Denys Vlasenko14551b72017-08-24 13:23:54 +0200144/*
145 * Returns 0 if xargs should continue (but may set G.xargs_exitcode to 123).
146 * Else sets G.xargs_exitcode to error code and returns nonzero.
147 *
148 * If G.max_procs == 0, performs final waitpid() loop for all children.
149 */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200150static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000151{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000152 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000153
Denys Vlasenko14551b72017-08-24 13:23:54 +0200154#if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200155 status = spawn_and_wait(G.args);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200156#else
157 if (G.max_procs == 1) {
158 status = spawn_and_wait(G.args);
159 } else {
160 pid_t pid;
161 int wstat;
162 again:
163 if (G.running_procs >= G.max_procs)
164 pid = safe_waitpid(-1, &wstat, 0);
165 else
166 pid = wait_any_nohang(&wstat);
167 if (pid > 0) {
168 /* We may have children we don't know about:
169 * sh -c 'sleep 1 & exec xargs ...'
170 * Do not make G.running_procs go negative.
171 */
172 if (G.running_procs != 0)
173 G.running_procs--;
174 status = WIFSIGNALED(wstat)
175 ? 0x180 + WTERMSIG(wstat)
176 : WEXITSTATUS(wstat);
177 if (status > 0 && status < 255) {
178 /* See below why 123 does not abort */
179 G.xargs_exitcode = 123;
180 status = 0;
181 }
182 if (status == 0)
183 goto again; /* maybe we have more children? */
184 /* else: "bad" status, will bail out */
185 } else if (G.max_procs != 0) {
186 /* Not in final waitpid() loop,
187 * and G.running_procs < G.max_procs: start more procs
188 */
189 status = spawn(G.args);
190 /* here "status" actually holds pid, or -1 */
191 if (status > 0) {
192 G.running_procs++;
193 status = 0;
194 }
195 /* else: status == -1 (failed to fork or exec) */
196 } else {
197 /* final waitpid() loop: must be ECHILD "no more children" */
198 status = 0;
199 }
200 }
201#endif
202 /* Manpage:
203 * """xargs exits with the following status:
204 * 0 if it succeeds
205 * 123 if any invocation of the command exited with status 1-125
206 * 124 if the command exited with status 255
207 * ("""If any invocation of the command exits with a status of 255,
208 * xargs will stop immediately without reading any further input.
209 * An error message is issued on stderr when this happens.""")
210 * 125 if the command is killed by a signal
211 * 126 if the command cannot be run
212 * 127 if the command is not found
213 * 1 if some other error occurred."""
214 */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000215 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200216 bb_simple_perror_msg(G.args[0]);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200217 status = (errno == ENOENT) ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000218 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200219 else if (status >= 0x180) {
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100220 bb_error_msg("'%s' terminated by signal %u",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200221 G.args[0], status - 0x180);
Denys Vlasenko14551b72017-08-24 13:23:54 +0200222 status = 125;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000223 }
Denys Vlasenko14551b72017-08-24 13:23:54 +0200224 else if (status != 0) {
225 if (status == 255) {
226 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100227 status = 124;
228 goto ret;
Denys Vlasenko14551b72017-08-24 13:23:54 +0200229 }
230 /* "123 if any invocation of the command exited with status 1-125"
231 * This implies that nonzero exit code is remembered,
232 * but does not cause xargs to stop: we return 0.
233 */
234 G.xargs_exitcode = 123;
235 status = 0;
236 }
Denys Vlasenkof3e28382018-10-30 14:40:17 +0100237 ret:
Denys Vlasenko14551b72017-08-24 13:23:54 +0200238 if (status != 0)
239 G.xargs_exitcode = status;
240 return status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000241}
Glenn L McGrath99825962003-10-09 11:06:45 +0000242
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200243/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
244 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
245 */
246#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000247
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200248static void store_param(char *s)
249{
250 /* Grow by 256 elements at once */
Denys Vlasenkob270be32017-08-24 11:21:27 +0200251 if (!(G.idx & 0xff)) { /* G.idx == N*256? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200252 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
253 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
254 }
255 G.args[G.idx++] = s;
256}
257
258/* process[0]_stdin:
259 * Read characters into buf[n_max_chars+1], and when parameter delimiter
260 * is seen, store the address of a new parameter to args[].
261 * If reading discovers that last chars do not form the complete
262 * parameter, the pointer to the first such "tail character" is returned.
Maninder Singh97c64912015-05-25 13:46:36 +0200263 * (buf has extra byte at the end to accommodate terminating NUL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200264 * of "tail characters" string).
265 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200266 * On entry, buf[] may contain some "seed chars" which are to become
267 * the beginning of the first parameter.
268 */
269
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000270#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200271static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000272{
Ron Yorston1ff70022020-01-24 13:16:45 +0000273#define q G.process_stdin__q
274#define state G.process_stdin__state
Denys Vlasenko237aece2010-06-15 10:18:01 +0200275 char *s = buf; /* start of the word */
276 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000277
Denys Vlasenko237aece2010-06-15 10:18:01 +0200278 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200279
280 /* "goto ret" is used instead of "break" to make control flow
281 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000282
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200283 while (1) {
284 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000285 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200286 if (p != s)
287 goto close_word;
288 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000289 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000290 if (state == BACKSLASH) {
291 state = NORM;
292 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200293 }
294 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000295 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000296 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000297 q = '\0';
298 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000299 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000300 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200301 if (p != s) {
302 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000303 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000304 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000305 goto set;
306 }
307 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000308 if (c == '\\') {
309 state = BACKSLASH;
310 } else if (c == '\'' || c == '"') {
311 q = c;
312 state = QUOTE;
313 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000314 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000315 *p++ = c;
316 }
317 }
318 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000319 if (state == SPACE) { /* word's delimiter or EOF detected */
Ron Yorston1c462d42020-02-19 11:01:39 +0000320 state = NORM;
Eric Andersen252183e2003-10-31 08:19:44 +0000321 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000322 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000323 q == '\'' ? "single" : "double");
324 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200325 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200326 if (G.eof_str) {
327 if (strcmp(s, G.eof_str) == 0) {
328 while (getchar() != EOF)
329 continue;
330 p = s;
331 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000332 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000333 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200334 store_param(s);
335 dbg_msg("args[]:'%s'", s);
336 s = p;
337 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200338 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200339 goto ret;
340 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200341 }
342 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200343 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000344 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000345 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200346 ret:
347 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200348 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200349 dbg_msg("return:'%s'", s);
350 return s;
Ron Yorston1ff70022020-01-24 13:16:45 +0000351#undef q
352#undef state
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000353}
Glenn L McGrath61796942003-10-10 12:10:18 +0000354#else
Eric Andersen252183e2003-10-31 08:19:44 +0000355/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200356static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000357{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200358 char *s = buf; /* start of the word */
359 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000360
Denys Vlasenko237aece2010-06-15 10:18:01 +0200361 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000362
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200363 while (1) {
364 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000365 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200366 if (p == s)
367 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200368 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000369 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200370 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000371 continue;
372 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000373 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000374 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000375 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200376 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200377 if (G.eof_str) {
378 if (strcmp(s, G.eof_str) == 0) {
379 while (getchar() != EOF)
380 continue;
381 p = s;
382 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000383 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000384 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200385 store_param(s);
386 dbg_msg("args[]:'%s'", s);
387 s = p;
388 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200389 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200390 goto ret;
391 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200392 }
393 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200394 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000395 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000396 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200397 ret:
398 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200399 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200400 dbg_msg("return:'%s'", s);
401 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000402}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000403#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000404
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000405#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200406static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000407{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200408 char *s = buf; /* start of the word */
409 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000410
Denys Vlasenko237aece2010-06-15 10:18:01 +0200411 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000412
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200413 while (1) {
414 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000415 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200416 if (p == s)
417 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000418 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000419 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000420 *p++ = c;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100421 if (c == '\0') { /* NUL or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200422 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200423 store_param(s);
424 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200425 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200426 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200427 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200428 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000429 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200430 }
431 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200432 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000433 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000434 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200435 ret:
436 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200437 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200438 dbg_msg("return:'%s'", s);
439 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000440}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000441#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000442
Denys Vlasenko6f068902014-02-27 11:17:06 +0100443#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
444/*
445 * Used if -I<repl> was specified.
446 * In this mode, words aren't appended to PROG ARGS.
447 * Instead, entire input line is read, then <repl> string
448 * in every PROG and ARG is replaced with the line:
449 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
450 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
451 * -n MAX_ARGS seems to be ignored.
452 * Tested with GNU findutils 4.5.10.
453 */
454//FIXME: n_max_chars is not handled the same way as in GNU findutils.
455//FIXME: quoting is not implemented.
456static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
457{
458 int i;
459 char *end, *p;
460
461 /* Free strings from last invocation, if any */
462 for (i = 0; G.args && G.args[i]; i++)
463 if (G.args[i] != G.argv[i])
464 free(G.args[i]);
465
466 end = buf + n_max_chars;
467 p = buf;
468
469 while (1) {
470 int c = getchar();
Denys Vlasenkobd202a52020-09-30 00:00:43 +0200471 if (p == buf) {
472 if (c == EOF)
473 goto ret; /* last line is empty, return "" */
474 if (c == G.eol_ch)
475 continue; /* empty line, ignore */
476 /* Skip leading whitespace of each line: try
477 * echo -e ' \t\v1 2 3 ' | xargs -I% echo '[%]'
478 */
479 if (ISSPACE(c))
480 continue;
481 }
Denys Vlasenko6f068902014-02-27 11:17:06 +0100482 if (c == EOF || c == G.eol_ch) {
Denys Vlasenko6f068902014-02-27 11:17:06 +0100483 c = '\0';
484 }
485 *p++ = c;
486 if (c == '\0') { /* EOL or EOF detected */
487 i = 0;
488 while (G.argv[i]) {
489 char *arg = G.argv[i];
490 int count = count_strstr(arg, G.repl_str);
491 if (count != 0)
Denys Vlasenko37952662014-02-27 15:02:36 +0100492 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100493 store_param(arg);
494 dbg_msg("args[]:'%s'", arg);
495 i++;
496 }
497 p = buf;
498 goto ret;
499 }
500 if (p == end) {
501 goto ret;
502 }
503 }
504 ret:
505 *p = '\0';
506 /* store_param(NULL) - caller will do it */
507 dbg_msg("return:'%s'", buf);
508 return buf;
509}
510#endif
511
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200512#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
513/* Prompt the user for a response, and
Denys Vlasenko6f068902014-02-27 11:17:06 +0100514 * if user responds affirmatively, return true;
515 * otherwise, return false. Uses "/dev/tty", not stdin.
516 */
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200517static int xargs_ask_confirmation(void)
518{
519 FILE *tty_stream;
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200520 int r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200521
522 tty_stream = xfopen_for_read(CURRENT_TTY);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200523
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200524 fputs(" ?...", stderr);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200525 r = bb_ask_y_confirmation_FILE(tty_stream);
526
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200527 fclose(tty_stream);
Denys Vlasenkobae8fc42018-04-07 15:21:35 +0200528
529 return r;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200530}
531#else
532# define xargs_ask_confirmation() 1
533#endif
534
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200535//usage:#define xargs_trivial_usage
536//usage: "[OPTIONS] [PROG ARGS]"
537//usage:#define xargs_full_usage "\n\n"
538//usage: "Run PROG on every item given by stdin\n"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200539//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
Denys Vlasenko1e7ca182021-08-22 15:43:29 +0200540//usage: "\n -0 NUL terminated input"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200541//usage: )
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200542//usage: IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
543//usage: "\n -a FILE Read from FILE instead of stdin"
544//usage: )
Denys Vlasenko24522472019-03-10 14:29:03 +0100545//usage: "\n -r Don't run command if input is empty"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200546//usage: "\n -t Print the command on stderr before execution"
Denys Vlasenko24522472019-03-10 14:29:03 +0100547//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
548//usage: "\n -p Ask user whether to run each command"
549//usage: )
550//usage: "\n -E STR,-e[STR] STR stops input processing"
Denys Vlasenko6f068902014-02-27 11:17:06 +0100551//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
552//usage: "\n -I STR Replace STR within PROG ARGS with input line"
553//usage: )
Denys Vlasenko24522472019-03-10 14:29:03 +0100554//usage: "\n -n N Pass no more than N args to PROG"
555//usage: "\n -s N Pass command line of no more than N bytes"
Denys Vlasenko14551b72017-08-24 13:23:54 +0200556//usage: IF_FEATURE_XARGS_SUPPORT_PARALLEL(
557//usage: "\n -P N Run up to N PROGs in parallel"
558//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200559//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
560//usage: "\n -x Exit if size is exceeded"
561//usage: )
562//usage:#define xargs_example_usage
563//usage: "$ ls | xargs gzip\n"
564//usage: "$ find . -name '*.c' -print | xargs rm\n"
565
Denis Vlasenko6248a732006-09-29 08:20:30 +0000566/* Correct regardless of combination of CONFIG_xxx */
567enum {
568 OPTBIT_VERBOSE = 0,
569 OPTBIT_NO_EMPTY,
570 OPTBIT_UPTO_NUMBER,
571 OPTBIT_UPTO_SIZE,
572 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000573 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000574 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
575 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
576 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100577 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
578 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000579
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000580 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
581 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
582 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
583 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
584 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
585 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000586 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
587 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
588 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denys Vlasenko6f068902014-02-27 11:17:06 +0100589 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
590 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000591};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000592#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000593 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
594 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100595 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
Denys Vlasenko14551b72017-08-24 13:23:54 +0200596 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200597 IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \
598 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:")
Glenn L McGrath61796942003-10-10 12:10:18 +0000599
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000600int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200601int xargs_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000602{
Denys Vlasenkob270be32017-08-24 11:21:27 +0200603 int initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200604 int i;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200605 char *max_args;
606 char *max_chars;
607 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000608 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200609 int n_max_chars;
610 int n_max_arg;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100611#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
612 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200613 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000614#else
615#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000616#endif
Denys Vlasenko58bf9022017-08-30 13:44:27 +0200617 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
Glenn L McGrath61796942003-10-10 12:10:18 +0000618
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200619 INIT_G();
620
Denys Vlasenko036585a2017-08-08 16:38:18 +0200621 opt = getopt32long(argv, OPTION_STR,
622 "no-run-if-empty\0" No_argument "r",
Denys Vlasenko6f068902014-02-27 11:17:06 +0100623 &max_args, &max_chars, &G.eof_str, &G.eof_str
624 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
Denys Vlasenko14551b72017-08-24 13:23:54 +0200625 IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200626 IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100627 );
Glenn L McGrath61796942003-10-10 12:10:18 +0000628
Denys Vlasenko14551b72017-08-24 13:23:54 +0200629#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
630 if (G.max_procs <= 0) /* -P0 means "run lots of them" */
631 G.max_procs = 100; /* let's not go crazy high */
632#endif
633
Johannes Schindelinf8ee8492017-08-25 22:42:05 +0200634#if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
635 if (opt_a)
636 xmove_fd(xopen(opt_a, O_RDONLY), 0);
637#endif
638
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000639 /* -E ""? You may wonder why not just omit -E?
640 * This is used for portability:
641 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200642 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
643 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000644
Denys Vlasenko6f068902014-02-27 11:17:06 +0100645 if (opt & OPT_ZEROTERM) {
646 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
647 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
648 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000649
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000650 argv += optind;
Denys Vlasenkob270be32017-08-24 11:21:27 +0200651 //argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200652 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000653 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200654 *--argv = (char*)"echo";
Denys Vlasenkob270be32017-08-24 11:21:27 +0200655 //argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000656 }
657
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200658 /*
Denys Vlasenko4a966172010-06-19 21:44:01 +0200659 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200660 * "The xargs utility shall limit the command line length such that
661 * when the command line is invoked, the combined argument
662 * and environment lists (see the exec family of functions
663 * in the System Interfaces volume of IEEE Std 1003.1-2001)
664 * shall not exceed {ARG_MAX}-2048 bytes".
665 */
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200666 n_max_chars = bb_arg_max();
667 if (n_max_chars > 32 * 1024)
668 n_max_chars = 32 * 1024;
669 /*
670 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
671 * so that the process may safely modify its environment.
672 */
673 n_max_chars -= 2048;
674
Denis Vlasenko6248a732006-09-29 08:20:30 +0000675 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200676 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200677 }
678 /* Account for prepended fixed arguments */
679 {
680 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200681 for (i = 0; argv[i]; i++) {
682 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000683 }
684 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200685 }
686 /* Sanity check */
687 if (n_max_chars <= 0) {
James Byrne69374872019-07-02 11:35:03 +0200688 bb_simple_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000689 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200690
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200691 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000692
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200693 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000694 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200695 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200696 /* Not necessary, we use growable args[]: */
697 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000698 }
699
Denys Vlasenko6f068902014-02-27 11:17:06 +0100700#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
701 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
702 /*
703 * -I<str>:
704 * Unmodified args are kept in G.argv[i],
705 * G.args[i] receives malloced G.argv[i] with <str> replaced
706 * with input line. Setting this up:
707 */
708 G.args = NULL;
709 G.argv = argv;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100710 read_args = process_stdin_with_replace;
Aaro Koskinen6d777b72015-10-24 22:01:29 +0200711 /* Make -I imply -r. GNU findutils seems to do the same: */
712 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
713 opt |= OPT_NO_EMPTY;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100714 } else
715#endif
716 {
Denys Vlasenkob270be32017-08-24 11:21:27 +0200717 /* Store the command to be executed, part 1.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100718 * We can statically allocate (argc + n_max_arg + 1) elements
719 * and do not bother with resizing args[], but on 64-bit machines
720 * this results in args[] vector which is ~8 times bigger
721 * than n_max_chars! That is, with n_max_chars == 20k,
722 * args[] will take 160k (!), which will most likely be
723 * almost entirely unused.
Denys Vlasenko6f068902014-02-27 11:17:06 +0100724 */
Denys Vlasenko6f068902014-02-27 11:17:06 +0100725 for (i = 0; argv[i]; i++)
Denys Vlasenkob270be32017-08-24 11:21:27 +0200726 store_param(argv[i]);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100727 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200728
Denys Vlasenkob270be32017-08-24 11:21:27 +0200729 initial_idx = G.idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200730 while (1) {
731 char *rem;
732
Denys Vlasenkob270be32017-08-24 11:21:27 +0200733 G.idx = initial_idx;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200734 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200735 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200736
Denys Vlasenkob270be32017-08-24 11:21:27 +0200737 if (!G.args[initial_idx]) { /* not even one ARG was added? */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200738 if (*rem != '\0')
James Byrne69374872019-07-02 11:35:03 +0200739 bb_simple_error_msg_and_die("argument line too long");
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200740 if (opt & OPT_NO_EMPTY)
741 break;
742 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000743 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200744
Denis Vlasenko6248a732006-09-29 08:20:30 +0000745 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200746 const char *fmt = " %s" + 1;
747 char **args = G.args;
748 for (i = 0; args[i]; i++) {
749 fprintf(stderr, fmt, args[i]);
750 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000751 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000752 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200753 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000754 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200755
Denis Vlasenko6248a732006-09-29 08:20:30 +0000756 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko14551b72017-08-24 13:23:54 +0200757 if (xargs_exec() != 0)
758 break; /* G.xargs_exitcode is set by xargs_exec() */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000759 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200760
761 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000762 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200763
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200764 if (ENABLE_FEATURE_CLEAN_UP) {
765 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200766 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200767 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200768
Denys Vlasenko14551b72017-08-24 13:23:54 +0200769#if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
770 G.max_procs = 0;
771 xargs_exec(); /* final waitpid() loop */
772#endif
773
774 return G.xargs_exitcode;
Glenn L McGrath61796942003-10-10 12:10:18 +0000775}
776
777
778#ifdef TEST
779
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000780const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000781
782void bb_show_usage(void)
783{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000784 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000785 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000786 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000787}
788
789int main(int argc, char **argv)
790{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000791 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000792}
Eric Andersen252183e2003-10-31 08:19:44 +0000793#endif /* TEST */