Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/coreutils/sort.c
- Timestamp:
- Dec 20, 2016, 4:07:32 PM (7 years ago)
- Location:
- branches/3.3
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3/mindi-busybox/coreutils/sort.c
r3232 r3621 15 15 //usage:#define sort_trivial_usage 16 16 //usage: "[-nru" 17 //usage: IF_FEATURE_SORT_BIG("gMcszbdfi mSTokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")17 //usage: IF_FEATURE_SORT_BIG("gMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR") 18 18 //usage: "] [FILE]..." 19 19 //usage:#define sort_full_usage "\n\n" 20 20 //usage: "Sort lines of text\n" 21 21 //usage: IF_FEATURE_SORT_BIG( 22 //usage: "\n -o FILE Output to FILE" 23 //usage: "\n -c Check whether input is sorted" 22 24 //usage: "\n -b Ignore leading blanks" 23 //usage: "\n -c Check whether input is sorted" 25 //usage: "\n -f Ignore case" 26 //usage: "\n -i Ignore unprintable characters" 24 27 //usage: "\n -d Dictionary order (blank or alphanumeric only)" 25 //usage: "\n -f Ignore case"26 28 //usage: "\n -g General numerical sort" 27 //usage: "\n -i Ignore unprintable characters"28 //usage: "\n -k Sort key"29 29 //usage: "\n -M Sort month" 30 30 //usage: ) 31 //-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G) 31 32 //usage: "\n -n Sort numbers" 32 33 //usage: IF_FEATURE_SORT_BIG( 33 //usage: "\n -o Output to file" 34 //usage: "\n -k Sort by key" 35 //usage: "\n -t CHAR Key separator" 34 //usage: "\n -t CHAR Field separator" 35 //usage: "\n -k N[,M] Sort by Nth field" 36 36 //usage: ) 37 37 //usage: "\n -r Reverse sort order" … … 42 42 //usage: IF_FEATURE_SORT_BIG( 43 43 //usage: "\n -z Lines are terminated by NUL, not newline" 44 //usage: "\n -mST Ignored for GNU compatibility") 44 ////usage: "\n -m Ignored for GNU compatibility" 45 ////usage: "\n -S BUFSZ Ignored for GNU compatibility" 46 ////usage: "\n -T TMPDIR Ignored for GNU compatibility" 47 //usage: ) 45 48 //usage: 46 49 //usage:#define sort_example_usage … … 107 110 static char *get_key(char *str, struct sort_key *key, int flags) 108 111 { 109 int start = 0, end = 0, len, j; 112 int start = start; /* for compiler */ 113 int end; 114 int len, j; 110 115 unsigned i; 111 116 … … 124 129 /* Loop through fields */ 125 130 else { 131 unsigned char ch = 0; 132 126 133 end = 0; 127 134 for (i = 1; i < key->range[2*j] + j; i++) { 128 135 if (key_separator) { 129 136 /* Skip body of key and separator */ 130 while (str[end]) { 131 if (str[end++] == key_separator) 137 while ((ch = str[end]) != '\0') { 138 end++; 139 if (ch == key_separator) 132 140 break; 133 141 } … … 137 145 end++; 138 146 /* Skip body of key */ 139 while (str[end] ) {147 while (str[end] != '\0') { 140 148 if (isspace(str[end])) 141 149 break; … … 144 152 } 145 153 } 154 /* Remove last delim: "abc:def:" => "abc:def" */ 155 if (j && ch) { 156 //if (str[end-1] != key_separator) 157 // bb_error_msg(_and_die("BUG! " 158 // "str[start:%d,end:%d]:'%.*s'", 159 // start, end, (int)(end-start), &str[start]); 160 end--; 161 } 146 162 } 147 163 if (!j) start = end; 148 164 } 149 165 /* Strip leading whitespace if necessary */ 150 //XXX: skip_whitespace()151 166 if (flags & FLAG_b) 167 /* not using skip_whitespace() for speed */ 152 168 while (isspace(str[start])) start++; 153 169 /* Strip trailing whitespace if necessary */ 154 170 if (flags & FLAG_bb) 155 171 while (end > start && isspace(str[end-1])) end--; 156 /* Handle offsets on start and end*/172 /* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */ 157 173 if (key->range[3]) { 158 end += key->range[3] - 1;174 end = key->range[3]; 159 175 if (end > len) end = len; 160 176 } 177 /* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */ 161 178 if (key->range[1]) { 162 179 start += key->range[1] - 1; … … 164 181 } 165 182 /* Make the copy */ 166 if (end < start) end = start; 183 if (end < start) 184 end = start; 167 185 str = xstrndup(str+start, end-start); 168 186 /* Handle -d */ … … 226 244 #endif 227 245 /* Perform actual comparison */ 228 switch (flags & 7) {246 switch (flags & (FLAG_n | FLAG_M | FLAG_g)) { 229 247 default: 230 248 bb_error_msg_and_die("unknown sort type"); … … 278 296 retval = 1; 279 297 else 280 retval = (dx == thyme.tm_mon) ? 0 :dx - thyme.tm_mon;298 retval = dx - thyme.tm_mon; 281 299 break; 282 300 } … … 303 321 304 322 /* Perform fallback sort if necessary */ 305 if (!retval && !(option_mask32 & FLAG_s)) 323 if (!retval && !(option_mask32 & FLAG_s)) { 324 flags = option_mask32; 306 325 retval = strcmp(*(char **)xarg, *(char **)yarg); 307 308 if (flags & FLAG_r) return -retval; 326 } 327 328 if (flags & FLAG_r) 329 return -retval; 330 309 331 return retval; 310 332 } … … 329 351 char *str_ignored, *str_o, *str_t; 330 352 llist_t *lst_k = NULL; 331 int i , flag;353 int i; 332 354 int linecount; 333 355 unsigned opts; … … 352 374 * since that reduces register pressure and makes code smaller */ 353 375 354 /* parse sort key */376 /* Parse sort key */ 355 377 while (lst_k) { 356 378 enum { … … 379 401 } 380 402 while (*str_k) { 381 const char *temp2; 403 int flag; 404 const char *idx; 382 405 383 406 if (*str_k == ',' && !i++) { … … 386 409 } /* no else needed: fall through to syntax error 387 410 because comma isn't in OPT_STR */ 388 temp2= strchr(OPT_STR, *str_k);389 if (! temp2)411 idx = strchr(OPT_STR, *str_k); 412 if (!idx) 390 413 bb_error_msg_and_die("unknown key option"); 391 flag = 1 << ( temp2- OPT_STR);414 flag = 1 << (idx - OPT_STR); 392 415 if (flag & ~FLAG_allowed_for_k) 393 416 bb_error_msg_and_die("unknown sort type"); … … 423 446 424 447 #if ENABLE_FEATURE_SORT_BIG 425 /* if no key, perform alphabetic sort */448 /* If no key, perform alphabetic sort */ 426 449 if (!key_list) 427 450 add_key()->range[0] = 1; 428 /* handle -c */451 /* Handle -c */ 429 452 if (option_mask32 & FLAG_c) { 430 453 int j = (option_mask32 & FLAG_u) ? -1 : 0; … … 440 463 /* Perform the actual sort */ 441 464 qsort(lines, linecount, sizeof(lines[0]), compare_keys); 442 /* handle -u */ 465 466 /* Handle -u */ 443 467 if (option_mask32 & FLAG_u) { 444 flag= 0;468 int j = 0; 445 469 /* coreutils 6.3 drop lines for which only key is the same */ 446 470 /* -- disabling last-resort compare... */ 447 471 option_mask32 |= FLAG_s; 448 472 for (i = 1; i < linecount; i++) { 449 if (compare_keys(&lines[ flag], &lines[i]) == 0)473 if (compare_keys(&lines[j], &lines[i]) == 0) 450 474 free(lines[i]); 451 475 else 452 lines[++ flag] = lines[i];476 lines[++j] = lines[i]; 453 477 } 454 478 if (linecount) 455 linecount = flag+1;479 linecount = j+1; 456 480 } 457 481 … … 462 486 xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); 463 487 #endif 464 flag = (option_mask32 & FLAG_z) ? '\0' : '\n'; 465 for (i = 0; i < linecount; i++) 466 printf("%s%c", lines[i], flag); 488 { 489 int ch = (option_mask32 & FLAG_z) ? '\0' : '\n'; 490 for (i = 0; i < linecount; i++) 491 printf("%s%c", lines[i], ch); 492 } 467 493 468 494 fflush_stdout_and_exit(EXIT_SUCCESS);
Note:
See TracChangeset
for help on using the changeset viewer.