blob: 25e47ec8908e6e2e5b56e4b7b92e870385ef1a0c [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"
Glenn L McGrath61796942003-10-10 12:10:18 +000069
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020070/* This is a NOEXEC applet. Be very careful! */
71
72
73//#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
74#define dbg_msg(...) ((void)0)
75
Glenn L McGrath61796942003-10-10 12:10:18 +000076
77#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000078# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
79# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000080# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000081# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
82# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000083# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000084# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
85# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000086# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000087# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
88# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000089# endif
90#endif
91
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020092
93struct globals {
94 char **args;
Denys Vlasenko6f068902014-02-27 11:17:06 +010095#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
96 char **argv;
97 const char *repl_str;
98 char eol_ch;
99#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200100 const char *eof_str;
101 int idx;
102} FIX_ALIASING;
103#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100104#define INIT_G() do { \
105 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100106 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
107 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100108} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200109
110
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200111static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000112{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000113 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000114
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200115 status = spawn_and_wait(G.args);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000116 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200117 bb_simple_perror_msg(G.args[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000118 return errno == ENOENT ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000119 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000120 if (status == 255) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200121 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000122 return 124;
123 }
Denys Vlasenko8531d762010-03-18 22:44:00 +0100124 if (status >= 0x180) {
Denis Vlasenko6248a732006-09-29 08:20:30 +0000125 bb_error_msg("%s: terminated by signal %d",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200126 G.args[0], status - 0x180);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000127 return 125;
128 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000129 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +0000130 return 123;
131 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000132}
Glenn L McGrath99825962003-10-09 11:06:45 +0000133
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200134/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
135 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
136 */
137#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000138
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200139static void store_param(char *s)
140{
141 /* Grow by 256 elements at once */
142 if (!(G.idx & 0xff)) { /* G.idx == N*256 */
143 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
144 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
145 }
146 G.args[G.idx++] = s;
147}
148
149/* process[0]_stdin:
150 * Read characters into buf[n_max_chars+1], and when parameter delimiter
151 * is seen, store the address of a new parameter to args[].
152 * If reading discovers that last chars do not form the complete
153 * parameter, the pointer to the first such "tail character" is returned.
Maninder Singh97c64912015-05-25 13:46:36 +0200154 * (buf has extra byte at the end to accommodate terminating NUL
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200155 * of "tail characters" string).
156 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200157 * On entry, buf[] may contain some "seed chars" which are to become
158 * the beginning of the first parameter.
159 */
160
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000161#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200162static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000163{
164#define NORM 0
165#define QUOTE 1
166#define BACKSLASH 2
167#define SPACE 4
Denys Vlasenko237aece2010-06-15 10:18:01 +0200168 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000169 char state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200170 char *s = buf; /* start of the word */
171 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000172
Denys Vlasenko237aece2010-06-15 10:18:01 +0200173 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200174
175 /* "goto ret" is used instead of "break" to make control flow
176 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000177
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200178 while (1) {
179 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000180 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200181 if (p != s)
182 goto close_word;
183 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000184 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000185 if (state == BACKSLASH) {
186 state = NORM;
187 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200188 }
189 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000190 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000191 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000192 q = '\0';
193 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000194 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000195 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200196 if (p != s) {
197 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000198 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000199 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000200 goto set;
201 }
202 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000203 if (c == '\\') {
204 state = BACKSLASH;
205 } else if (c == '\'' || c == '"') {
206 q = c;
207 state = QUOTE;
208 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000209 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000210 *p++ = c;
211 }
212 }
213 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000214 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000215 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000216 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000217 q == '\'' ? "single" : "double");
218 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200219 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200220 if (G.eof_str) {
221 if (strcmp(s, G.eof_str) == 0) {
222 while (getchar() != EOF)
223 continue;
224 p = s;
225 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000226 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000227 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200228 store_param(s);
229 dbg_msg("args[]:'%s'", s);
230 s = p;
231 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200232 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200233 goto ret;
234 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000235 state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200236 }
237 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200238 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000239 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000240 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200241 ret:
242 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200243 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200244 dbg_msg("return:'%s'", s);
245 return s;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000246}
Glenn L McGrath61796942003-10-10 12:10:18 +0000247#else
Eric Andersen252183e2003-10-31 08:19:44 +0000248/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200249static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000250{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200251 char *s = buf; /* start of the word */
252 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000253
Denys Vlasenko237aece2010-06-15 10:18:01 +0200254 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000255
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200256 while (1) {
257 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000258 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200259 if (p == s)
260 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200261 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000262 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200263 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000264 continue;
265 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000266 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000267 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000268 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200269 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200270 if (G.eof_str) {
271 if (strcmp(s, G.eof_str) == 0) {
272 while (getchar() != EOF)
273 continue;
274 p = s;
275 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000276 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000277 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200278 store_param(s);
279 dbg_msg("args[]:'%s'", s);
280 s = p;
281 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200282 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200283 goto ret;
284 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200285 }
286 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200287 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000288 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000289 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200290 ret:
291 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200292 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200293 dbg_msg("return:'%s'", s);
294 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000295}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000296#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000297
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000298#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200299static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000300{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200301 char *s = buf; /* start of the word */
302 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000303
Denys Vlasenko237aece2010-06-15 10:18:01 +0200304 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000305
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200306 while (1) {
307 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000308 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200309 if (p == s)
310 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000311 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000312 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000313 *p++ = c;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100314 if (c == '\0') { /* NUL or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200315 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200316 store_param(s);
317 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200318 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200319 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200320 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200321 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000322 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200323 }
324 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200325 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000326 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000327 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200328 ret:
329 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200330 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200331 dbg_msg("return:'%s'", s);
332 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000333}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000334#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000335
Denys Vlasenko6f068902014-02-27 11:17:06 +0100336#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
337/*
338 * Used if -I<repl> was specified.
339 * In this mode, words aren't appended to PROG ARGS.
340 * Instead, entire input line is read, then <repl> string
341 * in every PROG and ARG is replaced with the line:
342 * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
343 * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
344 * -n MAX_ARGS seems to be ignored.
345 * Tested with GNU findutils 4.5.10.
346 */
347//FIXME: n_max_chars is not handled the same way as in GNU findutils.
348//FIXME: quoting is not implemented.
349static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
350{
351 int i;
352 char *end, *p;
353
354 /* Free strings from last invocation, if any */
355 for (i = 0; G.args && G.args[i]; i++)
356 if (G.args[i] != G.argv[i])
357 free(G.args[i]);
358
359 end = buf + n_max_chars;
360 p = buf;
361
362 while (1) {
363 int c = getchar();
364 if (c == EOF || c == G.eol_ch) {
365 if (p == buf)
366 goto ret; /* empty line */
367 c = '\0';
368 }
369 *p++ = c;
370 if (c == '\0') { /* EOL or EOF detected */
371 i = 0;
372 while (G.argv[i]) {
373 char *arg = G.argv[i];
374 int count = count_strstr(arg, G.repl_str);
375 if (count != 0)
Denys Vlasenko37952662014-02-27 15:02:36 +0100376 arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
Denys Vlasenko6f068902014-02-27 11:17:06 +0100377 store_param(arg);
378 dbg_msg("args[]:'%s'", arg);
379 i++;
380 }
381 p = buf;
382 goto ret;
383 }
384 if (p == end) {
385 goto ret;
386 }
387 }
388 ret:
389 *p = '\0';
390 /* store_param(NULL) - caller will do it */
391 dbg_msg("return:'%s'", buf);
392 return buf;
393}
394#endif
395
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200396#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
397/* Prompt the user for a response, and
Denys Vlasenko6f068902014-02-27 11:17:06 +0100398 * if user responds affirmatively, return true;
399 * otherwise, return false. Uses "/dev/tty", not stdin.
400 */
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200401static int xargs_ask_confirmation(void)
402{
403 FILE *tty_stream;
404 int c, savec;
405
406 tty_stream = xfopen_for_read(CURRENT_TTY);
407 fputs(" ?...", stderr);
408 fflush_all();
409 c = savec = getc(tty_stream);
410 while (c != EOF && c != '\n')
411 c = getc(tty_stream);
412 fclose(tty_stream);
413 return (savec == 'y' || savec == 'Y');
414}
415#else
416# define xargs_ask_confirmation() 1
417#endif
418
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200419//usage:#define xargs_trivial_usage
420//usage: "[OPTIONS] [PROG ARGS]"
421//usage:#define xargs_full_usage "\n\n"
422//usage: "Run PROG on every item given by stdin\n"
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200423//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
424//usage: "\n -p Ask user whether to run each command"
425//usage: )
426//usage: "\n -r Don't run command if input is empty"
427//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
428//usage: "\n -0 Input is separated by NUL characters"
429//usage: )
430//usage: "\n -t Print the command on stderr before execution"
431//usage: "\n -e[STR] STR stops input processing"
432//usage: "\n -n N Pass no more than N args to PROG"
433//usage: "\n -s N Pass command line of no more than N bytes"
Denys Vlasenko6f068902014-02-27 11:17:06 +0100434//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
435//usage: "\n -I STR Replace STR within PROG ARGS with input line"
436//usage: )
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200437//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
438//usage: "\n -x Exit if size is exceeded"
439//usage: )
440//usage:#define xargs_example_usage
441//usage: "$ ls | xargs gzip\n"
442//usage: "$ find . -name '*.c' -print | xargs rm\n"
443
Denis Vlasenko6248a732006-09-29 08:20:30 +0000444/* Correct regardless of combination of CONFIG_xxx */
445enum {
446 OPTBIT_VERBOSE = 0,
447 OPTBIT_NO_EMPTY,
448 OPTBIT_UPTO_NUMBER,
449 OPTBIT_UPTO_SIZE,
450 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000451 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000452 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
453 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
454 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Denys Vlasenko6f068902014-02-27 11:17:06 +0100455 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
456 IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000457
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000458 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
459 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
460 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
461 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
462 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
463 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000464 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
465 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
466 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denys Vlasenko6f068902014-02-27 11:17:06 +0100467 OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
468 OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000469};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000470#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000471 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
472 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
Denys Vlasenko6f068902014-02-27 11:17:06 +0100473 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
474 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::")
Glenn L McGrath61796942003-10-10 12:10:18 +0000475
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000476int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000477int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000478{
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200479 int i;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000480 int child_error = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200481 char *max_args;
482 char *max_chars;
483 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000484 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200485 int n_max_chars;
486 int n_max_arg;
Denys Vlasenko6f068902014-02-27 11:17:06 +0100487#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
488 || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200489 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000490#else
491#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000492#endif
493
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200494 INIT_G();
495
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100496#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
497 /* For example, Fedora's build system uses --no-run-if-empty */
498 applet_long_options =
499 "no-run-if-empty\0" No_argument "r"
500 ;
501#endif
Denys Vlasenko6f068902014-02-27 11:17:06 +0100502 opt = getopt32(argv, OPTION_STR,
503 &max_args, &max_chars, &G.eof_str, &G.eof_str
504 IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
505 );
Glenn L McGrath61796942003-10-10 12:10:18 +0000506
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000507 /* -E ""? You may wonder why not just omit -E?
508 * This is used for portability:
509 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200510 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
511 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000512
Denys Vlasenko6f068902014-02-27 11:17:06 +0100513 if (opt & OPT_ZEROTERM) {
514 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
515 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
516 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000517
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000518 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000519 argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200520 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000521 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200522 *--argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000523 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000524 }
525
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200526 /*
Denys Vlasenko4a966172010-06-19 21:44:01 +0200527 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200528 * "The xargs utility shall limit the command line length such that
529 * when the command line is invoked, the combined argument
530 * and environment lists (see the exec family of functions
531 * in the System Interfaces volume of IEEE Std 1003.1-2001)
532 * shall not exceed {ARG_MAX}-2048 bytes".
533 */
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200534 n_max_chars = bb_arg_max();
535 if (n_max_chars > 32 * 1024)
536 n_max_chars = 32 * 1024;
537 /*
538 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
539 * so that the process may safely modify its environment.
540 */
541 n_max_chars -= 2048;
542
Denis Vlasenko6248a732006-09-29 08:20:30 +0000543 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200544 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200545 }
546 /* Account for prepended fixed arguments */
547 {
548 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200549 for (i = 0; argv[i]; i++) {
550 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000551 }
552 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200553 }
554 /* Sanity check */
555 if (n_max_chars <= 0) {
556 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000557 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200558
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200559 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000560
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200561 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000562 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200563 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200564 /* Not necessary, we use growable args[]: */
565 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000566 }
567
Denys Vlasenko6f068902014-02-27 11:17:06 +0100568#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
569 if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
570 /*
571 * -I<str>:
572 * Unmodified args are kept in G.argv[i],
573 * G.args[i] receives malloced G.argv[i] with <str> replaced
574 * with input line. Setting this up:
575 */
576 G.args = NULL;
577 G.argv = argv;
578 argc = 0;
579 read_args = process_stdin_with_replace;
580 } else
581#endif
582 {
583 /* Allocate pointers for execvp.
584 * We can statically allocate (argc + n_max_arg + 1) elements
585 * and do not bother with resizing args[], but on 64-bit machines
586 * this results in args[] vector which is ~8 times bigger
587 * than n_max_chars! That is, with n_max_chars == 20k,
588 * args[] will take 160k (!), which will most likely be
589 * almost entirely unused.
590 *
591 * See store_param() for matching 256-step growth logic
592 */
593 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
594 /* Store the command to be executed, part 1 */
595 for (i = 0; argv[i]; i++)
596 G.args[i] = argv[i];
597 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200598
599 while (1) {
600 char *rem;
601
602 G.idx = argc;
603 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200604 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200605
606 if (!G.args[argc]) {
607 if (*rem != '\0')
608 bb_error_msg_and_die("argument line too long");
609 if (opt & OPT_NO_EMPTY)
610 break;
611 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000612 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200613
Denis Vlasenko6248a732006-09-29 08:20:30 +0000614 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200615 const char *fmt = " %s" + 1;
616 char **args = G.args;
617 for (i = 0; args[i]; i++) {
618 fprintf(stderr, fmt, args[i]);
619 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000620 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000621 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200622 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000623 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200624
Denis Vlasenko6248a732006-09-29 08:20:30 +0000625 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200626 child_error = xargs_exec();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000627 }
628
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000629 if (child_error > 0 && child_error != 123) {
630 break;
631 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200632
633 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000634 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200635
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200636 if (ENABLE_FEATURE_CLEAN_UP) {
637 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200638 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200639 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200640
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000641 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000642}
643
644
645#ifdef TEST
646
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000647const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000648
649void bb_show_usage(void)
650{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000651 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000652 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000653 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000654}
655
656int main(int argc, char **argv)
657{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000658 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000659}
Eric Andersen252183e2003-10-31 08:19:44 +0000660#endif /* TEST */