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 | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | #include "busybox.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 | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame] | 33 | |
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 | int xargs_main(int argc, char **argv) |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 36 | { |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 37 | char *in_from_stdin = NULL; |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 38 | char *args = NULL; |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 39 | char *cmd_to_be_executed = NULL; |
| 40 | char traceflag = 0; |
Eric Andersen | d266748 | 2000-09-26 01:00:15 +0000 | [diff] [blame] | 41 | int len_args=0, len_cmd_to_be_executed, opt; |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 42 | pid_t pid; |
| 43 | int wpid, status; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 44 | |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 45 | /* Note that we do not use getopt here, since |
| 46 | * we only want to interpret initial options, |
| 47 | * not options passed to commands */ |
| 48 | while (--argc && **(++argv) == '-') { |
| 49 | while (*++(*argv)) { |
| 50 | switch (**argv) { |
| 51 | case 't': |
| 52 | traceflag=1; |
| 53 | break; |
| 54 | default: |
| 55 | fatalError(xargs_usage); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 56 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 59 | |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 60 | /* Store the command to be executed (taken from the command line) */ |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame] | 61 | if (argc == 0) { |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 62 | len_cmd_to_be_executed=6; |
| 63 | cmd_to_be_executed = xmalloc(len_cmd_to_be_executed); |
| 64 | strcat(cmd_to_be_executed, "echo"); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 65 | } else { |
Eric Andersen | 9ae3838 | 2000-09-24 01:12:54 +0000 | [diff] [blame] | 66 | opt=strlen(*argv); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 67 | len_cmd_to_be_executed = (opt > 10)? opt : 10; |
| 68 | cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); |
| 69 | strcat(cmd_to_be_executed, *argv); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 70 | } |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 71 | |
Eric Andersen | d266748 | 2000-09-26 01:00:15 +0000 | [diff] [blame] | 72 | //args=xrealloc(args, len_args); |
| 73 | // strcpy(args, " "); |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 74 | |
Eric Andersen | d89882d | 2000-09-25 22:53:05 +0000 | [diff] [blame] | 75 | /* Now, read in one line at a time from stdin, and store this |
| 76 | * line to be used later as an argument to the command */ |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 77 | in_from_stdin = get_line_from_file(stdin); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 78 | for (;in_from_stdin!=NULL;) { |
Eric Andersen | 96bdde9 | 2000-09-23 19:53:31 +0000 | [diff] [blame] | 79 | char *tmp; |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 80 | opt = strlen(in_from_stdin); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 81 | len_args += opt + 3; |
| 82 | args=xrealloc(args, len_args); |
Eric Andersen | bf73909 | 2000-09-25 18:41:18 +0000 | [diff] [blame] | 83 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 84 | /* Strip out the final \n */ |
| 85 | in_from_stdin[opt-1]=' '; |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 86 | |
| 87 | /* Replace any tabs with spaces */ |
| 88 | while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) |
| 89 | *tmp=' '; |
| 90 | |
| 91 | /* Strip out any extra intra-word spaces */ |
| 92 | while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { |
| 93 | opt = strlen(in_from_stdin); |
| 94 | memmove(tmp, tmp+1, opt-(tmp-in_from_stdin)); |
| 95 | } |
| 96 | |
Eric Andersen | 6f283c2 | 2000-09-24 02:40:56 +0000 | [diff] [blame] | 97 | /* trim trailing whitespace */ |
| 98 | opt = strlen(in_from_stdin) - 1; |
| 99 | while (isspace(in_from_stdin[opt])) |
| 100 | opt--; |
| 101 | in_from_stdin[++opt] = 0; |
| 102 | |
| 103 | /* Strip out any leading whitespace */ |
| 104 | tmp=in_from_stdin; |
| 105 | while(isspace(*tmp)) |
| 106 | tmp++; |
| 107 | |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 108 | strcat(args, tmp); |
| 109 | strcat(args, " "); |
| 110 | |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 111 | free(in_from_stdin); |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 112 | in_from_stdin = get_line_from_file(stdin); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 115 | if ((pid = fork()) == 0) { |
| 116 | char *cmd[255]; |
| 117 | int i=1; |
| 118 | |
Eric Andersen | d266748 | 2000-09-26 01:00:15 +0000 | [diff] [blame] | 119 | if (traceflag==1) { |
| 120 | fprintf(stderr, "%s ", cmd_to_be_executed); |
| 121 | } |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 122 | cmd[0] = cmd_to_be_executed; |
| 123 | while (--argc && ++argv && *argv ) { |
Eric Andersen | d266748 | 2000-09-26 01:00:15 +0000 | [diff] [blame] | 124 | if (traceflag==1) { |
| 125 | fprintf(stderr, "%s ", *argv); |
| 126 | } |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 127 | cmd[i++]=*argv; |
| 128 | } |
Eric Andersen | d266748 | 2000-09-26 01:00:15 +0000 | [diff] [blame] | 129 | if (traceflag==1) { |
| 130 | fprintf(stderr, "%s\n", args); |
| 131 | } |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 132 | cmd[i++] = args; |
| 133 | cmd[i] = NULL; |
| 134 | execvp(cmd_to_be_executed, cmd); |
| 135 | |
| 136 | /* What? Still here? Exit with an error */ |
| 137 | fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno)); |
| 138 | } |
| 139 | /* Wait for a child process to exit */ |
| 140 | wpid = wait(&status); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | #ifdef BB_FEATURE_CLEAN_UP |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 144 | free(args); |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 145 | free(cmd_to_be_executed); |
| 146 | #endif |
| 147 | |
Eric Andersen | e081eae | 2000-09-25 20:23:21 +0000 | [diff] [blame] | 148 | if (wpid > 0) |
| 149 | return (WEXITSTATUS(status)); |
| 150 | else |
| 151 | return EXIT_FAILURE; |
Eric Andersen | 92a61c1 | 2000-09-22 20:01:23 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | a37d5b7 | 2000-09-23 06:10:14 +0000 | [diff] [blame] | 153 | /* |
| 154 | Local Variables: |
| 155 | c-file-style: "linux" |
| 156 | c-basic-offset: 4 |
| 157 | tab-width: 4 |
| 158 | End: |
| 159 | */ |