blob: ee16ea675da26cd5f0c582735c52f45bc0793cfe [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)
28#define ARG_MAX sysconf (_SC_ARG_MAX)
29#endif
30#ifndef ARG_MAX
31#define ARG_MAX 470
32#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 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000067/* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
Denis Vlasenko6248a732006-09-29 08:20:30 +000068 if (WIFSTOPPED(status)) {
69 bb_error_msg("%s: stopped by signal %d",
70 args[0], WSTOPSIG(status));
71 return 125;
72 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000073*/
74 if (status >= 1000) {
Denis Vlasenko6248a732006-09-29 08:20:30 +000075 bb_error_msg("%s: terminated by signal %d",
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000076 args[0], status - 1000);
Denis Vlasenko6248a732006-09-29 08:20:30 +000077 return 125;
78 }
Denis Vlasenkocd7001f2007-04-09 21:32:30 +000079 if (status)
Denis Vlasenko6248a732006-09-29 08:20:30 +000080 return 123;
81 return 0;
Glenn L McGrath61796942003-10-10 12:10:18 +000082}
Glenn L McGrath99825962003-10-09 11:06:45 +000083
Glenn L McGrath61796942003-10-10 12:10:18 +000084
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000085typedef struct xlist_t {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000086 struct xlist_t *link;
Denis Vlasenko58394b12007-04-15 08:38:50 +000087 size_t length;
88 char xstr[1];
Glenn L McGrath61796942003-10-10 12:10:18 +000089} xlist_t;
90
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000091static smallint eof_stdin_detected;
Glenn L McGrath61796942003-10-10 12:10:18 +000092
93#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
Denis Vlasenko6248a732006-09-29 08:20:30 +000094#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
Glenn L McGrath61796942003-10-10 12:10:18 +000095 || (c) == '\f' || (c) == '\v')
96
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +000097#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
98static xlist_t *process_stdin(xlist_t *list_arg,
Eric Andersen252183e2003-10-31 08:19:44 +000099 const char *eof_str, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000100{
101#define NORM 0
102#define QUOTE 1
103#define BACKSLASH 2
104#define SPACE 4
105
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000106 char *s = NULL; /* start word */
107 char *p = NULL; /* pointer to end word */
Denis Vlasenko58394b12007-04-15 08:38:50 +0000108 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000109 char state = NORM;
110 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000111 size_t line_l = 0; /* size loaded args line */
112 int c; /* current char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000113 xlist_t *cur;
114 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000115
Denis Vlasenko89054962007-04-10 21:41:16 +0000116 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000117 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000118 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000119 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000120 line_l += cur->length;
121 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000122 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000123
124 while (!eof_stdin_detected) {
125 c = getchar();
126 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000127 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000128 if (s)
129 goto unexpected_eof;
130 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000131 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000132 if (eof_str_detected)
133 continue;
134 if (state == BACKSLASH) {
135 state = NORM;
136 goto set;
137 } else if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000138 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000139 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000140 q = '\0';
141 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000142 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000143 if (ISSPACE(c)) {
144 if (s) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000145 unexpected_eof:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000146 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000147 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000148 goto set;
149 }
150 } else {
151 if (s == NULL)
152 s = p = buf;
153 if (c == '\\') {
154 state = BACKSLASH;
155 } else if (c == '\'' || c == '"') {
156 q = c;
157 state = QUOTE;
158 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000159 set:
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000160 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000161 bb_error_msg_and_die("argument line too long");
162 *p++ = c;
163 }
164 }
165 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000166 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000167 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000168 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000169 q == '\'' ? "single" : "double");
170 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000171 /* word loaded */
172 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000173 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000174 }
175 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000176 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000177 /* Dont xzalloc - it can be quite big */
178 cur = xmalloc(offsetof(xlist_t, xstr) + length);
179 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000180 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000181 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000182 if (prev == NULL) {
183 list_arg = cur;
184 } else {
185 prev->link = cur;
186 }
187 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000188 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000189 if (line_l > mc) {
190 /* stop memory usage :-) */
191 break;
192 }
193 }
194 s = NULL;
195 state = NORM;
196 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000197 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000198 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000199}
Glenn L McGrath61796942003-10-10 12:10:18 +0000200#else
Eric Andersen252183e2003-10-31 08:19:44 +0000201/* The variant does not support single quotes, double quotes or backslash */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000202static xlist_t *process_stdin(xlist_t *list_arg,
203 const char *eof_str, size_t mc, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000204{
Eric Andersen92a61c12000-09-22 20:01:23 +0000205
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000206 int c; /* current char */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000207 char eof_str_detected = 0;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000208 char *s = NULL; /* start word */
209 char *p = NULL; /* pointer to end word */
210 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000211 xlist_t *cur;
212 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000213
Denis Vlasenko89054962007-04-10 21:41:16 +0000214 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000215 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000216 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000217 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000218 line_l += cur->length;
219 cur = cur->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000220 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000221
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000222 while (!eof_stdin_detected) {
223 c = getchar();
224 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000225 eof_stdin_detected = 1;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000226 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000227 if (eof_str_detected)
228 continue;
229 if (c == EOF || ISSPACE(c)) {
230 if (s == NULL)
231 continue;
232 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000233 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000234 if (s == NULL)
235 s = p = buf;
236 if ((p - buf) >= mc)
237 bb_error_msg_and_die("argument line too long");
Denis Vlasenko58394b12007-04-15 08:38:50 +0000238 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000239 if (c == EOF) { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000240 /* word loaded */
241 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000242 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000243 }
244 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000245 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000246 /* Dont xzalloc - it can be quite big */
247 cur = xmalloc(offsetof(xlist_t, xstr) + length);
248 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000249 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000250 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000251 if (prev == NULL) {
252 list_arg = cur;
253 } else {
254 prev->link = cur;
255 }
256 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000257 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000258 if (line_l > mc) {
259 /* stop memory usage :-) */
260 break;
261 }
262 s = NULL;
263 }
264 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000265 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000266 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000267}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000268#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000269
270
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000271#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
Glenn L McGrath61796942003-10-10 12:10:18 +0000272/* Prompt the user for a response, and
273 if the user responds affirmatively, return true;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000274 otherwise, return false. Uses "/dev/tty", not stdin. */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000275static int xargs_ask_confirmation(void)
Glenn L McGrath61796942003-10-10 12:10:18 +0000276{
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000277 FILE *tty_stream;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000278 int c, savec;
Glenn L McGrath61796942003-10-10 12:10:18 +0000279
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000280 tty_stream = xfopen(CURRENT_TTY, "r");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000281 fputs(" ?...", stderr);
282 fflush(stderr);
283 c = savec = getc(tty_stream);
284 while (c != EOF && c != '\n')
285 c = getc(tty_stream);
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000286 fclose(tty_stream);
287 return (savec == 'y' || savec == 'Y');
Glenn L McGrath61796942003-10-10 12:10:18 +0000288}
Glenn L McGrath61796942003-10-10 12:10:18 +0000289#else
Glenn L McGrath61796942003-10-10 12:10:18 +0000290# define xargs_ask_confirmation() 1
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000291#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
Glenn L McGrath61796942003-10-10 12:10:18 +0000292
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000293#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
294static xlist_t *process0_stdin(xlist_t *list_arg,
295 const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000296{
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000297 int c; /* current char */
298 char *s = NULL; /* start word */
299 char *p = NULL; /* pointer to end word */
300 size_t line_l = 0; /* size loaded args line */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000301 xlist_t *cur;
302 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000303
Denis Vlasenko89054962007-04-10 21:41:16 +0000304 prev = cur = list_arg;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000305 while (1) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000306 if (!cur) break;
Denis Vlasenko89054962007-04-10 21:41:16 +0000307 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000308 line_l += cur->length;
309 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000310 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000311
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000312 while (!eof_stdin_detected) {
313 c = getchar();
314 if (c == EOF) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000315 eof_stdin_detected = 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000316 if (s == NULL)
317 break;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000318 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000319 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000320 if (s == NULL)
321 s = p = buf;
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000322 if ((size_t)(p - buf) >= mc)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000323 bb_error_msg_and_die("argument line too long");
324 *p++ = c;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000325 if (c == '\0') { /* word's delimiter or EOF detected */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000326 /* word loaded */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000327 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000328 /* Dont xzalloc - it can be quite big */
329 cur = xmalloc(offsetof(xlist_t, xstr) + length);
330 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000331 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000332 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000333 if (prev == NULL) {
334 list_arg = cur;
335 } else {
336 prev->link = cur;
337 }
338 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000339 line_l += length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000340 if (line_l > mc) {
341 /* stop memory usage :-) */
342 break;
343 }
344 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000345 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000346 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000347 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000348}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000349#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000350
Denis Vlasenko6248a732006-09-29 08:20:30 +0000351/* Correct regardless of combination of CONFIG_xxx */
352enum {
353 OPTBIT_VERBOSE = 0,
354 OPTBIT_NO_EMPTY,
355 OPTBIT_UPTO_NUMBER,
356 OPTBIT_UPTO_SIZE,
357 OPTBIT_EOF_STRING,
358 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
359 USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
360 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000361
Denis Vlasenko6248a732006-09-29 08:20:30 +0000362 OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
363 OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
364 OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
365 OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
366 OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
367 OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
368 OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
369 OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
370};
371#define OPTION_STR "+trn:s:e::" \
372 USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
373 USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
374 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000375
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000376int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000377int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000378{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000379 char **args;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000380 int i, n;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000381 xlist_t *list = NULL;
382 xlist_t *cur;
383 int child_error = 0;
384 char *max_args, *max_chars;
385 int n_max_arg;
386 size_t n_chars = 0;
387 long orig_arg_max;
388 const char *eof_str = "_";
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000389 unsigned opt;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000390 size_t n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000391#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
392 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
393#else
394#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000395#endif
396
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000397 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000398
Denis Vlasenko6248a732006-09-29 08:20:30 +0000399 if (opt & OPT_ZEROTERM)
400 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000401
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000402 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000403 argc -= optind;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000404 if (!argc) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000405 /* default behavior is to echo all the filenames */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000406 *argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000407 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000408 }
409
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000410 orig_arg_max = ARG_MAX;
411 if (orig_arg_max == -1)
412 orig_arg_max = LONG_MAX;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000413 orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
414
415 if (opt & OPT_UPTO_SIZE) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000416 n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
Denis Vlasenko6248a732006-09-29 08:20:30 +0000417 for (i = 0; i < argc; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000418 n_chars += strlen(*argv) + 1;
Glenn L McGrath61796942003-10-10 12:10:18 +0000419 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000420 if (n_max_chars < n_chars) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000421 bb_error_msg_and_die("cannot fit single argument within argument list size limit");
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000422 }
423 n_max_chars -= n_chars;
424 } else {
425 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
426 have it at 1 meg). Things will work fine with a large ARG_MAX but it
427 will probably hurt the system more than it needs to; an array of this
428 size is allocated. */
429 if (orig_arg_max > 20 * 1024)
430 orig_arg_max = 20 * 1024;
431 n_max_chars = orig_arg_max;
Glenn L McGrath61796942003-10-10 12:10:18 +0000432 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000433 max_chars = xmalloc(n_max_chars);
434
Denis Vlasenko6248a732006-09-29 08:20:30 +0000435 if (opt & OPT_UPTO_NUMBER) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000436 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000437 } else {
438 n_max_arg = n_max_chars;
Glenn L McGrath61796942003-10-10 12:10:18 +0000439 }
440
Denis Vlasenko6248a732006-09-29 08:20:30 +0000441 while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
442 !(opt & OPT_NO_EMPTY))
Eric Andersen252183e2003-10-31 08:19:44 +0000443 {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000444 opt |= OPT_NO_EMPTY;
445 n = 0;
446 n_chars = 0;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000447#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000448 for (cur = list; cur;) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000449 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000450 n++;
451 cur = cur->link;
452 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
453 if (opt & OPT_TERMINATE)
454 bb_error_msg_and_die("argument list too long");
455 break;
456 }
457 }
458#else
459 for (cur = list; cur; cur = cur->link) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000460 n_chars += cur->length;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000461 n++;
462 if (n_chars > n_max_chars || n == n_max_arg) {
463 break;
464 }
465 }
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000466#endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000467
Denis Vlasenko6248a732006-09-29 08:20:30 +0000468 /* allocate pointers for execvp:
469 argc*arg, n*arg from stdin, NULL */
470 args = xzalloc((n + argc + 1) * sizeof(char *));
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000471
Denis Vlasenko6248a732006-09-29 08:20:30 +0000472 /* store the command to be executed
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000473 (taken from the command line) */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000474 for (i = 0; i < argc; i++)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000475 args[i] = argv[i];
476 /* (taken from stdin) */
477 for (cur = list; n; cur = cur->link) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000478 args[i++] = cur->xstr;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000479 n--;
480 }
481
Denis Vlasenko6248a732006-09-29 08:20:30 +0000482 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000483 for (i = 0; args[i]; i++) {
484 if (i)
485 fputc(' ', stderr);
486 fputs(args[i], stderr);
487 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000488 if (!(opt & OPT_INTERACTIVE))
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000489 fputc('\n', stderr);
490 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000491 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000492 child_error = xargs_exec(args);
493 }
494
495 /* clean up */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000496 for (i = argc; args[i]; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000497 cur = list;
498 list = list->link;
499 free(cur);
500 }
501 free(args);
502 if (child_error > 0 && child_error != 123) {
503 break;
504 }
505 }
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000506 if (ENABLE_FEATURE_CLEAN_UP)
507 free(max_chars);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000508 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000509}
510
511
512#ifdef TEST
513
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000514const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000515
516void bb_show_usage(void)
517{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000518 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000519 applet_name);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000520 exit(1);
Glenn L McGrath61796942003-10-10 12:10:18 +0000521}
522
523int main(int argc, char **argv)
524{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000525 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000526}
Eric Andersen252183e2003-10-31 08:19:44 +0000527#endif /* TEST */