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 | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 3 | * Only "-prt" options are supported in this version of xargs. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 5 | * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 6 | * (C) 2003 by Glenn McGrath <bug1@optushome.com.au> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 7 | * |
| 8 | * Special thanks Mark Whitley for stimul to rewrote :) |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 9 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 14 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 19 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 24 | * |
| 25 | * Reference: |
| 26 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html |
| 27 | * |
| 28 | * |
| 29 | * BUGS: |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 30 | * - E option doesnt allow spaces before argument |
| 31 | * - exit value of isnt correct |
| 32 | * - doesnt print quoted string properly |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 33 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 34 | |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 37 | #include <string.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
| 39 | #include <getopt.h> |
| 40 | #include <errno.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 43 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 44 | #include "busybox.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 45 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 46 | /* |
| 47 | This function have special algorithm. |
| 48 | Don`t use fork and include to main! |
| 49 | */ |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 50 | static int xargs_exec(char **args) |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 51 | { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 52 | pid_t p; |
| 53 | volatile int exec_errno; /* shared vfork stack */ |
| 54 | |
| 55 | exec_errno = 0; |
| 56 | p = vfork(); |
| 57 | if (p < 0) { |
| 58 | bb_perror_msg_and_die("vfork"); |
| 59 | } else if (p == 0) { |
| 60 | /* vfork -- child */ |
| 61 | execvp(args[0], args); |
| 62 | exec_errno = errno; /* set error to shared stack */ |
| 63 | _exit(1); |
| 64 | } else { |
| 65 | /* vfork -- parent */ |
| 66 | int status; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 68 | while (wait(&status) == (pid_t) - 1) { |
| 69 | if (errno != EINTR) { |
| 70 | break; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 73 | |
| 74 | if (exec_errno) { |
| 75 | errno = exec_errno; |
| 76 | bb_perror_msg("%s", args[0]); |
| 77 | return exec_errno == ENOENT ? 127 : 126; |
| 78 | } else if (WEXITSTATUS(status) == 255) { |
| 79 | bb_error_msg("%s: exited with status 255; aborting", args[0]); |
| 80 | return 124; |
| 81 | } else if (WIFSTOPPED(status)) { |
| 82 | bb_error_msg("%s: stopped by signal %d", args[0], |
| 83 | WSTOPSIG(status)); |
| 84 | return 125; |
| 85 | } else if (WIFSIGNALED(status)) { |
| 86 | bb_error_msg("%s: terminated by signal %d", args[0], |
| 87 | WTERMSIG(status)); |
| 88 | return 125; |
| 89 | } else if (WEXITSTATUS(status)) { |
| 90 | return 123; |
| 91 | } else { |
| 92 | return 0; |
| 93 | } |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 97 | #define OPT_VERBOSE 0x1 |
| 98 | #define OPT_TERMINATE 0x2 |
| 99 | #define OPT_UPTO_NUMBER 0x4 |
| 100 | #define OPT_UPTO_SIZE 0x8 |
| 101 | #define OPT_EOF_STRING 0x10 |
Glenn L McGrath | 07cf926 | 2003-10-03 13:15:44 +0000 | [diff] [blame] | 102 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 103 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 104 | { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 105 | #ifdef CONFIG_FEATURE_XARGS_FANCY |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 106 | char *s_max_args = NULL; |
| 107 | char *s_line_size = NULL; |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 108 | #endif |
| 109 | char *eof_string = "_"; |
Glenn L McGrath | 07cf926 | 2003-10-03 13:15:44 +0000 | [diff] [blame] | 110 | unsigned long flg; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 111 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 112 | int line_size; |
| 113 | unsigned int max_args = LINE_MAX; |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 114 | |
| 115 | char *line_buffer = NULL; |
| 116 | char *line_buffer_ptr_ptr; |
| 117 | char *old_arg = NULL; |
| 118 | |
| 119 | char **args; |
| 120 | char *args_entry_ptr; |
| 121 | |
| 122 | int i; |
| 123 | int a; |
| 124 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 125 | #if 0 |
| 126 | /* Default to maximum line length */ |
| 127 | if (LINE_MAX > ARG_MAX - 2048) { |
| 128 | /* Minimum command line length */ |
| 129 | line_size = LINE_MAX; |
| 130 | } else { |
| 131 | /* Maximum command line length */ |
| 132 | line_size = ARG_MAX = 2048; |
| 133 | } |
| 134 | #else |
| 135 | line_size = LINE_MAX; |
| 136 | #endif |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 137 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 138 | #ifndef CONFIG_FEATURE_XARGS_FANCY |
| 139 | flg = bb_getopt_ulflags(argc, argv, "+t"); |
| 140 | #else |
| 141 | flg = bb_getopt_ulflags(argc, argv, "+txn:s:E::", &s_max_args, &s_line_size, &eof_string); |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 142 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 143 | if (s_line_size) { |
| 144 | line_size = bb_xgetularg10_bnd(s_max_args, 1, line_size); |
| 145 | } |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 146 | if (s_max_args) { |
| 147 | max_args = bb_xgetularg10(s_max_args); |
| 148 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 149 | #endif |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 150 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 151 | a = argc - optind; |
| 152 | argv += optind; |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 153 | if (a == 0) { |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 154 | /* default behavior is to echo all the filenames */ |
Glenn L McGrath | 07cf926 | 2003-10-03 13:15:44 +0000 | [diff] [blame] | 155 | *argv = "echo"; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 156 | a++; |
| 157 | } |
| 158 | /* allocating pointers for execvp: a*arg, arg from stdin, NULL */ |
Glenn L McGrath | 07cf926 | 2003-10-03 13:15:44 +0000 | [diff] [blame] | 159 | args = xcalloc(a + 2, sizeof(char *)); |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 160 | |
| 161 | /* Store the command to be executed (taken from the command line) */ |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 162 | for (i = 0; i < a; i++) { |
| 163 | line_size -= strlen(*argv) + 1; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 164 | args[i] = *argv++; |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 165 | } |
| 166 | if (line_size < 1) { |
| 167 | bb_error_msg_and_die("can not fit single argument within argument list size limit"); |
| 168 | } |
| 169 | |
| 170 | args[i] = xmalloc(line_size); |
| 171 | args_entry_ptr = args[i]; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 172 | |
Eric Andersen | d89882d | 2000-09-25 22:53:05 +0000 | [diff] [blame] | 173 | /* Now, read in one line at a time from stdin, and store this |
| 174 | * line to be used later as an argument to the command */ |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 175 | do { |
| 176 | char *line_buffer_ptr = NULL; |
| 177 | unsigned int arg_count = 0; |
| 178 | unsigned int arg_size = 0; |
| 179 | |
| 180 | *args_entry_ptr = '\0'; |
| 181 | |
| 182 | /* Get the required number of entries from stdin */ |
| 183 | do { |
| 184 | /* (Re)fill the line buffer */ |
| 185 | if (line_buffer == NULL) { |
| 186 | line_buffer = bb_get_chomped_line_from_file(stdin); |
| 187 | if (line_buffer == NULL) { |
| 188 | /* EOF, exit outer loop */ |
| 189 | break; |
| 190 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 191 | line_buffer_ptr = |
| 192 | strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr); |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 193 | } else { |
| 194 | if (old_arg) { |
| 195 | line_buffer_ptr = old_arg; |
| 196 | old_arg = NULL; |
| 197 | } else { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 198 | line_buffer_ptr = |
| 199 | strtok_r(NULL, " \t", &line_buffer_ptr_ptr); |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | /* If no arguments left go back and get another line */ |
| 203 | if (line_buffer_ptr == NULL) { |
| 204 | free(line_buffer); |
| 205 | line_buffer = NULL; |
| 206 | continue; |
| 207 | } |
| 208 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 209 | #ifdef CONFIG_FEATURE_XARGS_FANCY |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 210 | if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 211 | #else |
| 212 | if (strcmp(line_buffer_ptr, eof_string) == 0) { |
| 213 | #endif |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 214 | /* logical EOF, exit outer loop */ |
| 215 | line_buffer = NULL; |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | /* Check the next argument will fit */ |
| 220 | arg_size += 1 + strlen(line_buffer_ptr); |
| 221 | if (arg_size > line_size) { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 222 | if ((arg_count == 0) |
| 223 | #ifdef CONFIG_FEATURE_XARGS_FANCY |
| 224 | || ((flg & OPT_TERMINATE) && (arg_count != max_args))) { |
| 225 | #else |
| 226 | ) { |
| 227 | #endif |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 228 | bb_error_msg_and_die("argument line too long"); |
| 229 | } |
| 230 | old_arg = line_buffer_ptr; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | /* Add the entry to our pre-allocated space */ |
| 235 | strcat(args_entry_ptr, line_buffer_ptr); |
| 236 | strcat(args_entry_ptr, " "); |
| 237 | arg_count++; |
| 238 | } while (arg_count < max_args); |
| 239 | |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 240 | /* Remove the last space */ |
| 241 | args_entry_ptr[arg_size - 1] = '\0'; |
| 242 | |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 243 | if (*args_entry_ptr != '\0') { |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 244 | if (flg & OPT_VERBOSE) { |
| 245 | for (i = 0; args[i]; i++) { |
| 246 | if (i) { |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 247 | fputc(' ', stderr); |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 248 | } |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 249 | fputs(args[i], stderr); |
| 250 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 251 | fputc('\n', stderr); |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 252 | } |
Glenn L McGrath | 9982596 | 2003-10-09 11:06:45 +0000 | [diff] [blame^] | 253 | xargs_exec(args); |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 254 | } |
Glenn L McGrath | add3ead | 2003-10-04 14:44:27 +0000 | [diff] [blame] | 255 | } while (line_buffer); |
| 256 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 257 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 258 | free(args); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 259 | #endif |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 260 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 261 | } |