blob: ae01a49beaf8cb151ddece900e50df613d22bb84 [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 */
Eric Andersen92a61c12000-09-22 20:01:23 +000017
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020018//config:config XARGS
19//config: bool "xargs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020020//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020021//config: help
22//config: xargs is used to execute a specified command for
23//config: every item from standard input.
24//config:
25//config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
26//config: bool "Enable -p: prompt and confirmation"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020027//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020028//config: depends on XARGS
29//config: help
30//config: Support -p: prompt the user whether to run each command
31//config: line and read a line from the terminal.
32//config:
33//config:config FEATURE_XARGS_SUPPORT_QUOTES
34//config: bool "Enable single and double quotes and backslash"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020035//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020036//config: depends on XARGS
37//config: help
38//config: Support quoting in the input.
39//config:
40//config:config FEATURE_XARGS_SUPPORT_TERMOPT
41//config: bool "Enable -x: exit if -s or -n is exceeded"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020042//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020043//config: depends on XARGS
44//config: help
45//config: Support -x: exit if the command size (see the -s or -n option)
46//config: is exceeded.
47//config:
48//config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
49//config: bool "Enable -0: NUL-terminated input"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020050//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020051//config: depends on XARGS
52//config: help
53//config: Support -0: input items are terminated by a NUL character
54//config: instead of whitespace, and the quotes and backslash
55//config: are not special.
Denys Vlasenko6f068902014-02-27 11:17:06 +010056//config:
57//config:config FEATURE_XARGS_SUPPORT_REPL_STR
58//config: bool "Enable -I STR: string to replace"
59//config: default y
60//config: depends on XARGS
61//config: help
62//config: Support -I STR and -i[STR] options.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020063
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +010064//applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
65
66//kbuild:lib-$(CONFIG_XARGS) += xargs.o
67
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000068#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020069#include "common_bufsiz.h"
Glenn L McGrath61796942003-10-10 12:10:18 +000070
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020071/* This is a NOEXEC applet. Be very careful! */
72
73
74//#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
75#define dbg_msg(...) ((void)0)
76
Glenn L McGrath61796942003-10-10 12:10:18 +000077
78#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000079# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
80# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000081# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000082# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
83# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000084# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000085# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
86# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000087# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000088# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
89# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000090# endif
91#endif
92
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020093
94struct globals {
95 char **args;
Denys Vlasenko6f068902014-02-27 11:17:06 +010096#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
97 char **argv;
98 const char *repl_str;
99 char eol_ch;
100#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200101 const char *eof_str;
102 int idx;
103} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200104#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100105#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200106 setup_common_bufsiz(); \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100107 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100108 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
109 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100110} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200111
112
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200113static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000114{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000115 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000116
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200117 status = spawn_and_wait(G.args);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000118 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200119 bb_simple_perror_msg(G.args[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000120 return errno == ENOENT ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000121 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000122 if (status == 255) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200123 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000124 return 124;
125 }
Denys Vlasenko8531d762010-03-18 22:44:00 +0100126 if (status >= 0x180) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200127 bb_error_msg("'%s' terminated by signal %d",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200128 G.args[0], status - 0x180);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000129 return 125;
130 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000131 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +0000132 return 123;
133 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000134}
Glenn L McGrath99825962003-10-09 11:06:45 +0000135
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200136/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
137 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
138 */
139#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000140
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200141static void store_param(char *s)
142{
143 /* Grow by 256 elements at once */
144 if (!(G.idx & 0xff)) { /* G.idx == N*256 */
145 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
146 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
147 }
148 G.args[G.idx++] = s;
149}
150
151/* process[0]_stdin:
152 * Read characters into buf[n_max_chars+1], and when parameter delimiter
153 * is seen, store the address of a new parameter to args[].
154 * If reading discovers that last chars do not form the complete
155 * parameter, the pointer to the first such "tail character" is returned.
Maninder Singh97c64912015-05-25 13:46:36 +0200156 * (buf has extra byte at the end to accommodate terminating NUL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200157 * of "tail characters" string).
158 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200159 * On entry, buf[] may contain some "seed chars" which are to become
160 * the beginning of the first parameter.
161 */
162
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000163#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200164static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000165{
166#define NORM 0
167#define QUOTE 1
168#define BACKSLASH 2
169#define SPACE 4
Denys Vlasenko237aece2010-06-15 10:18:01 +0200170 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000171 char state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200172 char *s = buf; /* start of the word */
173 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000174
Denys Vlasenko237aece2010-06-15 10:18:01 +0200175 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200176
177 /* "goto ret" is used instead of "break" to make control flow
178 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000179
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200180 while (1) {
181 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000182 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200183 if (p != s)
184 goto close_word;
185 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000186 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000187 if (state == BACKSLASH) {
188 state = NORM;
189 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200190 }
191 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000192 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000193 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000194 q = '\0';
195 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000196 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000197 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200198 if (p != s) {
199 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000200 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000201 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000202 goto set;
203 }
204 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000205 if (c == '\\') {
206 state = BACKSLASH;
207 } else if (c == '\'' || c == '"') {
208 q = c;
209 state = QUOTE;
210 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000211 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000212 *p++ = c;
213 }
214 }
215 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000216 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000217 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000218 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000219 q == '\'' ? "single" : "double");
220 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200221 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200222 if (G.eof_str) {
223 if (strcmp(s, G.eof_str) == 0) {
224 while (getchar() != EOF)
225 continue;
226 p = s;
227 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000228 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000229 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200230 store_param(s);
231 dbg_msg("args[]:'%s'", s);
232 s = p;
233 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200234 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200235 goto ret;
236 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000237 state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200238 }
239 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200240 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000241 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000242 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200243 ret:
244 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200245 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200246 dbg_msg("return:'%s'", s);
247 return s;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000248}
Glenn L McGrath61796942003-10-10 12:10:18 +0000249#else
Eric Andersen252183e2003-10-31 08:19:44 +0000250/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200251static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000252{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200253 char *s = buf; /* start of the word */
254 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000255
Denys Vlasenko237aece2010-06-15 10:18:01 +0200256 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000257
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200258 while (1) {
259 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000260 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200261 if (p == s)
262 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200263 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000264 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200265 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000266 continue;
267 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000268 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000269 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000270 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200271 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200272 if (G.eof_str) {
273 if (strcmp(s, G.eof_str) == 0) {
274 while (getchar() != EOF)
275 continue;
276 p = s;
277 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000278 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000279 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200280 store_param(s);
281 dbg_msg("args[]:'%s'", s);
282 s = p;
283 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200284 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200285 goto ret;
286 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200287 }
288 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200289 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000290 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000291 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200292 ret:
293 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200294 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200295 dbg_msg("return:'%s'", s);
296 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000297}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000298#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000299
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000300#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200301static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000302{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200303 char *s = buf; /* start of the word */
304 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000305
Denys Vlasenko237aece2010-06-15 10:18:01 +0200306 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000307
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200308 while (1) {
309 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000310 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200311 if (p == s)
312 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000313 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000314 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000315 *p++ = c;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100316 if (c == '\0') { /* NUL or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200317 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200318 store_param(s);
319 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200320 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200321 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200322 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200323 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000324 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200325 }
326 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200327 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000328 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000329 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200330 ret:
331 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200332 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200333 dbg_msg("return:'%s'", s);
334 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000335}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000336#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000337
Denys Vlasenko6f068902014-02-27 11:17:06 +0100338#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
339/*
340 * Used if -I<repl> was specified.
341 * In this mode, words aren't appended to PROG ARGS.
342 * Instead, entire input line is read, then <repl> string
343 * in every PROG and ARG is replaced with the line:
344 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
345 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
346 * -n MAX_ARGS seems to be ignored.
347 * Tested with GNU findutils 4.5.10.
348 */
349//FIXME: n_max_chars is not handled the same way as in GNU findutils.
350//FIXME: quoting is not implemented.
351static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
352{
353 int i;
354 char *end, *p;
355
356 /* Free strings from last invocation, if any */
357 for (i = 0; G.args && G.args[i]; i++)
358 if (G.args[i] != G.argv[i])
359 free(G.args[i]);
360
361 end = buf + n_max_chars;
362 p = buf;
363
364 while (1) {
365 int c = getchar();
366 if (c == EOF || c == G.eol_ch) {
367 if (p == buf)
368 goto ret; /* empty line */
369 c = '\0';
370 }
371 *p++ = c;
372 if (c == '\0') { /* EOL or EOF detected */
373 i = 0;
374 while (G.argv[i]) {
375 char *arg = G.argv[i];
376 int count = count_strstr(arg, G.repl_str);
377 if (count != 0)
Denys Vlasenko37952662014-02-27 15:02:36 +0100378 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100379 store_param(arg);
380 dbg_msg("args[]:'%s'", arg);
381 i++;
382 }
383 p = buf;
384 goto ret;
385 }
386 if (p == end) {
387 goto ret;
388 }
389 }
390 ret:
391 *p = '\0';
392 /* store_param(NULL) - caller will do it */
393 dbg_msg("return:'%s'", buf);
394 return buf;
395}
396#endif
397
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200398#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
399/* Prompt the user for a response, and
Denys Vlasenko6f068902014-02-27 11:17:06 +0100400 * if user responds affirmatively, return true;
401 * otherwise, return false. Uses "/dev/tty", not stdin.
402 */
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200403static int xargs_ask_confirmation(void)
404{
405 FILE *tty_stream;
406 int c, savec;
407
408 tty_stream = xfopen_for_read(CURRENT_TTY);
409 fputs(" ?...", stderr);
410 fflush_all();
411 c = savec = getc(tty_stream);
412 while (c != EOF && c != '\n')
413 c = getc(tty_stream);
414 fclose(tty_stream);
415 return (savec == 'y' || savec == 'Y');
416}
417#else
418# define xargs_ask_confirmation() 1
419#endif
420
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200421//usage:#define xargs_trivial_usage
422//usage: "[OPTIONS] [PROG ARGS]"
423//usage:#define xargs_full_usage "\n\n"
424//usage: "Run PROG on every item given by stdin\n"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200425//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
426//usage: "\n -p Ask user whether to run each command"
427//usage: )
428//usage: "\n -r Don't run command if input is empty"
429//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
430//usage: "\n -0 Input is separated by NUL characters"
431//usage: )
432//usage: "\n -t Print the command on stderr before execution"
433//usage: "\n -e[STR] STR stops input processing"
434//usage: "\n -n N Pass no more than N args to PROG"
435//usage: "\n -s N Pass command line of no more than N bytes"
Denys Vlasenko6f068902014-02-27 11:17:06 +0100436//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
437//usage: "\n -I STR Replace STR within PROG ARGS with input line"
438//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200439//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
440//usage: "\n -x Exit if size is exceeded"
441//usage: )
442//usage:#define xargs_example_usage
443//usage: "$ ls | xargs gzip\n"
444//usage: "$ find . -name '*.c' -print | xargs rm\n"
445
Denis Vlasenko6248a732006-09-29 08:20:30 +0000446/* Correct regardless of combination of CONFIG_xxx */
447enum {
448 OPTBIT_VERBOSE = 0,
449 OPTBIT_NO_EMPTY,
450 OPTBIT_UPTO_NUMBER,
451 OPTBIT_UPTO_SIZE,
452 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000453 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000454 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
455 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
456 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100457 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
458 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000459
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000460 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
461 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
462 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
463 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
464 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
465 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000466 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
467 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
468 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denys Vlasenko6f068902014-02-27 11:17:06 +0100469 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
470 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000471};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000472#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000473 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
474 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100475 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
476 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::")
Glenn L McGrath61796942003-10-10 12:10:18 +0000477
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000478int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000479int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000480{
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200481 int i;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000482 int child_error = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200483 char *max_args;
484 char *max_chars;
485 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000486 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200487 int n_max_chars;
488 int n_max_arg;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100489#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
490 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200491 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000492#else
493#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000494#endif
495
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200496 INIT_G();
497
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100498#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
499 /* For example, Fedora's build system uses --no-run-if-empty */
500 applet_long_options =
501 "no-run-if-empty\0" No_argument "r"
502 ;
503#endif
Denys Vlasenko6f068902014-02-27 11:17:06 +0100504 opt = getopt32(argv, OPTION_STR,
505 &max_args, &max_chars, &G.eof_str, &G.eof_str
506 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
507 );
Glenn L McGrath61796942003-10-10 12:10:18 +0000508
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000509 /* -E ""? You may wonder why not just omit -E?
510 * This is used for portability:
511 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200512 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
513 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000514
Denys Vlasenko6f068902014-02-27 11:17:06 +0100515 if (opt & OPT_ZEROTERM) {
516 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
517 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
518 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000519
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000520 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000521 argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200522 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000523 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200524 *--argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000525 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000526 }
527
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200528 /*
Denys Vlasenko4a966172010-06-19 21:44:01 +0200529 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200530 * "The xargs utility shall limit the command line length such that
531 * when the command line is invoked, the combined argument
532 * and environment lists (see the exec family of functions
533 * in the System Interfaces volume of IEEE Std 1003.1-2001)
534 * shall not exceed {ARG_MAX}-2048 bytes".
535 */
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200536 n_max_chars = bb_arg_max();
537 if (n_max_chars > 32 * 1024)
538 n_max_chars = 32 * 1024;
539 /*
540 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
541 * so that the process may safely modify its environment.
542 */
543 n_max_chars -= 2048;
544
Denis Vlasenko6248a732006-09-29 08:20:30 +0000545 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200546 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200547 }
548 /* Account for prepended fixed arguments */
549 {
550 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200551 for (i = 0; argv[i]; i++) {
552 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000553 }
554 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200555 }
556 /* Sanity check */
557 if (n_max_chars <= 0) {
558 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000559 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200560
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200561 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000562
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200563 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000564 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200565 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200566 /* Not necessary, we use growable args[]: */
567 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000568 }
569
Denys Vlasenko6f068902014-02-27 11:17:06 +0100570#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
571 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
572 /*
573 * -I<str>:
574 * Unmodified args are kept in G.argv[i],
575 * G.args[i] receives malloced G.argv[i] with <str> replaced
576 * with input line. Setting this up:
577 */
578 G.args = NULL;
579 G.argv = argv;
580 argc = 0;
581 read_args = process_stdin_with_replace;
Aaro Koskinen6d777b72015-10-24 22:01:29 +0200582 /* Make -I imply -r. GNU findutils seems to do the same: */
583 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
584 opt |= OPT_NO_EMPTY;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100585 } else
586#endif
587 {
588 /* Allocate pointers for execvp.
589 * We can statically allocate (argc + n_max_arg + 1) elements
590 * and do not bother with resizing args[], but on 64-bit machines
591 * this results in args[] vector which is ~8 times bigger
592 * than n_max_chars! That is, with n_max_chars == 20k,
593 * args[] will take 160k (!), which will most likely be
594 * almost entirely unused.
595 *
596 * See store_param() for matching 256-step growth logic
597 */
598 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
599 /* Store the command to be executed, part 1 */
600 for (i = 0; argv[i]; i++)
601 G.args[i] = argv[i];
602 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200603
604 while (1) {
605 char *rem;
606
607 G.idx = argc;
608 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200609 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200610
611 if (!G.args[argc]) {
612 if (*rem != '\0')
613 bb_error_msg_and_die("argument line too long");
614 if (opt & OPT_NO_EMPTY)
615 break;
616 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000617 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200618
Denis Vlasenko6248a732006-09-29 08:20:30 +0000619 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200620 const char *fmt = " %s" + 1;
621 char **args = G.args;
622 for (i = 0; args[i]; i++) {
623 fprintf(stderr, fmt, args[i]);
624 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000625 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000626 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200627 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000628 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200629
Denis Vlasenko6248a732006-09-29 08:20:30 +0000630 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200631 child_error = xargs_exec();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000632 }
633
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000634 if (child_error > 0 && child_error != 123) {
635 break;
636 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200637
638 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000639 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200640
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200641 if (ENABLE_FEATURE_CLEAN_UP) {
642 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200643 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200644 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200645
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000646 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000647}
648
649
650#ifdef TEST
651
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000652const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000653
654void bb_show_usage(void)
655{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000656 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000657 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000658 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000659}
660
661int main(int argc, char **argv)
662{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000663 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000664}
Eric Andersen252183e2003-10-31 08:19:44 +0000665#endif /* TEST */