blob: a6f8058fc2997dcdad8a55b6be8432e4bfdfa416 [file] [log] [blame]
Eric Andersena37d5b72000-09-23 06:10:14 +00001/* vi: set sw=4 ts=4: */
Eric Andersen5b176932000-09-22 20:22:28 +00002/*
Eric Andersena37d5b72000-09-23 06:10:14 +00003 * Mini xargs implementation for busybox
Eric Andersen5b176932000-09-22 20:22:28 +00004 *
Eric Andersena37d5b72000-09-23 06:10:14 +00005 * Copyright (C) 2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersen5b176932000-09-22 20:22:28 +00007 *
Eric Andersena37d5b72000-09-23 06:10:14 +00008 * 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.
Eric Andersen5b176932000-09-22 20:22:28 +000012 *
Eric Andersena37d5b72000-09-23 06:10:14 +000013 * 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.
Eric Andersen5b176932000-09-22 20:22:28 +000017 *
Eric Andersena37d5b72000-09-23 06:10:14 +000018 * 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 *
Eric Andersen5b176932000-09-22 20:22:28 +000022 */
Eric Andersen92a61c12000-09-22 20:01:23 +000023
Eric Andersena37d5b72000-09-23 06:10:14 +000024#include "internal.h"
Eric Andersen5b176932000-09-22 20:22:28 +000025#include <stdlib.h>
Eric Andersen5b176932000-09-22 20:22:28 +000026#include <stdio.h>
Eric Andersena37d5b72000-09-23 06:10:14 +000027#include <string.h>
28#include <errno.h>
Eric Andersen92a61c12000-09-22 20:01:23 +000029#include <getopt.h>
30
Eric Andersen92a61c12000-09-22 20:01:23 +000031
Eric Andersena37d5b72000-09-23 06:10:14 +000032int xargs_main(int argc, char **argv)
Eric Andersen92a61c12000-09-22 20:01:23 +000033{
Eric Andersena37d5b72000-09-23 06:10:14 +000034 char *in_from_stdin = NULL;
35 char *args_from_cmdline = NULL;
36 char *cmd_to_be_executed = NULL;
37 char traceflag = 0;
38 int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
Eric Andersen92a61c12000-09-22 20:01:23 +000039
Eric Andersen9ae38382000-09-24 01:12:54 +000040 /* Note that we do not use getopt here, since
41 * we only want to interpret initial options,
42 * not options passed to commands */
43 while (--argc && **(++argv) == '-') {
44 while (*++(*argv)) {
45 switch (**argv) {
46 case 't':
47 traceflag=1;
48 break;
49 default:
50 fatalError(xargs_usage);
51 }
Eric Andersena37d5b72000-09-23 06:10:14 +000052 }
53 }
Eric Andersen92a61c12000-09-22 20:01:23 +000054
Eric Andersena37d5b72000-09-23 06:10:14 +000055 /* Store the command and arguments to be executed (from the command line) */
Eric Andersen9ae38382000-09-24 01:12:54 +000056 if (argc == 1) {
Eric Andersena37d5b72000-09-23 06:10:14 +000057 len_args_from_cmdline = 6;
58 args_from_cmdline = xmalloc(len_args_from_cmdline);
59 strcat(args_from_cmdline, "echo ");
60 } else {
Eric Andersen9ae38382000-09-24 01:12:54 +000061 opt=strlen(*argv);
Eric Andersena37d5b72000-09-23 06:10:14 +000062 len_args_from_cmdline = (opt > 10)? opt : 10;
63 args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
Eric Andersen9ae38382000-09-24 01:12:54 +000064 while (argc-- > 0) {
65 if (strlen(*argv) + strlen(args_from_cmdline) >
Eric Andersena37d5b72000-09-23 06:10:14 +000066 len_args_from_cmdline) {
Eric Andersen9ae38382000-09-24 01:12:54 +000067 len_args_from_cmdline += strlen(*argv);
Eric Andersena37d5b72000-09-23 06:10:14 +000068 args_from_cmdline =
69 xrealloc(args_from_cmdline,
70 len_args_from_cmdline+1);
71 }
Eric Andersen9ae38382000-09-24 01:12:54 +000072 strcat(args_from_cmdline, *argv);
Eric Andersena37d5b72000-09-23 06:10:14 +000073 strcat(args_from_cmdline, " ");
74 }
75 }
Eric Andersen92a61c12000-09-22 20:01:23 +000076
Eric Andersena37d5b72000-09-23 06:10:14 +000077 /* Set up some space for the command to be executed to be held in */
78 len_cmd_to_be_executed=10;
79 cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
80 strcpy(cmd_to_be_executed, args_from_cmdline);
81 strcat(cmd_to_be_executed, " ");
Eric Andersen92a61c12000-09-22 20:01:23 +000082
Eric Andersena37d5b72000-09-23 06:10:14 +000083 /* Now, read in one line at a time from stdin, and run command+args on it */
84 in_from_stdin = get_line_from_file(stdin);
85 for (;in_from_stdin!=NULL;) {
Eric Andersen96bdde92000-09-23 19:53:31 +000086 char *tmp;
Eric Andersena37d5b72000-09-23 06:10:14 +000087 len = strlen(in_from_stdin) + len_args_from_cmdline;
Eric Andersen96bdde92000-09-23 19:53:31 +000088 len_cmd_to_be_executed+=len+3;
89 cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed);
90
91 /* Strip out any \n's, so we just get one command to run */
92 while( (tmp = strchr(in_from_stdin, '\n')) != NULL )
93 *tmp=' ';
94
Eric Andersena37d5b72000-09-23 06:10:14 +000095 strcat(cmd_to_be_executed, in_from_stdin);
Eric Andersena37d5b72000-09-23 06:10:14 +000096 strcat(cmd_to_be_executed, " ");
97
98 free(in_from_stdin);
99 in_from_stdin = get_line_from_file(stdin);
100 }
101
102 if (traceflag==1)
103 fputs(cmd_to_be_executed, stderr);
104
105 if ((system(cmd_to_be_executed) != 0) && (errno != 0))
106 fatalError("%s", strerror(errno));
107
108
109#ifdef BB_FEATURE_CLEAN_UP
110 free(args_from_cmdline);
111 free(cmd_to_be_executed);
112#endif
113
114 return 0;
Eric Andersen92a61c12000-09-22 20:01:23 +0000115}
Eric Andersena37d5b72000-09-23 06:10:14 +0000116/*
117Local Variables:
118c-file-style: "linux"
119c-basic-offset: 4
120tab-width: 4
121End:
122*/
123