include busybox after libc includes
[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  *
7  * Special thanks Mark Whitley for stimul to rewrote :)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include "busybox.h"
33
34
35 /*
36    This function have special algorithm.
37    Don`t use fork and include to main!
38 */
39 static void xargs_exec(char * const * args)
40 {
41         int p;
42         int common[4];  /* shared vfork stack */
43
44         common[0] = 0;
45         if ((p = vfork()) >= 0) {
46                 if (p == 0) {
47                         /* vfork -- child */
48                         execvp(args[0], args);
49                         common[0] = errno; /* set error to shared stack */
50                         _exit(1);
51                 } else {
52                         /* vfork -- parent */
53                         wait(NULL);
54                         if(common[0]) {
55                                 errno = common[0];
56                                 perror_msg_and_die("%s", args[0]);
57                         }
58                 }
59         } else {
60                 perror_msg_and_die("vfork");
61         }
62 }
63
64 int xargs_main(int argc, char **argv)
65 {
66         char *file_to_act_on;
67         char **args;
68         int  i, a;
69         char flg_vi       = 0;    /* verbose |& interactive */
70         char flg_no_empty = 0;
71
72         while ((a = getopt(argc, argv, "prt")) > 0) {
73                 switch(a) {
74                         case 'p':
75                                 flg_vi |= 3;
76                                 break;
77                         case 't':
78                                 flg_vi |= 1;
79                                 break;
80                         case 'r':
81                                 flg_no_empty = 1;
82                                 break;
83                         default:
84                                 show_usage();
85                 }
86         }
87
88         a = argc - optind;
89         argv += optind;
90         if(a==0) {
91                 /* default behavior is to echo all the filenames */
92                 *argv = "/bin/echo";
93                 a++;
94         }
95         /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
96         args = xcalloc(a + 3, sizeof(char *));
97
98         /* Store the command to be executed (taken from the command line) */
99         for (i = 0; i < a; i++)
100                 args[i] = *argv++;
101
102         /* Now, read in one line at a time from stdin, and store this 
103          * line to be used later as an argument to the command */
104         while ((file_to_act_on = get_line_from_file(stdin)) != NULL) {
105                 /* eat the newline off the filename. */
106                 chomp(file_to_act_on);
107                 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
108                         args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
109                         if(flg_vi) {
110                                 for(i=0; args[i]; i++) {
111                                         if(i)
112                                                 fputc(' ', stderr);
113                                         fputs(args[i], stderr);
114                                 }
115                                 fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n"));
116                         }
117                         if((flg_vi & 2) == 0 || ask_confirmation() != 0 ) {
118                                 xargs_exec(args);
119                         }
120                 }
121                 /* clean up */
122                 free(file_to_act_on);
123         }
124 #ifdef CONFIG_FEATURE_CLEAN_UP
125         free(args);
126 #endif
127         return 0;
128 }