diff options
Diffstat (limited to 'tr.c')
-rw-r--r-- | tr.c | 40 |
1 files changed, 26 insertions, 14 deletions
@@ -34,14 +34,15 @@ | |||
34 | #define bb_need_write_error | 34 | #define bb_need_write_error |
35 | #include "messages.c" | 35 | #include "messages.c" |
36 | 36 | ||
37 | #define ASCII 0377 | 37 | static const int ASCII = 0377; |
38 | 38 | ||
39 | /* some glabals shared across this file */ | 39 | /* some glabals shared across this file */ |
40 | static char com_fl, del_fl, sq_fl; | 40 | static char com_fl, del_fl, sq_fl; |
41 | static unsigned char output[BUFSIZ], input[BUFSIZ]; | ||
42 | static unsigned char vector[ASCII + 1]; | ||
43 | static char invec[ASCII + 1], outvec[ASCII + 1]; | ||
44 | static short in_index, out_index; | 41 | static short in_index, out_index; |
42 | /* these last are pointers to static buffers declared in tr_main */ | ||
43 | static unsigned char *poutput, *pinput; | ||
44 | static unsigned char *pvector; | ||
45 | static char *pinvec, *poutvec; | ||
45 | 46 | ||
46 | 47 | ||
47 | static void convert() | 48 | static void convert() |
@@ -52,22 +53,22 @@ static void convert() | |||
52 | 53 | ||
53 | for (;;) { | 54 | for (;;) { |
54 | if (in_index == read_chars) { | 55 | if (in_index == read_chars) { |
55 | if ((read_chars = read(0, (char *) input, BUFSIZ)) <= 0) { | 56 | if ((read_chars = read(0, (char *) pinput, BUFSIZ)) <= 0) { |
56 | if (write(1, (char *) output, out_index) != out_index) | 57 | if (write(1, (char *) poutput, out_index) != out_index) |
57 | write(2, write_error, strlen(write_error)); | 58 | write(2, write_error, strlen(write_error)); |
58 | exit(0); | 59 | exit(0); |
59 | } | 60 | } |
60 | in_index = 0; | 61 | in_index = 0; |
61 | } | 62 | } |
62 | c = input[in_index++]; | 63 | c = pinput[in_index++]; |
63 | coded = vector[c]; | 64 | coded = pvector[c]; |
64 | if (del_fl && invec[c]) | 65 | if (del_fl && pinvec[c]) |
65 | continue; | 66 | continue; |
66 | if (sq_fl && last == coded && (invec[c] || outvec[coded])) | 67 | if (sq_fl && last == coded && (pinvec[c] || poutvec[coded])) |
67 | continue; | 68 | continue; |
68 | output[out_index++] = last = coded; | 69 | poutput[out_index++] = last = coded; |
69 | if (out_index == BUFSIZ) { | 70 | if (out_index == BUFSIZ) { |
70 | if (write(1, (char *) output, out_index) != out_index) { | 71 | if (write(1, (char *) poutput, out_index) != out_index) { |
71 | write(2, write_error, strlen(write_error)); | 72 | write(2, write_error, strlen(write_error)); |
72 | exit(1); | 73 | exit(1); |
73 | } | 74 | } |
@@ -86,9 +87,9 @@ static void map(register unsigned char *string1, unsigned int string1_len, | |||
86 | 87 | ||
87 | for (j = 0, i = 0; i < string1_len; i++) { | 88 | for (j = 0, i = 0; i < string1_len; i++) { |
88 | if (string2_len <= j) | 89 | if (string2_len <= j) |
89 | vector[string1[i]] = last; | 90 | pvector[string1[i]] = last; |
90 | else | 91 | else |
91 | vector[string1[i]] = last = string2[j++]; | 92 | pvector[string1[i]] = last = string2[j++]; |
92 | } | 93 | } |
93 | } | 94 | } |
94 | 95 | ||
@@ -143,6 +144,17 @@ extern int tr_main(int argc, char **argv) | |||
143 | int output_length=0, input_length; | 144 | int output_length=0, input_length; |
144 | int index = 1; | 145 | int index = 1; |
145 | int i; | 146 | int i; |
147 | /* set up big arrays here (better than making a bunch of static arrays up top) */ | ||
148 | unsigned char output[BUFSIZ], input[BUFSIZ]; | ||
149 | unsigned char vector[ASCII + 1]; | ||
150 | char invec[ASCII + 1], outvec[ASCII + 1]; | ||
151 | |||
152 | /* ... but make them available globally */ | ||
153 | poutput = output; | ||
154 | pinput = input; | ||
155 | pvector = vector; | ||
156 | pinvec = invec; | ||
157 | poutvec = outvec; | ||
146 | 158 | ||
147 | if (argc > 1 && argv[index][0] == '-') { | 159 | if (argc > 1 && argv[index][0] == '-') { |
148 | for (ptr = (unsigned char *) &argv[index][1]; *ptr; ptr++) { | 160 | for (ptr = (unsigned char *) &argv[index][1]; *ptr; ptr++) { |