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 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 4 | * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 5 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 6 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 7 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 8 | * Special thanks |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 9 | * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 10 | * - Mike Rendell <michael@cs.mun.ca> |
| 11 | * and David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 12 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 13 | * 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] | 14 | * |
Glenn L McGrath | 40c9489 | 2003-10-30 22:51:33 +0000 | [diff] [blame] | 15 | * xargs is described in the Single Unix Specification v3 at |
| 16 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html |
| 17 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 18 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 354 | /* Correct regardless of combination of CONFIG_xxx */ |
| 355 | enum { |
| 356 | OPTBIT_VERBOSE = 0, |
| 357 | OPTBIT_NO_EMPTY, |
| 358 | OPTBIT_UPTO_NUMBER, |
| 359 | OPTBIT_UPTO_SIZE, |
| 360 | OPTBIT_EOF_STRING, |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 361 | OPTBIT_EOF_STRING1, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 362 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) |
| 363 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) |
| 364 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 365 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 366 | OPT_VERBOSE = 1 << OPTBIT_VERBOSE , |
| 367 | OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY , |
| 368 | OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER, |
| 369 | OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE , |
| 370 | OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */ |
| 371 | OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 372 | OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0, |
| 373 | OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0, |
| 374 | OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0, |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 375 | }; |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 376 | #define OPTION_STR "+trn:s:e::E:" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 377 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ |
| 378 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ |
| 379 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 381 | int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 382 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 383 | { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 384 | int i; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 385 | int child_error = 0; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 386 | char *max_args; |
| 387 | char *max_chars; |
| 388 | char *buf; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 389 | unsigned opt; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 390 | int n_max_chars; |
| 391 | int n_max_arg; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 392 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 393 | char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 394 | #else |
| 395 | #define read_args process_stdin |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 396 | #endif |
| 397 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 398 | INIT_G(); |
| 399 | |
| 400 | G.eof_str = NULL; |
| 401 | 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] | 402 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 403 | /* -E ""? You may wonder why not just omit -E? |
| 404 | * This is used for portability: |
| 405 | * old xargs was using "_" as default for -E / -e */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 406 | if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0') |
| 407 | G.eof_str = NULL; |
Denis Vlasenko | cc08ad2 | 2008-08-03 19:12:25 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 409 | if (opt & OPT_ZEROTERM) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 410 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 411 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 412 | argv += optind; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 413 | argc -= optind; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 414 | if (!argv[0]) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 415 | /* default behavior is to echo all the filenames */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 416 | *--argv = (char*)"echo"; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 417 | argc++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 420 | /* The Open Group Base Specifications Issue 6: |
| 421 | * "The xargs utility shall limit the command line length such that |
| 422 | * when the command line is invoked, the combined argument |
| 423 | * and environment lists (see the exec family of functions |
| 424 | * in the System Interfaces volume of IEEE Std 1003.1-2001) |
| 425 | * shall not exceed {ARG_MAX}-2048 bytes". |
| 426 | */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 427 | n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */ |
| 428 | if (n_max_chars < 4*1024); /* paranoia */ |
| 429 | n_max_chars = 4*1024; |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 430 | n_max_chars -= 2048; |
| 431 | /* 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] | 432 | * have it at 1 meg). Things will work fine with a large ARG_MAX |
| 433 | * but it will probably hurt the system more than it needs to; |
| 434 | * an array of this size is allocated. |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 435 | */ |
| 436 | if (n_max_chars > 20 * 1024) |
| 437 | n_max_chars = 20 * 1024; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 438 | |
| 439 | if (opt & OPT_UPTO_SIZE) { |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 440 | size_t n_chars = 0; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 441 | n_max_chars = xatou_range(max_chars, 1, INT_MAX); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 442 | for (i = 0; argv[i]; i++) { |
| 443 | n_chars += strlen(argv[i]) + 1; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 444 | } |
| 445 | n_max_chars -= n_chars; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 446 | if (n_max_chars <= 0) { |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 447 | bb_error_msg_and_die("can't fit single argument within argument list size limit"); |
| 448 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 449 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 450 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 451 | buf = xzalloc(n_max_chars + 1); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 452 | |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 453 | n_max_arg = n_max_chars; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 454 | if (opt & OPT_UPTO_NUMBER) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 455 | n_max_arg = xatou_range(max_args, 1, INT_MAX); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 458 | /* Allocate pointers for execvp */ |
| 459 | /* We can statically allocate (argc + n_max_arg + 1) elements |
| 460 | * and do not bother with resizing args[], but on 64-bit machines |
| 461 | * this results in args[] vector which is ~8 times bigger |
| 462 | * than n_max_chars! That is, with n_max_chars == 20k, |
| 463 | * args[] will take 160k (!), which will most likely be |
| 464 | * almost entirely unused. |
| 465 | */ |
| 466 | /* See store_param() for matching 256-step growth logic */ |
| 467 | G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff)); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 468 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 469 | /* Store the command to be executed, part 1 */ |
| 470 | for (i = 0; argv[i]; i++) |
| 471 | G.args[i] = argv[i]; |
| 472 | |
| 473 | while (1) { |
| 474 | char *rem; |
| 475 | |
| 476 | G.idx = argc; |
| 477 | rem = read_args(n_max_chars, n_max_arg, buf); |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 478 | store_param(NULL); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 479 | |
| 480 | if (!G.args[argc]) { |
| 481 | if (*rem != '\0') |
| 482 | bb_error_msg_and_die("argument line too long"); |
| 483 | if (opt & OPT_NO_EMPTY) |
| 484 | break; |
| 485 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 486 | opt |= OPT_NO_EMPTY; |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 487 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 488 | if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 489 | const char *fmt = " %s" + 1; |
| 490 | char **args = G.args; |
| 491 | for (i = 0; args[i]; i++) { |
| 492 | fprintf(stderr, fmt, args[i]); |
| 493 | fmt = " %s"; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 494 | } |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 495 | if (!(opt & OPT_INTERACTIVE)) |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 496 | bb_putchar_stderr('\n'); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 497 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 498 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 499 | if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 500 | child_error = xargs_exec(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 503 | if (child_error > 0 && child_error != 123) { |
| 504 | break; |
| 505 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 506 | |
| 507 | overlapping_strcpy(buf, rem); |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 508 | } /* while */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 509 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 510 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 511 | free(G.args); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 512 | free(buf); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 513 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 514 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 515 | return child_error; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | |
| 519 | #ifdef TEST |
| 520 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 521 | const char *applet_name = "debug stuff usage"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 522 | |
| 523 | void bb_show_usage(void) |
| 524 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 525 | 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] | 526 | applet_name); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 527 | exit(EXIT_FAILURE); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | int main(int argc, char **argv) |
| 531 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 532 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 533 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 534 | #endif /* TEST */ |