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