Remove gratuitous and unnecessary "BusyBox" refernece in login prompt
[oweals/busybox.git] / findutils / xargs.c
1 /*
2  * Mini xargs implementation for busybox
3  *
4  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
5  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
6  * Remixed by Mark Whitley <markw@codepoet.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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "busybox.h"
28
29 int xargs_main(int argc, char **argv)
30 {
31         char *cmd_to_be_executed;
32         char *file_to_act_on;
33         int i;
34         int len;
35
36         /*
37          * No options are supported in this version of xargs; no getopt.
38          *
39          * Re: The missing -t flag: Most programs that produce output also print
40          * the filename, so xargs doesn't really need to do it again. Supporting
41          * the -t flag =greatly= bloats up the size of this app and the memory it
42          * uses because you have to buffer all the input file strings in memory. If
43          * you really want to see the filenames that xargs will act on, just run it
44          * once with no args and xargs will echo the filename. Simple.
45          */
46
47         argv++;
48         len = argc;     /* arg = count for ' ' + trailing '\0' */
49         /* Store the command to be executed (taken from the command line) */
50         if (argc == 1) {
51                 /* default behavior is to echo all the filenames */
52                 argv[0] = "/bin/echo";
53                 len++;  /* space for trailing '\0' */
54         } else {
55                 argc--;
56                 }
57         /* concatenate all the arguments passed to xargs together */
58         for (i = 0; i < argc; i++)
59                 len += strlen(argv[i]);
60         cmd_to_be_executed = xmalloc (len);
61         for (i = len = 0; i < argc; i++) {
62                 len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]);
63         }
64
65         /* Now, read in one line at a time from stdin, and store this 
66          * line to be used later as an argument to the command */
67         while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
68
69                 FILE *cmd_output;
70                 char *output_line;
71                 char *execstr;
72
73                 /* eat the newline off the filename. */
74                 chomp(file_to_act_on);
75
76                 /* eat blank lines */
77                 if (file_to_act_on[0] == 0)
78                         continue;
79
80                 /* assemble the command and execute it */
81                 bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on);
82                 
83                 cmd_output = popen(execstr, "r");
84                 if (cmd_output == NULL)
85                         perror_msg_and_die("popen");
86
87                 /* harvest the output */
88                 while ((output_line = get_line_from_file(cmd_output)) != NULL) {
89                         fputs(output_line, stdout);
90                         free(output_line);
91                 }
92
93                 /* clean up */
94                 pclose(cmd_output);
95                 free(execstr);
96                 free(file_to_act_on);
97         }
98
99 #ifdef CONFIG_FEATURE_CLEAN_UP
100         free(cmd_to_be_executed);
101 #endif
102
103         return 0;
104 }
105
106 /* vi: set sw=4 ts=4: */