last_patch95 from vodz:
[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                                 bb_perror_msg_and_die("%s", args[0]);
57                         }
58                 }
59         } else {
60                 bb_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;            /* verbose |& interactive */
70         char flg_no_empty;
71
72         bb_opt_complementaly = "pt";
73         a = bb_getopt_ulflags(argc, argv, "tpr");
74         flg_vi = a & 3;
75         flg_no_empty = a & 4;
76
77         a = argc - optind;
78         argv += optind;
79         if(a==0) {
80                 /* default behavior is to echo all the filenames */
81                 *argv = "/bin/echo";
82                 a++;
83         }
84         /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
85         args = xcalloc(a + 3, sizeof(char *));
86
87         /* Store the command to be executed (taken from the command line) */
88         for (i = 0; i < a; i++)
89                 args[i] = *argv++;
90
91         /* Now, read in one line at a time from stdin, and store this 
92          * line to be used later as an argument to the command */
93         while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
94                 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
95                         args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
96                         if(flg_vi) {
97                                 for(i=0; args[i]; i++) {
98                                         if(i)
99                                                 fputc(' ', stderr);
100                                         fputs(args[i], stderr);
101                                 }
102                                 fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr);
103                         }
104                         if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
105                                 xargs_exec(args);
106                         }
107                 }
108                 /* clean up */
109                 free(file_to_act_on);
110         }
111 #ifdef CONFIG_FEATURE_CLEAN_UP
112         free(args);
113 #endif
114         return 0;
115 }