Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 2 | * Mini xargs implementation for busybox |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 3 | * 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] | 4 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 5 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 7 | * Special thanks |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 8 | * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 9 | * - Mike Rendell <michael@cs.mun.ca> |
| 10 | * and David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 16 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 21 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
Glenn L McGrath | 40c9489 | 2003-10-30 22:51:33 +0000 | [diff] [blame] | 26 | * xargs is described in the Single Unix Specification v3 at |
| 27 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html |
| 28 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 29 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 33 | #include <string.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <getopt.h> |
| 36 | #include <errno.h> |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 37 | #include <fcntl.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/wait.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 40 | #include "busybox.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 42 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. |
| 43 | We try to make it as large as possible. */ |
| 44 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) |
| 45 | #define ARG_MAX sysconf (_SC_ARG_MAX) |
| 46 | #endif |
| 47 | #ifndef ARG_MAX |
| 48 | #define ARG_MAX 470 |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | #ifdef TEST |
| 53 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 54 | # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 55 | # endif |
| 56 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
| 57 | # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
| 58 | # endif |
| 59 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 60 | # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 61 | # endif |
| 62 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 63 | # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 64 | # endif |
| 65 | #endif |
| 66 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 67 | /* |
| 68 | This function have special algorithm. |
| 69 | Don`t use fork and include to main! |
| 70 | */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 71 | static int xargs_exec(char *const *args) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 72 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 73 | pid_t p; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 74 | volatile int exec_errno = 0; /* shared vfork stack */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 75 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 76 | if ((p = vfork()) >= 0) { |
| 77 | if (p == 0) { |
| 78 | /* vfork -- child */ |
| 79 | execvp(args[0], args); |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 80 | exec_errno = errno; /* set error to shared stack */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 81 | _exit(1); |
| 82 | } else { |
| 83 | /* vfork -- parent */ |
| 84 | int status; |
| 85 | |
| 86 | while (wait(&status) == (pid_t) - 1) |
| 87 | if (errno != EINTR) |
| 88 | break; |
| 89 | if (exec_errno) { |
| 90 | errno = exec_errno; |
| 91 | bb_perror_msg("%s", args[0]); |
| 92 | return exec_errno == ENOENT ? 127 : 126; |
| 93 | } else { |
| 94 | if (WEXITSTATUS(status) == 255) { |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 95 | bb_error_msg("%s: exited with status 255; aborting", args[0]); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 96 | return 124; |
| 97 | } |
| 98 | if (WIFSTOPPED(status)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 99 | bb_error_msg("%s: stopped by signal %d", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 100 | args[0], WSTOPSIG(status)); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 101 | return 125; |
| 102 | } |
| 103 | if (WIFSIGNALED(status)) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | bb_error_msg("%s: terminated by signal %d", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 105 | args[0], WTERMSIG(status)); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 106 | return 125; |
| 107 | } |
| 108 | if (WEXITSTATUS(status) != 0) |
| 109 | return 123; |
| 110 | return 0; |
| 111 | } |
| 112 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 113 | } else { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 114 | bb_perror_msg_and_die("vfork"); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 115 | } |
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 | |
| 119 | typedef struct xlist_s { |
| 120 | char *data; |
| 121 | size_t lenght; |
| 122 | struct xlist_s *link; |
| 123 | } xlist_t; |
| 124 | |
| 125 | static int eof_stdin_detected; |
| 126 | |
| 127 | #define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
| 128 | #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \ |
| 129 | || (c) == '\f' || (c) == '\v') |
| 130 | |
| 131 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 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 */ |
| 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 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 150 | for (prev = cur = list_arg; cur; cur = cur->link) { |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 151 | line_l += cur->lenght; /* previous allocated */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 152 | if (prev != cur) |
| 153 | prev = prev->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 155 | |
| 156 | while (!eof_stdin_detected) { |
| 157 | c = getchar(); |
| 158 | if (c == EOF) { |
| 159 | eof_stdin_detected++; |
| 160 | if (s) |
| 161 | goto unexpected_eof; |
| 162 | break; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 163 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 164 | if (eof_str_detected) |
| 165 | continue; |
| 166 | if (state == BACKSLASH) { |
| 167 | state = NORM; |
| 168 | goto set; |
| 169 | } else if (state == QUOTE) { |
| 170 | if (c == q) { |
| 171 | q = 0; |
| 172 | state = NORM; |
| 173 | } else { |
| 174 | goto set; |
| 175 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 176 | } else { /* if(state == NORM) */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 177 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 178 | if (ISSPACE(c)) { |
| 179 | if (s) { |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 180 | unexpected_eof: |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 181 | state = SPACE; |
| 182 | c = 0; |
| 183 | goto set; |
| 184 | } |
| 185 | } else { |
| 186 | if (s == NULL) |
| 187 | s = p = buf; |
| 188 | if (c == '\\') { |
| 189 | state = BACKSLASH; |
| 190 | } else if (c == '\'' || c == '"') { |
| 191 | q = c; |
| 192 | state = QUOTE; |
| 193 | } else { |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 194 | set: |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 195 | if ((size_t)(p - buf) >= mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 196 | bb_error_msg_and_die("argument line too long"); |
| 197 | *p++ = c; |
| 198 | } |
| 199 | } |
| 200 | } |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 201 | if (state == SPACE) { /* word's delimiter or EOF detected */ |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 202 | if (q) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 203 | bb_error_msg_and_die("unmatched %s quote", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 204 | q == '\'' ? "single" : "double"); |
| 205 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 206 | /* word loaded */ |
| 207 | if (eof_str) { |
| 208 | eof_str_detected = strcmp(s, eof_str) == 0; |
| 209 | } |
| 210 | if (!eof_str_detected) { |
| 211 | size_t lenght = (p - buf); |
| 212 | |
| 213 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 214 | cur->data = memcpy(cur + 1, s, lenght); |
| 215 | cur->lenght = lenght; |
| 216 | cur->link = NULL; |
| 217 | if (prev == NULL) { |
| 218 | list_arg = cur; |
| 219 | } else { |
| 220 | prev->link = cur; |
| 221 | } |
| 222 | prev = cur; |
| 223 | line_l += lenght; |
| 224 | if (line_l > mc) { |
| 225 | /* stop memory usage :-) */ |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | s = NULL; |
| 230 | state = NORM; |
| 231 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 232 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 233 | return list_arg; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 234 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 235 | #else |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 236 | /* The variant does not support single quotes, double quotes or backslash */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 237 | static xlist_t *process_stdin(xlist_t * list_arg, |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 238 | const char *eof_str, size_t mc, char *buf) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 239 | { |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 240 | |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 241 | int c; /* current char */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 242 | int eof_str_detected = 0; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 243 | char *s = NULL; /* start word */ |
| 244 | char *p = NULL; /* pointer to end word */ |
| 245 | size_t line_l = 0; /* size loaded args line */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 246 | xlist_t *cur; |
| 247 | xlist_t *prev; |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 248 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 249 | for (prev = cur = list_arg; cur; cur = cur->link) { |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 250 | line_l += cur->lenght; /* previous allocated */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 251 | if (prev != cur) |
| 252 | prev = prev->link; |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame] | 253 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 254 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 255 | while (!eof_stdin_detected) { |
| 256 | c = getchar(); |
| 257 | if (c == EOF) { |
| 258 | eof_stdin_detected++; |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 259 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 260 | if (eof_str_detected) |
| 261 | continue; |
| 262 | if (c == EOF || ISSPACE(c)) { |
| 263 | if (s == NULL) |
| 264 | continue; |
| 265 | c = EOF; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 266 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 267 | if (s == NULL) |
| 268 | s = p = buf; |
| 269 | if ((p - buf) >= mc) |
| 270 | bb_error_msg_and_die("argument line too long"); |
| 271 | *p++ = c == EOF ? 0 : c; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 272 | if (c == EOF) { /* word's delimiter or EOF detected */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 273 | /* word loaded */ |
| 274 | if (eof_str) { |
| 275 | eof_str_detected = strcmp(s, eof_str) == 0; |
| 276 | } |
| 277 | if (!eof_str_detected) { |
| 278 | size_t lenght = (p - buf); |
| 279 | |
| 280 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 281 | cur->data = memcpy(cur + 1, s, lenght); |
| 282 | cur->lenght = lenght; |
| 283 | cur->link = NULL; |
| 284 | if (prev == NULL) { |
| 285 | list_arg = cur; |
| 286 | } else { |
| 287 | prev->link = cur; |
| 288 | } |
| 289 | prev = cur; |
| 290 | line_l += lenght; |
| 291 | if (line_l > mc) { |
| 292 | /* stop memory usage :-) */ |
| 293 | break; |
| 294 | } |
| 295 | s = NULL; |
| 296 | } |
| 297 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 298 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 299 | return list_arg; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 300 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 301 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 302 | |
| 303 | |
| 304 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 305 | /* Prompt the user for a response, and |
| 306 | if the user responds affirmatively, return true; |
| 307 | otherwise, return false. Used "/dev/tty", not stdin. */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 308 | static int xargs_ask_confirmation(void) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 309 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 310 | static FILE *tty_stream; |
| 311 | int c, savec; |
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 (!tty_stream) { |
| 314 | tty_stream = fopen("/dev/tty", "r"); |
| 315 | if (!tty_stream) |
| 316 | bb_perror_msg_and_die("/dev/tty"); |
| 317 | /* pranoidal security by vodz */ |
| 318 | fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC); |
| 319 | } |
| 320 | fputs(" ?...", stderr); |
| 321 | fflush(stderr); |
| 322 | c = savec = getc(tty_stream); |
| 323 | while (c != EOF && c != '\n') |
| 324 | c = getc(tty_stream); |
| 325 | if (savec == 'y' || savec == 'Y') |
| 326 | return 1; |
| 327 | return 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 328 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 329 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 330 | # define OPT_INC_P 1 |
| 331 | #else |
| 332 | # define OPT_INC_P 0 |
| 333 | # define xargs_ask_confirmation() 1 |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 334 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 337 | # define OPT_INC_X 1 |
| 338 | #else |
| 339 | # define OPT_INC_X 0 |
| 340 | #endif |
| 341 | |
| 342 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 343 | static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED, |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 344 | size_t mc, char *buf) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 345 | { |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 346 | int c; /* current char */ |
| 347 | char *s = NULL; /* start word */ |
| 348 | char *p = NULL; /* pointer to end word */ |
| 349 | size_t line_l = 0; /* size loaded args line */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 350 | xlist_t *cur; |
| 351 | xlist_t *prev; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 352 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 353 | for (prev = cur = list_arg; cur; cur = cur->link) { |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 354 | line_l += cur->lenght; /* previous allocated */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 355 | if (prev != cur) |
| 356 | prev = prev->link; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 357 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 358 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 359 | while (!eof_stdin_detected) { |
| 360 | c = getchar(); |
| 361 | if (c == EOF) { |
| 362 | eof_stdin_detected++; |
| 363 | if (s == NULL) |
| 364 | break; |
| 365 | c = 0; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 366 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 367 | if (s == NULL) |
| 368 | s = p = buf; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 369 | if ((size_t)(p - buf) >= mc) |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 370 | bb_error_msg_and_die("argument line too long"); |
| 371 | *p++ = c; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 372 | if (c == 0) { /* word's delimiter or EOF detected */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 373 | /* word loaded */ |
| 374 | size_t lenght = (p - buf); |
| 375 | |
| 376 | cur = xmalloc(sizeof(xlist_t) + lenght); |
| 377 | cur->data = memcpy(cur + 1, s, lenght); |
| 378 | cur->lenght = lenght; |
| 379 | cur->link = NULL; |
| 380 | if (prev == NULL) { |
| 381 | list_arg = cur; |
| 382 | } else { |
| 383 | prev->link = cur; |
| 384 | } |
| 385 | prev = cur; |
| 386 | line_l += lenght; |
| 387 | if (line_l > mc) { |
| 388 | /* stop memory usage :-) */ |
| 389 | break; |
| 390 | } |
| 391 | s = NULL; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 392 | } |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 393 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 394 | return list_arg; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 395 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 396 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 397 | # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc) |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 398 | # define OPT_INC_0 1 /* future use */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 399 | #else |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 400 | # define OPT_INC_0 0 /* future use */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 401 | # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc) |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 402 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 403 | |
| 404 | |
| 405 | #define OPT_VERBOSE (1<<0) |
| 406 | #define OPT_NO_EMPTY (1<<1) |
| 407 | #define OPT_UPTO_NUMBER (1<<2) |
| 408 | #define OPT_UPTO_SIZE (1<<3) |
| 409 | #define OPT_EOF_STRING (1<<4) |
| 410 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
| 411 | #define OPT_INTERACTIVE (1<<5) |
| 412 | #else |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 413 | #define OPT_INTERACTIVE (0) /* require for algorithm &| */ |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 414 | #endif |
| 415 | #define OPT_TERMINATE (1<<(5+OPT_INC_P)) |
| 416 | #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X)) |
| 417 | /* next future |
| 418 | #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0)) |
| 419 | */ |
| 420 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 421 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 422 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 423 | char **args; |
| 424 | int i, a, n; |
| 425 | xlist_t *list = NULL; |
| 426 | xlist_t *cur; |
| 427 | int child_error = 0; |
| 428 | char *max_args, *max_chars; |
| 429 | int n_max_arg; |
| 430 | size_t n_chars = 0; |
| 431 | long orig_arg_max; |
| 432 | const char *eof_str = "_"; |
| 433 | unsigned long opt; |
| 434 | size_t n_max_chars; |
| 435 | |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 436 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 437 | xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 438 | #endif |
| 439 | |
| 440 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
"Vladimir N. Oleynik" | 27421a1 | 2005-09-05 14:46:07 +0000 | [diff] [blame] | 441 | bb_opt_complementally = "pt"; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 442 | #endif |
| 443 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 444 | opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 445 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 446 | "p" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 447 | #endif |
| 448 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 449 | "x" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 450 | #endif |
| 451 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 452 | "0" |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 453 | #endif |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 454 | ,&max_args, &max_chars, &eof_str); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 455 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 456 | a = argc - optind; |
| 457 | argv += optind; |
| 458 | if (a == 0) { |
| 459 | /* default behavior is to echo all the filenames */ |
| 460 | *argv = "echo"; |
| 461 | a++; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 464 | orig_arg_max = ARG_MAX; |
| 465 | if (orig_arg_max == -1) |
| 466 | orig_arg_max = LONG_MAX; |
"Vladimir N. Oleynik" | 59c4e5c | 2006-01-30 13:51:50 +0000 | [diff] [blame] | 467 | orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 468 | if ((opt & OPT_UPTO_SIZE)) { |
| 469 | n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max); |
| 470 | for (i = 0; i < a; i++) { |
| 471 | n_chars += strlen(*argv) + 1; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 472 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 473 | if (n_max_chars < n_chars) { |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 474 | bb_error_msg_and_die("can not fit single argument within argument list size limit"); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 475 | } |
| 476 | n_max_chars -= n_chars; |
| 477 | } else { |
| 478 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which |
| 479 | have it at 1 meg). Things will work fine with a large ARG_MAX but it |
| 480 | will probably hurt the system more than it needs to; an array of this |
| 481 | size is allocated. */ |
| 482 | if (orig_arg_max > 20 * 1024) |
| 483 | orig_arg_max = 20 * 1024; |
| 484 | n_max_chars = orig_arg_max; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 485 | } |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 486 | max_chars = xmalloc(n_max_chars); |
| 487 | |
| 488 | if ((opt & OPT_UPTO_NUMBER)) { |
| 489 | n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX); |
| 490 | } else { |
| 491 | n_max_arg = n_max_chars; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 494 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM |
| 495 | if (opt & OPT_ZEROTERM) |
| 496 | read_args = process0_stdin; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 497 | #endif |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 498 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 499 | while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL || |
| 500 | (opt & OPT_NO_EMPTY) == 0) |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 501 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 502 | opt |= OPT_NO_EMPTY; |
| 503 | n = 0; |
| 504 | n_chars = 0; |
| 505 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT |
| 506 | for (cur = list; cur;) { |
| 507 | n_chars += cur->lenght; |
| 508 | n++; |
| 509 | cur = cur->link; |
| 510 | if (n_chars > n_max_chars || (n == n_max_arg && cur)) { |
| 511 | if (opt & OPT_TERMINATE) |
| 512 | bb_error_msg_and_die("argument list too long"); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | #else |
| 517 | for (cur = list; cur; cur = cur->link) { |
| 518 | n_chars += cur->lenght; |
| 519 | n++; |
| 520 | if (n_chars > n_max_chars || n == n_max_arg) { |
| 521 | break; |
| 522 | } |
| 523 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 524 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 525 | |
| 526 | /* allocating pointers for execvp: |
| 527 | a*arg, n*arg from stdin, NULL */ |
| 528 | args = xcalloc(n + a + 1, sizeof(char *)); |
| 529 | |
| 530 | /* Store the command to be executed |
| 531 | (taken from the command line) */ |
| 532 | for (i = 0; i < a; i++) |
| 533 | args[i] = argv[i]; |
| 534 | /* (taken from stdin) */ |
| 535 | for (cur = list; n; cur = cur->link) { |
| 536 | args[i++] = cur->data; |
| 537 | n--; |
| 538 | } |
| 539 | |
| 540 | if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) { |
| 541 | for (i = 0; args[i]; i++) { |
| 542 | if (i) |
| 543 | fputc(' ', stderr); |
| 544 | fputs(args[i], stderr); |
| 545 | } |
| 546 | if ((opt & OPT_INTERACTIVE) == 0) |
| 547 | fputc('\n', stderr); |
| 548 | } |
| 549 | if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) { |
| 550 | child_error = xargs_exec(args); |
| 551 | } |
| 552 | |
| 553 | /* clean up */ |
| 554 | for (i = a; args[i]; i++) { |
| 555 | cur = list; |
| 556 | list = list->link; |
| 557 | free(cur); |
| 558 | } |
| 559 | free(args); |
| 560 | if (child_error > 0 && child_error != 123) { |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 565 | free(max_chars); |
| 566 | #endif |
| 567 | return child_error; |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
| 571 | #ifdef TEST |
| 572 | |
| 573 | const char *bb_applet_name = "debug stuff usage"; |
| 574 | |
| 575 | void bb_show_usage(void) |
| 576 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 577 | fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 578 | bb_applet_name); |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 579 | exit(1); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | int main(int argc, char **argv) |
| 583 | { |
Glenn L McGrath | 09c295a | 2003-10-30 22:47:16 +0000 | [diff] [blame] | 584 | return xargs_main(argc, argv); |
Glenn L McGrath | 6179694 | 2003-10-10 12:10:18 +0000 | [diff] [blame] | 585 | } |
Eric Andersen | 252183e | 2003-10-31 08:19:44 +0000 | [diff] [blame] | 586 | #endif /* TEST */ |