blob: 48adae90a2870dc590fb7a7514abd69baa68485b [file] [log] [blame]
Eric Andersen5b176932000-09-22 20:22:28 +00001/*
Eric Andersena37d5b72000-09-23 06:10:14 +00002 * Mini xargs implementation for busybox
Eric Andersen5b176932000-09-22 20:22:28 +00003 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersena37d5b72000-09-23 06:10:14 +00005 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Mark Whitley6c6ea6c2001-01-04 22:21:13 +00006 * Remixed by Mark Whitley <markw@lineo.com>, <markw@codepoet.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 Andersen5b176932000-09-22 20:22:28 +000024#include <stdio.h>
Mark Whitleye2e2c292000-11-14 22:43:21 +000025#include <stdlib.h>
Eric Andersena37d5b72000-09-23 06:10:14 +000026#include <string.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000028
Eric Andersena37d5b72000-09-23 06:10:14 +000029int xargs_main(int argc, char **argv)
Eric Andersen92a61c12000-09-22 20:01:23 +000030{
Eric Andersena37d5b72000-09-23 06:10:14 +000031 char *cmd_to_be_executed = NULL;
Mark Whitleye2e2c292000-11-14 22:43:21 +000032 char *file_to_act_on = NULL;
Eric Andersen92a61c12000-09-22 20:01:23 +000033
Mark Whitleye2e2c292000-11-14 22:43:21 +000034 /*
35 * No options are supported in this version of xargs; no getopt.
36 *
37 * Re: The missing -t flag: Most programs that produce output also print
38 * the filename, so xargs doesn't really need to do it again. Supporting
39 * the -t flag =greatly= bloats up the size of this app and the memory it
40 * uses because you have to buffer all the input file strings in memory. If
41 * you really want to see the filenames that xargs will act on, just run it
42 * once with no args and xargs will echo the filename. Simple.
43 */
Eric Andersen92a61c12000-09-22 20:01:23 +000044
Eric Andersene081eae2000-09-25 20:23:21 +000045 /* Store the command to be executed (taken from the command line) */
Mark Whitleye2e2c292000-11-14 22:43:21 +000046 if (argc == 1) {
47 /* default behavior is to echo all the filenames */
Eric Andersen6f3240a2001-03-23 17:11:22 +000048 cmd_to_be_executed = xstrdup("/bin/echo ");
Eric Andersena37d5b72000-09-23 06:10:14 +000049 } else {
Mark Whitleye2e2c292000-11-14 22:43:21 +000050 /* concatenate all the arguments passed to xargs together */
51 int i;
52 int len = 1; /* for the '\0' */
53 for (i = 1; i < argc; i++) {
54 len += strlen(argv[i]);
55 len += 1; /* for the space between the args */
56 cmd_to_be_executed = xrealloc(cmd_to_be_executed, len);
57 strcat(cmd_to_be_executed, argv[i]);
58 strcat(cmd_to_be_executed, " ");
59 }
Eric Andersena37d5b72000-09-23 06:10:14 +000060 }
Eric Andersen92a61c12000-09-22 20:01:23 +000061
Eric Andersend89882d2000-09-25 22:53:05 +000062 /* Now, read in one line at a time from stdin, and store this
63 * line to be used later as an argument to the command */
Mark Whitleye2e2c292000-11-14 22:43:21 +000064 while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
Eric Andersenbf739092000-09-25 18:41:18 +000065
Mark Whitleye2e2c292000-11-14 22:43:21 +000066 FILE *cmd_output = NULL;
67 char *output_line = NULL;
68 char *execstr = NULL;
Eric Andersene081eae2000-09-25 20:23:21 +000069
Mark Whitleye2e2c292000-11-14 22:43:21 +000070 /* eat the newline off the filename. */
Matt Kraai05e782d2001-02-01 16:49:30 +000071 chomp(file_to_act_on);
Eric Andersene081eae2000-09-25 20:23:21 +000072
Mark Whitleye2e2c292000-11-14 22:43:21 +000073 /* eat blank lines */
74 if (strlen(file_to_act_on) == 0)
75 continue;
76
77 /* assemble the command and execute it */
78 execstr = xcalloc(strlen(cmd_to_be_executed) +
79 strlen(file_to_act_on) + 1, sizeof(char));
80 strcat(execstr, cmd_to_be_executed);
81 strcat(execstr, file_to_act_on);
82 cmd_output = popen(execstr, "r");
Matt Kraaia9819b22000-12-22 01:48:07 +000083 if (cmd_output == NULL)
84 perror_msg_and_die("popen");
Eric Andersene081eae2000-09-25 20:23:21 +000085
Mark Whitleye2e2c292000-11-14 22:43:21 +000086 /* harvest the output */
87 while ((output_line = get_line_from_file(cmd_output)) != NULL) {
88 fputs(output_line, stdout);
89 free(output_line);
90 }
Eric Andersen6f283c22000-09-24 02:40:56 +000091
Mark Whitleye2e2c292000-11-14 22:43:21 +000092 /* clean up */
93 pclose(cmd_output);
94 free(execstr);
95 free(file_to_act_on);
Eric Andersena37d5b72000-09-23 06:10:14 +000096 }
97
Eric Andersena37d5b72000-09-23 06:10:14 +000098#ifdef BB_FEATURE_CLEAN_UP
Eric Andersena37d5b72000-09-23 06:10:14 +000099 free(cmd_to_be_executed);
100#endif
101
Mark Whitleye2e2c292000-11-14 22:43:21 +0000102 return 0;
Eric Andersen92a61c12000-09-22 20:01:23 +0000103}
Mark Whitleye2e2c292000-11-14 22:43:21 +0000104
105/* vi: set sw=4 ts=4: */