2 * Mini xargs implementation for busybox
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>
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.
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.
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
29 int xargs_main(int argc, char **argv)
31 char *cmd_to_be_executed;
37 * No options are supported in this version of xargs; no getopt.
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.
48 len = argc; /* arg = count for ' ' + trailing '\0' */
49 /* Store the command to be executed (taken from the command line) */
51 /* default behavior is to echo all the filenames */
52 argv[0] = "/bin/echo";
53 len++; /* space for trailing '\0' */
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]);
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) {
73 /* eat the newline off the filename. */
74 chomp(file_to_act_on);
77 if (file_to_act_on[0] == 0)
80 /* assemble the command and execute it */
81 bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on);
83 cmd_output = popen(execstr, "r");
84 if (cmd_output == NULL)
85 perror_msg_and_die("popen");
87 /* harvest the output */
88 while ((output_line = get_line_from_file(cmd_output)) != NULL) {
89 fputs(output_line, stdout);
99 #ifdef CONFIG_FEATURE_CLEAN_UP
100 free(cmd_to_be_executed);
106 /* vi: set sw=4 ts=4: */