blob: 3785f1ec002501874d2d879c57c07247a46d7086 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000021
Denis Vlasenko99912ca2007-04-10 15:43:37 +000022/* This is a NOEXEC applet. Be very careful! */
23
24
Glenn L McGrath61796942003-10-10 12:10:18 +000025/* COMPAT: SYSV version defaults size (and has a max value of) to 470.
26 We try to make it as large as possible. */
27#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
Denys Vlasenkod7b52892010-04-09 14:58:40 +020028# define ARG_MAX sysconf(_SC_ARG_MAX)
Glenn L McGrath61796942003-10-10 12:10:18 +000029#endif
Denys Vlasenkod7b52892010-04-09 14:58:40 +020030#if !defined(ARG_MAX)
31# define ARG_MAX 470
Glenn L McGrath61796942003-10-10 12:10:18 +000032#endif
33
34
35#ifdef TEST
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000036# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
37# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
Glenn L McGrath61796942003-10-10 12:10:18 +000038# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000039# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
40# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
Glenn L McGrath61796942003-10-10 12:10:18 +000041# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000042# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
43# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
Glenn L McGrath61796942003-10-10 12:10:18 +000044# endif
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000045# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
46# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
Glenn L McGrath61796942003-10-10 12:10:18 +000047# endif
48#endif
49
Glenn L McGrathf57674e2002-11-10 21:47:17 +000050/*
Denis Vlasenko6248a732006-09-29 08:20:30 +000051 This function has special algorithm.
52 Don't use fork and include to main!
Glenn L McGrathf57674e2002-11-10 21:47:17 +000053*/
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000054static int xargs_exec(char **args)
Glenn L McGrathf57674e2002-11-10 21:47:17 +000055{
Denis Vlasenko6248a732006-09-29 08:20:30 +000056 int status;
Glenn L McGrath61796942003-10-10 12:10:18 +000057
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000058 status = spawn_and_wait(args);
59 if (status < 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000060 bb_simple_perror_msg(args[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000061 return errno == ENOENT ? 127 : 126;
Denis Vlasenko6248a732006-09-29 08:20:30 +000062 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000063 if (status == 255) {
Denis Vlasenko6248a732006-09-29 08:20:30 +000064 bb_error_msg("%s: exited with status 255; aborting", args[0]);
65 return 124;
66 }
Denys Vlasenko8531d762010-03-18 22:44:00 +010067 if (status >= 0x180) {
Denis Vlasenko6248a732006-09-29 08:20:30 +000068 bb_error_msg("%s: terminated by signal %d",
Denys Vlasenko8531d762010-03-18 22:44:00 +010069 args[0], status - 0x180);
Denis Vlasenko6248a732006-09-29 08:20:30 +000070 return 125;
71 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000072 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +000073 return 123;
74 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +000075}
Glenn L McGrath99825962003-10-09 11:06:45 +000076
Glenn L McGrath61796942003-10-10 12:10:18 +000077
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000078typedef struct xlist_t {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000079 struct xlist_t *link;
Denis Vlasenko58394b12007-04-15 08:38:50 +000080 size_t length;
81 char xstr[1];
Glenn L McGrath61796942003-10-10 12:10:18 +000082} xlist_t;
83
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000084static smallint eof_stdin_detected;
Glenn L McGrath61796942003-10-10 12:10:18 +000085
86#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
Denis Vlasenko6248a732006-09-29 08:20:30 +000087#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
Glenn L McGrath61796942003-10-10 12:10:18 +000088 || (c) == '\f' || (c) == '\v')
89
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000090#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
91static xlist_t *process_stdin(xlist_t *list_arg,
Eric Andersen252183e2003-10-31 08:19:44 +000092 const char *eof_str, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +000093{
94#define NORM 0
95#define QUOTE 1
96#define BACKSLASH 2
97#define SPACE 4
98
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +000099 char *s = NULL; /* start word */
100 char *p = NULL; /* pointer to end word */
Denis Vlasenko58394b12007-04-15 08:38:50 +0000101 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000102 char state = NORM;
103 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000104 size_t line_l = 0; /* size loaded args line */
105 int c; /* current char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000106 xlist_t *cur;
107 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000108
Denis Vlasenko89054962007-04-10 21:41:16 +0000109 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000110 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000111 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000112 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000113 line_l += cur->length;
114 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000115 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000116
117 while (!eof_stdin_detected) {
118 c = getchar();
119 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000120 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000121 if (s)
122 goto unexpected_eof;
123 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000124 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000125 if (eof_str_detected)
126 continue;
127 if (state == BACKSLASH) {
128 state = NORM;
129 goto set;
130 } else if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000131 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000132 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000133 q = '\0';
134 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000135 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000136 if (ISSPACE(c)) {
137 if (s) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000138 unexpected_eof:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000139 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000140 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000141 goto set;
142 }
143 } else {
144 if (s == NULL)
145 s = p = buf;
146 if (c == '\\') {
147 state = BACKSLASH;
148 } else if (c == '\'' || c == '"') {
149 q = c;
150 state = QUOTE;
151 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000152 set:
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000153 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000154 bb_error_msg_and_die("argument line too long");
155 *p++ = c;
156 }
157 }
158 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000159 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000160 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000161 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000162 q == '\'' ? "single" : "double");
163 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000164 /* word loaded */
165 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000166 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000167 }
168 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000169 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000170 /* Dont xzalloc - it can be quite big */
171 cur = xmalloc(offsetof(xlist_t, xstr) + length);
172 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000173 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000174 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000175 if (prev == NULL) {
176 list_arg = cur;
177 } else {
178 prev->link = cur;
179 }
180 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000181 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000182 if (line_l > mc) {
183 /* stop memory usage :-) */
184 break;
185 }
186 }
187 s = NULL;
188 state = NORM;
189 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000190 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000191 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000192}
Glenn L McGrath61796942003-10-10 12:10:18 +0000193#else
Eric Andersen252183e2003-10-31 08:19:44 +0000194/* The variant does not support single quotes, double quotes or backslash */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000195static xlist_t *process_stdin(xlist_t *list_arg,
196 const char *eof_str, size_t mc, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000197{
Eric Andersen92a61c12000-09-22 20:01:23 +0000198
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000199 int c; /* current char */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000200 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000201 char *s = NULL; /* start word */
202 char *p = NULL; /* pointer to end word */
203 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000204 xlist_t *cur;
205 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000206
Denis Vlasenko89054962007-04-10 21:41:16 +0000207 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000208 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000209 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000210 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000211 line_l += cur->length;
212 cur = cur->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000213 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000214
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000215 while (!eof_stdin_detected) {
216 c = getchar();
217 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000218 eof_stdin_detected = 1;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000219 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000220 if (eof_str_detected)
221 continue;
222 if (c == EOF || ISSPACE(c)) {
223 if (s == NULL)
224 continue;
225 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000226 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000227 if (s == NULL)
228 s = p = buf;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000229 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000230 bb_error_msg_and_die("argument line too long");
Denis Vlasenko58394b12007-04-15 08:38:50 +0000231 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000232 if (c == EOF) { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000233 /* word loaded */
234 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000235 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000236 }
237 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000238 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000239 /* Dont xzalloc - it can be quite big */
240 cur = xmalloc(offsetof(xlist_t, xstr) + length);
241 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000242 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000243 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000244 if (prev == NULL) {
245 list_arg = cur;
246 } else {
247 prev->link = cur;
248 }
249 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000250 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000251 if (line_l > mc) {
252 /* stop memory usage :-) */
253 break;
254 }
255 s = NULL;
256 }
257 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000258 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000259 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000260}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000261#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000262
263
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000264#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
Glenn L McGrath61796942003-10-10 12:10:18 +0000265/* Prompt the user for a response, and
266 if the user responds affirmatively, return true;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000267 otherwise, return false. Uses "/dev/tty", not stdin. */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000268static int xargs_ask_confirmation(void)
Glenn L McGrath61796942003-10-10 12:10:18 +0000269{
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000270 FILE *tty_stream;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000271 int c, savec;
Glenn L McGrath61796942003-10-10 12:10:18 +0000272
Denis Vlasenko5415c852008-07-21 23:05:26 +0000273 tty_stream = xfopen_for_read(CURRENT_TTY);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000274 fputs(" ?...", stderr);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100275 fflush_all();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000276 c = savec = getc(tty_stream);
277 while (c != EOF && c != '\n')
278 c = getc(tty_stream);
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000279 fclose(tty_stream);
280 return (savec == 'y' || savec == 'Y');
Glenn L McGrath61796942003-10-10 12:10:18 +0000281}
Glenn L McGrath61796942003-10-10 12:10:18 +0000282#else
Glenn L McGrath61796942003-10-10 12:10:18 +0000283# define xargs_ask_confirmation() 1
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000284#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
Glenn L McGrath61796942003-10-10 12:10:18 +0000285
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000286#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
287static xlist_t *process0_stdin(xlist_t *list_arg,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000288 const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000289{
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000290 int c; /* current char */
291 char *s = NULL; /* start word */
292 char *p = NULL; /* pointer to end word */
293 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000294 xlist_t *cur;
295 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000296
Denis Vlasenko89054962007-04-10 21:41:16 +0000297 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000298 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000299 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000300 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000301 line_l += cur->length;
302 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000303 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000304
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000305 while (!eof_stdin_detected) {
306 c = getchar();
307 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000308 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000309 if (s == NULL)
310 break;
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 if (s == NULL)
314 s = p = buf;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000315 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000316 bb_error_msg_and_die("argument line too long");
317 *p++ = c;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000318 if (c == '\0') { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000319 /* word loaded */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000320 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000321 /* Dont xzalloc - it can be quite big */
322 cur = xmalloc(offsetof(xlist_t, xstr) + length);
323 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000324 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000325 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000326 if (prev == NULL) {
327 list_arg = cur;
328 } else {
329 prev->link = cur;
330 }
331 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000332 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000333 if (line_l > mc) {
334 /* stop memory usage :-) */
335 break;
336 }
337 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000338 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000339 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000340 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000341}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000342#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000343
Denis Vlasenko6248a732006-09-29 08:20:30 +0000344/* Correct regardless of combination of CONFIG_xxx */
345enum {
346 OPTBIT_VERBOSE = 0,
347 OPTBIT_NO_EMPTY,
348 OPTBIT_UPTO_NUMBER,
349 OPTBIT_UPTO_SIZE,
350 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000351 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000352 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
353 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
354 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000355
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000356 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
357 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
358 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
359 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
360 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
361 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000362 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
363 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
364 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000365};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000366#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000367 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
368 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
369 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000370
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000371int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000372int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000373{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000374 char **args;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000375 int i, n;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000376 xlist_t *list = NULL;
377 xlist_t *cur;
378 int child_error = 0;
379 char *max_args, *max_chars;
380 int n_max_arg;
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000381 const char *eof_str = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000382 unsigned opt;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000383 size_t n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000384#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
385 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
386#else
387#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000388#endif
389
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000390 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000391
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000392 /* -E ""? You may wonder why not just omit -E?
393 * This is used for portability:
394 * old xargs was using "_" as default for -E / -e */
395 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000396 eof_str = NULL;
397
Denis Vlasenko6248a732006-09-29 08:20:30 +0000398 if (opt & OPT_ZEROTERM)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000399 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000400
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000401 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000402 argc -= optind;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000403 if (!argc) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000404 /* default behavior is to echo all the filenames */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000405 *argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000406 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000407 }
408
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200409 n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */
410 if (n_max_chars < 4*1024); /* paranoia */
411 n_max_chars = LONG_MAX;
412 /* The Open Group Base Specifications Issue 6:
413 * "The xargs utility shall limit the command line length such that
414 * when the command line is invoked, the combined argument
415 * and environment lists (see the exec family of functions
416 * in the System Interfaces volume of IEEE Std 1003.1-2001)
417 * shall not exceed {ARG_MAX}-2048 bytes".
418 */
419 n_max_chars -= 2048;
420 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
421 * have it at 1 meg). Things will work fine with a large ARG_MAX but it
422 * will probably hurt the system more than it needs to; an array of this
423 * size is allocated.
424 */
425 if (n_max_chars > 20 * 1024)
426 n_max_chars = 20 * 1024;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000427
428 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200429 size_t n_chars = 0;
430 n_max_chars = xatoul_range(max_chars, 1, n_max_chars);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000431 for (i = 0; i < argc; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000432 n_chars += strlen(*argv) + 1;
Glenn L McGrath61796942003-10-10 12:10:18 +0000433 }
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200434 if (n_max_chars <= n_chars) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100435 bb_error_msg_and_die("can't fit single argument within argument list size limit");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000436 }
437 n_max_chars -= n_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000438 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000439 max_chars = xmalloc(n_max_chars);
440
Denis Vlasenko6248a732006-09-29 08:20:30 +0000441 if (opt & OPT_UPTO_NUMBER) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000442 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000443 } else {
444 n_max_arg = n_max_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000445 }
446
Denis Vlasenko6248a732006-09-29 08:20:30 +0000447 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
448 !(opt & OPT_NO_EMPTY))
Eric Andersen252183e2003-10-31 08:19:44 +0000449 {
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200450 size_t n_chars = 0;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000451 opt |= OPT_NO_EMPTY;
452 n = 0;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000453#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000454 for (cur = list; cur;) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000455 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000456 n++;
457 cur = cur->link;
458 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
459 if (opt & OPT_TERMINATE)
460 bb_error_msg_and_die("argument list too long");
461 break;
462 }
463 }
464#else
465 for (cur = list; cur; cur = cur->link) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000466 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000467 n++;
468 if (n_chars > n_max_chars || n == n_max_arg) {
469 break;
470 }
471 }
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000472#endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000473
Denis Vlasenko6248a732006-09-29 08:20:30 +0000474 /* allocate pointers for execvp:
475 argc*arg, n*arg from stdin, NULL */
476 args = xzalloc((n + argc + 1) * sizeof(char *));
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000477
Denis Vlasenko6248a732006-09-29 08:20:30 +0000478 /* store the command to be executed
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000479 (taken from the command line) */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000480 for (i = 0; i < argc; i++)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000481 args[i] = argv[i];
482 /* (taken from stdin) */
483 for (cur = list; n; cur = cur->link) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000484 args[i++] = cur->xstr;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000485 n--;
486 }
487
Denis Vlasenko6248a732006-09-29 08:20:30 +0000488 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000489 for (i = 0; args[i]; i++) {
490 if (i)
491 fputc(' ', stderr);
492 fputs(args[i], stderr);
493 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000494 if (!(opt & OPT_INTERACTIVE))
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000495 fputc('\n', stderr);
496 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000497 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000498 child_error = xargs_exec(args);
499 }
500
501 /* clean up */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000502 for (i = argc; args[i]; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000503 cur = list;
504 list = list->link;
505 free(cur);
506 }
507 free(args);
508 if (child_error > 0 && child_error != 123) {
509 break;
510 }
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000511 } /* while */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000512 if (ENABLE_FEATURE_CLEAN_UP)
513 free(max_chars);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000514 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000515}
516
517
518#ifdef TEST
519
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000520const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000521
522void bb_show_usage(void)
523{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000524 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000525 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000526 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000527}
528
529int main(int argc, char **argv)
530{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000531 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000532}
Eric Andersen252183e2003-10-31 08:19:44 +0000533#endif /* TEST */