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 |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 3 | * Only "-prt" options are supported in this version of xargs. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 5 | * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
| 6 | * |
| 7 | * Special thanks Mark Whitley for stimul to rewrote :) |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 8 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 13 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 18 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 23 | */ |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 24 | |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <getopt.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 33 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | This function have special algorithm. |
| 37 | Don`t use fork and include to main! |
| 38 | */ |
| 39 | static void xargs_exec(char * const * args) |
| 40 | { |
| 41 | int p; |
| 42 | int common[4]; /* shared vfork stack */ |
| 43 | |
| 44 | common[0] = 0; |
| 45 | if ((p = vfork()) >= 0) { |
| 46 | if (p == 0) { |
| 47 | /* vfork -- child */ |
| 48 | execvp(args[0], args); |
| 49 | common[0] = errno; /* set error to shared stack */ |
| 50 | _exit(1); |
| 51 | } else { |
| 52 | /* vfork -- parent */ |
| 53 | wait(NULL); |
| 54 | if(common[0]) { |
| 55 | errno = common[0]; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | bb_perror_msg_and_die("%s", args[0]); |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | bb_perror_msg_and_die("vfork"); |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 64 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 65 | { |
Eric Andersen | d4ee989 | 2002-09-16 10:44:24 +0000 | [diff] [blame] | 66 | char *file_to_act_on; |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 67 | char **args; |
| 68 | int i, a; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame^] | 69 | char flg_vi; /* verbose |& interactive */ |
| 70 | char flg_no_empty; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame^] | 72 | bb_opt_complementaly = "pt"; |
| 73 | a = bb_getopt_ulflags(argc, argv, "tpr"); |
| 74 | flg_vi = a & 3; |
| 75 | flg_no_empty = a & 4; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 77 | a = argc - optind; |
| 78 | argv += optind; |
| 79 | if(a==0) { |
| 80 | /* default behavior is to echo all the filenames */ |
| 81 | *argv = "/bin/echo"; |
| 82 | a++; |
| 83 | } |
| 84 | /* allocating pointers for execvp: a*arg, arg from stdin, NULL */ |
| 85 | args = xcalloc(a + 3, sizeof(char *)); |
| 86 | |
| 87 | /* Store the command to be executed (taken from the command line) */ |
| 88 | for (i = 0; i < a; i++) |
| 89 | args[i] = *argv++; |
| 90 | |
Eric Andersen | d89882d | 2000-09-25 22:53:05 +0000 | [diff] [blame] | 91 | /* Now, read in one line at a time from stdin, and store this |
| 92 | * line to be used later as an argument to the command */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) { |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 94 | if(file_to_act_on[0] != 0 || flg_no_empty == 0) { |
| 95 | args[a] = file_to_act_on[0] ? file_to_act_on : NULL; |
| 96 | if(flg_vi) { |
| 97 | for(i=0; args[i]; i++) { |
| 98 | if(i) |
| 99 | fputc(' ', stderr); |
| 100 | fputs(args[i], stderr); |
| 101 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame^] | 102 | fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr); |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 103 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) { |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 105 | xargs_exec(args); |
| 106 | } |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 107 | } |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 108 | /* clean up */ |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 109 | free(file_to_act_on); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 111 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 112 | free(args); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 113 | #endif |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 114 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 115 | } |