1af42384b986c7ad3cb99394b4a2d8104b91dcea
[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  *      - E option doesnt allow spaces before argument
31  *      - exit value of isnt correct 
32  *      - doesnt print quoted string properly
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <getopt.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43
44 #include "busybox.h"
45
46 /*
47    This function have special algorithm.
48    Don`t use fork and include to main!
49 */
50 static int xargs_exec(char **args)
51 {
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;
67
68                 while (wait(&status) == (pid_t) - 1) {
69                         if (errno != EINTR) {
70                                 break;
71                         }
72                 }
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                 }
94         }
95 }
96
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
102
103 int xargs_main(int argc, char **argv)
104 {
105 #ifdef CONFIG_FEATURE_XARGS_FANCY
106         char *s_max_args = NULL;
107         char *s_line_size = NULL;
108 #endif
109         char *eof_string = "_";
110         unsigned long flg;
111
112         int line_size;
113         unsigned int max_args = LINE_MAX;
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
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
137         
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);
142
143         if (s_line_size) {
144                 line_size =     bb_xgetularg10_bnd(s_max_args, 1, line_size);
145         }
146         if (s_max_args) {
147                 max_args = bb_xgetularg10(s_max_args);
148         }
149 #endif
150
151         a = argc - optind;
152         argv += optind;
153         if (a == 0) {
154                 /* default behavior is to echo all the filenames */
155                 *argv = "echo";
156                 a++;
157         }
158         /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
159         args = xcalloc(a + 2, sizeof(char *));
160
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;
164                 args[i] = *argv++;
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];
172
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 */
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                                 }
191                                 line_buffer_ptr =
192                                         strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
193                         } else {
194                                 if (old_arg) {
195                                         line_buffer_ptr = old_arg;
196                                         old_arg = NULL;
197                                 } else {
198                                         line_buffer_ptr =
199                                                 strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
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
209 #ifdef CONFIG_FEATURE_XARGS_FANCY
210                         if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
211 #else
212                         if (strcmp(line_buffer_ptr, eof_string) == 0) {
213 #endif
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) {
222                                 if ((arg_count == 0)
223 #ifdef CONFIG_FEATURE_XARGS_FANCY
224                                         || ((flg & OPT_TERMINATE) && (arg_count != max_args))) {
225 #else
226                                         ) {
227 #endif
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
240                 /* Remove the last space */
241                 args_entry_ptr[arg_size - 1] = '\0';
242
243                 if (*args_entry_ptr != '\0') {
244                         if (flg & OPT_VERBOSE) {
245                                 for (i = 0; args[i]; i++) {
246                                         if (i) {
247                                                 fputc(' ', stderr);
248                                         }
249                                         fputs(args[i], stderr);
250                                 }
251                                 fputc('\n', stderr);
252                         }
253                         xargs_exec(args);
254                 }
255         } while (line_buffer);
256
257 #ifdef CONFIG_FEATURE_CLEAN_UP
258         free(args);
259 #endif
260         return 0;
261 }