diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-09-23 06:10:14 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-09-23 06:10:14 +0000 |
commit | a37d5b772b1851109605129de8331abf00f13ad4 (patch) | |
tree | b28e252fde2c7a82ed62c0097d1f8220360c10f0 /findutils/xargs.c | |
parent | 5b17693f0a07d0fa8af08e3c0d6b43c7b826a13d (diff) | |
download | busybox-w32-a37d5b772b1851109605129de8331abf00f13ad4.tar.gz busybox-w32-a37d5b772b1851109605129de8331abf00f13ad4.tar.bz2 busybox-w32-a37d5b772b1851109605129de8331abf00f13ad4.zip |
rewrite, so it should be firly clean now
Diffstat (limited to 'findutils/xargs.c')
-rw-r--r-- | findutils/xargs.c | 195 |
1 files changed, 97 insertions, 98 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c index be1fada78..24daf5085 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c | |||
@@ -1,116 +1,115 @@ | |||
1 | /* minix xargs - Make and execute commands | 1 | /* vi: set sw=4 ts=4: */ |
2 | * Author: Ian Nicholls: 1 Mar 90 */ | ||
3 | |||
4 | /* | 2 | /* |
5 | * xargs - Accept words from stdin until, combined with the arguments | 3 | * Mini xargs implementation for busybox |
6 | * given on the command line, just fit into the command line limit. | 4 | * |
7 | * Then, execute the result. | 5 | * Copyright (C) 2000 by Lineo, inc. |
8 | * e.g. ls | xargs compress | 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
9 | * find . -name '*.s' -print | xargs ar qv libc.a | ||
10 | * | 7 | * |
11 | * flags: -t Print the command just before it is run | 8 | * This program is free software; you can redistribute it and/or modify |
12 | * -l len Use len as maximum line length (default 490, max 1023) | 9 | * it under the terms of the GNU General Public License as published by |
13 | * -e ending Append ending to the command before executing it. | 10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. | ||
14 | * | 12 | * |
15 | * Exits with: 0 No errors. | 13 | * This program is distributed in the hope that it will be useful, |
16 | * 1 If any system(3) call returns a nonzero status. | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * 2 Usage error | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * 3 Line length too short to contain some single argument. | 16 | * General Public License for more details. |
19 | * | 17 | * |
20 | * Examples: xargs ar qv libc.a < liborder # Create a new libc.a | 18 | * You should have received a copy of the GNU General Public License |
21 | * find . -name '*.s' -print | xargs rm # Remove all .s files | 19 | * along with this program; if not, write to the Free Software |
22 | * find . -type f ! -name '*.Z' \ # Compress old files. | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 | * -atime +60 -print | xargs compress -v | ||
24 | * | 21 | * |
25 | * Bugs: If the command contains unquoted wildflags, then the system(3) call | ||
26 | * call may expand this to larger than the maximum line size. | ||
27 | * The command is not executed if nothing was read from stdin. | ||
28 | * xargs may give up too easily when the command returns nonzero. | ||
29 | */ | 22 | */ |
30 | #define USAGE "usage: xargs [-t] [-l len] [-e endargs] command [args...]\n" | ||
31 | 23 | ||
32 | #include <errno.h> | 24 | #include "internal.h" |
33 | #include <stdlib.h> | 25 | #include <stdlib.h> |
34 | #include <string.h> | ||
35 | #include <stdio.h> | 26 | #include <stdio.h> |
27 | #include <string.h> | ||
28 | #include <errno.h> | ||
36 | #include <getopt.h> | 29 | #include <getopt.h> |
37 | 30 | ||
38 | #ifndef MAX_ARGLINE | ||
39 | # define MAX_ARGLINE 1023 | ||
40 | #endif | ||
41 | #ifndef min | ||
42 | # define min(a,b) ((a) < (b) ? (a) : (b)) | ||
43 | #endif | ||
44 | |||
45 | char outlin[MAX_ARGLINE]; | ||
46 | char inlin[MAX_ARGLINE]; | ||
47 | char startlin[MAX_ARGLINE]; | ||
48 | char *ending = NULL; | ||
49 | char traceflag = 0; | ||
50 | 31 | ||
51 | int xargs_main(int ac, char **av) | 32 | int xargs_main(int argc, char **argv) |
52 | { | 33 | { |
53 | int outlen, inlen, startlen, endlen=0, i; | 34 | char *in_from_stdin = NULL; |
54 | char errflg = 0; | 35 | char *args_from_cmdline = NULL; |
55 | int maxlin = MAX_ARGLINE; | 36 | char *cmd_to_be_executed = NULL; |
37 | char traceflag = 0; | ||
38 | int len_args_from_cmdline, len_cmd_to_be_executed, len, opt; | ||
56 | 39 | ||
57 | while ((i = getopt(ac, av, "tl:e:")) != EOF) | 40 | while ((opt = getopt(argc, argv, "t")) != EOF) { |
58 | switch (i) { | 41 | switch (opt) { |
59 | case 't': traceflag = 1; break; | 42 | case 't': |
60 | case 'l': maxlin = min(MAX_ARGLINE, atoi(optarg)); break; | 43 | traceflag=1; |
61 | case 'e': ending = optarg; break; | 44 | break; |
62 | case '?': errflg++; break; | 45 | default: |
63 | } | 46 | fatalError(xargs_usage); |
64 | if (errflg) { | 47 | } |
65 | fprintf(stderr, USAGE); | 48 | } |
66 | exit(2); | ||
67 | } | ||
68 | 49 | ||
69 | startlin[0] = 0; | 50 | /* Store the command and arguments to be executed (from the command line) */ |
70 | if (optind == ac) { | 51 | if (optind == argc) { |
71 | strcat(startlin, "echo "); | 52 | len_args_from_cmdline = 6; |
72 | } | 53 | args_from_cmdline = xmalloc(len_args_from_cmdline); |
73 | else for ( ; optind < ac; optind++) { | 54 | strcat(args_from_cmdline, "echo "); |
74 | strcat(startlin, av[optind]); | 55 | } else { |
75 | strcat(startlin, " "); | 56 | opt=strlen(argv[optind]); |
76 | } | 57 | len_args_from_cmdline = (opt > 10)? opt : 10; |
77 | startlen = strlen(startlin); | 58 | args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char)); |
78 | if (ending) endlen = strlen(ending); | 59 | for (; optind < argc; optind++) { |
79 | maxlin = maxlin - 1 - endlen; /* Pre-compute */ | 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 | } | ||
80 | 71 | ||
81 | strcpy(outlin, startlin); | 72 | /* Set up some space for the command to be executed to be held in */ |
82 | outlen = startlen; | 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, " "); | ||
83 | 77 | ||
84 | while (gets(inlin) != NULL) { | 78 | /* Now, read in one line at a time from stdin, and run command+args on it */ |
85 | inlen = strlen(inlin); | 79 | in_from_stdin = get_line_from_file(stdin); |
86 | if (maxlin <= (outlen + inlen)) { | 80 | for (;in_from_stdin!=NULL;) { |
87 | if (outlen == startlen) { | 81 | len = strlen(in_from_stdin) + len_args_from_cmdline; |
88 | fprintf(stderr, "%s: Line length too short to process '%s'\n", | 82 | if ( len > len_cmd_to_be_executed ) { |
89 | av[0], inlin); | 83 | len_cmd_to_be_executed=len+3; |
90 | exit(3); | 84 | cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); |
91 | } | 85 | } |
92 | if (ending) strcat(outlin, ending); | 86 | strcat(cmd_to_be_executed, in_from_stdin); |
93 | if (traceflag) fputs(outlin,stderr); | 87 | strcat(cmd_to_be_executed+strlen(cmd_to_be_executed)-2, " "); |
94 | errno = 0; | 88 | strcat(cmd_to_be_executed, " "); |
95 | if (0 != system(outlin)) { | 89 | |
96 | if (errno != 0) perror("xargs"); | 90 | free(in_from_stdin); |
97 | exit(1); | 91 | in_from_stdin = get_line_from_file(stdin); |
98 | } | 92 | } |
99 | strcpy(outlin, startlin); | 93 | |
100 | outlen = startlen; | 94 | if (traceflag==1) |
101 | } | 95 | fputs(cmd_to_be_executed, stderr); |
102 | strcat(outlin, inlin); | 96 | |
103 | strcat(outlin, " "); | 97 | if ((system(cmd_to_be_executed) != 0) && (errno != 0)) |
104 | outlen = outlen + inlen + 1; | 98 | fatalError("%s", strerror(errno)); |
105 | } | 99 | |
106 | if (outlen != startlen) { | 100 | |
107 | if (ending) strcat(outlin, ending); | 101 | #ifdef BB_FEATURE_CLEAN_UP |
108 | if (traceflag) fputs(outlin,stderr); | 102 | free(args_from_cmdline); |
109 | errno = 0; | 103 | free(cmd_to_be_executed); |
110 | if (0 != system(outlin)) { | 104 | #endif |
111 | if (errno != 0) perror("xargs"); | 105 | |
112 | exit(1); | 106 | return 0; |
113 | } | ||
114 | } | ||
115 | return 0; | ||
116 | } | 107 | } |
108 | /* | ||
109 | Local Variables: | ||
110 | c-file-style: "linux" | ||
111 | c-basic-offset: 4 | ||
112 | tab-width: 4 | ||
113 | End: | ||
114 | */ | ||
115 | |||