summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko2007-11-13 22:23:57 +0000
committerDenis Vlasenko2007-11-13 22:23:57 +0000
commit8d0a734d91ff197a86ce0b8fc892e24a15783395 (patch)
tree6d120200aaec21430b677dca9559c1b09ccdf187 /coreutils
parent3d461676a90eecf584dc07317cdb52eba96aa2d9 (diff)
downloadbusybox-8d0a734d91ff197a86ce0b8fc892e24a15783395.zip
busybox-8d0a734d91ff197a86ce0b8fc892e24a15783395.tar.gz
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.c48
1 files changed, 18 insertions, 30 deletions
diff --git a/coreutils/tr.c b/coreutils/tr.c
index f6f458b..95f79b2 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -22,10 +22,6 @@
#define ASCII 0377
-#define TR_OPT_complement (1<<0)
-#define TR_OPT_delete (1<<1)
-#define TR_OPT_squeeze_reps (1<<2)
-
static void map(char *pvector,
unsigned char *string1, unsigned int string1_len,
unsigned char *string2, unsigned int string2_len)
@@ -180,47 +176,39 @@ static int complement(char *buffer, int buffer_len)
int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int tr_main(int argc, char **argv)
{
- unsigned char *ptr;
int output_length = 0, input_length;
- int idx = 1;
int i;
- smalluint flags = 0;
+ smalluint flags;
ssize_t read_chars = 0;
size_t in_index = 0, out_index = 0;
unsigned last = UCHAR_MAX + 1; /* not equal to any char */
unsigned char coded, c;
- RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
- RESERVE_CONFIG_BUFFER(vector, ASCII+1);
- RESERVE_CONFIG_BUFFER(invec, ASCII+1);
- RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
+ unsigned char *output = xmalloc(BUFSIZ);
+ char *vector = xzalloc((ASCII+1) * 3);
+ char *invec = vector + (ASCII+1);
+ char *outvec = vector + (ASCII+1) * 2;
+
+#define TR_OPT_complement (1 << 0)
+#define TR_OPT_delete (1 << 1)
+#define TR_OPT_squeeze_reps (1 << 2)
+
+ flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
+ argv += optind;
- if (argc > 1 && argv[idx][0] == '-') {
- for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
- if (*ptr == 'c')
- flags |= TR_OPT_complement;
- else if (*ptr == 'd')
- flags |= TR_OPT_delete;
- else if (*ptr == 's')
- flags |= TR_OPT_squeeze_reps;
- else
- bb_show_usage();
- }
- idx++;
- }
for (i = 0; i <= ASCII; i++) {
vector[i] = i;
- invec[i] = outvec[i] = FALSE;
+ /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
}
#define tr_buf bb_common_bufsiz1
- if (argv[idx] != NULL) {
- input_length = expand(argv[idx++], tr_buf);
+ if (*argv != NULL) {
+ input_length = expand(*argv++, tr_buf);
if (flags & TR_OPT_complement)
input_length = complement(tr_buf, input_length);
- if (argv[idx]) {
- if (argv[idx][0] == '\0')
+ if (*argv) {
+ if (argv[0][0] == '\0')
bb_error_msg_and_die("STRING2 cannot be empty");
- output_length = expand(argv[idx], output);
+ output_length = expand(*argv, output);
map(vector, tr_buf, input_length, output, output_length);
}
for (i = 0; i < input_length; i++)