blob: 9988e3dd50d751ff911a82061ea4129c564f396c [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"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020024//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020025//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"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020031//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020032//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"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020039//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020040//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"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020046//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020047//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"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020054//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020055//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/*
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +020092 * This function has special algorithm.
93 * Don't use fork and include to main!
94 */
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;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200121 size_t length; /* length of xstr[] including NUL */
Denis Vlasenko58394b12007-04-15 08:38:50 +0000122 char xstr[1];
Glenn L McGrath61796942003-10-10 12:10:18 +0000123} xlist_t;
124
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200125/* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
126 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
127 */
128#define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
Glenn L McGrath61796942003-10-10 12:10:18 +0000129
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000130#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200131static xlist_t* process_stdin(xlist_t *list_arg,
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200132 const char *eof_str, size_t n_max_chars, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000133{
134#define NORM 0
135#define QUOTE 1
136#define BACKSLASH 2
137#define SPACE 4
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200138 char *s = NULL; /* start of the word */
139 char *p = NULL; /* pointer to end of the word */
Denis Vlasenko58394b12007-04-15 08:38:50 +0000140 char q = '\0'; /* quote char */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000141 char state = NORM;
142 char eof_str_detected = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200143 size_t line_l = 0; /* size of loaded args */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000144 xlist_t *cur;
145 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000146
Denis Vlasenko89054962007-04-10 21:41:16 +0000147 prev = cur = list_arg;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200148 while (cur) {
Denis Vlasenko89054962007-04-10 21:41:16 +0000149 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000150 line_l += cur->length;
151 cur = cur->link;
Glenn L McGrath61796942003-10-10 12:10:18 +0000152 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000153
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200154 while (1) {
155 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000156 if (c == EOF) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000157 if (s)
158 goto unexpected_eof;
159 break;
Glenn L McGrath61796942003-10-10 12:10:18 +0000160 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200161 if (eof_str_detected) /* skip till EOF */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000162 continue;
163 if (state == BACKSLASH) {
164 state = NORM;
165 goto set;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200166 }
167 if (state == QUOTE) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000168 if (c != q)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000169 goto set;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000170 q = '\0';
171 state = NORM;
Denis Vlasenko51742f42007-04-12 00:32:05 +0000172 } else { /* if (state == NORM) */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000173 if (ISSPACE(c)) {
174 if (s) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000175 unexpected_eof:
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000176 state = SPACE;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000177 c = '\0';
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000178 goto set;
179 }
180 } else {
181 if (s == NULL)
182 s = p = buf;
183 if (c == '\\') {
184 state = BACKSLASH;
185 } else if (c == '\'' || c == '"') {
186 q = c;
187 state = QUOTE;
188 } else {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000189 set:
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200190 if ((size_t)(p - buf) >= n_max_chars)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000191 bb_error_msg_and_die("argument line too long");
192 *p++ = c;
193 }
194 }
195 }
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000196 if (state == SPACE) { /* word's delimiter or EOF detected */
Eric Andersen252183e2003-10-31 08:19:44 +0000197 if (q) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000198 bb_error_msg_and_die("unmatched %s quote",
Eric Andersen252183e2003-10-31 08:19:44 +0000199 q == '\'' ? "single" : "double");
200 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200201 /* A full word is loaded */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000202 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000203 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000204 }
205 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000206 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000207 /* Dont xzalloc - it can be quite big */
208 cur = xmalloc(offsetof(xlist_t, xstr) + length);
209 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000210 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000211 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000212 if (prev == NULL) {
213 list_arg = cur;
214 } else {
215 prev->link = cur;
216 }
217 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000218 line_l += length;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200219 if (line_l >= n_max_chars) /* limit memory usage */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000220 break;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000221 }
222 s = NULL;
223 state = NORM;
224 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000225 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000226 return list_arg;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000227}
Glenn L McGrath61796942003-10-10 12:10:18 +0000228#else
Eric Andersen252183e2003-10-31 08:19:44 +0000229/* The variant does not support single quotes, double quotes or backslash */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200230static xlist_t* process_stdin(xlist_t *list_arg,
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200231 const char *eof_str, size_t n_max_chars, char *buf)
Eric Andersen92a61c12000-09-22 20:01:23 +0000232{
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000233 char eof_str_detected = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200234 char *s = NULL; /* start of the word */
235 char *p = NULL; /* pointer to end of the word */
236 size_t line_l = 0; /* size of loaded args */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000237 xlist_t *cur;
238 xlist_t *prev;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000239
Denis Vlasenko89054962007-04-10 21:41:16 +0000240 prev = cur = list_arg;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200241 while (cur) {
Denis Vlasenko89054962007-04-10 21:41:16 +0000242 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000243 line_l += cur->length;
244 cur = cur->link;
Glenn L McGrath99825962003-10-09 11:06:45 +0000245 }
Eric Andersen92a61c12000-09-22 20:01:23 +0000246
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200247 while (1) {
248 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000249 if (c == EOF) {
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200250 if (s == NULL)
251 break;
Mark Whitleye2e2c292000-11-14 22:43:21 +0000252 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200253 if (eof_str_detected) { /* skip till EOF */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000254 continue;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200255 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000256 if (c == EOF || ISSPACE(c)) {
257 if (s == NULL)
258 continue;
259 c = EOF;
Glenn L McGrath61796942003-10-10 12:10:18 +0000260 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000261 if (s == NULL)
262 s = p = buf;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200263 if ((size_t)(p - buf) >= n_max_chars)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000264 bb_error_msg_and_die("argument line too long");
Denis Vlasenko58394b12007-04-15 08:38:50 +0000265 *p++ = (c == EOF ? '\0' : c);
"Vladimir N. Oleynik"59c4e5c2006-01-30 13:51:50 +0000266 if (c == EOF) { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200267 /* A full word is loaded */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000268 if (eof_str) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000269 eof_str_detected = (strcmp(s, eof_str) == 0);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000270 }
271 if (!eof_str_detected) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000272 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000273 /* Dont xzalloc - it can be quite big */
274 cur = xmalloc(offsetof(xlist_t, xstr) + length);
275 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000276 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000277 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000278 if (prev == NULL) {
279 list_arg = cur;
280 } else {
281 prev->link = cur;
282 }
283 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000284 line_l += length;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200285 if (line_l >= n_max_chars) /* limit memory usage */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000286 break;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000287 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200288 s = NULL;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000289 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000290 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000291 return list_arg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000292}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000293#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
Glenn L McGrath61796942003-10-10 12:10:18 +0000294
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000295#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200296static xlist_t* process0_stdin(xlist_t *list_arg,
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200297 const char *eof_str UNUSED_PARAM, size_t n_max_chars, char *buf)
Glenn L McGrath61796942003-10-10 12:10:18 +0000298{
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200299 char *s = NULL; /* start of the word */
300 char *p = NULL; /* pointer to end of the word */
301 size_t line_l = 0; /* size of loaded args */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000302 xlist_t *cur;
303 xlist_t *prev;
Glenn L McGrath61796942003-10-10 12:10:18 +0000304
Denis Vlasenko89054962007-04-10 21:41:16 +0000305 prev = cur = list_arg;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200306 while (cur) {
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
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200312 while (1) {
313 int c = getchar();
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000314 if (c == EOF) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000315 if (s == NULL)
316 break;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000317 c = '\0';
Glenn L McGrath61796942003-10-10 12:10:18 +0000318 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000319 if (s == NULL)
320 s = p = buf;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200321 if ((size_t)(p - buf) >= n_max_chars)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000322 bb_error_msg_and_die("argument line too long");
323 *p++ = c;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000324 if (c == '\0') { /* word's delimiter or EOF detected */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200325 /* A full word is loaded */
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000326 size_t length = (p - buf);
Denis Vlasenko58394b12007-04-15 08:38:50 +0000327 /* Dont xzalloc - it can be quite big */
328 cur = xmalloc(offsetof(xlist_t, xstr) + length);
329 cur->link = NULL;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000330 cur->length = length;
Denis Vlasenko58394b12007-04-15 08:38:50 +0000331 memcpy(cur->xstr, s, length);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000332 if (prev == NULL) {
333 list_arg = cur;
334 } else {
335 prev->link = cur;
336 }
337 prev = cur;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000338 line_l += length;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200339 if (line_l >= n_max_chars) /* limit memory usage */
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000340 break;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000341 s = NULL;
Glenn L McGrath61796942003-10-10 12:10:18 +0000342 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000343 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000344 return list_arg;
Glenn L McGrath61796942003-10-10 12:10:18 +0000345}
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000346#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
Glenn L McGrath61796942003-10-10 12:10:18 +0000347
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200348#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
349/* Prompt the user for a response, and
350 if the user responds affirmatively, return true;
351 otherwise, return false. Uses "/dev/tty", not stdin. */
352static int xargs_ask_confirmation(void)
353{
354 FILE *tty_stream;
355 int c, savec;
356
357 tty_stream = xfopen_for_read(CURRENT_TTY);
358 fputs(" ?...", stderr);
359 fflush_all();
360 c = savec = getc(tty_stream);
361 while (c != EOF && c != '\n')
362 c = getc(tty_stream);
363 fclose(tty_stream);
364 return (savec == 'y' || savec == 'Y');
365}
366#else
367# define xargs_ask_confirmation() 1
368#endif
369
Denis Vlasenko6248a732006-09-29 08:20:30 +0000370/* Correct regardless of combination of CONFIG_xxx */
371enum {
372 OPTBIT_VERBOSE = 0,
373 OPTBIT_NO_EMPTY,
374 OPTBIT_UPTO_NUMBER,
375 OPTBIT_UPTO_SIZE,
376 OPTBIT_EOF_STRING,
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000377 OPTBIT_EOF_STRING1,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000378 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
379 IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
380 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
Glenn L McGrath61796942003-10-10 12:10:18 +0000381
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000382 OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
383 OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
384 OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
385 OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
386 OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
387 OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000388 OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
389 OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
390 OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
Denis Vlasenko6248a732006-09-29 08:20:30 +0000391};
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000392#define OPTION_STR "+trn:s:e::E:" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000393 IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
394 IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
395 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
Glenn L McGrath61796942003-10-10 12:10:18 +0000396
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000397int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000398int xargs_main(int argc, char **argv)
Glenn L McGrath61796942003-10-10 12:10:18 +0000399{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000400 xlist_t *list = NULL;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000401 int child_error = 0;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200402 char *max_args;
403 char *max_chars;
404 char *buf;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000405 int n_max_arg;
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000406 const char *eof_str = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000407 unsigned opt;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000408 size_t n_max_chars;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000409#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
410 xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
411#else
412#define read_args process_stdin
Glenn L McGrath61796942003-10-10 12:10:18 +0000413#endif
414
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000415 opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
Glenn L McGrath61796942003-10-10 12:10:18 +0000416
Denis Vlasenko82ad0322008-08-04 21:30:55 +0000417 /* -E ""? You may wonder why not just omit -E?
418 * This is used for portability:
419 * old xargs was using "_" as default for -E / -e */
420 if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
Denis Vlasenkocc08ad22008-08-03 19:12:25 +0000421 eof_str = NULL;
422
Denis Vlasenko6248a732006-09-29 08:20:30 +0000423 if (opt & OPT_ZEROTERM)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000424 IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
Glenn L McGrath61796942003-10-10 12:10:18 +0000425
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000426 argv += optind;
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000427 argc -= optind;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200428 if (!argv[0]) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000429 /* default behavior is to echo all the filenames */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200430 *--argv = (char*)"echo";
Denis Vlasenko6248a732006-09-29 08:20:30 +0000431 argc++;
Glenn L McGrath61796942003-10-10 12:10:18 +0000432 }
433
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200434 /* The Open Group Base Specifications Issue 6:
435 * "The xargs utility shall limit the command line length such that
436 * when the command line is invoked, the combined argument
437 * and environment lists (see the exec family of functions
438 * in the System Interfaces volume of IEEE Std 1003.1-2001)
439 * shall not exceed {ARG_MAX}-2048 bytes".
440 */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200441 n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */
442 if (n_max_chars < 4*1024); /* paranoia */
443 n_max_chars = 4*1024;
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200444 n_max_chars -= 2048;
445 /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200446 * have it at 1 meg). Things will work fine with a large ARG_MAX
447 * but it will probably hurt the system more than it needs to;
448 * an array of this size is allocated.
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200449 */
450 if (n_max_chars > 20 * 1024)
451 n_max_chars = 20 * 1024;
Denis Vlasenko6248a732006-09-29 08:20:30 +0000452
453 if (opt & OPT_UPTO_SIZE) {
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200454 int i;
Denys Vlasenkod7b52892010-04-09 14:58:40 +0200455 size_t n_chars = 0;
Denys Vlasenko1613de82010-06-13 03:44:27 +0200456 n_max_chars = xatoul_range(max_chars, 1, INT_MAX);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200457 for (i = 0; argv[i]; i++) {
458 n_chars += strlen(argv[i]) + 1;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000459 }
460 n_max_chars -= n_chars;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200461 if ((ssize_t)n_max_chars <= 0) {
462 bb_error_msg_and_die("can't fit single argument within argument list size limit");
463 }
Glenn L McGrath61796942003-10-10 12:10:18 +0000464 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200465
466 buf = xmalloc(n_max_chars);
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000467
Denis Vlasenko6248a732006-09-29 08:20:30 +0000468 if (opt & OPT_UPTO_NUMBER) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000469 n_max_arg = xatoul_range(max_args, 1, INT_MAX);
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200470 if (n_max_arg < n_max_chars)
471 goto skip;
Glenn L McGrath61796942003-10-10 12:10:18 +0000472 }
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200473 n_max_arg = n_max_chars;
474 skip:
Glenn L McGrath61796942003-10-10 12:10:18 +0000475
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200476 while ((list = read_args(list, eof_str, n_max_chars, buf)) != NULL
477 || !(opt & OPT_NO_EMPTY)
478 ) {
479 char **args;
480 xlist_t *cur;
481 int i, n;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200482 size_t n_chars;
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200483
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000484 opt |= OPT_NO_EMPTY;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200485
486 /* take args from list, not exceeding arg and char limits */
487 n_chars = 0;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000488 n = 0;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200489 for (cur = list; cur; cur = cur->link) {
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000490 n_chars += cur->length;
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200491 if (n_chars > n_max_chars || n >= n_max_arg) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000492 if (opt & OPT_TERMINATE)
493 bb_error_msg_and_die("argument list too long");
494 break;
495 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000496 n++;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000497 }
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000498
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200499 /* allocate pointers for execvp */
500 args = xzalloc(sizeof(args[0]) * (argc + n + 1));
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000501
Denis Vlasenko6248a732006-09-29 08:20:30 +0000502 /* store the command to be executed
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200503 * (taken from the command line) */
504 for (i = 0; argv[i]; i++)
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000505 args[i] = argv[i];
506 /* (taken from stdin) */
507 for (cur = list; n; cur = cur->link) {
Denis Vlasenko58394b12007-04-15 08:38:50 +0000508 args[i++] = cur->xstr;
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000509 n--;
510 }
511
Denis Vlasenko6248a732006-09-29 08:20:30 +0000512 if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000513 for (i = 0; args[i]; i++) {
514 if (i)
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200515 bb_putchar_stderr(' ');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000516 fputs(args[i], stderr);
517 }
Denis Vlasenko6248a732006-09-29 08:20:30 +0000518 if (!(opt & OPT_INTERACTIVE))
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200519 bb_putchar_stderr('\n');
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000520 }
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200521
Denis Vlasenko6248a732006-09-29 08:20:30 +0000522 if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000523 child_error = xargs_exec(args);
524 }
525
Denys Vlasenkoaaa24e02010-06-13 12:43:54 +0200526 /* remove list elements which we consumed */
Denis Vlasenko6248a732006-09-29 08:20:30 +0000527 for (i = argc; args[i]; i++) {
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000528 cur = list;
529 list = list->link;
530 free(cur);
531 }
532 free(args);
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200533
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000534 if (child_error > 0 && child_error != 123) {
535 break;
536 }
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000537 } /* while */
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200538
Denis Vlasenko1b4b2cb2007-04-09 21:30:53 +0000539 if (ENABLE_FEATURE_CLEAN_UP)
Denys Vlasenkod5fa1a02010-06-13 03:43:43 +0200540 free(buf);
541
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000542 return child_error;
Glenn L McGrath61796942003-10-10 12:10:18 +0000543}
544
545
546#ifdef TEST
547
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000548const char *applet_name = "debug stuff usage";
Glenn L McGrath61796942003-10-10 12:10:18 +0000549
550void bb_show_usage(void)
551{
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000552 fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000553 applet_name);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000554 exit(EXIT_FAILURE);
Glenn L McGrath61796942003-10-10 12:10:18 +0000555}
556
557int main(int argc, char **argv)
558{
Glenn L McGrath09c295a2003-10-30 22:47:16 +0000559 return xargs_main(argc, argv);
Glenn L McGrath61796942003-10-10 12:10:18 +0000560}
Eric Andersen252183e2003-10-31 08:19:44 +0000561#endif /* TEST */