blob: 967737133685dc56646b6d279adc507f873c7139 [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.
56
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +010057//applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
58
59//kbuild:lib-$(CONFIG_XARGS) += xargs.o
60
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000061#include "libbb.h"
Glenn L McGrath61796942003-10-10 12:10:18 +000062
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020063/* This is a NOEXEC applet. Be very careful! */
64
65
66//#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
67#define dbg_msg(...) ((void)0)
68
Glenn L McGrath61796942003-10-10 12:10:18 +000069
70#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000071# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
72# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000073# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000074# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
75# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000076# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000077# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
78# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000079# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000080# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
81# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000082# endif
83#endif
84
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020085
86struct globals {
87 char **args;
88 const char *eof_str;
89 int idx;
90} FIX_ALIASING;
91#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +010092#define INIT_G() do { \
93 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
94} while (0)
Denys Vlasenko7a4021d2010-06-14 00:57:05 +020095
96
Glenn L McGrathf57674e2002-11-10 21:47:17 +000097/*
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +020098 * This function has special algorithm.
99 * Don't use fork and include to main!
100 */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200101static int xargs_exec(void)
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000102{
Denis Vlasenko6248a732006-09-29 08:20:30 +0000103 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +0000104
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200105 status = spawn_and_wait(G.args);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000106 if (status < 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200107 bb_simple_perror_msg(G.args[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000108 return errno == ENOENT ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000109 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000110 if (status == 255) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200111 bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000112 return 124;
113 }
Denys Vlasenko8531d762010-03-18 22:44:00 +0100114 if (status >= 0x180) {
Denis Vlasenko6248a732006-09-29 08:20:30 +0000115 bb_error_msg("%s: terminated by signal %d",
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200116 G.args[0], status - 0x180);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000117 return 125;
118 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000119 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +0000120 return 123;
121 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000122}
Glenn L McGrath99825962003-10-09 11:06:45 +0000123
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200124/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
125 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
126 */
127#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000128
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200129static void store_param(char *s)
130{
131 /* Grow by 256 elements at once */
132 if (!(G.idx & 0xff)) { /* G.idx == N*256 */
133 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
134 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
135 }
136 G.args[G.idx++] = s;
137}
138
139/* process[0]_stdin:
140 * Read characters into buf[n_max_chars+1], and when parameter delimiter
141 * is seen, store the address of a new parameter to args[].
142 * If reading discovers that last chars do not form the complete
143 * parameter, the pointer to the first such "tail character" is returned.
144 * (buf has extra byte at the end to accomodate terminating NUL
145 * of "tail characters" string).
146 * Otherwise, the returned pointer points to NUL byte.
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200147 * On entry, buf[] may contain some "seed chars" which are to become
148 * the beginning of the first parameter.
149 */
150
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000151#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200152static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000153{
154#define NORM 0
155#define QUOTE 1
156#define BACKSLASH 2
157#define SPACE 4
Denys Vlasenko237aece2010-06-15 10:18:01 +0200158 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000159 char state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200160 char *s = buf; /* start of the word */
161 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000162
Denys Vlasenko237aece2010-06-15 10:18:01 +0200163 buf += n_max_chars; /* past buffer's end */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200164
165 /* "goto ret" is used instead of "break" to make control flow
166 * more obvious: */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000167
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200168 while (1) {
169 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000170 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200171 if (p != s)
172 goto close_word;
173 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000174 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000175 if (state == BACKSLASH) {
176 state = NORM;
177 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200178 }
179 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000180 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000181 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000182 q = '\0';
183 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000184 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000185 if (ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200186 if (p != s) {
187 close_word:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000188 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000189 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000190 goto set;
191 }
192 } else {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000193 if (c == '\\') {
194 state = BACKSLASH;
195 } else if (c == '\'' || c == '"') {
196 q = c;
197 state = QUOTE;
198 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000199 set:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000200 *p++ = c;
201 }
202 }
203 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000204 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000205 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000206 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000207 q == '\'' ? "single" : "double");
208 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200209 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200210 if (G.eof_str) {
211 if (strcmp(s, G.eof_str) == 0) {
212 while (getchar() != EOF)
213 continue;
214 p = s;
215 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000216 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000217 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200218 store_param(s);
219 dbg_msg("args[]:'%s'", s);
220 s = p;
221 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200222 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200223 goto ret;
224 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000225 state = NORM;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200226 }
227 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200228 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000229 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000230 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200231 ret:
232 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200233 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200234 dbg_msg("return:'%s'", s);
235 return s;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000236}
Glenn L McGrath61796942003-10-10 12:10:18 +0000237#else
Eric Andersen252183e2003-10-31 08:19:44 +0000238/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200239static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000240{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200241 char *s = buf; /* start of the word */
242 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000243
Denys Vlasenko237aece2010-06-15 10:18:01 +0200244 buf += n_max_chars; /* past buffer's end */
Eric Andersen92a61c12000-09-22 20:01:23 +0000245
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200246 while (1) {
247 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000248 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200249 if (p == s)
250 goto ret;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200251 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000252 if (c == EOF || ISSPACE(c)) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200253 if (p == s)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000254 continue;
255 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000256 }
Denis Vlasenko58394b12007-04-15 08:38:50 +0000257 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000258 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200259 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200260 if (G.eof_str) {
261 if (strcmp(s, G.eof_str) == 0) {
262 while (getchar() != EOF)
263 continue;
264 p = s;
265 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000266 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000267 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200268 store_param(s);
269 dbg_msg("args[]:'%s'", s);
270 s = p;
271 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200272 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200273 goto ret;
274 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200275 }
276 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200277 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000278 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000279 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200280 ret:
281 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200282 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200283 dbg_msg("return:'%s'", s);
284 return s;
Eric Andersen92a61c12000-09-22 20:01:23 +0000285}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000286#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000287
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000288#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200289static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000290{
Denys Vlasenko237aece2010-06-15 10:18:01 +0200291 char *s = buf; /* start of the word */
292 char *p = s + strlen(buf); /* end of the word */
Glenn L McGrath61796942003-10-10 12:10:18 +0000293
Denys Vlasenko237aece2010-06-15 10:18:01 +0200294 buf += n_max_chars; /* past buffer's end */
Glenn L McGrath61796942003-10-10 12:10:18 +0000295
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200296 while (1) {
297 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000298 if (c == EOF) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200299 if (p == s)
300 goto ret;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000301 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000302 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000303 *p++ = c;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000304 if (c == '\0') { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200305 /* A full word is loaded */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200306 store_param(s);
307 dbg_msg("args[]:'%s'", s);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200308 s = p;
Denys Vlasenkof7e929e2010-06-15 10:02:04 +0200309 n_max_arg--;
Denys Vlasenko237aece2010-06-15 10:18:01 +0200310 if (n_max_arg == 0) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200311 goto ret;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000312 }
Denys Vlasenko237aece2010-06-15 10:18:01 +0200313 }
314 if (p == buf) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200315 goto ret;
Glenn L McGrath61796942003-10-10 12:10:18 +0000316 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000317 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200318 ret:
319 *p = '\0';
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200320 /* store_param(NULL) - caller will do it */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200321 dbg_msg("return:'%s'", s);
322 return s;
Glenn L McGrath61796942003-10-10 12:10:18 +0000323}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000324#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000325
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200326#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
327/* Prompt the user for a response, and
328 if the user responds affirmatively, return true;
329 otherwise, return false. Uses "/dev/tty", not stdin. */
330static int xargs_ask_confirmation(void)
331{
332 FILE *tty_stream;
333 int c, savec;
334
335 tty_stream = xfopen_for_read(CURRENT_TTY);
336 fputs(" ?...", stderr);
337 fflush_all();
338 c = savec = getc(tty_stream);
339 while (c != EOF && c != '\n')
340 c = getc(tty_stream);
341 fclose(tty_stream);
342 return (savec == 'y' || savec == 'Y');
343}
344#else
345# define xargs_ask_confirmation() 1
346#endif
347
Denys Vlasenko4f731ce2010-06-15 15:40:16 +0200348//usage:#define xargs_trivial_usage
349//usage: "[OPTIONS] [PROG ARGS]"
350//usage:#define xargs_full_usage "\n\n"
351//usage: "Run PROG on every item given by stdin\n"
352//usage: "\nOptions:"
353//usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
354//usage: "\n -p Ask user whether to run each command"
355//usage: )
356//usage: "\n -r Don't run command if input is empty"
357//usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
358//usage: "\n -0 Input is separated by NUL characters"
359//usage: )
360//usage: "\n -t Print the command on stderr before execution"
361//usage: "\n -e[STR] STR stops input processing"
362//usage: "\n -n N Pass no more than N args to PROG"
363//usage: "\n -s N Pass command line of no more than N bytes"
364//usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
365//usage: "\n -x Exit if size is exceeded"
366//usage: )
367//usage:#define xargs_example_usage
368//usage: "$ ls | xargs gzip\n"
369//usage: "$ find . -name '*.c' -print | xargs rm\n"
370
Denis Vlasenko6248a732006-09-29 08:20:30 +0000371/* Correct regardless of combination of CONFIG_xxx */
372enum {
373 OPTBIT_VERBOSE = 0,
374 OPTBIT_NO_EMPTY,
375 OPTBIT_UPTO_NUMBER,
376 OPTBIT_UPTO_SIZE,
377 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000378 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000379 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
380 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
381 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000382
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000383 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
384 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
385 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
386 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
387 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
388 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000389 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
390 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
391 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000392};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000393#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000394 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
395 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
396 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000397
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000398int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000399int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000400{
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200401 int i;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000402 int child_error = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200403 char *max_args;
404 char *max_chars;
405 char *buf;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000406 unsigned opt;
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200407 int n_max_chars;
408 int n_max_arg;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000409#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200410 char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000411#else
412#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000413#endif
414
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200415 INIT_G();
416
Denys Vlasenko7cfe6ea2011-03-03 09:51:57 +0100417#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
418 /* For example, Fedora's build system uses --no-run-if-empty */
419 applet_long_options =
420 "no-run-if-empty\0" No_argument "r"
421 ;
422#endif
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200423 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &G.eof_str, &G.eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000424
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000425 /* -E ""? You may wonder why not just omit -E?
426 * This is used for portability:
427 * old xargs was using "_" as default for -E / -e */
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200428 if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
429 G.eof_str = NULL;
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000430
Denis Vlasenko6248a732006-09-29 08:20:30 +0000431 if (opt & OPT_ZEROTERM)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000432 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000433
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000434 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000435 argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200436 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000437 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200438 *--argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000439 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000440 }
441
Denys Vlasenko4a966172010-06-19 21:44:01 +0200442 /* -s NUM default. fileutils-4.4.2 uses 128k, but I heasitate
443 * to use such a big value - first need to change code to use
444 * growable buffer instead of fixed one.
445 */
446 n_max_chars = 32 * 1024;
447 /* Make smaller if system does not allow our default value.
448 * The Open Group Base Specifications Issue 6:
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200449 * "The xargs utility shall limit the command line length such that
450 * when the command line is invoked, the combined argument
451 * and environment lists (see the exec family of functions
452 * in the System Interfaces volume of IEEE Std 1003.1-2001)
453 * shall not exceed {ARG_MAX}-2048 bytes".
454 */
Denys Vlasenko4a966172010-06-19 21:44:01 +0200455 {
456 long arg_max = 0;
457#if defined _SC_ARG_MAX
458 arg_max = sysconf(_SC_ARG_MAX) - 2048;
459#elif defined ARG_MAX
460 arg_max = ARG_MAX - 2048;
461#endif
462 if (arg_max > 0 && n_max_chars > arg_max)
463 n_max_chars = arg_max;
464 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000465 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200466 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200467 }
468 /* Account for prepended fixed arguments */
469 {
470 size_t n_chars = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200471 for (i = 0; argv[i]; i++) {
472 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000473 }
474 n_max_chars -= n_chars;
Denys Vlasenko4a966172010-06-19 21:44:01 +0200475 }
476 /* Sanity check */
477 if (n_max_chars <= 0) {
478 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath61796942003-10-10 12:10:18 +0000479 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200480
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200481 buf = xzalloc(n_max_chars + 1);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000482
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200483 n_max_arg = n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000484 if (opt & OPT_UPTO_NUMBER) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200485 n_max_arg = xatou_range(max_args, 1, INT_MAX);
Denys Vlasenko4a966172010-06-19 21:44:01 +0200486 /* Not necessary, we use growable args[]: */
487 /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
Glenn L McGrath61796942003-10-10 12:10:18 +0000488 }
489
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200490 /* Allocate pointers for execvp */
491 /* We can statically allocate (argc + n_max_arg + 1) elements
492 * and do not bother with resizing args[], but on 64-bit machines
493 * this results in args[] vector which is ~8 times bigger
494 * than n_max_chars! That is, with n_max_chars == 20k,
495 * args[] will take 160k (!), which will most likely be
496 * almost entirely unused.
497 */
498 /* See store_param() for matching 256-step growth logic */
499 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200500
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200501 /* Store the command to be executed, part 1 */
502 for (i = 0; argv[i]; i++)
503 G.args[i] = argv[i];
504
505 while (1) {
506 char *rem;
507
508 G.idx = argc;
509 rem = read_args(n_max_chars, n_max_arg, buf);
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200510 store_param(NULL);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200511
512 if (!G.args[argc]) {
513 if (*rem != '\0')
514 bb_error_msg_and_die("argument line too long");
515 if (opt & OPT_NO_EMPTY)
516 break;
517 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000518 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200519
Denis Vlasenko6248a732006-09-29 08:20:30 +0000520 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Denys Vlasenkoc28cafb2010-06-14 12:38:36 +0200521 const char *fmt = " %s" + 1;
522 char **args = G.args;
523 for (i = 0; args[i]; i++) {
524 fprintf(stderr, fmt, args[i]);
525 fmt = " %s";
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000526 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000527 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200528 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000529 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200530
Denis Vlasenko6248a732006-09-29 08:20:30 +0000531 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200532 child_error = xargs_exec();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000533 }
534
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000535 if (child_error > 0 && child_error != 123) {
536 break;
537 }
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200538
539 overlapping_strcpy(buf, rem);
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000540 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200541
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200542 if (ENABLE_FEATURE_CLEAN_UP) {
543 free(G.args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200544 free(buf);
Denys Vlasenko7a4021d2010-06-14 00:57:05 +0200545 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200546
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000547 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000548}
549
550
551#ifdef TEST
552
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000553const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000554
555void bb_show_usage(void)
556{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000557 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000558 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000559 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000560}
561
562int main(int argc, char **argv)
563{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000564 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000565}
Eric Andersen252183e2003-10-31 08:19:44 +0000566#endif /* TEST */