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; |
| 69 | char flg_vi = 0; /* verbose |& interactive */ |
| 70 | char flg_no_empty = 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 72 | while ((a = getopt(argc, argv, "prt")) > 0) { |
| 73 | switch(a) { |
| 74 | case 'p': |
| 75 | flg_vi |= 3; |
| 76 | break; |
| 77 | case 't': |
| 78 | flg_vi |= 1; |
| 79 | break; |
| 80 | case 'r': |
| 81 | flg_no_empty = 1; |
| 82 | break; |
| 83 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 84 | bb_show_usage(); |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 85 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 86 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 87 | |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 88 | a = argc - optind; |
| 89 | argv += optind; |
| 90 | if(a==0) { |
| 91 | /* default behavior is to echo all the filenames */ |
| 92 | *argv = "/bin/echo"; |
| 93 | a++; |
| 94 | } |
| 95 | /* allocating pointers for execvp: a*arg, arg from stdin, NULL */ |
| 96 | args = xcalloc(a + 3, sizeof(char *)); |
| 97 | |
| 98 | /* Store the command to be executed (taken from the command line) */ |
| 99 | for (i = 0; i < a; i++) |
| 100 | args[i] = *argv++; |
| 101 | |
Eric Andersen | d89882d | 2000-09-25 22:53:05 +0000 | [diff] [blame] | 102 | /* Now, read in one line at a time from stdin, and store this |
| 103 | * line to be used later as an argument to the command */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 104 | 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] | 105 | if(file_to_act_on[0] != 0 || flg_no_empty == 0) { |
| 106 | args[a] = file_to_act_on[0] ? file_to_act_on : NULL; |
| 107 | if(flg_vi) { |
| 108 | for(i=0; args[i]; i++) { |
| 109 | if(i) |
| 110 | fputc(' ', stderr); |
| 111 | fputs(args[i], stderr); |
| 112 | } |
| 113 | fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n")); |
| 114 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 115 | if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) { |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 116 | xargs_exec(args); |
| 117 | } |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 118 | } |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 119 | /* clean up */ |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 120 | free(file_to_act_on); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 121 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 122 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Glenn L McGrath | f57674e | 2002-11-10 21:47:17 +0000 | [diff] [blame] | 123 | free(args); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 124 | #endif |
Mark Whitley | e2e2c29 | 2000-11-14 22:43:21 +0000 | [diff] [blame] | 125 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 126 | } |