diff options
-rw-r--r-- | coreutils/sort.c | 21 | ||||
-rwxr-xr-x | testsuite/sort.tests | 36 |
2 files changed, 52 insertions, 5 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index 36f02543b..7fb4df5bd 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
@@ -106,7 +106,9 @@ static struct sort_key { | |||
106 | 106 | ||
107 | static char *get_key(char *str, struct sort_key *key, int flags) | 107 | static char *get_key(char *str, struct sort_key *key, int flags) |
108 | { | 108 | { |
109 | int start = 0, end = 0, len, j; | 109 | int start = start; /* for compiler */ |
110 | int end; | ||
111 | int len, j; | ||
110 | unsigned i; | 112 | unsigned i; |
111 | 113 | ||
112 | /* Special case whole string, so we don't have to make a copy */ | 114 | /* Special case whole string, so we don't have to make a copy */ |
@@ -123,12 +125,15 @@ static char *get_key(char *str, struct sort_key *key, int flags) | |||
123 | end = len; | 125 | end = len; |
124 | /* Loop through fields */ | 126 | /* Loop through fields */ |
125 | else { | 127 | else { |
128 | unsigned char ch = 0; | ||
129 | |||
126 | end = 0; | 130 | end = 0; |
127 | for (i = 1; i < key->range[2*j] + j; i++) { | 131 | for (i = 1; i < key->range[2*j] + j; i++) { |
128 | if (key_separator) { | 132 | if (key_separator) { |
129 | /* Skip body of key and separator */ | 133 | /* Skip body of key and separator */ |
130 | while (str[end]) { | 134 | while ((ch = str[end]) != '\0') { |
131 | if (str[end++] == key_separator) | 135 | end++; |
136 | if (ch == key_separator) | ||
132 | break; | 137 | break; |
133 | } | 138 | } |
134 | } else { | 139 | } else { |
@@ -136,7 +141,7 @@ static char *get_key(char *str, struct sort_key *key, int flags) | |||
136 | while (isspace(str[end])) | 141 | while (isspace(str[end])) |
137 | end++; | 142 | end++; |
138 | /* Skip body of key */ | 143 | /* Skip body of key */ |
139 | while (str[end]) { | 144 | while (str[end] != '\0') { |
140 | if (isspace(str[end])) | 145 | if (isspace(str[end])) |
141 | break; | 146 | break; |
142 | end++; | 147 | end++; |
@@ -144,11 +149,17 @@ static char *get_key(char *str, struct sort_key *key, int flags) | |||
144 | } | 149 | } |
145 | } | 150 | } |
146 | /* Remove last delim: "abc:def:" => "abc:def" */ | 151 | /* Remove last delim: "abc:def:" => "abc:def" */ |
147 | if (key_separator && j && end != 0) | 152 | if (j && ch) { |
153 | //if (str[end-1] != key_separator) | ||
154 | // bb_error_msg(_and_die("BUG! " | ||
155 | // "str[start:%d,end:%d]:'%.*s'", | ||
156 | // start, end, (int)(end-start), &str[start]); | ||
148 | end--; | 157 | end--; |
158 | } | ||
149 | } | 159 | } |
150 | if (!j) start = end; | 160 | if (!j) start = end; |
151 | } | 161 | } |
162 | //bb_error_msg("start:%d,end:%d", start, end); | ||
152 | /* Strip leading whitespace if necessary */ | 163 | /* Strip leading whitespace if necessary */ |
153 | //XXX: skip_whitespace() | 164 | //XXX: skip_whitespace() |
154 | if (flags & FLAG_b) | 165 | if (flags & FLAG_b) |
diff --git a/testsuite/sort.tests b/testsuite/sort.tests index c4b223464..39c7af738 100755 --- a/testsuite/sort.tests +++ b/testsuite/sort.tests | |||
@@ -106,6 +106,42 @@ a/a:a | |||
106 | a:b | 106 | a:b |
107 | " "" | 107 | " "" |
108 | 108 | ||
109 | testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\ | ||
110 | GLIBC_2.1 | ||
111 | GLIBC_2.1.1 | ||
112 | GLIBC_2.2 | ||
113 | GLIBC_2.2.1 | ||
114 | GLIBC_2.10 | ||
115 | GLIBC_2.20 | ||
116 | GLIBC_2.21 | ||
117 | " "\ | ||
118 | GLIBC_2.21 | ||
119 | GLIBC_2.1.1 | ||
120 | GLIBC_2.2.1 | ||
121 | GLIBC_2.2 | ||
122 | GLIBC_2.20 | ||
123 | GLIBC_2.10 | ||
124 | GLIBC_2.1 | ||
125 | " "" | ||
126 | |||
127 | testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\ | ||
128 | GLIBC_2.1 | ||
129 | GLIBC_2.1.1 | ||
130 | GLIBC_2.2 | ||
131 | GLIBC_2.2.1 | ||
132 | GLIBC_2.10 | ||
133 | GLIBC_2.20 | ||
134 | GLIBC_2.21 | ||
135 | " "\ | ||
136 | GLIBC_2.10 | ||
137 | GLIBC_2.2.1 | ||
138 | GLIBC_2.1.1 | ||
139 | GLIBC_2.20 | ||
140 | GLIBC_2.2 | ||
141 | GLIBC_2.1 | ||
142 | GLIBC_2.21 | ||
143 | " "" | ||
144 | |||
109 | testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\ | 145 | testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\ |
110 | a c | 146 | a c |
111 | " "\ | 147 | " "\ |