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