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 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 4 | * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 5 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 6 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 7 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 8 | * Special thanks |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 9 | * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 10 | * - Mike Rendell <michael@cs.mun.ca> |
| 11 | * and David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 12 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 13 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 14 | * |
Glenn L McGrath | 40c9489 | 2003-10-30 22:51:33 +0000 | [diff] [blame] | 15 | * xargs is described in the Single Unix Specification v3 at |
| 16 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html |
| 17 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 18 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 19 | |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame^] | 20 | //kbuild:lib-$(CONFIG_XARGS) += xargs.o |
| 21 | //config: |
| 22 | //config:config XARGS |
| 23 | //config: bool "xargs" |
| 24 | //config: default n |
| 25 | //config: help |
| 26 | //config: xargs is used to execute a specified command for |
| 27 | //config: every item from standard input. |
| 28 | //config: |
| 29 | //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 30 | //config: bool "Enable -p: prompt and confirmation" |
| 31 | //config: default n |
| 32 | //config: depends on XARGS |
| 33 | //config: help |
| 34 | //config: Support -p: prompt the user whether to run each command |
| 35 | //config: line and read a line from the terminal. |
| 36 | //config: |
| 37 | //config:config FEATURE_XARGS_SUPPORT_QUOTES |
| 38 | //config: bool "Enable single and double quotes and backslash" |
| 39 | //config: default n |
| 40 | //config: depends on XARGS |
| 41 | //config: help |
| 42 | //config: Support quoting in the input. |
| 43 | //config: |
| 44 | //config:config FEATURE_XARGS_SUPPORT_TERMOPT |
| 45 | //config: bool "Enable -x: exit if -s or -n is exceeded" |
| 46 | //config: default n |
| 47 | //config: depends on XARGS |
| 48 | //config: help |
| 49 | //config: Support -x: exit if the command size (see the -s or -n option) |
| 50 | //config: is exceeded. |
| 51 | //config: |
| 52 | //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 53 | //config: bool "Enable -0: NUL-terminated input" |
| 54 | //config: default n |
| 55 | //config: depends on XARGS |
| 56 | //config: help |
| 57 | //config: Support -0: input items are terminated by a NUL character |
| 58 | //config: instead of whitespace, and the quotes and backslash |
| 59 | //config: are not special. |
| 60 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 61 | #include "libbb.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 63 | /* This is a NOEXEC applet. Be very careful! */ |
| 64 | |
| 65 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 66 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. |
| 67 | We try to make it as large as possible. */ |
| 68 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 69 | # define ARG_MAX sysconf(_SC_ARG_MAX) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 70 | #endif |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 71 | #if !defined(ARG_MAX) |
| 72 | # define ARG_MAX 470 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | |
| 76 | #ifdef TEST |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 77 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 78 | # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 79 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 80 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
| 81 | # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 82 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 83 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT |
| 84 | # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 85 | # endif |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 86 | # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 87 | # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1 |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 88 | # endif |
| 89 | #endif |
| 90 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 91 | /* |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 92 | This function has special algorithm. |
| 93 | Don't use fork and include to main! |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 94 | */ |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 95 | static int xargs_exec(char **args) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 97 | int status; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 99 | status = spawn_and_wait(args); |
| 100 | if (status < 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 101 | bb_simple_perror_msg(args[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 102 | return errno == ENOENT ? 127 : 126; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 103 | } |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 104 | if (status == 255) { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 105 | bb_error_msg("%s: exited with status 255; aborting", args[0]); |
| 106 | return 124; |
| 107 | } |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 108 | if (status >= 0x180) { |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 109 | bb_error_msg("%s: terminated by signal %d", |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 110 | args[0], status - 0x180); |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 111 | return 125; |
| 112 | } |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 113 | if (status) |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 114 | return 123; |
| 115 | return 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 116 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 117 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 119 | typedef struct xlist_t { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 120 | struct xlist_t *link; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 121 | size_t length; |
| 122 | char xstr[1]; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 123 | } xlist_t; |
| 124 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 125 | static smallint eof_stdin_detected; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 126 | |
| 127 | #define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 128 | #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 129 | || (c) == '\f' || (c) == '\v') |
| 130 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 131 | #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES |
| 132 | static xlist_t *process_stdin(xlist_t *list_arg, |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 133 | const char *eof_str, size_t mc, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 134 | { |
| 135 | #define NORM 0 |
| 136 | #define QUOTE 1 |
| 137 | #define BACKSLASH 2 |
| 138 | #define SPACE 4 |
| 139 | |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 140 | char *s = NULL; /* start word */ |
| 141 | char *p = NULL; /* pointer to end word */ |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 142 | char q = '\0'; /* quote char */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 143 | char state = NORM; |
| 144 | char eof_str_detected = 0; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 145 | size_t line_l = 0; /* size loaded args line */ |
| 146 | int c; /* current char */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 147 | xlist_t *cur; |
| 148 | xlist_t *prev; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 150 | prev = cur = list_arg; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 151 | while (1) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 152 | if (!cur) break; |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 153 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 154 | line_l += cur->length; |
| 155 | cur = cur->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 156 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 157 | |
| 158 | while (!eof_stdin_detected) { |
| 159 | c = getchar(); |
| 160 | if (c == EOF) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 161 | eof_stdin_detected = 1; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 162 | if (s) |
| 163 | goto unexpected_eof; |
| 164 | break; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 165 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 166 | if (eof_str_detected) |
| 167 | continue; |
| 168 | if (state == BACKSLASH) { |
| 169 | state = NORM; |
| 170 | goto set; |
| 171 | } else if (state == QUOTE) { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 172 | if (c != q) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 173 | goto set; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 174 | q = '\0'; |
| 175 | state = NORM; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 176 | } else { /* if (state == NORM) */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 177 | if (ISSPACE(c)) { |
| 178 | if (s) { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 179 | unexpected_eof: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 180 | state = SPACE; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 181 | c = '\0'; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 182 | goto set; |
| 183 | } |
| 184 | } else { |
| 185 | if (s == NULL) |
| 186 | s = p = buf; |
| 187 | if (c == '\\') { |
| 188 | state = BACKSLASH; |
| 189 | } else if (c == '\'' || c == '"') { |
| 190 | q = c; |
| 191 | state = QUOTE; |
| 192 | } else { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 193 | set: |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 194 | if ((size_t)(p - buf) >= mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 195 | bb_error_msg_and_die("argument line too long"); |
| 196 | *p++ = c; |
| 197 | } |
| 198 | } |
| 199 | } |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 200 | if (state == SPACE) { /* word's delimiter or EOF detected */ |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 201 | if (q) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 202 | bb_error_msg_and_die("unmatched %s quote", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 203 | q == '\'' ? "single" : "double"); |
| 204 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 205 | /* word loaded */ |
| 206 | if (eof_str) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 207 | eof_str_detected = (strcmp(s, eof_str) == 0); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 208 | } |
| 209 | if (!eof_str_detected) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 210 | size_t length = (p - buf); |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 211 | /* Dont xzalloc - it can be quite big */ |
| 212 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
| 213 | cur->link = NULL; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 214 | cur->length = length; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 215 | memcpy(cur->xstr, s, length); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 216 | if (prev == NULL) { |
| 217 | list_arg = cur; |
| 218 | } else { |
| 219 | prev->link = cur; |
| 220 | } |
| 221 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 222 | line_l += length; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 223 | if (line_l > mc) { |
| 224 | /* stop memory usage :-) */ |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | s = NULL; |
| 229 | state = NORM; |
| 230 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 231 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 232 | return list_arg; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 233 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 234 | #else |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 235 | /* The variant does not support single quotes, double quotes or backslash */ |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 236 | static xlist_t *process_stdin(xlist_t *list_arg, |
| 237 | const char *eof_str, size_t mc, char *buf) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 238 | { |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 239 | |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 240 | int c; /* current char */ |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 241 | char eof_str_detected = 0; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 242 | char *s = NULL; /* start word */ |
| 243 | char *p = NULL; /* pointer to end word */ |
| 244 | size_t line_l = 0; /* size loaded args line */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 245 | xlist_t *cur; |
| 246 | xlist_t *prev; |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 248 | prev = cur = list_arg; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 249 | while (1) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 250 | if (!cur) break; |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 251 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 252 | line_l += cur->length; |
| 253 | cur = cur->link; |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 254 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 255 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 256 | while (!eof_stdin_detected) { |
| 257 | c = getchar(); |
| 258 | if (c == EOF) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 259 | eof_stdin_detected = 1; |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 260 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 261 | if (eof_str_detected) |
| 262 | continue; |
| 263 | if (c == EOF || ISSPACE(c)) { |
| 264 | if (s == NULL) |
| 265 | continue; |
| 266 | c = EOF; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 267 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 268 | if (s == NULL) |
| 269 | s = p = buf; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 270 | if ((size_t)(p - buf) >= mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 271 | bb_error_msg_and_die("argument line too long"); |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 272 | *p++ = (c == EOF ? '\0' : c); |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 273 | if (c == EOF) { /* word's delimiter or EOF detected */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 274 | /* word loaded */ |
| 275 | if (eof_str) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 276 | eof_str_detected = (strcmp(s, eof_str) == 0); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 277 | } |
| 278 | if (!eof_str_detected) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 279 | size_t length = (p - buf); |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 280 | /* Dont xzalloc - it can be quite big */ |
| 281 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
| 282 | cur->link = NULL; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 283 | cur->length = length; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 284 | memcpy(cur->xstr, s, length); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 285 | if (prev == NULL) { |
| 286 | list_arg = cur; |
| 287 | } else { |
| 288 | prev->link = cur; |
| 289 | } |
| 290 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 291 | line_l += length; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 292 | if (line_l > mc) { |
| 293 | /* stop memory usage :-) */ |
| 294 | break; |
| 295 | } |
| 296 | s = NULL; |
| 297 | } |
| 298 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 299 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 300 | return list_arg; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 301 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 302 | #endif /* FEATURE_XARGS_SUPPORT_QUOTES */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 303 | |
| 304 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 305 | #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 306 | /* Prompt the user for a response, and |
| 307 | if the user responds affirmatively, return true; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 308 | otherwise, return false. Uses "/dev/tty", not stdin. */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 309 | static int xargs_ask_confirmation(void) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 310 | { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 311 | FILE *tty_stream; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 312 | int c, savec; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 313 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 314 | tty_stream = xfopen_for_read(CURRENT_TTY); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 315 | fputs(" ?...", stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 316 | fflush_all(); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 317 | c = savec = getc(tty_stream); |
| 318 | while (c != EOF && c != '\n') |
| 319 | c = getc(tty_stream); |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 320 | fclose(tty_stream); |
| 321 | return (savec == 'y' || savec == 'Y'); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 322 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 323 | #else |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 324 | # define xargs_ask_confirmation() 1 |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 325 | #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 327 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 328 | static xlist_t *process0_stdin(xlist_t *list_arg, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 329 | const char *eof_str UNUSED_PARAM, size_t mc, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 330 | { |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 331 | int c; /* current char */ |
| 332 | char *s = NULL; /* start word */ |
| 333 | char *p = NULL; /* pointer to end word */ |
| 334 | size_t line_l = 0; /* size loaded args line */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 335 | xlist_t *cur; |
| 336 | xlist_t *prev; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 338 | prev = cur = list_arg; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 339 | while (1) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 340 | if (!cur) break; |
Denis Vlasenko | 8905496 | 2007-04-10 21:41:16 +0000 | [diff] [blame] | 341 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 342 | line_l += cur->length; |
| 343 | cur = cur->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 344 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 345 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 346 | while (!eof_stdin_detected) { |
| 347 | c = getchar(); |
| 348 | if (c == EOF) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 349 | eof_stdin_detected = 1; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 350 | if (s == NULL) |
| 351 | break; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 352 | c = '\0'; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 353 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 354 | if (s == NULL) |
| 355 | s = p = buf; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 356 | if ((size_t)(p - buf) >= mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 357 | bb_error_msg_and_die("argument line too long"); |
| 358 | *p++ = c; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 359 | if (c == '\0') { /* word's delimiter or EOF detected */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 360 | /* word loaded */ |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 361 | size_t length = (p - buf); |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 362 | /* Dont xzalloc - it can be quite big */ |
| 363 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
| 364 | cur->link = NULL; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 365 | cur->length = length; |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 366 | memcpy(cur->xstr, s, length); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 367 | if (prev == NULL) { |
| 368 | list_arg = cur; |
| 369 | } else { |
| 370 | prev->link = cur; |
| 371 | } |
| 372 | prev = cur; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 373 | line_l += length; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 374 | if (line_l > mc) { |
| 375 | /* stop memory usage :-) */ |
| 376 | break; |
| 377 | } |
| 378 | s = NULL; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 379 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 380 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 381 | return list_arg; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 382 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 383 | #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 384 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 385 | /* Correct regardless of combination of CONFIG_xxx */ |
| 386 | enum { |
| 387 | OPTBIT_VERBOSE = 0, |
| 388 | OPTBIT_NO_EMPTY, |
| 389 | OPTBIT_UPTO_NUMBER, |
| 390 | OPTBIT_UPTO_SIZE, |
| 391 | OPTBIT_EOF_STRING, |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 392 | OPTBIT_EOF_STRING1, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 393 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) |
| 394 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) |
| 395 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 396 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 397 | OPT_VERBOSE = 1 << OPTBIT_VERBOSE , |
| 398 | OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY , |
| 399 | OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER, |
| 400 | OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE , |
| 401 | OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */ |
| 402 | OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 403 | OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0, |
| 404 | OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0, |
| 405 | OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0, |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 406 | }; |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 407 | #define OPTION_STR "+trn:s:e::E:" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 408 | IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ |
| 409 | IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ |
| 410 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 411 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 412 | int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 413 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 414 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 415 | char **args; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 416 | int i, n; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 417 | xlist_t *list = NULL; |
| 418 | xlist_t *cur; |
| 419 | int child_error = 0; |
| 420 | char *max_args, *max_chars; |
| 421 | int n_max_arg; |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 422 | const char *eof_str = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 423 | unsigned opt; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 424 | size_t n_max_chars; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 425 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 426 | xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin; |
| 427 | #else |
| 428 | #define read_args process_stdin |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 429 | #endif |
| 430 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 431 | opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 432 | |
Denis Vlasenko | 82ad032 | 2008-08-04 21:30:55 +0000 | [diff] [blame] | 433 | /* -E ""? You may wonder why not just omit -E? |
| 434 | * This is used for portability: |
| 435 | * old xargs was using "_" as default for -E / -e */ |
| 436 | if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0') |
Denis Vlasenko | cc08ad2 | 2008-08-03 19:12:25 +0000 | [diff] [blame] | 437 | eof_str = NULL; |
| 438 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 439 | if (opt & OPT_ZEROTERM) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 440 | IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 441 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 442 | argv += optind; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 443 | argc -= optind; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 444 | if (!argc) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 445 | /* default behavior is to echo all the filenames */ |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 446 | *argv = (char*)"echo"; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 447 | argc++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 450 | n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */ |
| 451 | if (n_max_chars < 4*1024); /* paranoia */ |
| 452 | n_max_chars = LONG_MAX; |
| 453 | /* The Open Group Base Specifications Issue 6: |
| 454 | * "The xargs utility shall limit the command line length such that |
| 455 | * when the command line is invoked, the combined argument |
| 456 | * and environment lists (see the exec family of functions |
| 457 | * in the System Interfaces volume of IEEE Std 1003.1-2001) |
| 458 | * shall not exceed {ARG_MAX}-2048 bytes". |
| 459 | */ |
| 460 | n_max_chars -= 2048; |
| 461 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which |
| 462 | * have it at 1 meg). Things will work fine with a large ARG_MAX but it |
| 463 | * will probably hurt the system more than it needs to; an array of this |
| 464 | * size is allocated. |
| 465 | */ |
| 466 | if (n_max_chars > 20 * 1024) |
| 467 | n_max_chars = 20 * 1024; |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 468 | |
| 469 | if (opt & OPT_UPTO_SIZE) { |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 470 | size_t n_chars = 0; |
| 471 | n_max_chars = xatoul_range(max_chars, 1, n_max_chars); |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 472 | for (i = 0; i < argc; i++) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 473 | n_chars += strlen(*argv) + 1; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 474 | } |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 475 | if (n_max_chars <= n_chars) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 476 | bb_error_msg_and_die("can't fit single argument within argument list size limit"); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 477 | } |
| 478 | n_max_chars -= n_chars; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 479 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 480 | max_chars = xmalloc(n_max_chars); |
| 481 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 482 | if (opt & OPT_UPTO_NUMBER) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 483 | n_max_arg = xatoul_range(max_args, 1, INT_MAX); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 484 | } else { |
| 485 | n_max_arg = n_max_chars; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 488 | while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL || |
| 489 | !(opt & OPT_NO_EMPTY)) |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 490 | { |
Denys Vlasenko | d7b5289 | 2010-04-09 14:58:40 +0200 | [diff] [blame] | 491 | size_t n_chars = 0; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 492 | opt |= OPT_NO_EMPTY; |
| 493 | n = 0; |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 494 | #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 495 | for (cur = list; cur;) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 496 | n_chars += cur->length; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 497 | n++; |
| 498 | cur = cur->link; |
| 499 | if (n_chars > n_max_chars || (n == n_max_arg && cur)) { |
| 500 | if (opt & OPT_TERMINATE) |
| 501 | bb_error_msg_and_die("argument list too long"); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | #else |
| 506 | for (cur = list; cur; cur = cur->link) { |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 507 | n_chars += cur->length; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 508 | n++; |
| 509 | if (n_chars > n_max_chars || n == n_max_arg) { |
| 510 | break; |
| 511 | } |
| 512 | } |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 513 | #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 514 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 515 | /* allocate pointers for execvp: |
| 516 | argc*arg, n*arg from stdin, NULL */ |
| 517 | args = xzalloc((n + argc + 1) * sizeof(char *)); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 518 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 519 | /* store the command to be executed |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 520 | (taken from the command line) */ |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 521 | for (i = 0; i < argc; i++) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 522 | args[i] = argv[i]; |
| 523 | /* (taken from stdin) */ |
| 524 | for (cur = list; n; cur = cur->link) { |
Denis Vlasenko | 58394b1 | 2007-04-15 08:38:50 +0000 | [diff] [blame] | 525 | args[i++] = cur->xstr; |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 526 | n--; |
| 527 | } |
| 528 | |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 529 | if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 530 | for (i = 0; args[i]; i++) { |
| 531 | if (i) |
| 532 | fputc(' ', stderr); |
| 533 | fputs(args[i], stderr); |
| 534 | } |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 535 | if (!(opt & OPT_INTERACTIVE)) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 536 | fputc('\n', stderr); |
| 537 | } |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 538 | if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 539 | child_error = xargs_exec(args); |
| 540 | } |
| 541 | |
| 542 | /* clean up */ |
Denis Vlasenko | 6248a73 | 2006-09-29 08:20:30 +0000 | [diff] [blame] | 543 | for (i = argc; args[i]; i++) { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 544 | cur = list; |
| 545 | list = list->link; |
| 546 | free(cur); |
| 547 | } |
| 548 | free(args); |
| 549 | if (child_error > 0 && child_error != 123) { |
| 550 | break; |
| 551 | } |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 552 | } /* while */ |
Denis Vlasenko | 1b4b2cb | 2007-04-09 21:30:53 +0000 | [diff] [blame] | 553 | if (ENABLE_FEATURE_CLEAN_UP) |
| 554 | free(max_chars); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 555 | return child_error; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
| 559 | #ifdef TEST |
| 560 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 561 | const char *applet_name = "debug stuff usage"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 562 | |
| 563 | void bb_show_usage(void) |
| 564 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 565 | 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] | 566 | applet_name); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 567 | exit(EXIT_FAILURE); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | int main(int argc, char **argv) |
| 571 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 572 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 573 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 574 | #endif /* TEST */ |