diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-13 22:23:57 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-11-13 22:23:57 +0000 |
commit | 8d0a734d91ff197a86ce0b8fc892e24a15783395 (patch) | |
tree | 6d120200aaec21430b677dca9559c1b09ccdf187 /coreutils | |
parent | 3d461676a90eecf584dc07317cdb52eba96aa2d9 (diff) | |
download | busybox-w32-8d0a734d91ff197a86ce0b8fc892e24a15783395.tar.gz busybox-w32-8d0a734d91ff197a86ce0b8fc892e24a15783395.tar.bz2 busybox-w32-8d0a734d91ff197a86ce0b8fc892e24a15783395.zip |
tr: more of code shrink (getopt32-ification)
runtest: cleanup. Method of getting $applets was truly bizarre
function old new delta
tr_main 655 527 -128
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/tr.c | 48 |
1 files changed, 18 insertions, 30 deletions
diff --git a/coreutils/tr.c b/coreutils/tr.c index f6f458bf0..95f79b269 100644 --- a/coreutils/tr.c +++ b/coreutils/tr.c | |||
@@ -22,10 +22,6 @@ | |||
22 | 22 | ||
23 | #define ASCII 0377 | 23 | #define ASCII 0377 |
24 | 24 | ||
25 | #define TR_OPT_complement (1<<0) | ||
26 | #define TR_OPT_delete (1<<1) | ||
27 | #define TR_OPT_squeeze_reps (1<<2) | ||
28 | |||
29 | static void map(char *pvector, | 25 | static void map(char *pvector, |
30 | unsigned char *string1, unsigned int string1_len, | 26 | unsigned char *string1, unsigned int string1_len, |
31 | unsigned char *string2, unsigned int string2_len) | 27 | unsigned char *string2, unsigned int string2_len) |
@@ -180,47 +176,39 @@ static int complement(char *buffer, int buffer_len) | |||
180 | int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 176 | int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
181 | int tr_main(int argc, char **argv) | 177 | int tr_main(int argc, char **argv) |
182 | { | 178 | { |
183 | unsigned char *ptr; | ||
184 | int output_length = 0, input_length; | 179 | int output_length = 0, input_length; |
185 | int idx = 1; | ||
186 | int i; | 180 | int i; |
187 | smalluint flags = 0; | 181 | smalluint flags; |
188 | ssize_t read_chars = 0; | 182 | ssize_t read_chars = 0; |
189 | size_t in_index = 0, out_index = 0; | 183 | size_t in_index = 0, out_index = 0; |
190 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ | 184 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ |
191 | unsigned char coded, c; | 185 | unsigned char coded, c; |
192 | RESERVE_CONFIG_UBUFFER(output, BUFSIZ); | 186 | unsigned char *output = xmalloc(BUFSIZ); |
193 | RESERVE_CONFIG_BUFFER(vector, ASCII+1); | 187 | char *vector = xzalloc((ASCII+1) * 3); |
194 | RESERVE_CONFIG_BUFFER(invec, ASCII+1); | 188 | char *invec = vector + (ASCII+1); |
195 | RESERVE_CONFIG_BUFFER(outvec, ASCII+1); | 189 | char *outvec = vector + (ASCII+1) * 2; |
190 | |||
191 | #define TR_OPT_complement (1 << 0) | ||
192 | #define TR_OPT_delete (1 << 1) | ||
193 | #define TR_OPT_squeeze_reps (1 << 2) | ||
194 | |||
195 | flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */ | ||
196 | argv += optind; | ||
196 | 197 | ||
197 | if (argc > 1 && argv[idx][0] == '-') { | ||
198 | for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) { | ||
199 | if (*ptr == 'c') | ||
200 | flags |= TR_OPT_complement; | ||
201 | else if (*ptr == 'd') | ||
202 | flags |= TR_OPT_delete; | ||
203 | else if (*ptr == 's') | ||
204 | flags |= TR_OPT_squeeze_reps; | ||
205 | else | ||
206 | bb_show_usage(); | ||
207 | } | ||
208 | idx++; | ||
209 | } | ||
210 | for (i = 0; i <= ASCII; i++) { | 198 | for (i = 0; i <= ASCII; i++) { |
211 | vector[i] = i; | 199 | vector[i] = i; |
212 | invec[i] = outvec[i] = FALSE; | 200 | /*invec[i] = outvec[i] = FALSE; - done by xzalloc */ |
213 | } | 201 | } |
214 | 202 | ||
215 | #define tr_buf bb_common_bufsiz1 | 203 | #define tr_buf bb_common_bufsiz1 |
216 | if (argv[idx] != NULL) { | 204 | if (*argv != NULL) { |
217 | input_length = expand(argv[idx++], tr_buf); | 205 | input_length = expand(*argv++, tr_buf); |
218 | if (flags & TR_OPT_complement) | 206 | if (flags & TR_OPT_complement) |
219 | input_length = complement(tr_buf, input_length); | 207 | input_length = complement(tr_buf, input_length); |
220 | if (argv[idx]) { | 208 | if (*argv) { |
221 | if (argv[idx][0] == '\0') | 209 | if (argv[0][0] == '\0') |
222 | bb_error_msg_and_die("STRING2 cannot be empty"); | 210 | bb_error_msg_and_die("STRING2 cannot be empty"); |
223 | output_length = expand(argv[idx], output); | 211 | output_length = expand(*argv, output); |
224 | map(vector, tr_buf, input_length, output, output_length); | 212 | map(vector, tr_buf, input_length, output, output_length); |
225 | } | 213 | } |
226 | for (i = 0; i < input_length; i++) | 214 | for (i = 0; i < input_length; i++) |