2 * Mini xargs implementation for busybox
3 * Only "-prt" options are supported in this version of xargs.
5 * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
6 * (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
8 * Special thanks Mark Whitley for stimul to rewrote :)
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.
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.
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
26 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
30 * - E option doesnt allow spaces before argument
31 * - exit value of isnt correct
32 * - doesnt print quoted string properly
41 #include <sys/types.h>
47 This function have special algorithm.
48 Don`t use fork and include to main!
50 static int xargs_exec(char **args)
53 volatile int exec_errno; /* shared vfork stack */
58 bb_perror_msg_and_die("vfork");
61 execvp(args[0], args);
62 exec_errno = errno; /* set error to shared stack */
68 while (wait(&status) == (pid_t) - 1) {
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]);
81 } else if (WIFSTOPPED(status)) {
82 bb_error_msg("%s: stopped by signal %d", args[0],
85 } else if (WIFSIGNALED(status)) {
86 bb_error_msg("%s: terminated by signal %d", args[0],
89 } else if (WEXITSTATUS(status)) {
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
103 int xargs_main(int argc, char **argv)
105 #ifdef CONFIG_FEATURE_XARGS_FANCY
106 char *s_max_args = NULL;
107 char *s_line_size = NULL;
109 char *eof_string = "_";
113 unsigned int max_args = LINE_MAX;
115 char *line_buffer = NULL;
116 char *line_buffer_ptr_ptr;
117 char *old_arg = NULL;
120 char *args_entry_ptr;
126 /* Default to maximum line length */
127 if (LINE_MAX > ARG_MAX - 2048) {
128 /* Minimum command line length */
129 line_size = LINE_MAX;
131 /* Maximum command line length */
132 line_size = ARG_MAX = 2048;
135 line_size = LINE_MAX;
138 #ifndef CONFIG_FEATURE_XARGS_FANCY
139 flg = bb_getopt_ulflags(argc, argv, "+t");
141 flg = bb_getopt_ulflags(argc, argv, "+txn:s:E::", &s_max_args, &s_line_size, &eof_string);
144 line_size = bb_xgetularg10_bnd(s_max_args, 1, line_size);
147 max_args = bb_xgetularg10(s_max_args);
154 /* default behavior is to echo all the filenames */
158 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
159 args = xcalloc(a + 2, sizeof(char *));
161 /* Store the command to be executed (taken from the command line) */
162 for (i = 0; i < a; i++) {
163 line_size -= strlen(*argv) + 1;
167 bb_error_msg_and_die("can not fit single argument within argument list size limit");
170 args[i] = xmalloc(line_size);
171 args_entry_ptr = args[i];
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 */
176 char *line_buffer_ptr = NULL;
177 unsigned int arg_count = 0;
178 unsigned int arg_size = 0;
180 *args_entry_ptr = '\0';
182 /* Get the required number of entries from stdin */
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 */
192 strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
195 line_buffer_ptr = old_arg;
199 strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
202 /* If no arguments left go back and get another line */
203 if (line_buffer_ptr == NULL) {
209 #ifdef CONFIG_FEATURE_XARGS_FANCY
210 if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
212 if (strcmp(line_buffer_ptr, eof_string) == 0) {
214 /* logical EOF, exit outer loop */
219 /* Check the next argument will fit */
220 arg_size += 1 + strlen(line_buffer_ptr);
221 if (arg_size > line_size) {
223 #ifdef CONFIG_FEATURE_XARGS_FANCY
224 || ((flg & OPT_TERMINATE) && (arg_count != max_args))) {
228 bb_error_msg_and_die("argument line too long");
230 old_arg = line_buffer_ptr;
234 /* Add the entry to our pre-allocated space */
235 strcat(args_entry_ptr, line_buffer_ptr);
236 strcat(args_entry_ptr, " ");
238 } while (arg_count < max_args);
240 /* Remove the last space */
241 args_entry_ptr[arg_size - 1] = '\0';
243 if (*args_entry_ptr != '\0') {
244 if (flg & OPT_VERBOSE) {
245 for (i = 0; args[i]; i++) {
249 fputs(args[i], stderr);
255 } while (line_buffer);
257 #ifdef CONFIG_FEATURE_CLEAN_UP