diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-19 19:13:06 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-19 19:13:06 +0200 |
| commit | d1ed3e68b8080161642cc106099c0a17ac7892e6 (patch) | |
| tree | d286f6fc0a7bf0ec68e2c27810ef7b3a0c863a5b /coreutils | |
| parent | fd19faf70569ca3173a94cf19ecc9b60efe3f480 (diff) | |
| download | busybox-w32-d1ed3e68b8080161642cc106099c0a17ac7892e6.tar.gz busybox-w32-d1ed3e68b8080161642cc106099c0a17ac7892e6.tar.bz2 busybox-w32-d1ed3e68b8080161642cc106099c0a17ac7892e6.zip | |
sort: code shrink
function old new delta
compare_keys 738 695 -43
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/sort.c | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index c2e8bb8de..f2bc5335f 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
| @@ -291,7 +291,7 @@ static int compare_keys(const void *xarg, const void *yarg) | |||
| 291 | else if (!yy) | 291 | else if (!yy) |
| 292 | retval = 1; | 292 | retval = 1; |
| 293 | else | 293 | else |
| 294 | retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon; | 294 | retval = dx - thyme.tm_mon; |
| 295 | break; | 295 | break; |
| 296 | } | 296 | } |
| 297 | /* Full floating point version of -n */ | 297 | /* Full floating point version of -n */ |
| @@ -317,8 +317,8 @@ static int compare_keys(const void *xarg, const void *yarg) | |||
| 317 | 317 | ||
| 318 | /* Perform fallback sort if necessary */ | 318 | /* Perform fallback sort if necessary */ |
| 319 | if (!retval && !(option_mask32 & FLAG_s)) { | 319 | if (!retval && !(option_mask32 & FLAG_s)) { |
| 320 | retval = strcmp(*(char **)xarg, *(char **)yarg); | ||
| 321 | flags = option_mask32; | 320 | flags = option_mask32; |
| 321 | retval = strcmp(*(char **)xarg, *(char **)yarg); | ||
| 322 | } | 322 | } |
| 323 | 323 | ||
| 324 | if (flags & FLAG_r) | 324 | if (flags & FLAG_r) |
| @@ -346,7 +346,7 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 346 | char *line, **lines; | 346 | char *line, **lines; |
| 347 | char *str_ignored, *str_o, *str_t; | 347 | char *str_ignored, *str_o, *str_t; |
| 348 | llist_t *lst_k = NULL; | 348 | llist_t *lst_k = NULL; |
| 349 | int i, flag; | 349 | int i; |
| 350 | int linecount; | 350 | int linecount; |
| 351 | unsigned opts; | 351 | unsigned opts; |
| 352 | 352 | ||
| @@ -369,7 +369,7 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 369 | /* note: below this point we use option_mask32, not opts, | 369 | /* note: below this point we use option_mask32, not opts, |
| 370 | * since that reduces register pressure and makes code smaller */ | 370 | * since that reduces register pressure and makes code smaller */ |
| 371 | 371 | ||
| 372 | /* parse sort key */ | 372 | /* Parse sort key */ |
| 373 | while (lst_k) { | 373 | while (lst_k) { |
| 374 | enum { | 374 | enum { |
| 375 | FLAG_allowed_for_k = | 375 | FLAG_allowed_for_k = |
| @@ -396,17 +396,18 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 396 | key->range[2*i+1] = str2u(&str_k); | 396 | key->range[2*i+1] = str2u(&str_k); |
| 397 | } | 397 | } |
| 398 | while (*str_k) { | 398 | while (*str_k) { |
| 399 | const char *temp2; | 399 | int flag; |
| 400 | const char *idx; | ||
| 400 | 401 | ||
| 401 | if (*str_k == ',' && !i++) { | 402 | if (*str_k == ',' && !i++) { |
| 402 | str_k++; | 403 | str_k++; |
| 403 | break; | 404 | break; |
| 404 | } /* no else needed: fall through to syntax error | 405 | } /* no else needed: fall through to syntax error |
| 405 | because comma isn't in OPT_STR */ | 406 | because comma isn't in OPT_STR */ |
| 406 | temp2 = strchr(OPT_STR, *str_k); | 407 | idx = strchr(OPT_STR, *str_k); |
| 407 | if (!temp2) | 408 | if (!idx) |
| 408 | bb_error_msg_and_die("unknown key option"); | 409 | bb_error_msg_and_die("unknown key option"); |
| 409 | flag = 1 << (temp2 - OPT_STR); | 410 | flag = 1 << (idx - OPT_STR); |
| 410 | if (flag & ~FLAG_allowed_for_k) | 411 | if (flag & ~FLAG_allowed_for_k) |
| 411 | bb_error_msg_and_die("unknown sort type"); | 412 | bb_error_msg_and_die("unknown sort type"); |
| 412 | /* b after ',' means strip _trailing_ space */ | 413 | /* b after ',' means strip _trailing_ space */ |
| @@ -440,10 +441,10 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 440 | } while (*++argv); | 441 | } while (*++argv); |
| 441 | 442 | ||
| 442 | #if ENABLE_FEATURE_SORT_BIG | 443 | #if ENABLE_FEATURE_SORT_BIG |
| 443 | /* if no key, perform alphabetic sort */ | 444 | /* If no key, perform alphabetic sort */ |
| 444 | if (!key_list) | 445 | if (!key_list) |
| 445 | add_key()->range[0] = 1; | 446 | add_key()->range[0] = 1; |
| 446 | /* handle -c */ | 447 | /* Handle -c */ |
| 447 | if (option_mask32 & FLAG_c) { | 448 | if (option_mask32 & FLAG_c) { |
| 448 | int j = (option_mask32 & FLAG_u) ? -1 : 0; | 449 | int j = (option_mask32 & FLAG_u) ? -1 : 0; |
| 449 | for (i = 1; i < linecount; i++) { | 450 | for (i = 1; i < linecount; i++) { |
| @@ -457,20 +458,21 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 457 | #endif | 458 | #endif |
| 458 | /* Perform the actual sort */ | 459 | /* Perform the actual sort */ |
| 459 | qsort(lines, linecount, sizeof(lines[0]), compare_keys); | 460 | qsort(lines, linecount, sizeof(lines[0]), compare_keys); |
| 460 | /* handle -u */ | 461 | |
| 462 | /* Handle -u */ | ||
| 461 | if (option_mask32 & FLAG_u) { | 463 | if (option_mask32 & FLAG_u) { |
| 462 | flag = 0; | 464 | int j = 0; |
| 463 | /* coreutils 6.3 drop lines for which only key is the same */ | 465 | /* coreutils 6.3 drop lines for which only key is the same */ |
| 464 | /* -- disabling last-resort compare... */ | 466 | /* -- disabling last-resort compare... */ |
| 465 | option_mask32 |= FLAG_s; | 467 | option_mask32 |= FLAG_s; |
| 466 | for (i = 1; i < linecount; i++) { | 468 | for (i = 1; i < linecount; i++) { |
| 467 | if (compare_keys(&lines[flag], &lines[i]) == 0) | 469 | if (compare_keys(&lines[j], &lines[i]) == 0) |
| 468 | free(lines[i]); | 470 | free(lines[i]); |
| 469 | else | 471 | else |
| 470 | lines[++flag] = lines[i]; | 472 | lines[++j] = lines[i]; |
| 471 | } | 473 | } |
| 472 | if (linecount) | 474 | if (linecount) |
| 473 | linecount = flag+1; | 475 | linecount = j+1; |
| 474 | } | 476 | } |
| 475 | 477 | ||
| 476 | /* Print it */ | 478 | /* Print it */ |
| @@ -479,9 +481,11 @@ int sort_main(int argc UNUSED_PARAM, char **argv) | |||
| 479 | if (option_mask32 & FLAG_o) | 481 | if (option_mask32 & FLAG_o) |
| 480 | xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); | 482 | xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); |
| 481 | #endif | 483 | #endif |
| 482 | flag = (option_mask32 & FLAG_z) ? '\0' : '\n'; | 484 | { |
| 483 | for (i = 0; i < linecount; i++) | 485 | int ch = (option_mask32 & FLAG_z) ? '\0' : '\n'; |
| 484 | printf("%s%c", lines[i], flag); | 486 | for (i = 0; i < linecount; i++) |
| 487 | printf("%s%c", lines[i], ch); | ||
| 488 | } | ||
| 485 | 489 | ||
| 486 | fflush_stdout_and_exit(EXIT_SUCCESS); | 490 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 487 | } | 491 | } |
