1 /* vi: set sw=4 ts=4: */
3 * Mini xargs implementation for busybox
5 * Copyright (C) 2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.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
32 /* get_sh_safe_line_from_file() - This function reads an entire line from a text file
33 * up to a newline. It returns a malloc'ed char * which must be stored and
34 * free'ed by the caller. */
35 extern char *get_sh_safe_line_from_file(FILE *file)
37 static const int GROWBY = 80; /* how large we will grow strings by */
50 /* grow the line buffer as necessary */
51 while (idx > linebufsz-4)
52 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
54 /* Remove any extra spaces */
55 if (last_ch == ' ' && ch == ' ')
58 /* Replace any tabs with spaces */
62 /* Escape any characters that are treated specially by /bin/sh */
64 if (strpbrk(tmp, "\\~`!$^&*()=|{}[];\"'<>?#") != NULL && last_ch!='\\') {
65 linebuf[idx++] = '\\';
74 if (idx == 0 && last_ch!=0)
86 int xargs_main(int argc, char **argv)
88 char *in_from_stdin = NULL;
89 char *args_from_cmdline = NULL;
90 char *cmd_to_be_executed = NULL;
92 int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
94 /* Note that we do not use getopt here, since
95 * we only want to interpret initial options,
96 * not options passed to commands */
97 while (--argc && **(++argv) == '-') {
104 fatalError(xargs_usage);
109 /* Store the command and arguments to be executed (from the command line) */
111 len_args_from_cmdline = 6;
112 args_from_cmdline = xmalloc(len_args_from_cmdline);
113 strcat(args_from_cmdline, "echo ");
116 len_args_from_cmdline = (opt > 10)? opt : 10;
117 args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
119 if (strlen(*argv) + strlen(args_from_cmdline) >
120 len_args_from_cmdline) {
121 len_args_from_cmdline += strlen(*argv);
123 xrealloc(args_from_cmdline,
124 len_args_from_cmdline+1);
126 strcat(args_from_cmdline, *argv);
127 strcat(args_from_cmdline, " ");
132 /* Set up some space for the command to be executed to be held in */
133 len_cmd_to_be_executed=10;
134 cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
135 strcpy(cmd_to_be_executed, args_from_cmdline);
137 /* Now, read in one line at a time from stdin, and run command+args on it */
138 in_from_stdin = get_sh_safe_line_from_file(stdin);
139 for (;in_from_stdin!=NULL;) {
141 opt = strlen(in_from_stdin);
142 len = opt + len_args_from_cmdline;
143 len_cmd_to_be_executed+=len+3;
144 cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed);
146 /* Strip out the final \n */
147 in_from_stdin[opt-1]=' ';
149 /* trim trailing whitespace */
150 opt = strlen(in_from_stdin) - 1;
151 while (isspace(in_from_stdin[opt]))
153 in_from_stdin[++opt] = 0;
155 /* Strip out any leading whitespace */
160 strcat(cmd_to_be_executed, tmp);
161 strcat(cmd_to_be_executed, " ");
164 in_from_stdin = get_sh_safe_line_from_file(stdin);
168 fputs(cmd_to_be_executed, stderr);
170 if ((system(cmd_to_be_executed) != 0) && (errno != 0))
171 fatalError("%s", strerror(errno));
174 #ifdef BB_FEATURE_CLEAN_UP
175 free(args_from_cmdline);
176 free(cmd_to_be_executed);
183 c-file-style: "linux"