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 | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 32 | /* get_sh_safe_line_from_file() - This function reads an entire line from a text file |
| 33 | * up to a newline. It returns a malloc'ed char * which must be stored and |
| 34 | * free'ed by the caller. */ |
| 35 | extern char *get_sh_safe_line_from_file(FILE *file) |
| 36 | { |
| 37 | static const int GROWBY = 80; /* how large we will grow strings by */ |
| 38 | |
| 39 | char ch, last_ch = 0; |
| 40 | char tmp[]=" "; |
| 41 | int idx = 0; |
| 42 | char *linebuf = NULL; |
| 43 | int linebufsz = 0; |
| 44 | |
| 45 | while (1) { |
| 46 | ch = fgetc(file); |
| 47 | if (ch == EOF) |
| 48 | break; |
| 49 | |
| 50 | /* grow the line buffer as necessary */ |
| 51 | while (idx > linebufsz-4) |
| 52 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); |
| 53 | |
| 54 | /* Remove any extra spaces */ |
| 55 | if (last_ch == ' ' && ch == ' ') |
| 56 | continue; |
| 57 | |
| 58 | /* Replace any tabs with spaces */ |
| 59 | if (ch == '\t') |
| 60 | ch=' '; |
| 61 | |
| 62 | /* Escape any characters that are treated specially by /bin/sh */ |
| 63 | *tmp=ch; |
| 64 | if (strpbrk(tmp, "\\~`!$^&*()=|{}[];\"'<>?#") != NULL && last_ch!='\\') { |
| 65 | linebuf[idx++] = '\\'; |
| 66 | } |
| 67 | |
| 68 | linebuf[idx++] = ch; |
| 69 | last_ch=ch; |
| 70 | |
| 71 | if (ch == '\n') |
| 72 | break; |
| 73 | } |
| 74 | if (idx == 0 && last_ch!=0) |
| 75 | linebuf[idx++]=' '; |
| 76 | |
| 77 | if (idx == 0) |
| 78 | return NULL; |
| 79 | |
| 80 | linebuf[idx] = 0; |
| 81 | return linebuf; |
| 82 | } |
| 83 | |
| 84 | |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 85 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 86 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 87 | { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 88 | char *in_from_stdin = NULL; |
| 89 | char *args_from_cmdline = NULL; |
| 90 | char *cmd_to_be_executed = NULL; |
| 91 | char traceflag = 0; |
| 92 | int len_args_from_cmdline, len_cmd_to_be_executed, len, opt; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 94 | /* Note that we do not use getopt here, since |
| 95 | * we only want to interpret initial options, |
| 96 | * not options passed to commands */ |
| 97 | while (--argc && **(++argv) == '-') { |
| 98 | while (*++(*argv)) { |
| 99 | switch (**argv) { |
| 100 | case 't': |
| 101 | traceflag=1; |
| 102 | break; |
| 103 | default: |
| 104 | fatalError(xargs_usage); |
| 105 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 108 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 109 | /* Store the command and arguments to be executed (from the command line) */ |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 110 | if (argc == 0) { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 111 | len_args_from_cmdline = 6; |
| 112 | args_from_cmdline = xmalloc(len_args_from_cmdline); |
| 113 | strcat(args_from_cmdline, "echo "); |
| 114 | } else { |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 115 | opt=strlen(*argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 116 | len_args_from_cmdline = (opt > 10)? opt : 10; |
| 117 | args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char)); |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 118 | while (argc-- > 0) { |
| 119 | if (strlen(*argv) + strlen(args_from_cmdline) > |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 120 | len_args_from_cmdline) { |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 121 | len_args_from_cmdline += strlen(*argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 122 | args_from_cmdline = |
| 123 | xrealloc(args_from_cmdline, |
| 124 | len_args_from_cmdline+1); |
| 125 | } |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 126 | strcat(args_from_cmdline, *argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 127 | strcat(args_from_cmdline, " "); |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 128 | ++argv; |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 131 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 132 | /* Set up some space for the command to be executed to be held in */ |
| 133 | len_cmd_to_be_executed=10; |
| 134 | cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); |
| 135 | strcpy(cmd_to_be_executed, args_from_cmdline); |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 136 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 137 | /* Now, read in one line at a time from stdin, and run command+args on it */ |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 138 | in_from_stdin = get_sh_safe_line_from_file(stdin); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 139 | for (;in_from_stdin!=NULL;) { |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 140 | char *tmp; |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 141 | opt = strlen(in_from_stdin); |
| 142 | len = opt + len_args_from_cmdline; |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 143 | len_cmd_to_be_executed+=len+3; |
| 144 | cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 145 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 146 | /* Strip out the final \n */ |
| 147 | in_from_stdin[opt-1]=' '; |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 148 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 149 | /* trim trailing whitespace */ |
| 150 | opt = strlen(in_from_stdin) - 1; |
| 151 | while (isspace(in_from_stdin[opt])) |
| 152 | opt--; |
| 153 | in_from_stdin[++opt] = 0; |
| 154 | |
| 155 | /* Strip out any leading whitespace */ |
| 156 | tmp=in_from_stdin; |
| 157 | while(isspace(*tmp)) |
| 158 | tmp++; |
| 159 | |
| 160 | strcat(cmd_to_be_executed, tmp); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 161 | strcat(cmd_to_be_executed, " "); |
| 162 | |
| 163 | free(in_from_stdin); |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame^] | 164 | in_from_stdin = get_sh_safe_line_from_file(stdin); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (traceflag==1) |
| 168 | fputs(cmd_to_be_executed, stderr); |
| 169 | |
| 170 | if ((system(cmd_to_be_executed) != 0) && (errno != 0)) |
| 171 | fatalError("%s", strerror(errno)); |
| 172 | |
| 173 | |
| 174 | #ifdef BB_FEATURE_CLEAN_UP |
| 175 | free(args_from_cmdline); |
| 176 | free(cmd_to_be_executed); |
| 177 | #endif |
| 178 | |
| 179 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 180 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 181 | /* |
| 182 | Local Variables: |
| 183 | c-file-style: "linux" |
| 184 | c-basic-offset: 4 |
| 185 | tab-width: 4 |
| 186 | End: |
| 187 | */ |
| 188 | |