blob: 471bae45be75e15f388488294c4e290065b27c0c [file] [log] [blame]
Glenn L McGrath60281112001-11-02 11:39:46 +00001/*
Glenn L McGrath59870e82002-11-10 21:52:59 +00002 * Mini xargs implementation for busybox
Glenn L McGrath60281112001-11-02 11:39:46 +00003 *
Glenn L McGrath59870e82002-11-10 21:52:59 +00004 * 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>
Glenn L McGrath60281112001-11-02 11:39:46 +00007 *
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 *
Glenn L McGrath60281112001-11-02 11:39:46 +000022 */
23
Glenn L McGrath59870e82002-11-10 21:52:59 +000024#include <stdio.h>
Glenn L McGrath60281112001-11-02 11:39:46 +000025#include <stdlib.h>
Glenn L McGrath59870e82002-11-10 21:52:59 +000026#include <string.h>
Glenn L McGrath60281112001-11-02 11:39:46 +000027#include "busybox.h"
28
Glenn L McGrath59870e82002-11-10 21:52:59 +000029int xargs_main(int argc, char **argv)
Glenn L McGrath60281112001-11-02 11:39:46 +000030{
Glenn L McGrath59870e82002-11-10 21:52:59 +000031 char *cmd_to_be_executed;
32 char *file_to_act_on;
33 int i;
34 int len;
Glenn L McGrath60281112001-11-02 11:39:46 +000035
36 /*
Glenn L McGrath59870e82002-11-10 21:52:59 +000037 * No options are supported in this version of xargs; no getopt.
Glenn L McGrath60281112001-11-02 11:39:46 +000038 *
Glenn L McGrath59870e82002-11-10 21:52:59 +000039 * 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.
Glenn L McGrath60281112001-11-02 11:39:46 +000045 */
Glenn L McGrath60281112001-11-02 11:39:46 +000046
Glenn L McGrath59870e82002-11-10 21:52:59 +000047 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--;
Glenn L McGrath60281112001-11-02 11:39:46 +000056 }
Glenn L McGrath59870e82002-11-10 21:52:59 +000057 /* 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]);
Glenn L McGrath60281112001-11-02 11:39:46 +000063 }
Glenn L McGrath60281112001-11-02 11:39:46 +000064
Glenn L McGrath59870e82002-11-10 21:52:59 +000065 /* 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) {
Glenn L McGrath60281112001-11-02 11:39:46 +000068
Glenn L McGrath59870e82002-11-10 21:52:59 +000069 FILE *cmd_output;
70 char *output_line;
71 char *execstr;
Glenn L McGrath60281112001-11-02 11:39:46 +000072
Glenn L McGrath59870e82002-11-10 21:52:59 +000073 /* eat the newline off the filename. */
74 chomp(file_to_act_on);
Glenn L McGrath60281112001-11-02 11:39:46 +000075
Glenn L McGrath59870e82002-11-10 21:52:59 +000076 /* 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);
Glenn L McGrath60281112001-11-02 11:39:46 +000091 }
92
Glenn L McGrath59870e82002-11-10 21:52:59 +000093 /* clean up */
94 pclose(cmd_output);
95 free(execstr);
96 free(file_to_act_on);
Glenn L McGrath60281112001-11-02 11:39:46 +000097 }
98
Glenn L McGrath59870e82002-11-10 21:52:59 +000099#ifdef CONFIG_FEATURE_CLEAN_UP
100 free(cmd_to_be_executed);
101#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000102
Glenn L McGrath59870e82002-11-10 21:52:59 +0000103 return 0;
Glenn L McGrath60281112001-11-02 11:39:46 +0000104}
105
Glenn L McGrath59870e82002-11-10 21:52:59 +0000106/* vi: set sw=4 ts=4: */