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 * p option doesnt accept user input, should read input from /dev/tty
32 * E option doesnt allow spaces before argument
34 * xargs should terminate if an invocation of a constructed command line
35 * returns an exit status of 255.
37 * exit value of isnt correct
39 * doesnt print quoted string properly
49 #include <sys/types.h>
54 This function have special algorithm.
55 Don`t use fork and include to main!
57 static void xargs_exec(char * const * args)
60 int common[4]; /* shared vfork stack */
63 if ((p = vfork()) >= 0) {
66 execvp(args[0], args);
67 common[0] = errno; /* set error to shared stack */
74 bb_perror_msg_and_die("%s", args[0]);
78 bb_perror_msg_and_die("vfork");
82 #define OPT_VERBOSE 0x2
83 #define OPT_INTERACTIVE 0x4
84 #define OPT_TERMINATE 0x8
85 #define OPT_UPTO_NUMBER 0x10
86 #define OPT_UPTO_SIZE 0x20
87 #define OPT_EOF_STRING 0x40
89 int xargs_main(int argc, char **argv)
91 char *s_max_args = NULL;
92 char *s_line_size = NULL;
95 char *eof_string = "_";
96 int line_size = LINE_MAX;
97 unsigned int max_args = LINE_MAX / 2;
99 char *line_buffer = NULL;
100 char *line_buffer_ptr_ptr;
101 char *old_arg = NULL;
104 char *args_entry_ptr;
110 bb_opt_complementaly = "pt";
112 flg = bb_getopt_ulflags(argc, argv, "+tpxn:s:E::", &s_max_args, &s_line_size, &eof_string);
115 max_args = bb_xgetularg10(s_max_args);
118 line_size = bb_xgetularg10(s_line_size);
124 /* default behavior is to echo all the filenames */
128 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
129 args = xcalloc(a + 2, sizeof(char *));
131 /* Store the command to be executed (taken from the command line) */
132 for (i = 0; i < a; i++) {
133 line_size -= strlen(*argv) + 1;
137 bb_error_msg_and_die("can not fit single argument within argument list size limit");
140 args[i] = xmalloc(line_size);
141 args_entry_ptr = args[i];
143 /* Now, read in one line at a time from stdin, and store this
144 * line to be used later as an argument to the command */
146 char *line_buffer_ptr = NULL;
147 unsigned int arg_count = 0;
148 unsigned int arg_size = 0;
150 *args_entry_ptr = '\0';
152 /* Get the required number of entries from stdin */
154 /* (Re)fill the line buffer */
155 if (line_buffer == NULL) {
156 line_buffer = bb_get_chomped_line_from_file(stdin);
157 if (line_buffer == NULL) {
158 /* EOF, exit outer loop */
161 line_buffer_ptr = strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
164 line_buffer_ptr = old_arg;
167 line_buffer_ptr = strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
170 /* If no arguments left go back and get another line */
171 if (line_buffer_ptr == NULL) {
177 if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
178 /* logical EOF, exit outer loop */
183 /* Check the next argument will fit */
184 arg_size += 1 + strlen(line_buffer_ptr);
185 if (arg_size > line_size) {
186 if ((arg_count == 0) || ((flg & OPT_TERMINATE) && (arg_count != max_args))){
187 bb_error_msg_and_die("argument line too long");
189 old_arg = line_buffer_ptr;
193 /* Add the entry to our pre-allocated space */
194 strcat(args_entry_ptr, line_buffer_ptr);
195 strcat(args_entry_ptr, " ");
197 } while (arg_count < max_args);
199 if (*args_entry_ptr != '\0') {
200 if(flg & (OPT_VERBOSE | OPT_INTERACTIVE)) {
201 for(i=0; args[i]; i++) {
204 fputs(args[i], stderr);
207 fputs(((flg & OPT_INTERACTIVE) ? " ?..." : "\n"), stderr);
210 if((flg & OPT_INTERACTIVE) == 0 || bb_ask_confirmation() != 0 ) {
214 } while (line_buffer);
216 #ifdef CONFIG_FEATURE_CLEAN_UP