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