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