Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 1 | /* |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 2 | * Mini xargs implementation for busybox |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 3 | * |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +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> |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 | * |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 26 | #include <string.h> |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
| 28 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 29 | int xargs_main(int argc, char **argv) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 30 | { |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 31 | char *cmd_to_be_executed; |
| 32 | char *file_to_act_on; |
| 33 | int i; |
| 34 | int len; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 37 | * No options are supported in this version of xargs; no getopt. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 38 | * |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 39 | * Re: The missing -t flag: Most programs that produce output also print |
| 40 | * the filename, so xargs doesn't really need to do it again. Supporting |
| 41 | * the -t flag =greatly= bloats up the size of this app and the memory it |
| 42 | * uses because you have to buffer all the input file strings in memory. If |
| 43 | * you really want to see the filenames that xargs will act on, just run it |
| 44 | * once with no args and xargs will echo the filename. Simple. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 45 | */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 46 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 47 | argv++; |
| 48 | len = argc; /* arg = count for ' ' + trailing '\0' */ |
| 49 | /* Store the command to be executed (taken from the command line) */ |
| 50 | if (argc == 1) { |
| 51 | /* default behavior is to echo all the filenames */ |
| 52 | argv[0] = "/bin/echo"; |
| 53 | len++; /* space for trailing '\0' */ |
| 54 | } else { |
| 55 | argc--; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 56 | } |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 57 | /* concatenate all the arguments passed to xargs together */ |
| 58 | for (i = 0; i < argc; i++) |
| 59 | len += strlen(argv[i]); |
| 60 | cmd_to_be_executed = xmalloc (len); |
| 61 | for (i = len = 0; i < argc; i++) { |
| 62 | len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 63 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 64 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 65 | /* Now, read in one line at a time from stdin, and store this |
| 66 | * line to be used later as an argument to the command */ |
| 67 | while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 69 | FILE *cmd_output; |
| 70 | char *output_line; |
| 71 | char *execstr; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 73 | /* eat the newline off the filename. */ |
| 74 | chomp(file_to_act_on); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 75 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 76 | /* eat blank lines */ |
| 77 | if (file_to_act_on[0] == 0) |
| 78 | continue; |
| 79 | |
| 80 | /* assemble the command and execute it */ |
| 81 | bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on); |
| 82 | |
| 83 | cmd_output = popen(execstr, "r"); |
| 84 | if (cmd_output == NULL) |
| 85 | perror_msg_and_die("popen"); |
| 86 | |
| 87 | /* harvest the output */ |
| 88 | while ((output_line = get_line_from_file(cmd_output)) != NULL) { |
| 89 | fputs(output_line, stdout); |
| 90 | free(output_line); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 93 | /* clean up */ |
| 94 | pclose(cmd_output); |
| 95 | free(execstr); |
| 96 | free(file_to_act_on); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 99 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 100 | free(cmd_to_be_executed); |
| 101 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 102 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 103 | return 0; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Glenn L McGrath | 59870e8 | 2002-11-10 21:52:59 +0000 | [diff] [blame] | 106 | /* vi: set sw=4 ts=4: */ |