aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/sort.c21
-rwxr-xr-xtestsuite/sort.tests36
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
107static char *get_key(char *str, struct sort_key *key, int flags) 107static 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
106a:b 106a:b
107" "" 107" ""
108 108
109testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
110GLIBC_2.1
111GLIBC_2.1.1
112GLIBC_2.2
113GLIBC_2.2.1
114GLIBC_2.10
115GLIBC_2.20
116GLIBC_2.21
117" "\
118GLIBC_2.21
119GLIBC_2.1.1
120GLIBC_2.2.1
121GLIBC_2.2
122GLIBC_2.20
123GLIBC_2.10
124GLIBC_2.1
125" ""
126
127testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\
128GLIBC_2.1
129GLIBC_2.1.1
130GLIBC_2.2
131GLIBC_2.2.1
132GLIBC_2.10
133GLIBC_2.20
134GLIBC_2.21
135" "\
136GLIBC_2.10
137GLIBC_2.2.1
138GLIBC_2.1.1
139GLIBC_2.20
140GLIBC_2.2
141GLIBC_2.1
142GLIBC_2.21
143" ""
144
109testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\ 145testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
110a c 146a c
111" "\ 147" "\