Add the x, n, s and E options, remove -r as its expected behaviour.
[oweals/busybox.git] / findutils / xargs.c
1 /*
2  * Mini xargs implementation for busybox
3  * Only "-prt" options are supported in this version of xargs.
4  *
5  * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
6  * (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
7  *
8  * Special thanks Mark Whitley for stimul to rewrote :)
9  *
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.
14  *
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.
19  *
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  *
24  *
25  * Reference:
26  *      http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
27  *
28  *
29  * BUGS:
30  *      p option doesnt accept user input, should read input from /dev/tty
31  *
32  *      E option doesnt allow spaces before argument
33  *
34  *      xargs should terminate if an invocation of a constructed command line
35  *  returns an exit status of 255.
36  *
37  *  exit value of isnt correct 
38  *
39  *  doesnt print quoted string properly
40  *
41  */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <getopt.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include "busybox.h"
52
53 /*
54    This function have special algorithm.
55    Don`t use fork and include to main!
56 */
57 static void xargs_exec(char * const * args)
58 {
59         int p;
60         int common[4];  /* shared vfork stack */
61
62         common[0] = 0;
63         if ((p = vfork()) >= 0) {
64                 if (p == 0) {
65                         /* vfork -- child */
66                         execvp(args[0], args);
67                         common[0] = errno; /* set error to shared stack */
68                         _exit(1);
69                 } else {
70                         /* vfork -- parent */
71                         wait(NULL);
72                         if(common[0]) {
73                                 errno = common[0];
74                                 bb_perror_msg_and_die("%s", args[0]);
75                         }
76                 }
77         } else {
78                 bb_perror_msg_and_die("vfork");
79         }
80 }
81
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
88
89 int xargs_main(int argc, char **argv)
90 {
91         char *s_max_args = NULL;
92         char *s_line_size = NULL;
93         unsigned long flg;
94
95         char *eof_string = "_";
96         int line_size = LINE_MAX;
97         unsigned int max_args = LINE_MAX / 2;
98
99         char *line_buffer = NULL;
100         char *line_buffer_ptr_ptr;
101         char *old_arg = NULL;
102
103         char **args;
104         char *args_entry_ptr;
105
106         int i;
107         int a;
108
109         
110         bb_opt_complementaly = "pt";
111
112         flg = bb_getopt_ulflags(argc, argv, "+tpxn:s:E::", &s_max_args, &s_line_size, &eof_string);
113
114         if (s_max_args) {
115                 max_args = bb_xgetularg10(s_max_args);
116         }
117         if (s_line_size) {
118                 line_size = bb_xgetularg10(s_line_size);
119         }
120
121         a = argc - optind;
122         argv += optind;
123         if(a==0) {
124                 /* default behavior is to echo all the filenames */
125                 *argv = "echo";
126                 a++;
127         }
128         /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
129         args = xcalloc(a + 2, sizeof(char *));
130
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;
134                 args[i] = *argv++;
135         }
136         if (line_size < 1) {
137                 bb_error_msg_and_die("can not fit single argument within argument list size limit");
138         }
139
140         args[i] = xmalloc(line_size);
141         args_entry_ptr = args[i];
142
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 */
145         do {
146                 char *line_buffer_ptr = NULL;
147                 unsigned int arg_count = 0;
148                 unsigned int arg_size = 0;
149
150                 *args_entry_ptr = '\0';
151
152                 /* Get the required number of entries from stdin */
153                 do {
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 */
159                                         break;
160                                 }
161                                 line_buffer_ptr = strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
162                         } else {
163                                 if (old_arg) {
164                                         line_buffer_ptr = old_arg;
165                                         old_arg = NULL;
166                                 } else {
167                                         line_buffer_ptr = strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
168                                 }
169                         }
170                         /* If no arguments left go back and get another line */
171                         if (line_buffer_ptr == NULL) {
172                                 free(line_buffer);
173                                 line_buffer = NULL;
174                                 continue;
175                         }
176
177                         if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
178                                 /* logical EOF, exit outer loop */
179                                 line_buffer = NULL;
180                                 break;
181                         }
182
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");
188                                 }
189                                 old_arg = line_buffer_ptr;
190                                 break;
191                         }
192
193                         /* Add the entry to our pre-allocated space */
194                         strcat(args_entry_ptr, line_buffer_ptr);
195                         strcat(args_entry_ptr, " ");
196                         arg_count++;
197                 } while (arg_count < max_args);
198
199                 if (*args_entry_ptr != '\0') {
200                         if(flg & (OPT_VERBOSE | OPT_INTERACTIVE)) {
201                                 for(i=0; args[i]; i++) {
202                                         if(i)
203                                                 fputc(' ', stderr);
204                                         fputs(args[i], stderr);
205                                 }
206
207                                 fputs(((flg & OPT_INTERACTIVE) ? " ?..." : "\n"), stderr);
208                         }
209
210                         if((flg & OPT_INTERACTIVE) == 0 || bb_ask_confirmation() != 0 ) {
211                                 xargs_exec(args);
212                         }
213                 }
214         } while (line_buffer);
215
216 #ifdef CONFIG_FEATURE_CLEAN_UP
217         free(args);
218 #endif
219         return 0;
220 }