blob: c55ac5724e795665c277da8abe9c3dc0df4fd64b [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
Glenn L McGrath61796942003-10-10 12:10:18 +00004 * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
Eric Andersen5b176932000-09-22 20:22:28 +00005 *
Glenn L McGrath61796942003-10-10 12:10:18 +00006 * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrathf57674e2002-11-10 21:47:17 +00007 *
Glenn L McGrath61796942003-10-10 12:10:18 +00008 * Special thanks
Eric Andersenaff114c2004-04-14 17:51:38 +00009 * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
Glenn L McGrath61796942003-10-10 12:10:18 +000010 * - Mike Rendell <michael@cs.mun.ca>
11 * and David MacKenzie <djm@gnu.ai.mit.edu>.
Eric Andersen5b176932000-09-22 20:22:28 +000012 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersena37d5b72000-09-23 06:10:14 +000014 *
Glenn L McGrath40c94892003-10-30 22:51:33 +000015 * xargs is described in the Single Unix Specification v3 at
16 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
17 *
Eric Andersen5b176932000-09-22 20:22:28 +000018 */
Eric Andersen92a61c12000-09-22 20:01:23 +000019
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020020//kbuild:lib-$(CONFIG_XARGS) += xargs.o
21//config:
22//config:config XARGS
23//config: bool "xargs"
24//config: default n
25//config: help
26//config: xargs is used to execute a specified command for
27//config: every item from standard input.
28//config:
29//config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
30//config: bool "Enable -p: prompt and confirmation"
31//config: default n
32//config: depends on XARGS
33//config: help
34//config: Support -p: prompt the user whether to run each command
35//config: line and read a line from the terminal.
36//config:
37//config:config FEATURE_XARGS_SUPPORT_QUOTES
38//config: bool "Enable single and double quotes and backslash"
39//config: default n
40//config: depends on XARGS
41//config: help
42//config: Support quoting in the input.
43//config:
44//config:config FEATURE_XARGS_SUPPORT_TERMOPT
45//config: bool "Enable -x: exit if -s or -n is exceeded"
46//config: default n
47//config: depends on XARGS
48//config: help
49//config: Support -x: exit if the command size (see the -s or -n option)
50//config: is exceeded.
51//config:
52//config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
53//config: bool "Enable -0: NUL-terminated input"
54//config: default n
55//config: depends on XARGS
56//config: help
57//config: Support -0: input items are terminated by a NUL character
58//config: instead of whitespace, and the quotes and backslash
59//config: are not special.
60
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000061#include "libbb.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000062
Denis Vlasenko99912ca2007-04-10 15:43:37 +000063/* This is a NOEXEC applet. Be very careful! */
64
65
Glenn L McGrath61796942003-10-10 12:10:18 +000066/* COMPAT: SYSV version defaults size (and has a max value of) to 470.
67 We try to make it as large as possible. */
68#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
Denys Vlasenkod7b52892010-04-09 14:58:40 +020069# define ARG_MAX sysconf(_SC_ARG_MAX)
Glenn L McGrath61796942003-10-10 12:10:18 +000070#endif
Denys Vlasenkod7b52892010-04-09 14:58:40 +020071#if !defined(ARG_MAX)
72# define ARG_MAX 470
Glenn L McGrath61796942003-10-10 12:10:18 +000073#endif
74
75
76#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000077# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
78# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000079# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000080# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
81# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000082# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000083# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
84# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000085# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000086# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
87# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000088# endif
89#endif
90
Glenn L McGrathf57674e2002-11-10 21:47:17 +000091/*
Denis Vlasenko6248a732006-09-29 08:20:30 +000092 This function has special algorithm.
93 Don't use fork and include to main!
Glenn L McGrathf57674e2002-11-10 21:47:17 +000094*/
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000095static int xargs_exec(char **args)
Glenn L McGrathf57674e2002-11-10 21:47:17 +000096{
Denis Vlasenko6248a732006-09-29 08:20:30 +000097 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +000098
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000099 status = spawn_and_wait(args);
100 if (status < 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000101 bb_simple_perror_msg(args[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000102 return errno == ENOENT ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000103 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000104 if (status == 255) {
Denis Vlasenko6248a732006-09-29 08:20:30 +0000105 bb_error_msg("%s: exited with status 255; aborting", args[0]);
106 return 124;
107 }
Denys Vlasenko8531d762010-03-18 22:44:00 +0100108 if (status >= 0x180) {
Denis Vlasenko6248a732006-09-29 08:20:30 +0000109 bb_error_msg("%s: terminated by signal %d",
Denys Vlasenko8531d762010-03-18 22:44:00 +0100110 args[0], status - 0x180);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000111 return 125;
112 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000113 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +0000114 return 123;
115 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +0000116}
Glenn L McGrath99825962003-10-09 11:06:45 +0000117
Glenn L McGrath61796942003-10-10 12:10:18 +0000118
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000119typedef struct xlist_t {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000120 struct xlist_t *link;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000121 size_t length;
122 char xstr[1];
Glenn L McGrath61796942003-10-10 12:10:18 +0000123} xlist_t;
124
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000125static smallint eof_stdin_detected;
Glenn L McGrath61796942003-10-10 12:10:18 +0000126
127#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
Denis Vlasenko6248a732006-09-29 08:20:30 +0000128#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
Glenn L McGrath61796942003-10-10 12:10:18 +0000129 || (c) == '\f' || (c) == '\v')
130
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000131#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
132static xlist_t *process_stdin(xlist_t *list_arg,
Eric Andersen252183e2003-10-31 08:19:44 +0000133 const char *eof_str, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000134{
135#define NORM 0
136#define QUOTE 1
137#define BACKSLASH 2
138#define SPACE 4
139
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000140 char *s = NULL; /* start word */
141 char *p = NULL; /* pointer to end word */
Denis Vlasenko58394b12007-04-15 08:38:50 +0000142 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000143 char state = NORM;
144 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000145 size_t line_l = 0; /* size loaded args line */
146 int c; /* current char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000147 xlist_t *cur;
148 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000149
Denis Vlasenko89054962007-04-10 21:41:16 +0000150 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000151 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000152 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000153 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000154 line_l += cur->length;
155 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000156 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000157
158 while (!eof_stdin_detected) {
159 c = getchar();
160 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000161 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000162 if (s)
163 goto unexpected_eof;
164 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000165 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000166 if (eof_str_detected)
167 continue;
168 if (state == BACKSLASH) {
169 state = NORM;
170 goto set;
171 } else if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000172 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000173 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000174 q = '\0';
175 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000176 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000177 if (ISSPACE(c)) {
178 if (s) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000179 unexpected_eof:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000180 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000181 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000182 goto set;
183 }
184 } else {
185 if (s == NULL)
186 s = p = buf;
187 if (c == '\\') {
188 state = BACKSLASH;
189 } else if (c == '\'' || c == '"') {
190 q = c;
191 state = QUOTE;
192 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000193 set:
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000194 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000195 bb_error_msg_and_die("argument line too long");
196 *p++ = c;
197 }
198 }
199 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000200 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000201 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000202 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000203 q == '\'' ? "single" : "double");
204 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000205 /* word loaded */
206 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000207 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000208 }
209 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000210 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000211 /* Dont xzalloc - it can be quite big */
212 cur = xmalloc(offsetof(xlist_t, xstr) + length);
213 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000214 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000215 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000216 if (prev == NULL) {
217 list_arg = cur;
218 } else {
219 prev->link = cur;
220 }
221 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000222 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000223 if (line_l > mc) {
224 /* stop memory usage :-) */
225 break;
226 }
227 }
228 s = NULL;
229 state = NORM;
230 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000231 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000232 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000233}
Glenn L McGrath61796942003-10-10 12:10:18 +0000234#else
Eric Andersen252183e2003-10-31 08:19:44 +0000235/* The variant does not support single quotes, double quotes or backslash */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000236static xlist_t *process_stdin(xlist_t *list_arg,
237 const char *eof_str, size_t mc, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000238{
Eric Andersen92a61c12000-09-22 20:01:23 +0000239
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000240 int c; /* current char */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000241 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000242 char *s = NULL; /* start word */
243 char *p = NULL; /* pointer to end word */
244 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000245 xlist_t *cur;
246 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000247
Denis Vlasenko89054962007-04-10 21:41:16 +0000248 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000249 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000250 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000251 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000252 line_l += cur->length;
253 cur = cur->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000254 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000255
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000256 while (!eof_stdin_detected) {
257 c = getchar();
258 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000259 eof_stdin_detected = 1;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000260 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000261 if (eof_str_detected)
262 continue;
263 if (c == EOF || ISSPACE(c)) {
264 if (s == NULL)
265 continue;
266 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000267 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000268 if (s == NULL)
269 s = p = buf;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000270 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000271 bb_error_msg_and_die("argument line too long");
Denis Vlasenko58394b12007-04-15 08:38:50 +0000272 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000273 if (c == EOF) { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000274 /* word loaded */
275 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000276 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000277 }
278 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000279 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000280 /* Dont xzalloc - it can be quite big */
281 cur = xmalloc(offsetof(xlist_t, xstr) + length);
282 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000283 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000284 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000285 if (prev == NULL) {
286 list_arg = cur;
287 } else {
288 prev->link = cur;
289 }
290 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000291 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000292 if (line_l > mc) {
293 /* stop memory usage :-) */
294 break;
295 }
296 s = NULL;
297 }
298 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000299 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000300 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000301}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000302#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000303
304
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000305#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
Glenn L McGrath61796942003-10-10 12:10:18 +0000306/* Prompt the user for a response, and
307 if the user responds affirmatively, return true;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000308 otherwise, return false. Uses "/dev/tty", not stdin. */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000309static int xargs_ask_confirmation(void)
Glenn L McGrath61796942003-10-10 12:10:18 +0000310{
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000311 FILE *tty_stream;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000312 int c, savec;
Glenn L McGrath61796942003-10-10 12:10:18 +0000313
Denis Vlasenko5415c852008-07-21 23:05:26 +0000314 tty_stream = xfopen_for_read(CURRENT_TTY);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000315 fputs(" ?...", stderr);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100316 fflush_all();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000317 c = savec = getc(tty_stream);
318 while (c != EOF && c != '\n')
319 c = getc(tty_stream);
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000320 fclose(tty_stream);
321 return (savec == 'y' || savec == 'Y');
Glenn L McGrath61796942003-10-10 12:10:18 +0000322}
Glenn L McGrath61796942003-10-10 12:10:18 +0000323#else
Glenn L McGrath61796942003-10-10 12:10:18 +0000324# define xargs_ask_confirmation() 1
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000325#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
Glenn L McGrath61796942003-10-10 12:10:18 +0000326
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000327#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
328static xlist_t *process0_stdin(xlist_t *list_arg,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000329 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000330{
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000331 int c; /* current char */
332 char *s = NULL; /* start word */
333 char *p = NULL; /* pointer to end word */
334 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000335 xlist_t *cur;
336 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000337
Denis Vlasenko89054962007-04-10 21:41:16 +0000338 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000339 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000340 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000341 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000342 line_l += cur->length;
343 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000344 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000345
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000346 while (!eof_stdin_detected) {
347 c = getchar();
348 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000349 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000350 if (s == NULL)
351 break;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000352 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000353 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000354 if (s == NULL)
355 s = p = buf;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000356 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000357 bb_error_msg_and_die("argument line too long");
358 *p++ = c;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000359 if (c == '\0') { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000360 /* word loaded */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000361 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000362 /* Dont xzalloc - it can be quite big */
363 cur = xmalloc(offsetof(xlist_t, xstr) + length);
364 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000365 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000366 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000367 if (prev == NULL) {
368 list_arg = cur;
369 } else {
370 prev->link = cur;
371 }
372 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000373 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000374 if (line_l > mc) {
375 /* stop memory usage :-) */
376 break;
377 }
378 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000379 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000380 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000381 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000382}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000383#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000384
Denis Vlasenko6248a732006-09-29 08:20:30 +0000385/* Correct regardless of combination of CONFIG_xxx */
386enum {
387 OPTBIT_VERBOSE = 0,
388 OPTBIT_NO_EMPTY,
389 OPTBIT_UPTO_NUMBER,
390 OPTBIT_UPTO_SIZE,
391 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000392 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000393 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
394 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
395 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000396
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000397 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
398 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
399 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
400 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
401 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
402 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000403 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
404 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
405 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000406};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000407#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000408 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
409 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
410 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000411
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000412int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000413int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000414{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000415 char **args;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000416 int i, n;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000417 xlist_t *list = NULL;
418 xlist_t *cur;
419 int child_error = 0;
420 char *max_args, *max_chars;
421 int n_max_arg;
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000422 const char *eof_str = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000423 unsigned opt;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000424 size_t n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000425#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
426 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
427#else
428#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000429#endif
430
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000431 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000432
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000433 /* -E ""? You may wonder why not just omit -E?
434 * This is used for portability:
435 * old xargs was using "_" as default for -E / -e */
436 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000437 eof_str = NULL;
438
Denis Vlasenko6248a732006-09-29 08:20:30 +0000439 if (opt & OPT_ZEROTERM)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000440 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000441
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000442 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000443 argc -= optind;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000444 if (!argc) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000445 /* default behavior is to echo all the filenames */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000446 *argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000447 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000448 }
449
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200450 n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */
451 if (n_max_chars < 4*1024); /* paranoia */
452 n_max_chars = LONG_MAX;
453 /* The Open Group Base Specifications Issue 6:
454 * "The xargs utility shall limit the command line length such that
455 * when the command line is invoked, the combined argument
456 * and environment lists (see the exec family of functions
457 * in the System Interfaces volume of IEEE Std 1003.1-2001)
458 * shall not exceed {ARG_MAX}-2048 bytes".
459 */
460 n_max_chars -= 2048;
461 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
462 * have it at 1 meg). Things will work fine with a large ARG_MAX but it
463 * will probably hurt the system more than it needs to; an array of this
464 * size is allocated.
465 */
466 if (n_max_chars > 20 * 1024)
467 n_max_chars = 20 * 1024;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000468
469 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200470 size_t n_chars = 0;
471 n_max_chars = xatoul_range(max_chars, 1, n_max_chars);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000472 for (i = 0; i < argc; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000473 n_chars += strlen(*argv) + 1;
Glenn L McGrath61796942003-10-10 12:10:18 +0000474 }
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200475 if (n_max_chars <= n_chars) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100476 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000477 }
478 n_max_chars -= n_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000479 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000480 max_chars = xmalloc(n_max_chars);
481
Denis Vlasenko6248a732006-09-29 08:20:30 +0000482 if (opt & OPT_UPTO_NUMBER) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000483 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000484 } else {
485 n_max_arg = n_max_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000486 }
487
Denis Vlasenko6248a732006-09-29 08:20:30 +0000488 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
489 !(opt & OPT_NO_EMPTY))
Eric Andersen252183e2003-10-31 08:19:44 +0000490 {
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200491 size_t n_chars = 0;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000492 opt |= OPT_NO_EMPTY;
493 n = 0;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000494#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000495 for (cur = list; cur;) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000496 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000497 n++;
498 cur = cur->link;
499 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
500 if (opt & OPT_TERMINATE)
501 bb_error_msg_and_die("argument list too long");
502 break;
503 }
504 }
505#else
506 for (cur = list; cur; cur = cur->link) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000507 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000508 n++;
509 if (n_chars > n_max_chars || n == n_max_arg) {
510 break;
511 }
512 }
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000513#endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000514
Denis Vlasenko6248a732006-09-29 08:20:30 +0000515 /* allocate pointers for execvp:
516 argc*arg, n*arg from stdin, NULL */
517 args = xzalloc((n + argc + 1) * sizeof(char *));
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000518
Denis Vlasenko6248a732006-09-29 08:20:30 +0000519 /* store the command to be executed
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000520 (taken from the command line) */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000521 for (i = 0; i < argc; i++)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000522 args[i] = argv[i];
523 /* (taken from stdin) */
524 for (cur = list; n; cur = cur->link) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000525 args[i++] = cur->xstr;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000526 n--;
527 }
528
Denis Vlasenko6248a732006-09-29 08:20:30 +0000529 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000530 for (i = 0; args[i]; i++) {
531 if (i)
532 fputc(' ', stderr);
533 fputs(args[i], stderr);
534 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000535 if (!(opt & OPT_INTERACTIVE))
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000536 fputc('\n', stderr);
537 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000538 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000539 child_error = xargs_exec(args);
540 }
541
542 /* clean up */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000543 for (i = argc; args[i]; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000544 cur = list;
545 list = list->link;
546 free(cur);
547 }
548 free(args);
549 if (child_error > 0 && child_error != 123) {
550 break;
551 }
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000552 } /* while */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000553 if (ENABLE_FEATURE_CLEAN_UP)
554 free(max_chars);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000555 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000556}
557
558
559#ifdef TEST
560
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000561const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000562
563void bb_show_usage(void)
564{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000565 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000566 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000567 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000568}
569
570int main(int argc, char **argv)
571{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000572 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000573}
Eric Andersen252183e2003-10-31 08:19:44 +0000574#endif /* TEST */