summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/tr.c48
-rwxr-xr-xtestsuite/runtest70
2 files changed, 48 insertions, 70 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
29static void map(char *pvector, 25static 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)
180int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 176int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
181int tr_main(int argc, char **argv) 177int 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++)
diff --git a/testsuite/runtest b/testsuite/runtest
index 279f51fbd..4946cbd0d 100755
--- a/testsuite/runtest
+++ b/testsuite/runtest
@@ -1,9 +1,5 @@
1#!/bin/sh 1#!/bin/sh
2 2
3[ -n "$srcdir" ] || srcdir=$(pwd)
4[ -n "$bindir" ] || bindir=$(dirname $(pwd))
5PATH=$bindir:$PATH
6
7# Run old-style test. 3# Run old-style test.
8 4
9run_applet_testcase() 5run_applet_testcase()
@@ -35,9 +31,9 @@ run_applet_testcase()
35 mkdir -p tmp 31 mkdir -p tmp
36 pushd tmp > /dev/null 32 pushd tmp > /dev/null
37 33
38 d=$srcdir sh -x -e $testcase >.logfile.txt 2>&1 || status=$? 34 d=$tsdir sh -x -e $testcase >.logfile.txt 2>&1 || status=$?
39 35
40 if [ $status -ne 0 ] ; then 36 if [ $status -ne 0 ]; then
41 echo FAIL: $testname 37 echo FAIL: $testname
42 if [ $verbose -gt 0 ]; then 38 if [ $verbose -gt 0 ]; then
43 cat .logfile.txt 39 cat .logfile.txt
@@ -61,14 +57,11 @@ run_applet_tests()
61 57
62 local status=0 58 local status=0
63 59
64 for testcase in $srcdir/$applet/*; do 60 for testcase in $tsdir/$applet/*; do
65 if [ "$testcase" = "$srcdir/$applet/CVS" ]; then 61 if [ "$testcase" = "$tsdir/$applet/CVS" ]; then
66 continue 62 continue
67 fi 63 fi
68 64 if ! run_applet_testcase $applet $testcase; then
69 if run_applet_testcase $applet $testcase; then
70 :
71 else
72 status=1 65 status=1
73 fi 66 fi
74 done 67 done
@@ -80,64 +73,61 @@ run_applet_tests()
80status=0 73status=0
81verbose=0 74verbose=0
82 75
76[ -n "$tsdir" ] || tsdir=$(pwd)
77[ -n "$bindir" ] || bindir=$(dirname $(pwd))
78PATH="$bindir:$PATH"
79
83if [ x"$1" = x"-v" ]; then 80if [ x"$1" = x"-v" ]; then
84 verbose=1 81 verbose=1
85 export VERBOSE=$verbose 82 export VERBOSE=$verbose
86 shift 83 shift
87fi 84fi
88 85
86implemented=$(
87 $bindir/busybox 2>&1 |
88 while read line; do
89 if test x"$line" = x"Currently defined functions:"; then
90 xargs | sed 's/,//g'
91 break
92 fi
93 done
94 )
95
96applets="$implemented"
89if [ $# -ne 0 ]; then 97if [ $# -ne 0 ]; then
90 applets=$(cd $srcdir ; for i in $@; do ls ${i}* ; done) 98 applets="$@"
91else
92 applets=$(ls $srcdir)
93fi 99fi
94 100
95# Populate a directory with links to all busybox applets 101# Populate a directory with links to all busybox applets
96 102
97LINKSDIR="$bindir/runtest-tempdir-links" 103LINKSDIR="$bindir/runtest-tempdir-links"
98implemented=$($bindir/busybox 2>&1 |
99 while read line
100 do
101 if test x"$line" = x"Currently defined functions:"
102 then
103 xargs | sed 's/,//g'
104 break
105 fi
106 done
107 )
108rm -rf "$LINKSDIR" 2>/dev/null 104rm -rf "$LINKSDIR" 2>/dev/null
109mkdir "$LINKSDIR" 105mkdir "$LINKSDIR"
110for i in $implemented 106for i in $implemented; do
111do
112 ln -s $bindir/busybox "$LINKSDIR"/$i 107 ln -s $bindir/busybox "$LINKSDIR"/$i
113done 108done
114 109
115# Set up option flags so tests can be selective. 110# Set up option flags so tests can be selective.
116 111
117configfile=${bindir:-../../}/.config 112configfile=${bindir}/.config
118export OPTIONFLAGS=:$(echo $(sed -nr 's/^CONFIG_(.*)=.*/\1/p' $configfile) | sed 's/ /:/g') 113export OPTIONFLAGS=:$(sed -nr 's/^CONFIG_(.*)=.*/\1/p' $configfile | xargs | sed 's/ /:/g')
119 114
120for applet in $applets; do 115for applet in $applets; do
121 if [ "$applet" = "links" ]; then continue; fi 116 if [ "$applet" = "links" ]; then continue; fi
122 if [ "$applet" != "CVS" -a -d "$srcdir/$applet" ]; then 117 if [ "$applet" != "CVS" -a -d "$tsdir/$applet" ]; then
123 if run_applet_tests $applet; then 118 if ! run_applet_tests $applet; then
124 :
125 else
126 status=1 119 status=1
127 fi 120 fi
128 fi 121 fi
129 122
130 # Is this a new-style test? 123 # Is this a new-style test?
131 applet=$(echo "$applet" | sed -n 's/\.tests$//p') 124 if [ -f ${applet}.tests ]; then
132 if [ ${#applet} -ne 0 ] 125 if [ ! -h "$LINKSDIR/$applet" ] && [ "${applet:0:4}" != "all_" ]; then
133 then
134 if [ ! -h "$LINKSDIR/$applet" ] && [ "${applet:0:4}" != "all_" ]
135 then
136 echo "SKIPPED: $applet (not built)" 126 echo "SKIPPED: $applet (not built)"
137 continue 127 continue
138 fi 128 fi
139 if PATH="$LINKSDIR":$srcdir:$bindir:$PATH \ 129 if PATH="$LINKSDIR":$tsdir:$bindir:$PATH \
140 "${srcdir:-.}/$applet".tests 130 "${tsdir:-.}/$applet".tests
141 then 131 then
142 : 132 :
143 else 133 else