Final (I think) version of xargs. Throw away all that tedious string
[oweals/busybox.git] / xargs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini xargs implementation for busybox
4  *
5  * Copyright (C) 2000 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34
35 int xargs_main(int argc, char **argv)
36 {
37         char *in_from_stdin = NULL;
38         char *args = NULL;
39         char *cmd_to_be_executed = NULL;
40         char traceflag = 0;
41         int len_args=10, len_cmd_to_be_executed, opt;
42         pid_t pid;
43         int wpid, status;
44
45         /* Note that we do not use getopt here, since
46          * we only want to interpret initial options,
47          * not options passed to commands */
48         while (--argc && **(++argv) == '-') {
49                 while (*++(*argv)) {
50                         switch (**argv) {
51                                 case 't':
52                                         traceflag=1;
53                                         break;
54                                 default:
55                                         fatalError(xargs_usage);
56                         }
57                 }
58         }
59
60         /* Store the command to be executed (taken from the command line) */
61         if (argc == 0) {
62                 len_cmd_to_be_executed=6;
63                 cmd_to_be_executed = xmalloc(len_cmd_to_be_executed);
64                 strcat(cmd_to_be_executed, "echo");
65         } else {
66                 opt=strlen(*argv);
67                 len_cmd_to_be_executed = (opt > 10)? opt : 10;
68                 cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
69                 strcat(cmd_to_be_executed, *argv);
70         }
71
72
73         /* Now, read in one line at a time from stdin, and stroe this to be used later 
74          * as an argument to the command we just stored */
75         in_from_stdin = get_line_from_file(stdin);
76         for (;in_from_stdin!=NULL;) {
77                 char *tmp;
78                 opt = strlen(in_from_stdin);
79                 len_args += opt + 3;
80                 args=xrealloc(args, len_args);
81
82                 /* Strip out the final \n */
83                 in_from_stdin[opt-1]=' ';
84
85                 /* Replace any tabs with spaces */
86                 while( (tmp = strchr(in_from_stdin, '\t')) != NULL )
87                         *tmp=' ';
88
89                 /* Strip out any extra intra-word spaces */
90                 while( (tmp = strstr(in_from_stdin, "  ")) != NULL ) {
91                         opt = strlen(in_from_stdin);
92                         memmove(tmp, tmp+1, opt-(tmp-in_from_stdin));
93                 }
94
95                 /* trim trailing whitespace */
96                 opt = strlen(in_from_stdin) - 1;
97                 while (isspace(in_from_stdin[opt]))
98                         opt--;
99                 in_from_stdin[++opt] = 0;
100
101                 /* Strip out any leading whitespace */
102                 tmp=in_from_stdin;
103                 while(isspace(*tmp))
104                         tmp++;
105
106                 strcat(args, tmp);
107                 strcat(args, " ");
108
109                 free(in_from_stdin);
110                 in_from_stdin = get_line_from_file(stdin);
111         }
112
113         if (traceflag==1) {
114                 fputs(cmd_to_be_executed, stderr);
115                 fputs(args, stderr);
116         }
117
118         if ((pid = fork()) == 0) {
119                 char *cmd[255];
120                 int i=1;
121
122                 //printf("argv[0]='%s'\n", cmd_to_be_executed);
123                 cmd[0] = cmd_to_be_executed;
124                 while (--argc && ++argv && *argv ) {
125                         //printf("argv[%d]='%s'\n", i, *argv);
126                         cmd[i++]=*argv;
127                 }
128                 //printf("argv[%d]='%s'\n", i, args);
129                 cmd[i++] = args;
130                 cmd[i] = NULL;
131                 execvp(cmd_to_be_executed, cmd);
132
133                 /* What?  Still here?  Exit with an error */
134                 fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno));
135         }
136         /* Wait for a child process to exit */
137         wpid = wait(&status);
138
139
140 #ifdef BB_FEATURE_CLEAN_UP
141         free(args);
142         free(cmd_to_be_executed);
143 #endif
144
145         if (wpid > 0)
146                 return (WEXITSTATUS(status));
147         else 
148                 return EXIT_FAILURE;
149 }
150 /*
151 Local Variables:
152 c-file-style: "linux"
153 c-basic-offset: 4
154 tab-width: 4
155 End:
156 */