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 | */ |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 17 | //config:config XARGS |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 18 | //config: bool "xargs (7.2 kb)" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 19 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 20 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 21 | //config: xargs is used to execute a specified command for |
| 22 | //config: every item from standard input. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 23 | //config: |
| 24 | //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 25 | //config: bool "Enable -p: prompt and confirmation" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 26 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 27 | //config: depends on XARGS |
| 28 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 29 | //config: Support -p: prompt the user whether to run each command |
| 30 | //config: line and read a line from the terminal. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 31 | //config: |
| 32 | //config:config FEATURE_XARGS_SUPPORT_QUOTES |
| 33 | //config: bool "Enable single and double quotes and backslash" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 34 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 35 | //config: depends on XARGS |
| 36 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 37 | //config: Support quoting in the input. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 38 | //config: |
| 39 | //config:config FEATURE_XARGS_SUPPORT_TERMOPT |
| 40 | //config: bool "Enable -x: exit if -s or -n is exceeded" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 41 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 42 | //config: depends on XARGS |
| 43 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 44 | //config: Support -x: exit if the command size (see the -s or -n option) |
| 45 | //config: is exceeded. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 46 | //config: |
| 47 | //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 48 | //config: bool "Enable -0: NUL-terminated input" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 49 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 50 | //config: depends on XARGS |
| 51 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 52 | //config: Support -0: input items are terminated by a NUL character |
| 53 | //config: instead of whitespace, and the quotes and backslash |
| 54 | //config: are not special. |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 55 | //config: |
| 56 | //config:config FEATURE_XARGS_SUPPORT_REPL_STR |
| 57 | //config: bool "Enable -I STR: string to replace" |
| 58 | //config: default y |
| 59 | //config: depends on XARGS |
| 60 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 61 | //config: Support -I STR and -i[STR] options. |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 62 | //config: |
| 63 | //config:config FEATURE_XARGS_SUPPORT_PARALLEL |
| 64 | //config: bool "Enable -P N: processes to run in parallel" |
| 65 | //config: default y |
| 66 | //config: depends on XARGS |
Johannes Schindelin | f8ee849 | 2017-08-25 22:42:05 +0200 | [diff] [blame] | 67 | //config: |
| 68 | //config:config FEATURE_XARGS_SUPPORT_ARGS_FILE |
| 69 | //config: bool "Enable -a FILE: use FILE instead of stdin" |
| 70 | //config: default y |
| 71 | //config: depends on XARGS |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 72 | |
Denys Vlasenko | 7cfe6ea | 2011-03-03 09:51:57 +0100 | [diff] [blame] | 73 | //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs)) |
| 74 | |
| 75 | //kbuild:lib-$(CONFIG_XARGS) += xargs.o |
| 76 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 77 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 78 | #include "common_bufsiz.h" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 79 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 80 | /* This is a NOEXEC applet. Be very careful! */ |
| 81 | |
| 82 | |
| 83 | //#define dbg_msg(...) bb_error_msg(__VA_ARGS__) |
| 84 | #define dbg_msg(...) ((void)0) |
| 85 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 86 | |
| 87 | #ifdef TEST |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 88 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 89 | # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 90 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 91 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
| 92 | # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 93 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 94 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT |
| 95 | # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 96 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 97 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 98 | # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 99 | # endif |
| 100 | #endif |
| 101 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 102 | |
| 103 | struct globals { |
| 104 | char **args; |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 105 | #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR |
| 106 | char **argv; |
| 107 | const char *repl_str; |
| 108 | char eol_ch; |
| 109 | #endif |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 110 | const char *eof_str; |
| 111 | int idx; |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 112 | #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL |
| 113 | int running_procs; |
| 114 | int max_procs; |
| 115 | #endif |
| 116 | smalluint xargs_exitcode; |
Ron Yorston | 1ff7002 | 2020-01-24 13:16:45 +0000 | [diff] [blame] | 117 | #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
| 118 | #define NORM 0 |
| 119 | #define QUOTE 1 |
| 120 | #define BACKSLASH 2 |
| 121 | #define SPACE 4 |
| 122 | smalluint process_stdin__state; |
| 123 | char process_stdin__q; |
| 124 | #endif |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 125 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 126 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 7cfe6ea | 2011-03-03 09:51:57 +0100 | [diff] [blame] | 127 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 128 | setup_common_bufsiz(); \ |
Ron Yorston | 1ff7002 | 2020-01-24 13:16:45 +0000 | [diff] [blame] | 129 | IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \ |
| 130 | IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \ |
| 131 | /* Even zero values are set because we are NOEXEC applet */ \ |
| 132 | G.eof_str = NULL; \ |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 133 | G.idx = 0; \ |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 134 | IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \ |
| 135 | IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \ |
| 136 | G.xargs_exitcode = 0; \ |
Ron Yorston | 1ff7002 | 2020-01-24 13:16:45 +0000 | [diff] [blame] | 137 | IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__state = NORM;) \ |
| 138 | IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__q = '\0';) \ |
Denys Vlasenko | 7cfe6ea | 2011-03-03 09:51:57 +0100 | [diff] [blame] | 139 | } while (0) |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 140 | |
| 141 | |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Returns 0 if xargs should continue (but may set G.xargs_exitcode to 123). |
| 144 | * Else sets G.xargs_exitcode to error code and returns nonzero. |
| 145 | * |
| 146 | * If G.max_procs == 0, performs final waitpid() loop for all children. |
| 147 | */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 148 | static int xargs_exec(void) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 149 | { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 150 | int status; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 151 | |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 152 | #if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 153 | status = spawn_and_wait(G.args); |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 154 | #else |
| 155 | if (G.max_procs == 1) { |
| 156 | status = spawn_and_wait(G.args); |
| 157 | } else { |
| 158 | pid_t pid; |
| 159 | int wstat; |
| 160 | again: |
| 161 | if (G.running_procs >= G.max_procs) |
| 162 | pid = safe_waitpid(-1, &wstat, 0); |
| 163 | else |
| 164 | pid = wait_any_nohang(&wstat); |
| 165 | if (pid > 0) { |
| 166 | /* We may have children we don't know about: |
| 167 | * sh -c 'sleep 1 & exec xargs ...' |
| 168 | * Do not make G.running_procs go negative. |
| 169 | */ |
| 170 | if (G.running_procs != 0) |
| 171 | G.running_procs--; |
| 172 | status = WIFSIGNALED(wstat) |
| 173 | ? 0x180 + WTERMSIG(wstat) |
| 174 | : WEXITSTATUS(wstat); |
| 175 | if (status > 0 && status < 255) { |
| 176 | /* See below why 123 does not abort */ |
| 177 | G.xargs_exitcode = 123; |
| 178 | status = 0; |
| 179 | } |
| 180 | if (status == 0) |
| 181 | goto again; /* maybe we have more children? */ |
| 182 | /* else: "bad" status, will bail out */ |
| 183 | } else if (G.max_procs != 0) { |
| 184 | /* Not in final waitpid() loop, |
| 185 | * and G.running_procs < G.max_procs: start more procs |
| 186 | */ |
| 187 | status = spawn(G.args); |
| 188 | /* here "status" actually holds pid, or -1 */ |
| 189 | if (status > 0) { |
| 190 | G.running_procs++; |
| 191 | status = 0; |
| 192 | } |
| 193 | /* else: status == -1 (failed to fork or exec) */ |
| 194 | } else { |
| 195 | /* final waitpid() loop: must be ECHILD "no more children" */ |
| 196 | status = 0; |
| 197 | } |
| 198 | } |
| 199 | #endif |
| 200 | /* Manpage: |
| 201 | * """xargs exits with the following status: |
| 202 | * 0 if it succeeds |
| 203 | * 123 if any invocation of the command exited with status 1-125 |
| 204 | * 124 if the command exited with status 255 |
| 205 | * ("""If any invocation of the command exits with a status of 255, |
| 206 | * xargs will stop immediately without reading any further input. |
| 207 | * An error message is issued on stderr when this happens.""") |
| 208 | * 125 if the command is killed by a signal |
| 209 | * 126 if the command cannot be run |
| 210 | * 127 if the command is not found |
| 211 | * 1 if some other error occurred.""" |
| 212 | */ |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 213 | if (status < 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 214 | bb_simple_perror_msg(G.args[0]); |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 215 | status = (errno == ENOENT) ? 127 : 126; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 216 | } |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 217 | else if (status >= 0x180) { |
Denys Vlasenko | f3e2838 | 2018-10-30 14:40:17 +0100 | [diff] [blame] | 218 | bb_error_msg("'%s' terminated by signal %u", |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 219 | G.args[0], status - 0x180); |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 220 | status = 125; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 221 | } |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 222 | else if (status != 0) { |
| 223 | if (status == 255) { |
| 224 | bb_error_msg("%s: exited with status 255; aborting", G.args[0]); |
Denys Vlasenko | f3e2838 | 2018-10-30 14:40:17 +0100 | [diff] [blame] | 225 | status = 124; |
| 226 | goto ret; |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 227 | } |
| 228 | /* "123 if any invocation of the command exited with status 1-125" |
| 229 | * This implies that nonzero exit code is remembered, |
| 230 | * but does not cause xargs to stop: we return 0. |
| 231 | */ |
| 232 | G.xargs_exitcode = 123; |
| 233 | status = 0; |
| 234 | } |
Denys Vlasenko | f3e2838 | 2018-10-30 14:40:17 +0100 | [diff] [blame] | 235 | ret: |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 236 | if (status != 0) |
| 237 | G.xargs_exitcode = status; |
| 238 | return status; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 239 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 240 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 241 | /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space. |
| 242 | * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13. |
| 243 | */ |
| 244 | #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] | 245 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 246 | static void store_param(char *s) |
| 247 | { |
| 248 | /* Grow by 256 elements at once */ |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 249 | if (!(G.idx & 0xff)) { /* G.idx == N*256? */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 250 | /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */ |
| 251 | G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100)); |
| 252 | } |
| 253 | G.args[G.idx++] = s; |
| 254 | } |
| 255 | |
| 256 | /* process[0]_stdin: |
| 257 | * Read characters into buf[n_max_chars+1], and when parameter delimiter |
| 258 | * is seen, store the address of a new parameter to args[]. |
| 259 | * If reading discovers that last chars do not form the complete |
| 260 | * parameter, the pointer to the first such "tail character" is returned. |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 261 | * (buf has extra byte at the end to accommodate terminating NUL |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 262 | * of "tail characters" string). |
| 263 | * Otherwise, the returned pointer points to NUL byte. |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 264 | * On entry, buf[] may contain some "seed chars" which are to become |
| 265 | * the beginning of the first parameter. |
| 266 | */ |
| 267 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 268 | #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 269 | 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] | 270 | { |
Ron Yorston | 1ff7002 | 2020-01-24 13:16:45 +0000 | [diff] [blame] | 271 | #define q G.process_stdin__q |
| 272 | #define state G.process_stdin__state |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 273 | char *s = buf; /* start of the word */ |
| 274 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 275 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 276 | buf += n_max_chars; /* past buffer's end */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 277 | |
| 278 | /* "goto ret" is used instead of "break" to make control flow |
| 279 | * more obvious: */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 280 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 281 | while (1) { |
| 282 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 283 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 284 | if (p != s) |
| 285 | goto close_word; |
| 286 | goto ret; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 287 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 288 | if (state == BACKSLASH) { |
| 289 | state = NORM; |
| 290 | goto set; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 291 | } |
| 292 | if (state == QUOTE) { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 293 | if (c != q) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 294 | goto set; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 295 | q = '\0'; |
| 296 | state = NORM; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 297 | } else { /* if (state == NORM) */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 298 | if (ISSPACE(c)) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 299 | if (p != s) { |
| 300 | close_word: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 301 | state = SPACE; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 302 | c = '\0'; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 303 | goto set; |
| 304 | } |
| 305 | } else { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 306 | if (c == '\\') { |
| 307 | state = BACKSLASH; |
| 308 | } else if (c == '\'' || c == '"') { |
| 309 | q = c; |
| 310 | state = QUOTE; |
| 311 | } else { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 312 | set: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 313 | *p++ = c; |
| 314 | } |
| 315 | } |
| 316 | } |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 317 | if (state == SPACE) { /* word's delimiter or EOF detected */ |
Ron Yorston | 1c462d4 | 2020-02-19 11:01:39 +0000 | [diff] [blame^] | 318 | state = NORM; |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 319 | if (q) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 320 | bb_error_msg_and_die("unmatched %s quote", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 321 | q == '\'' ? "single" : "double"); |
| 322 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 323 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 324 | if (G.eof_str) { |
| 325 | if (strcmp(s, G.eof_str) == 0) { |
| 326 | while (getchar() != EOF) |
| 327 | continue; |
| 328 | p = s; |
| 329 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 330 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 331 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 332 | store_param(s); |
| 333 | dbg_msg("args[]:'%s'", s); |
| 334 | s = p; |
| 335 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 336 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 337 | goto ret; |
| 338 | } |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 339 | } |
| 340 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 341 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 342 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 343 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 344 | ret: |
| 345 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 346 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 347 | dbg_msg("return:'%s'", s); |
| 348 | return s; |
Ron Yorston | 1ff7002 | 2020-01-24 13:16:45 +0000 | [diff] [blame] | 349 | #undef q |
| 350 | #undef state |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 351 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 352 | #else |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 353 | /* The variant does not support single quotes, double quotes or backslash */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 354 | 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] | 355 | { |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 356 | char *s = buf; /* start of the word */ |
| 357 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 358 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 359 | buf += n_max_chars; /* past buffer's end */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 360 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 361 | while (1) { |
| 362 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 363 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 364 | if (p == s) |
| 365 | goto ret; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 366 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 367 | if (c == EOF || ISSPACE(c)) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 368 | if (p == s) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 369 | continue; |
| 370 | c = EOF; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 371 | } |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 372 | *p++ = (c == EOF ? '\0' : c); |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 373 | if (c == EOF) { /* word's delimiter or EOF detected */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 374 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 375 | if (G.eof_str) { |
| 376 | if (strcmp(s, G.eof_str) == 0) { |
| 377 | while (getchar() != EOF) |
| 378 | continue; |
| 379 | p = s; |
| 380 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 381 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 382 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 383 | store_param(s); |
| 384 | dbg_msg("args[]:'%s'", s); |
| 385 | s = p; |
| 386 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 387 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 388 | goto ret; |
| 389 | } |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 390 | } |
| 391 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 392 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 393 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 394 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 395 | ret: |
| 396 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 397 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 398 | dbg_msg("return:'%s'", s); |
| 399 | return s; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 400 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 401 | #endif /* FEATURE_XARGS_SUPPORT_QUOTES */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 402 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 403 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 404 | 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] | 405 | { |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 406 | char *s = buf; /* start of the word */ |
| 407 | char *p = s + strlen(buf); /* end of the word */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 408 | |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 409 | buf += n_max_chars; /* past buffer's end */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 410 | |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 411 | while (1) { |
| 412 | int c = getchar(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 413 | if (c == EOF) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 414 | if (p == s) |
| 415 | goto ret; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 416 | c = '\0'; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 417 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 418 | *p++ = c; |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 419 | if (c == '\0') { /* NUL or EOF detected */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 420 | /* A full word is loaded */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 421 | store_param(s); |
| 422 | dbg_msg("args[]:'%s'", s); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 423 | s = p; |
Denys Vlasenko | f7e929e | 2010-06-15 10:02:04 +0200 | [diff] [blame] | 424 | n_max_arg--; |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 425 | if (n_max_arg == 0) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 426 | goto ret; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 427 | } |
Denys Vlasenko | 237aece | 2010-06-15 10:18:01 +0200 | [diff] [blame] | 428 | } |
| 429 | if (p == buf) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 430 | goto ret; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 431 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 432 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 433 | ret: |
| 434 | *p = '\0'; |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 435 | /* store_param(NULL) - caller will do it */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 436 | dbg_msg("return:'%s'", s); |
| 437 | return s; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 438 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 439 | #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 440 | |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 441 | #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR |
| 442 | /* |
| 443 | * Used if -I<repl> was specified. |
| 444 | * In this mode, words aren't appended to PROG ARGS. |
| 445 | * Instead, entire input line is read, then <repl> string |
| 446 | * in every PROG and ARG is replaced with the line: |
| 447 | * echo -e "ho ho\nhi" | xargs -I_ cmd __ _ |
| 448 | * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'". |
| 449 | * -n MAX_ARGS seems to be ignored. |
| 450 | * Tested with GNU findutils 4.5.10. |
| 451 | */ |
| 452 | //FIXME: n_max_chars is not handled the same way as in GNU findutils. |
| 453 | //FIXME: quoting is not implemented. |
| 454 | static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf) |
| 455 | { |
| 456 | int i; |
| 457 | char *end, *p; |
| 458 | |
| 459 | /* Free strings from last invocation, if any */ |
| 460 | for (i = 0; G.args && G.args[i]; i++) |
| 461 | if (G.args[i] != G.argv[i]) |
| 462 | free(G.args[i]); |
| 463 | |
| 464 | end = buf + n_max_chars; |
| 465 | p = buf; |
| 466 | |
| 467 | while (1) { |
| 468 | int c = getchar(); |
| 469 | if (c == EOF || c == G.eol_ch) { |
| 470 | if (p == buf) |
| 471 | goto ret; /* empty line */ |
| 472 | c = '\0'; |
| 473 | } |
| 474 | *p++ = c; |
| 475 | if (c == '\0') { /* EOL or EOF detected */ |
| 476 | i = 0; |
| 477 | while (G.argv[i]) { |
| 478 | char *arg = G.argv[i]; |
| 479 | int count = count_strstr(arg, G.repl_str); |
| 480 | if (count != 0) |
Denys Vlasenko | 3795266 | 2014-02-27 15:02:36 +0100 | [diff] [blame] | 481 | arg = xmalloc_substitute_string(arg, count, G.repl_str, buf); |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 482 | store_param(arg); |
| 483 | dbg_msg("args[]:'%s'", arg); |
| 484 | i++; |
| 485 | } |
| 486 | p = buf; |
| 487 | goto ret; |
| 488 | } |
| 489 | if (p == end) { |
| 490 | goto ret; |
| 491 | } |
| 492 | } |
| 493 | ret: |
| 494 | *p = '\0'; |
| 495 | /* store_param(NULL) - caller will do it */ |
| 496 | dbg_msg("return:'%s'", buf); |
| 497 | return buf; |
| 498 | } |
| 499 | #endif |
| 500 | |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 501 | #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 502 | /* Prompt the user for a response, and |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 503 | * if user responds affirmatively, return true; |
| 504 | * otherwise, return false. Uses "/dev/tty", not stdin. |
| 505 | */ |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 506 | static int xargs_ask_confirmation(void) |
| 507 | { |
| 508 | FILE *tty_stream; |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 509 | int r; |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 510 | |
| 511 | tty_stream = xfopen_for_read(CURRENT_TTY); |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 512 | |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 513 | fputs(" ?...", stderr); |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 514 | r = bb_ask_y_confirmation_FILE(tty_stream); |
| 515 | |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 516 | fclose(tty_stream); |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 517 | |
| 518 | return r; |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 519 | } |
| 520 | #else |
| 521 | # define xargs_ask_confirmation() 1 |
| 522 | #endif |
| 523 | |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 524 | //usage:#define xargs_trivial_usage |
| 525 | //usage: "[OPTIONS] [PROG ARGS]" |
| 526 | //usage:#define xargs_full_usage "\n\n" |
| 527 | //usage: "Run PROG on every item given by stdin\n" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 528 | //usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( |
Denys Vlasenko | 4d0e2d5 | 2018-07-06 20:49:08 +0200 | [diff] [blame] | 529 | //usage: "\n -0 Input is separated by NULs" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 530 | //usage: ) |
Johannes Schindelin | f8ee849 | 2017-08-25 22:42:05 +0200 | [diff] [blame] | 531 | //usage: IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( |
| 532 | //usage: "\n -a FILE Read from FILE instead of stdin" |
| 533 | //usage: ) |
Denys Vlasenko | 2452247 | 2019-03-10 14:29:03 +0100 | [diff] [blame] | 534 | //usage: "\n -r Don't run command if input is empty" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 535 | //usage: "\n -t Print the command on stderr before execution" |
Denys Vlasenko | 2452247 | 2019-03-10 14:29:03 +0100 | [diff] [blame] | 536 | //usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION( |
| 537 | //usage: "\n -p Ask user whether to run each command" |
| 538 | //usage: ) |
| 539 | //usage: "\n -E STR,-e[STR] STR stops input processing" |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 540 | //usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR( |
| 541 | //usage: "\n -I STR Replace STR within PROG ARGS with input line" |
| 542 | //usage: ) |
Denys Vlasenko | 2452247 | 2019-03-10 14:29:03 +0100 | [diff] [blame] | 543 | //usage: "\n -n N Pass no more than N args to PROG" |
| 544 | //usage: "\n -s N Pass command line of no more than N bytes" |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 545 | //usage: IF_FEATURE_XARGS_SUPPORT_PARALLEL( |
| 546 | //usage: "\n -P N Run up to N PROGs in parallel" |
| 547 | //usage: ) |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 548 | //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT( |
| 549 | //usage: "\n -x Exit if size is exceeded" |
| 550 | //usage: ) |
| 551 | //usage:#define xargs_example_usage |
| 552 | //usage: "$ ls | xargs gzip\n" |
| 553 | //usage: "$ find . -name '*.c' -print | xargs rm\n" |
| 554 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 555 | /* Correct regardless of combination of CONFIG_xxx */ |
| 556 | enum { |
| 557 | OPTBIT_VERBOSE = 0, |
| 558 | OPTBIT_NO_EMPTY, |
| 559 | OPTBIT_UPTO_NUMBER, |
| 560 | OPTBIT_UPTO_SIZE, |
| 561 | OPTBIT_EOF_STRING, |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 562 | OPTBIT_EOF_STRING1, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 563 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) |
| 564 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) |
| 565 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 566 | IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,) |
| 567 | IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 568 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 569 | OPT_VERBOSE = 1 << OPTBIT_VERBOSE , |
| 570 | OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY , |
| 571 | OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER, |
| 572 | OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE , |
| 573 | OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */ |
| 574 | OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 575 | OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0, |
| 576 | OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0, |
| 577 | OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0, |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 578 | OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0, |
| 579 | OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0, |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 580 | }; |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 581 | #define OPTION_STR "+trn:s:e::E:" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 582 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ |
| 583 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 584 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \ |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 585 | IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \ |
Johannes Schindelin | f8ee849 | 2017-08-25 22:42:05 +0200 | [diff] [blame] | 586 | IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \ |
| 587 | IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:") |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 588 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 589 | int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 590 | int xargs_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 591 | { |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 592 | int initial_idx; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 593 | int i; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 594 | char *max_args; |
| 595 | char *max_chars; |
| 596 | char *buf; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 597 | unsigned opt; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 598 | int n_max_chars; |
| 599 | int n_max_arg; |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 600 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \ |
| 601 | || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 602 | char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 603 | #else |
| 604 | #define read_args process_stdin |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 605 | #endif |
Denys Vlasenko | 58bf902 | 2017-08-30 13:44:27 +0200 | [diff] [blame] | 606 | IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 607 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 608 | INIT_G(); |
| 609 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 610 | opt = getopt32long(argv, OPTION_STR, |
| 611 | "no-run-if-empty\0" No_argument "r", |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 612 | &max_args, &max_chars, &G.eof_str, &G.eof_str |
| 613 | IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str) |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 614 | IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs) |
Johannes Schindelin | f8ee849 | 2017-08-25 22:42:05 +0200 | [diff] [blame] | 615 | IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a) |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 616 | ); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 617 | |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 618 | #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL |
| 619 | if (G.max_procs <= 0) /* -P0 means "run lots of them" */ |
| 620 | G.max_procs = 100; /* let's not go crazy high */ |
| 621 | #endif |
| 622 | |
Johannes Schindelin | f8ee849 | 2017-08-25 22:42:05 +0200 | [diff] [blame] | 623 | #if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE |
| 624 | if (opt_a) |
| 625 | xmove_fd(xopen(opt_a, O_RDONLY), 0); |
| 626 | #endif |
| 627 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 628 | /* -E ""? You may wonder why not just omit -E? |
| 629 | * This is used for portability: |
| 630 | * old xargs was using "_" as default for -E / -e */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 631 | if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0') |
| 632 | G.eof_str = NULL; |
Denis Vlasenko | cc08ad2 | 2008-08-03 19:12:25 +0000 | [diff] [blame] | 633 | |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 634 | if (opt & OPT_ZEROTERM) { |
| 635 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;) |
| 636 | IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';) |
| 637 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 638 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 639 | argv += optind; |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 640 | //argc -= optind; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 641 | if (!argv[0]) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 642 | /* default behavior is to echo all the filenames */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 643 | *--argv = (char*)"echo"; |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 644 | //argc++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 647 | /* |
Denys Vlasenko | 4a96617 | 2010-06-19 21:44:01 +0200 | [diff] [blame] | 648 | * The Open Group Base Specifications Issue 6: |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 649 | * "The xargs utility shall limit the command line length such that |
| 650 | * when the command line is invoked, the combined argument |
| 651 | * and environment lists (see the exec family of functions |
| 652 | * in the System Interfaces volume of IEEE Std 1003.1-2001) |
| 653 | * shall not exceed {ARG_MAX}-2048 bytes". |
| 654 | */ |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 655 | n_max_chars = bb_arg_max(); |
| 656 | if (n_max_chars > 32 * 1024) |
| 657 | n_max_chars = 32 * 1024; |
| 658 | /* |
| 659 | * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX) |
| 660 | * so that the process may safely modify its environment. |
| 661 | */ |
| 662 | n_max_chars -= 2048; |
| 663 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 664 | if (opt & OPT_UPTO_SIZE) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 665 | n_max_chars = xatou_range(max_chars, 1, INT_MAX); |
Denys Vlasenko | 4a96617 | 2010-06-19 21:44:01 +0200 | [diff] [blame] | 666 | } |
| 667 | /* Account for prepended fixed arguments */ |
| 668 | { |
| 669 | size_t n_chars = 0; |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 670 | for (i = 0; argv[i]; i++) { |
| 671 | n_chars += strlen(argv[i]) + 1; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 672 | } |
| 673 | n_max_chars -= n_chars; |
Denys Vlasenko | 4a96617 | 2010-06-19 21:44:01 +0200 | [diff] [blame] | 674 | } |
| 675 | /* Sanity check */ |
| 676 | if (n_max_chars <= 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 677 | bb_simple_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] | 678 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 679 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 680 | buf = xzalloc(n_max_chars + 1); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 681 | |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 682 | n_max_arg = n_max_chars; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 683 | if (opt & OPT_UPTO_NUMBER) { |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 684 | n_max_arg = xatou_range(max_args, 1, INT_MAX); |
Denys Vlasenko | 4a96617 | 2010-06-19 21:44:01 +0200 | [diff] [blame] | 685 | /* Not necessary, we use growable args[]: */ |
| 686 | /* 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] | 687 | } |
| 688 | |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 689 | #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR |
| 690 | if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) { |
| 691 | /* |
| 692 | * -I<str>: |
| 693 | * Unmodified args are kept in G.argv[i], |
| 694 | * G.args[i] receives malloced G.argv[i] with <str> replaced |
| 695 | * with input line. Setting this up: |
| 696 | */ |
| 697 | G.args = NULL; |
| 698 | G.argv = argv; |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 699 | read_args = process_stdin_with_replace; |
Aaro Koskinen | 6d777b7 | 2015-10-24 22:01:29 +0200 | [diff] [blame] | 700 | /* Make -I imply -r. GNU findutils seems to do the same: */ |
| 701 | /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */ |
| 702 | opt |= OPT_NO_EMPTY; |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 703 | } else |
| 704 | #endif |
| 705 | { |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 706 | /* Store the command to be executed, part 1. |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 707 | * We can statically allocate (argc + n_max_arg + 1) elements |
| 708 | * and do not bother with resizing args[], but on 64-bit machines |
| 709 | * this results in args[] vector which is ~8 times bigger |
| 710 | * than n_max_chars! That is, with n_max_chars == 20k, |
| 711 | * args[] will take 160k (!), which will most likely be |
| 712 | * almost entirely unused. |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 713 | */ |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 714 | for (i = 0; argv[i]; i++) |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 715 | store_param(argv[i]); |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 716 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 717 | |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 718 | initial_idx = G.idx; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 719 | while (1) { |
| 720 | char *rem; |
| 721 | |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 722 | G.idx = initial_idx; |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 723 | rem = read_args(n_max_chars, n_max_arg, buf); |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 724 | store_param(NULL); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 725 | |
Denys Vlasenko | b270be3 | 2017-08-24 11:21:27 +0200 | [diff] [blame] | 726 | if (!G.args[initial_idx]) { /* not even one ARG was added? */ |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 727 | if (*rem != '\0') |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 728 | bb_simple_error_msg_and_die("argument line too long"); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 729 | if (opt & OPT_NO_EMPTY) |
| 730 | break; |
| 731 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 732 | opt |= OPT_NO_EMPTY; |
Denys Vlasenko | aaa24e0 | 2010-06-13 12:43:54 +0200 | [diff] [blame] | 733 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 734 | if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { |
Denys Vlasenko | c28cafb | 2010-06-14 12:38:36 +0200 | [diff] [blame] | 735 | const char *fmt = " %s" + 1; |
| 736 | char **args = G.args; |
| 737 | for (i = 0; args[i]; i++) { |
| 738 | fprintf(stderr, fmt, args[i]); |
| 739 | fmt = " %s"; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 740 | } |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 741 | if (!(opt & OPT_INTERACTIVE)) |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 742 | bb_putchar_stderr('\n'); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 743 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 744 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 745 | if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 746 | if (xargs_exec() != 0) |
| 747 | break; /* G.xargs_exitcode is set by xargs_exec() */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 748 | } |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 749 | |
| 750 | overlapping_strcpy(buf, rem); |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 751 | } /* while */ |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 752 | |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 753 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 754 | free(G.args); |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 755 | free(buf); |
Denys Vlasenko | 7a4021d | 2010-06-14 00:57:05 +0200 | [diff] [blame] | 756 | } |
Denys Vlasenko | d5fa1a0 | 2010-06-13 03:43:43 +0200 | [diff] [blame] | 757 | |
Denys Vlasenko | 14551b7 | 2017-08-24 13:23:54 +0200 | [diff] [blame] | 758 | #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL |
| 759 | G.max_procs = 0; |
| 760 | xargs_exec(); /* final waitpid() loop */ |
| 761 | #endif |
| 762 | |
| 763 | return G.xargs_exitcode; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
| 767 | #ifdef TEST |
| 768 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 769 | const char *applet_name = "debug stuff usage"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 770 | |
| 771 | void bb_show_usage(void) |
| 772 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 773 | 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] | 774 | applet_name); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 775 | exit(EXIT_FAILURE); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | int main(int argc, char **argv) |
| 779 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 780 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 781 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 782 | #endif /* TEST */ |