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
31 #include <sys/types.h>
35 int xargs_main(int argc, char **argv)
37 char *in_from_stdin = NULL;
39 char *cmd_to_be_executed = NULL;
41 int len_args=0, len_cmd_to_be_executed, opt;
45 /* Note that we do not use getopt here, since
46 * we only want to interpret initial options,
47 * not options passed to commands */
48 while (--argc && **(++argv) == '-') {
55 fatalError(xargs_usage);
60 /* Store the command to be executed (taken from the command line) */
62 len_cmd_to_be_executed=6;
63 cmd_to_be_executed = xmalloc(len_cmd_to_be_executed);
64 strcat(cmd_to_be_executed, "echo");
67 len_cmd_to_be_executed = (opt > 10)? opt : 10;
68 cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
69 strcat(cmd_to_be_executed, *argv);
72 //args=xrealloc(args, len_args);
75 /* Now, read in one line at a time from stdin, and store this
76 * line to be used later as an argument to the command */
77 in_from_stdin = get_line_from_file(stdin);
78 for (;in_from_stdin!=NULL;) {
80 opt = strlen(in_from_stdin);
82 args=xrealloc(args, len_args);
84 /* Strip out the final \n */
85 in_from_stdin[opt-1]=' ';
87 /* Replace any tabs with spaces */
88 while( (tmp = strchr(in_from_stdin, '\t')) != NULL )
91 /* Strip out any extra intra-word spaces */
92 while( (tmp = strstr(in_from_stdin, " ")) != NULL ) {
93 opt = strlen(in_from_stdin);
94 memmove(tmp, tmp+1, opt-(tmp-in_from_stdin));
97 /* trim trailing whitespace */
98 opt = strlen(in_from_stdin) - 1;
99 while (isspace(in_from_stdin[opt]))
101 in_from_stdin[++opt] = 0;
103 /* Strip out any leading whitespace */
112 in_from_stdin = get_line_from_file(stdin);
115 if ((pid = fork()) == 0) {
120 fprintf(stderr, "%s ", cmd_to_be_executed);
122 cmd[0] = cmd_to_be_executed;
123 while (--argc && ++argv && *argv ) {
125 fprintf(stderr, "%s ", *argv);
130 fprintf(stderr, "%s\n", args);
134 execvp(cmd_to_be_executed, cmd);
136 /* What? Still here? Exit with an error */
137 fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno));
139 /* Wait for a child process to exit */
140 wpid = wait(&status);
143 #ifdef BB_FEATURE_CLEAN_UP
145 free(cmd_to_be_executed);
149 return (WEXITSTATUS(status));
155 c-file-style: "linux"