Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 2 | * Mini xargs implementation for busybox |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 3 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen |
| 5 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> |
| 6 | * Remixed by Mark Whitley <markw@codepoet.org> |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 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. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 12 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 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. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 17 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 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 |
| 21 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 22 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 23 | |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 26 | #include <string.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 28 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 29 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 30 | { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 31 | char *cmd_to_be_executed = NULL; |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 32 | char *file_to_act_on = NULL; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 33 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 34 | /* |
| 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 Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 44 | |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 45 | /* Store the command to be executed (taken from the command line) */ |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 46 | if (argc == 1) { |
| 47 | /* default behavior is to echo all the filenames */ |
Eric Andersen | 6f3240a | 2001-03-23 17:11:22 +0000 | [diff] [blame] | 48 | cmd_to_be_executed = xstrdup("/bin/echo "); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 49 | } else { |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 50 | /* concatenate all the arguments passed to xargs together */ |
| 51 | int i; |
| 52 | int len = 1; /* for the '\0' */ |
Eric Andersen | d81891a | 2001-11-19 10:49:30 +0000 | [diff] [blame^] | 53 | cmd_to_be_executed = xmalloc(80); |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 54 | for (i = 1; i < argc; i++) { |
| 55 | len += strlen(argv[i]); |
| 56 | len += 1; /* for the space between the args */ |
| 57 | cmd_to_be_executed = xrealloc(cmd_to_be_executed, len); |
| 58 | strcat(cmd_to_be_executed, argv[i]); |
| 59 | strcat(cmd_to_be_executed, " "); |
| 60 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 62 | |
Eric Andersen | d89882d | 2000-09-25 22:53:05 +0000 | [diff] [blame] | 63 | /* Now, read in one line at a time from stdin, and store this |
| 64 | * line to be used later as an argument to the command */ |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 65 | while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame] | 66 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 67 | FILE *cmd_output = NULL; |
| 68 | char *output_line = NULL; |
| 69 | char *execstr = NULL; |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 70 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 71 | /* eat the newline off the filename. */ |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 72 | chomp(file_to_act_on); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 73 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 74 | /* eat blank lines */ |
| 75 | if (strlen(file_to_act_on) == 0) |
| 76 | continue; |
| 77 | |
| 78 | /* assemble the command and execute it */ |
| 79 | execstr = xcalloc(strlen(cmd_to_be_executed) + |
| 80 | strlen(file_to_act_on) + 1, sizeof(char)); |
| 81 | strcat(execstr, cmd_to_be_executed); |
| 82 | strcat(execstr, file_to_act_on); |
| 83 | cmd_output = popen(execstr, "r"); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 84 | if (cmd_output == NULL) |
| 85 | perror_msg_and_die("popen"); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 86 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 87 | /* harvest the output */ |
| 88 | while ((output_line = get_line_from_file(cmd_output)) != NULL) { |
| 89 | fputs(output_line, stdout); |
| 90 | free(output_line); |
| 91 | } |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 92 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 93 | /* clean up */ |
| 94 | pclose(cmd_output); |
| 95 | free(execstr); |
| 96 | free(file_to_act_on); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 99 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 100 | free(cmd_to_be_executed); |
| 101 | #endif |
| 102 | |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 103 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 104 | } |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 105 | |
| 106 | /* vi: set sw=4 ts=4: */ |