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> |
| 30 | |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 31 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 32 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 33 | { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 34 | char *in_from_stdin = NULL; |
| 35 | char *args_from_cmdline = NULL; |
| 36 | char *cmd_to_be_executed = NULL; |
| 37 | char traceflag = 0; |
| 38 | int len_args_from_cmdline, len_cmd_to_be_executed, len, opt; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 39 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 40 | while ((opt = getopt(argc, argv, "t")) != EOF) { |
| 41 | switch (opt) { |
| 42 | case 't': |
| 43 | traceflag=1; |
| 44 | break; |
| 45 | default: |
| 46 | fatalError(xargs_usage); |
| 47 | } |
| 48 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 49 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 50 | /* Store the command and arguments to be executed (from the command line) */ |
| 51 | if (optind == argc) { |
| 52 | len_args_from_cmdline = 6; |
| 53 | args_from_cmdline = xmalloc(len_args_from_cmdline); |
| 54 | strcat(args_from_cmdline, "echo "); |
| 55 | } else { |
| 56 | opt=strlen(argv[optind]); |
| 57 | len_args_from_cmdline = (opt > 10)? opt : 10; |
| 58 | args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char)); |
| 59 | for (; optind < argc; optind++) { |
| 60 | if (strlen(argv[optind]) + strlen(args_from_cmdline) > |
| 61 | len_args_from_cmdline) { |
| 62 | len_args_from_cmdline += strlen(argv[optind]); |
| 63 | args_from_cmdline = |
| 64 | xrealloc(args_from_cmdline, |
| 65 | len_args_from_cmdline+1); |
| 66 | } |
| 67 | strcat(args_from_cmdline, argv[optind]); |
| 68 | strcat(args_from_cmdline, " "); |
| 69 | } |
| 70 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 71 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 72 | /* Set up some space for the command to be executed to be held in */ |
| 73 | len_cmd_to_be_executed=10; |
| 74 | cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); |
| 75 | strcpy(cmd_to_be_executed, args_from_cmdline); |
| 76 | strcat(cmd_to_be_executed, " "); |
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 | /* Now, read in one line at a time from stdin, and run command+args on it */ |
| 79 | in_from_stdin = get_line_from_file(stdin); |
| 80 | for (;in_from_stdin!=NULL;) { |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame^] | 81 | char *tmp; |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 82 | len = strlen(in_from_stdin) + len_args_from_cmdline; |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame^] | 83 | len_cmd_to_be_executed+=len+3; |
| 84 | cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); |
| 85 | |
| 86 | /* Strip out any \n's, so we just get one command to run */ |
| 87 | while( (tmp = strchr(in_from_stdin, '\n')) != NULL ) |
| 88 | *tmp=' '; |
| 89 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 90 | strcat(cmd_to_be_executed, in_from_stdin); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 91 | strcat(cmd_to_be_executed, " "); |
| 92 | |
| 93 | free(in_from_stdin); |
| 94 | in_from_stdin = get_line_from_file(stdin); |
| 95 | } |
| 96 | |
| 97 | if (traceflag==1) |
| 98 | fputs(cmd_to_be_executed, stderr); |
| 99 | |
| 100 | if ((system(cmd_to_be_executed) != 0) && (errno != 0)) |
| 101 | fatalError("%s", strerror(errno)); |
| 102 | |
| 103 | |
| 104 | #ifdef BB_FEATURE_CLEAN_UP |
| 105 | free(args_from_cmdline); |
| 106 | free(cmd_to_be_executed); |
| 107 | #endif |
| 108 | |
| 109 | return 0; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 111 | /* |
| 112 | Local Variables: |
| 113 | c-file-style: "linux" |
| 114 | c-basic-offset: 4 |
| 115 | tab-width: 4 |
| 116 | End: |
| 117 | */ |
| 118 | |