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