diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 21:47:17 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 21:47:17 +0000 |
commit | f57674e9a9b0b29102981e1694b76170b78dfb43 (patch) | |
tree | 86dd9ea4d8b1bd757609849977321bbef83a2c6f /findutils | |
parent | 907a240b1c4bbc65f9010f5b2fd31157f9d083c3 (diff) | |
download | busybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.tar.gz busybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.tar.bz2 busybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.zip |
Rewrite of xargs by Vladimir N. Oleynik
Diffstat (limited to 'findutils')
-rw-r--r-- | findutils/xargs.c | 146 |
1 files changed, 84 insertions, 62 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c index 471bae45b..677618dc0 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c | |||
@@ -1,9 +1,10 @@ | |||
1 | /* | 1 | /* |
2 | * Mini xargs implementation for busybox | 2 | * Mini xargs implementation for busybox |
3 | * Only "-prt" options are supported in this version of xargs. | ||
3 | * | 4 | * |
4 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen | 5 | * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
5 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> | 6 | * |
6 | * Remixed by Mark Whitley <markw@codepoet.org> | 7 | * Special thanks Mark Whitley for stimul to rewrote :) |
7 | * | 8 | * |
8 | * This program is free software; you can redistribute it and/or modify | 9 | * 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 | * it under the terms of the GNU General Public License as published by |
@@ -23,84 +24,105 @@ | |||
23 | 24 | ||
24 | #include <stdio.h> | 25 | #include <stdio.h> |
25 | #include <stdlib.h> | 26 | #include <stdlib.h> |
26 | #include <string.h> | 27 | #include <unistd.h> |
28 | #include <getopt.h> | ||
29 | #include <errno.h> | ||
30 | #include <sys/types.h> | ||
31 | #include <sys/wait.h> | ||
27 | #include "busybox.h" | 32 | #include "busybox.h" |
28 | 33 | ||
34 | |||
35 | /* | ||
36 | This function have special algorithm. | ||
37 | Don`t use fork and include to main! | ||
38 | */ | ||
39 | static void xargs_exec(char * const * args) | ||
40 | { | ||
41 | int p; | ||
42 | int common[4]; /* shared vfork stack */ | ||
43 | |||
44 | common[0] = 0; | ||
45 | if ((p = vfork()) >= 0) { | ||
46 | if (p == 0) { | ||
47 | /* vfork -- child */ | ||
48 | execvp(args[0], args); | ||
49 | common[0] = errno; /* set error to shared stack */ | ||
50 | _exit(1); | ||
51 | } else { | ||
52 | /* vfork -- parent */ | ||
53 | wait(NULL); | ||
54 | if(common[0]) { | ||
55 | errno = common[0]; | ||
56 | perror_msg_and_die("%s", args[0]); | ||
57 | } | ||
58 | } | ||
59 | } else { | ||
60 | perror_msg_and_die("vfork"); | ||
61 | } | ||
62 | } | ||
63 | |||
29 | int xargs_main(int argc, char **argv) | 64 | int xargs_main(int argc, char **argv) |
30 | { | 65 | { |
31 | char *cmd_to_be_executed; | ||
32 | char *file_to_act_on; | 66 | char *file_to_act_on; |
33 | int i; | 67 | char **args; |
34 | int len; | 68 | int i, a; |
69 | char flg_vi = 0; /* verbose |& interactive */ | ||
70 | char flg_no_empty = 0; | ||
35 | 71 | ||
36 | /* | 72 | while ((a = getopt(argc, argv, "prt")) > 0) { |
37 | * No options are supported in this version of xargs; no getopt. | 73 | switch(a) { |
38 | * | 74 | case 'p': |
39 | * Re: The missing -t flag: Most programs that produce output also print | 75 | flg_vi |= 3; |
40 | * the filename, so xargs doesn't really need to do it again. Supporting | 76 | break; |
41 | * the -t flag =greatly= bloats up the size of this app and the memory it | 77 | case 't': |
42 | * uses because you have to buffer all the input file strings in memory. If | 78 | flg_vi |= 1; |
43 | * you really want to see the filenames that xargs will act on, just run it | 79 | break; |
44 | * once with no args and xargs will echo the filename. Simple. | 80 | case 'r': |
45 | */ | 81 | flg_no_empty = 1; |
82 | break; | ||
83 | default: | ||
84 | show_usage(); | ||
85 | } | ||
86 | } | ||
46 | 87 | ||
47 | argv++; | 88 | a = argc - optind; |
48 | len = argc; /* arg = count for ' ' + trailing '\0' */ | 89 | argv += optind; |
49 | /* Store the command to be executed (taken from the command line) */ | 90 | if(a==0) { |
50 | if (argc == 1) { | ||
51 | /* default behavior is to echo all the filenames */ | 91 | /* default behavior is to echo all the filenames */ |
52 | argv[0] = "/bin/echo"; | 92 | *argv = "/bin/echo"; |
53 | len++; /* space for trailing '\0' */ | 93 | a++; |
54 | } else { | ||
55 | argc--; | ||
56 | } | ||
57 | /* concatenate all the arguments passed to xargs together */ | ||
58 | for (i = 0; i < argc; i++) | ||
59 | len += strlen(argv[i]); | ||
60 | cmd_to_be_executed = xmalloc (len); | ||
61 | for (i = len = 0; i < argc; i++) { | ||
62 | len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]); | ||
63 | } | 94 | } |
95 | /* allocating pointers for execvp: a*arg, arg from stdin, NULL */ | ||
96 | args = xcalloc(a + 3, sizeof(char *)); | ||
97 | |||
98 | /* Store the command to be executed (taken from the command line) */ | ||
99 | for (i = 0; i < a; i++) | ||
100 | args[i] = *argv++; | ||
64 | 101 | ||
65 | /* Now, read in one line at a time from stdin, and store this | 102 | /* Now, read in one line at a time from stdin, and store this |
66 | * line to be used later as an argument to the command */ | 103 | * line to be used later as an argument to the command */ |
67 | while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { | 104 | while ((file_to_act_on = get_line_from_file(stdin)) != NULL) { |
68 | |||
69 | FILE *cmd_output; | ||
70 | char *output_line; | ||
71 | char *execstr; | ||
72 | |||
73 | /* eat the newline off the filename. */ | 105 | /* eat the newline off the filename. */ |
74 | chomp(file_to_act_on); | 106 | chomp(file_to_act_on); |
75 | 107 | if(file_to_act_on[0] != 0 || flg_no_empty == 0) { | |
76 | /* eat blank lines */ | 108 | args[a] = file_to_act_on[0] ? file_to_act_on : NULL; |
77 | if (file_to_act_on[0] == 0) | 109 | if(flg_vi) { |
78 | continue; | 110 | for(i=0; args[i]; i++) { |
79 | 111 | if(i) | |
80 | /* assemble the command and execute it */ | 112 | fputc(' ', stderr); |
81 | bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on); | 113 | fputs(args[i], stderr); |
82 | 114 | } | |
83 | cmd_output = popen(execstr, "r"); | 115 | fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n")); |
84 | if (cmd_output == NULL) | 116 | } |
85 | perror_msg_and_die("popen"); | 117 | if((flg_vi & 2) == 0 || ask_confirmation() != 0 ) { |
86 | 118 | xargs_exec(args); | |
87 | /* harvest the output */ | 119 | } |
88 | while ((output_line = get_line_from_file(cmd_output)) != NULL) { | ||
89 | fputs(output_line, stdout); | ||
90 | free(output_line); | ||
91 | } | 120 | } |
92 | |||
93 | /* clean up */ | 121 | /* clean up */ |
94 | pclose(cmd_output); | ||
95 | free(execstr); | ||
96 | free(file_to_act_on); | 122 | free(file_to_act_on); |
97 | } | 123 | } |
98 | |||
99 | #ifdef CONFIG_FEATURE_CLEAN_UP | 124 | #ifdef CONFIG_FEATURE_CLEAN_UP |
100 | free(cmd_to_be_executed); | 125 | free(args); |
101 | #endif | 126 | #endif |
102 | |||
103 | return 0; | 127 | return 0; |
104 | } | 128 | } |
105 | |||
106 | /* vi: set sw=4 ts=4: */ | ||