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