Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 3 | * Mini xargs implementation for busybox |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 5 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 7 | * Special thanks |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 8 | * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 9 | * - Mike Rendell <michael@cs.mun.ca> |
| 10 | * and David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 11 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 12 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 13 | * |
Glenn L McGrath | 40c9489 | 2003-10-30 22:51:33 +0000 | [diff] [blame] | 14 | * xargs is described in the Single Unix Specification v3 at |
| 15 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 16 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 17 | |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 18 | //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, _BB_DIR_USR_BIN, _BB_SUID_DROP, xargs)) |
| 19 | |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 20 | //kbuild:lib-$(CONFIG_XARGS) += xargs.o |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 21 | |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 22 | //config:config XARGS |
| 23 | //config: bool "xargs" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 24 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 31 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 39 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 46 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 54 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 61 | #include "libbb.h" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 62 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. |
| 63 | We try to make it as large as possible. */ |
| 64 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 65 | # define ARG_MAX sysconf(_SC_ARG_MAX) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 66 | #endif |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 67 | #if !defined(ARG_MAX) |
| 68 | # define ARG_MAX 470 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 71 | /* This is a NOEXEC applet. Be very careful! */ |
| 72 | |
| 73 | |
| 74 | //#define dbg_msg(...) bb_error_msg(__VA_ARGS__) |
| 75 | #define dbg_msg(...) ((void)0) |
| 76 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 77 | |
| 78 | #ifdef TEST |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 79 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 80 | # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 81 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 82 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
| 83 | # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 84 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 85 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT |
| 86 | # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 87 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 88 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 89 | # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 90 | # endif |
| 91 | #endif |
| 92 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 93 | |
| 94 | struct globals { |
| 95 | char **args; |
| 96 | const char *eof_str; |
| 97 | int idx; |
| 98 | } FIX_ALIASING; |
| 99 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 100 | #define INIT_G() do { } while (0) |
| 101 | |
| 102 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 103 | /* |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 104 | * This function has special algorithm. |
| 105 | * Don't use fork and include to main! |
| 106 | */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 107 | static int xargs_exec(void) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 109 | int status; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 111 | status = spawn_and_wait(G.args); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 112 | if (status < 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 113 | bb_simple_perror_msg(G.args[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 114 | return errno == ENOENT ? 127 : 126; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 115 | } |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 116 | if (status == 255) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 117 | bb_error_msg("%s: exited with status 255; aborting", G.args[0]); |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 118 | return 124; |
| 119 | } |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 120 | if (status >= 0x180) { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 121 | bb_error_msg("%s: terminated by signal %d", |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 122 | G.args[0], status - 0x180); |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 123 | return 125; |
| 124 | } |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 125 | if (status) |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 126 | return 123; |
| 127 | return 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 128 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 129 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 130 | /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space. |
| 131 | * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13. |
| 132 | */ |
| 133 | #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); }) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 134 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 135 | static void store_param(char *s) |
| 136 | { |
| 137 | /* Grow by 256 elements at once */ |
| 138 | if (!(G.idx & 0xff)) { /* G.idx == N*256 */ |
| 139 | /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */ |
| 140 | G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100)); |
| 141 | } |
| 142 | G.args[G.idx++] = s; |
| 143 | } |
| 144 | |
| 145 | /* process[0]_stdin: |
| 146 | * Read characters into buf[n_max_chars+1], and when parameter delimiter |
| 147 | * is seen, store the address of a new parameter to args[]. |
| 148 | * If reading discovers that last chars do not form the complete |
| 149 | * parameter, the pointer to the first such "tail character" is returned. |
| 150 | * (buf has extra byte at the end to accomodate terminating NUL |
| 151 | * of "tail characters" string). |
| 152 | * Otherwise, the returned pointer points to NUL byte. |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 153 | * On entry, buf[] may contain some "seed chars" which are to become |
| 154 | * the beginning of the first parameter. |
| 155 | */ |
| 156 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 157 | #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 158 | static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 159 | { |
| 160 | #define NORM 0 |
| 161 | #define QUOTE 1 |
| 162 | #define BACKSLASH 2 |
| 163 | #define SPACE 4 |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 164 | char q = '\0'; /* quote char */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 165 | char state = NORM; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 166 | char *s = buf; /* start of the word */ |
| 167 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 168 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 169 | buf += n_max_chars; /* past buffer's end */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 170 | |
| 171 | /* "goto ret" is used instead of "break" to make control flow |
| 172 | * more obvious: */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 173 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 174 | while (1) { |
| 175 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 176 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 177 | if (p != s) |
| 178 | goto close_word; |
| 179 | goto ret; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 180 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 181 | if (state == BACKSLASH) { |
| 182 | state = NORM; |
| 183 | goto set; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 184 | } |
| 185 | if (state == QUOTE) { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 186 | if (c != q) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 187 | goto set; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 188 | q = '\0'; |
| 189 | state = NORM; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 190 | } else { /* if (state == NORM) */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 191 | if (ISSPACE(c)) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 192 | if (p != s) { |
| 193 | close_word: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 194 | state = SPACE; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 195 | c = '\0'; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 196 | goto set; |
| 197 | } |
| 198 | } else { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 199 | if (c == '\\') { |
| 200 | state = BACKSLASH; |
| 201 | } else if (c == '\'' || c == '"') { |
| 202 | q = c; |
| 203 | state = QUOTE; |
| 204 | } else { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 205 | set: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 206 | *p++ = c; |
| 207 | } |
| 208 | } |
| 209 | } |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 210 | if (state == SPACE) { /* word's delimiter or EOF detected */ |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 211 | if (q) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 212 | bb_error_msg_and_die("unmatched %s quote", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 213 | q == '\'' ? "single" : "double"); |
| 214 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 215 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 216 | if (G.eof_str) { |
| 217 | if (strcmp(s, G.eof_str) == 0) { |
| 218 | while (getchar() != EOF) |
| 219 | continue; |
| 220 | p = s; |
| 221 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 222 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 223 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 224 | store_param(s); |
| 225 | dbg_msg("args[]:'%s'", s); |
| 226 | s = p; |
| 227 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 228 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 229 | goto ret; |
| 230 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 231 | state = NORM; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 232 | } |
| 233 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 234 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 235 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 236 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 237 | ret: |
| 238 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 239 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 240 | dbg_msg("return:'%s'", s); |
| 241 | return s; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 242 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 243 | #else |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 244 | /* The variant does not support single quotes, double quotes or backslash */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 245 | static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 246 | { |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 247 | char *s = buf; /* start of the word */ |
| 248 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 249 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 250 | buf += n_max_chars; /* past buffer's end */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 251 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 252 | while (1) { |
| 253 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 254 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 255 | if (p == s) |
| 256 | goto ret; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 257 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 258 | if (c == EOF || ISSPACE(c)) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 259 | if (p == s) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 260 | continue; |
| 261 | c = EOF; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 262 | } |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 263 | *p++ = (c == EOF ? '\0' : c); |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 264 | if (c == EOF) { /* word's delimiter or EOF detected */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 265 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 266 | if (G.eof_str) { |
| 267 | if (strcmp(s, G.eof_str) == 0) { |
| 268 | while (getchar() != EOF) |
| 269 | continue; |
| 270 | p = s; |
| 271 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 272 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 273 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 274 | store_param(s); |
| 275 | dbg_msg("args[]:'%s'", s); |
| 276 | s = p; |
| 277 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 278 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 279 | goto ret; |
| 280 | } |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 281 | } |
| 282 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 283 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 284 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 285 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 286 | ret: |
| 287 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 288 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 289 | dbg_msg("return:'%s'", s); |
| 290 | return s; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 291 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 292 | #endif /* FEATURE_XARGS_SUPPORT_QUOTES */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 293 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 294 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 295 | static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 296 | { |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 297 | char *s = buf; /* start of the word */ |
| 298 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 299 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 300 | buf += n_max_chars; /* past buffer's end */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 301 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 302 | while (1) { |
| 303 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 304 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 305 | if (p == s) |
| 306 | goto ret; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 307 | c = '\0'; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 308 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 309 | *p++ = c; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 310 | if (c == '\0') { /* word's delimiter or EOF detected */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 311 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 312 | store_param(s); |
| 313 | dbg_msg("args[]:'%s'", s); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 314 | s = p; |
Denys Vlasenko | f7e929e | 2010-06-15 10:02:04 +0200 | [diff] [blame] | 315 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 316 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 317 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 318 | } |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 319 | } |
| 320 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 321 | goto ret; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 322 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 323 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 324 | ret: |
| 325 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 326 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 327 | dbg_msg("return:'%s'", s); |
| 328 | return s; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 329 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 330 | #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 331 | |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 332 | #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 333 | /* Prompt the user for a response, and |
| 334 | if the user responds affirmatively, return true; |
| 335 | otherwise, return false. Uses "/dev/tty", not stdin. */ |
| 336 | static int xargs_ask_confirmation(void) |
| 337 | { |
| 338 | FILE *tty_stream; |
| 339 | int c, savec; |
| 340 | |
| 341 | tty_stream = xfopen_for_read(CURRENT_TTY); |
| 342 | fputs(" ?...", stderr); |
| 343 | fflush_all(); |
| 344 | c = savec = getc(tty_stream); |
| 345 | while (c != EOF && c != '\n') |
| 346 | c = getc(tty_stream); |
| 347 | fclose(tty_stream); |
| 348 | return (savec == 'y' || savec == 'Y'); |
| 349 | } |
| 350 | #else |
| 351 | # define xargs_ask_confirmation() 1 |
| 352 | #endif |
| 353 | |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 354 | //usage:#define xargs_trivial_usage |
| 355 | //usage: "[OPTIONS] [PROG ARGS]" |
| 356 | //usage:#define xargs_full_usage "\n\n" |
| 357 | //usage: "Run PROG on every item given by stdin\n" |
| 358 | //usage: "\nOptions:" |
| 359 | //usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION( |
| 360 | //usage: "\n -p Ask user whether to run each command" |
| 361 | //usage: ) |
| 362 | //usage: "\n -r Don't run command if input is empty" |
| 363 | //usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( |
| 364 | //usage: "\n -0 Input is separated by NUL characters" |
| 365 | //usage: ) |
| 366 | //usage: "\n -t Print the command on stderr before execution" |
| 367 | //usage: "\n -e[STR] STR stops input processing" |
| 368 | //usage: "\n -n N Pass no more than N args to PROG" |
| 369 | //usage: "\n -s N Pass command line of no more than N bytes" |
| 370 | //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT( |
| 371 | //usage: "\n -x Exit if size is exceeded" |
| 372 | //usage: ) |
| 373 | //usage:#define xargs_example_usage |
| 374 | //usage: "$ ls | xargs gzip\n" |
| 375 | //usage: "$ find . -name '*.c' -print | xargs rm\n" |
| 376 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 377 | /* Correct regardless of combination of CONFIG_xxx */ |
| 378 | enum { |
| 379 | OPTBIT_VERBOSE = 0, |
| 380 | OPTBIT_NO_EMPTY, |
| 381 | OPTBIT_UPTO_NUMBER, |
| 382 | OPTBIT_UPTO_SIZE, |
| 383 | OPTBIT_EOF_STRING, |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 384 | OPTBIT_EOF_STRING1, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 385 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) |
| 386 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) |
| 387 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 388 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 389 | OPT_VERBOSE = 1 << OPTBIT_VERBOSE , |
| 390 | OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY , |
| 391 | OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER, |
| 392 | OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE , |
| 393 | OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */ |
| 394 | OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 395 | OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0, |
| 396 | OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0, |
| 397 | OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0, |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 398 | }; |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 399 | #define OPTION_STR "+trn:s:e::E:" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 400 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ |
| 401 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ |
| 402 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 403 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 404 | int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 405 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 406 | { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 407 | int i; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 408 | int child_error = 0; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 409 | char *max_args; |
| 410 | char *max_chars; |
| 411 | char *buf; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 412 | unsigned opt; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 413 | int n_max_chars; |
| 414 | int n_max_arg; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 415 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 416 | char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 417 | #else |
| 418 | #define read_args process_stdin |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 419 | #endif |
| 420 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 421 | INIT_G(); |
| 422 | |
| 423 | G.eof_str = NULL; |
| 424 | opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &G.eof_str, &G.eof_str); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 425 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 426 | /* -E ""? You may wonder why not just omit -E? |
| 427 | * This is used for portability: |
| 428 | * old xargs was using "_" as default for -E / -e */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 429 | if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0') |
| 430 | G.eof_str = NULL; |
Denis Vlasenko | cc08ad2 | 2008-08-03 19:12:25 +0000 | [diff] [blame] | 431 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 432 | if (opt & OPT_ZEROTERM) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 433 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 434 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 435 | argv += optind; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 436 | argc -= optind; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 437 | if (!argv[0]) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 438 | /* default behavior is to echo all the filenames */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 439 | *--argv = (char*)"echo"; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 440 | argc++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 443 | /* The Open Group Base Specifications Issue 6: |
| 444 | * "The xargs utility shall limit the command line length such that |
| 445 | * when the command line is invoked, the combined argument |
| 446 | * and environment lists (see the exec family of functions |
| 447 | * in the System Interfaces volume of IEEE Std 1003.1-2001) |
| 448 | * shall not exceed {ARG_MAX}-2048 bytes". |
| 449 | */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 450 | n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */ |
| 451 | if (n_max_chars < 4*1024); /* paranoia */ |
| 452 | n_max_chars = 4*1024; |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 453 | n_max_chars -= 2048; |
| 454 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 455 | * have it at 1 meg). Things will work fine with a large ARG_MAX |
| 456 | * but it will probably hurt the system more than it needs to; |
| 457 | * an array of this size is allocated. |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 458 | */ |
| 459 | if (n_max_chars > 20 * 1024) |
| 460 | n_max_chars = 20 * 1024; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 461 | |
| 462 | if (opt & OPT_UPTO_SIZE) { |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 463 | size_t n_chars = 0; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 464 | n_max_chars = xatou_range(max_chars, 1, INT_MAX); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 465 | for (i = 0; argv[i]; i++) { |
| 466 | n_chars += strlen(argv[i]) + 1; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 467 | } |
| 468 | n_max_chars -= n_chars; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 469 | if (n_max_chars <= 0) { |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 470 | bb_error_msg_and_die("can't fit single argument within argument list size limit"); |
| 471 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 472 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 473 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 474 | buf = xzalloc(n_max_chars + 1); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 475 | |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 476 | n_max_arg = n_max_chars; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 477 | if (opt & OPT_UPTO_NUMBER) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 478 | n_max_arg = xatou_range(max_args, 1, INT_MAX); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 481 | /* Allocate pointers for execvp */ |
| 482 | /* We can statically allocate (argc + n_max_arg + 1) elements |
| 483 | * and do not bother with resizing args[], but on 64-bit machines |
| 484 | * this results in args[] vector which is ~8 times bigger |
| 485 | * than n_max_chars! That is, with n_max_chars == 20k, |
| 486 | * args[] will take 160k (!), which will most likely be |
| 487 | * almost entirely unused. |
| 488 | */ |
| 489 | /* See store_param() for matching 256-step growth logic */ |
| 490 | G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff)); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 491 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 492 | /* Store the command to be executed, part 1 */ |
| 493 | for (i = 0; argv[i]; i++) |
| 494 | G.args[i] = argv[i]; |
| 495 | |
| 496 | while (1) { |
| 497 | char *rem; |
| 498 | |
| 499 | G.idx = argc; |
| 500 | rem = read_args(n_max_chars, n_max_arg, buf); |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 501 | store_param(NULL); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 502 | |
| 503 | if (!G.args[argc]) { |
| 504 | if (*rem != '\0') |
| 505 | bb_error_msg_and_die("argument line too long"); |
| 506 | if (opt & OPT_NO_EMPTY) |
| 507 | break; |
| 508 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 509 | opt |= OPT_NO_EMPTY; |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 510 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 511 | if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 512 | const char *fmt = " %s" + 1; |
| 513 | char **args = G.args; |
| 514 | for (i = 0; args[i]; i++) { |
| 515 | fprintf(stderr, fmt, args[i]); |
| 516 | fmt = " %s"; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 517 | } |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 518 | if (!(opt & OPT_INTERACTIVE)) |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 519 | bb_putchar_stderr('\n'); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 520 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 521 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 522 | if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 523 | child_error = xargs_exec(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 526 | if (child_error > 0 && child_error != 123) { |
| 527 | break; |
| 528 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 529 | |
| 530 | overlapping_strcpy(buf, rem); |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 531 | } /* while */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 532 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 533 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 534 | free(G.args); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 535 | free(buf); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 536 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 537 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 538 | return child_error; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | |
| 542 | #ifdef TEST |
| 543 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 544 | const char *applet_name = "debug stuff usage"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 545 | |
| 546 | void bb_show_usage(void) |
| 547 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 548 | fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 549 | applet_name); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 550 | exit(EXIT_FAILURE); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | int main(int argc, char **argv) |
| 554 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 555 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 556 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 557 | #endif /* TEST */ |