blob: 433567b7745608e6c0822e8991964acaa3d080cb [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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.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>
Eric Andersen6f283c22000-09-24 02:40:56 +000030#include <ctype.h>
Eric Andersene081eae2000-09-25 20:23:21 +000031#include <sys/types.h>
32#include <sys/wait.h>
Eric Andersenbf739092000-09-25 18:41:18 +000033
Eric Andersen92a61c12000-09-22 20:01:23 +000034
Eric Andersena37d5b72000-09-23 06:10:14 +000035int xargs_main(int argc, char **argv)
Eric Andersen92a61c12000-09-22 20:01:23 +000036{
Eric Andersena37d5b72000-09-23 06:10:14 +000037 char *in_from_stdin = NULL;
Eric Andersene081eae2000-09-25 20:23:21 +000038 char *args = NULL;
Eric Andersena37d5b72000-09-23 06:10:14 +000039 char *cmd_to_be_executed = NULL;
40 char traceflag = 0;
Eric Andersend2667482000-09-26 01:00:15 +000041 int len_args=0, len_cmd_to_be_executed, opt;
Eric Andersene081eae2000-09-25 20:23:21 +000042 pid_t pid;
43 int wpid, status;
Eric Andersen92a61c12000-09-22 20:01:23 +000044
Eric Andersen9ae38382000-09-24 01:12:54 +000045 /* 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) == '-') {
49 while (*++(*argv)) {
50 switch (**argv) {
51 case 't':
52 traceflag=1;
53 break;
54 default:
55 fatalError(xargs_usage);
Eric Andersene081eae2000-09-25 20:23:21 +000056 }
Eric Andersena37d5b72000-09-23 06:10:14 +000057 }
58 }
Eric Andersen92a61c12000-09-22 20:01:23 +000059
Eric Andersene081eae2000-09-25 20:23:21 +000060 /* Store the command to be executed (taken from the command line) */
Eric Andersenbf739092000-09-25 18:41:18 +000061 if (argc == 0) {
Eric Andersene081eae2000-09-25 20:23:21 +000062 len_cmd_to_be_executed=6;
63 cmd_to_be_executed = xmalloc(len_cmd_to_be_executed);
64 strcat(cmd_to_be_executed, "echo");
Eric Andersena37d5b72000-09-23 06:10:14 +000065 } else {
Eric Andersen9ae38382000-09-24 01:12:54 +000066 opt=strlen(*argv);
Eric Andersene081eae2000-09-25 20:23:21 +000067 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);
Eric Andersena37d5b72000-09-23 06:10:14 +000070 }
Eric Andersen92a61c12000-09-22 20:01:23 +000071
Eric Andersend2667482000-09-26 01:00:15 +000072 //args=xrealloc(args, len_args);
73// strcpy(args, " ");
Eric Andersen92a61c12000-09-22 20:01:23 +000074
Eric Andersend89882d2000-09-25 22:53:05 +000075 /* 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 */
Eric Andersene081eae2000-09-25 20:23:21 +000077 in_from_stdin = get_line_from_file(stdin);
Eric Andersena37d5b72000-09-23 06:10:14 +000078 for (;in_from_stdin!=NULL;) {
Eric Andersen96bdde92000-09-23 19:53:31 +000079 char *tmp;
Eric Andersen6f283c22000-09-24 02:40:56 +000080 opt = strlen(in_from_stdin);
Eric Andersene081eae2000-09-25 20:23:21 +000081 len_args += opt + 3;
82 args=xrealloc(args, len_args);
Eric Andersenbf739092000-09-25 18:41:18 +000083
Eric Andersen6f283c22000-09-24 02:40:56 +000084 /* Strip out the final \n */
85 in_from_stdin[opt-1]=' ';
Eric Andersene081eae2000-09-25 20:23:21 +000086
87 /* Replace any tabs with spaces */
88 while( (tmp = strchr(in_from_stdin, '\t')) != NULL )
89 *tmp=' ';
90
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));
95 }
96
Eric Andersen6f283c22000-09-24 02:40:56 +000097 /* trim trailing whitespace */
98 opt = strlen(in_from_stdin) - 1;
99 while (isspace(in_from_stdin[opt]))
100 opt--;
101 in_from_stdin[++opt] = 0;
102
103 /* Strip out any leading whitespace */
104 tmp=in_from_stdin;
105 while(isspace(*tmp))
106 tmp++;
107
Eric Andersene081eae2000-09-25 20:23:21 +0000108 strcat(args, tmp);
109 strcat(args, " ");
110
Eric Andersena37d5b72000-09-23 06:10:14 +0000111 free(in_from_stdin);
Eric Andersene081eae2000-09-25 20:23:21 +0000112 in_from_stdin = get_line_from_file(stdin);
Eric Andersena37d5b72000-09-23 06:10:14 +0000113 }
114
Eric Andersene081eae2000-09-25 20:23:21 +0000115 if ((pid = fork()) == 0) {
116 char *cmd[255];
117 int i=1;
118
Eric Andersend2667482000-09-26 01:00:15 +0000119 if (traceflag==1) {
120 fprintf(stderr, "%s ", cmd_to_be_executed);
121 }
Eric Andersene081eae2000-09-25 20:23:21 +0000122 cmd[0] = cmd_to_be_executed;
123 while (--argc && ++argv && *argv ) {
Eric Andersend2667482000-09-26 01:00:15 +0000124 if (traceflag==1) {
125 fprintf(stderr, "%s ", *argv);
126 }
Eric Andersene081eae2000-09-25 20:23:21 +0000127 cmd[i++]=*argv;
128 }
Eric Andersend2667482000-09-26 01:00:15 +0000129 if (traceflag==1) {
130 fprintf(stderr, "%s\n", args);
131 }
Eric Andersene081eae2000-09-25 20:23:21 +0000132 cmd[i++] = args;
133 cmd[i] = NULL;
134 execvp(cmd_to_be_executed, cmd);
135
136 /* What? Still here? Exit with an error */
137 fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno));
138 }
139 /* Wait for a child process to exit */
140 wpid = wait(&status);
Eric Andersena37d5b72000-09-23 06:10:14 +0000141
142
143#ifdef BB_FEATURE_CLEAN_UP
Eric Andersene081eae2000-09-25 20:23:21 +0000144 free(args);
Eric Andersena37d5b72000-09-23 06:10:14 +0000145 free(cmd_to_be_executed);
146#endif
147
Eric Andersene081eae2000-09-25 20:23:21 +0000148 if (wpid > 0)
149 return (WEXITSTATUS(status));
150 else
151 return EXIT_FAILURE;
Eric Andersen92a61c12000-09-22 20:01:23 +0000152}
Eric Andersena37d5b72000-09-23 06:10:14 +0000153/*
154Local Variables:
155c-file-style: "linux"
156c-basic-offset: 4
157tab-width: 4
158End:
159*/