Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 3 | * Mini xargs implementation for busybox |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.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 | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 24 | #include "internal.h" |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Eric Andersen | 5b17693 | 2000-09-22 20:22:28 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <errno.h> |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 32 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 33 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 34 | { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 35 | char *in_from_stdin = NULL; |
| 36 | char *args_from_cmdline = NULL; |
| 37 | char *cmd_to_be_executed = NULL; |
| 38 | char traceflag = 0; |
| 39 | int len_args_from_cmdline, len_cmd_to_be_executed, len, opt; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 40 | |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 41 | /* Note that we do not use getopt here, since |
| 42 | * we only want to interpret initial options, |
| 43 | * not options passed to commands */ |
| 44 | while (--argc && **(++argv) == '-') { |
| 45 | while (*++(*argv)) { |
| 46 | switch (**argv) { |
| 47 | case 't': |
| 48 | traceflag=1; |
| 49 | break; |
| 50 | default: |
| 51 | fatalError(xargs_usage); |
| 52 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 55 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 56 | /* Store the command and arguments to be executed (from the command line) */ |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 57 | if (argc == 1) { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 58 | len_args_from_cmdline = 6; |
| 59 | args_from_cmdline = xmalloc(len_args_from_cmdline); |
| 60 | strcat(args_from_cmdline, "echo "); |
| 61 | } else { |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 62 | opt=strlen(*argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 63 | len_args_from_cmdline = (opt > 10)? opt : 10; |
| 64 | args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char)); |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 65 | while (argc-- > 0) { |
| 66 | if (strlen(*argv) + strlen(args_from_cmdline) > |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 67 | len_args_from_cmdline) { |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 68 | len_args_from_cmdline += strlen(*argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 69 | args_from_cmdline = |
| 70 | xrealloc(args_from_cmdline, |
| 71 | len_args_from_cmdline+1); |
| 72 | } |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 73 | strcat(args_from_cmdline, *argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 74 | strcat(args_from_cmdline, " "); |
| 75 | } |
| 76 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 77 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 78 | /* Set up some space for the command to be executed to be held in */ |
| 79 | len_cmd_to_be_executed=10; |
| 80 | cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); |
| 81 | strcpy(cmd_to_be_executed, args_from_cmdline); |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 82 | strcat(cmd_to_be_executed, " \""); |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 83 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 84 | /* Now, read in one line at a time from stdin, and run command+args on it */ |
| 85 | in_from_stdin = get_line_from_file(stdin); |
| 86 | for (;in_from_stdin!=NULL;) { |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 87 | char *tmp; |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 88 | opt = strlen(in_from_stdin); |
| 89 | len = opt + len_args_from_cmdline; |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 90 | len_cmd_to_be_executed+=len+3; |
| 91 | cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 92 | |
| 93 | /* Strip out the final \n */ |
| 94 | in_from_stdin[opt-1]=' '; |
| 95 | |
| 96 | /* Replace any tabs with spaces */ |
| 97 | while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 98 | *tmp=' '; |
| 99 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 100 | /* Strip out any extra intra-word spaces */ |
| 101 | while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { |
| 102 | opt = strlen(in_from_stdin); |
| 103 | memmove(tmp, tmp+1, opt-(tmp-in_from_stdin)); |
| 104 | } |
| 105 | |
| 106 | /* trim trailing whitespace */ |
| 107 | opt = strlen(in_from_stdin) - 1; |
| 108 | while (isspace(in_from_stdin[opt])) |
| 109 | opt--; |
| 110 | in_from_stdin[++opt] = 0; |
| 111 | |
| 112 | /* Strip out any leading whitespace */ |
| 113 | tmp=in_from_stdin; |
| 114 | while(isspace(*tmp)) |
| 115 | tmp++; |
| 116 | |
| 117 | strcat(cmd_to_be_executed, tmp); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 118 | strcat(cmd_to_be_executed, " "); |
| 119 | |
| 120 | free(in_from_stdin); |
| 121 | in_from_stdin = get_line_from_file(stdin); |
| 122 | } |
| 123 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 124 | strcat(cmd_to_be_executed, "\""); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 125 | if (traceflag==1) |
| 126 | fputs(cmd_to_be_executed, stderr); |
| 127 | |
| 128 | if ((system(cmd_to_be_executed) != 0) && (errno != 0)) |
| 129 | fatalError("%s", strerror(errno)); |
| 130 | |
| 131 | |
| 132 | #ifdef BB_FEATURE_CLEAN_UP |
| 133 | free(args_from_cmdline); |
| 134 | free(cmd_to_be_executed); |
| 135 | #endif |
| 136 | |
| 137 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 138 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 139 | /* |
| 140 | Local Variables: |
| 141 | c-file-style: "linux" |
| 142 | c-basic-offset: 4 |
| 143 | tab-width: 4 |
| 144 | End: |
| 145 | */ |
| 146 | |