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