aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-01-18 13:02:27 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-01-18 13:02:27 +0100
commit7be97c5b6b0f186edd893ce3696047709f638cf6 (patch)
treedc033678465579843d93d5cd51f8ae1e81d649e5 /coreutils
parent573ba4e92e920df656e80ccdb11677315843ba7f (diff)
downloadbusybox-w32-7be97c5b6b0f186edd893ce3696047709f638cf6.tar.gz
busybox-w32-7be97c5b6b0f186edd893ce3696047709f638cf6.tar.bz2
busybox-w32-7be97c5b6b0f186edd893ce3696047709f638cf6.zip
ls: fix sort of very large files. +21 byte
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/ls.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index 153977f3f..b8da1adbb 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -481,35 +481,47 @@ static int sortcmp(const void *a, const void *b)
481 struct dnode *d1 = *(struct dnode **)a; 481 struct dnode *d1 = *(struct dnode **)a;
482 struct dnode *d2 = *(struct dnode **)b; 482 struct dnode *d2 = *(struct dnode **)b;
483 unsigned sort_opts = all_fmt & SORT_MASK; 483 unsigned sort_opts = all_fmt & SORT_MASK;
484 int dif; 484 off_t dif;
485 485
486 dif = 0; /* assume SORT_NAME */ 486 dif = 0; /* assume SORT_NAME */
487 // TODO: use pre-initialized function pointer 487 // TODO: use pre-initialized function pointer
488 // instead of branch forest 488 // instead of branch forest
489 if (sort_opts == SORT_SIZE) { 489 if (sort_opts == SORT_SIZE) {
490 dif = (int) (d2->dstat.st_size - d1->dstat.st_size); 490 dif = (d2->dstat.st_size - d1->dstat.st_size);
491 } else if (sort_opts == SORT_ATIME) { 491 } else if (sort_opts == SORT_ATIME) {
492 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime); 492 dif = (d2->dstat.st_atime - d1->dstat.st_atime);
493 } else if (sort_opts == SORT_CTIME) { 493 } else if (sort_opts == SORT_CTIME) {
494 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime); 494 dif = (d2->dstat.st_ctime - d1->dstat.st_ctime);
495 } else if (sort_opts == SORT_MTIME) { 495 } else if (sort_opts == SORT_MTIME) {
496 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime); 496 dif = (d2->dstat.st_mtime - d1->dstat.st_mtime);
497 } else if (sort_opts == SORT_DIR) { 497 } else if (sort_opts == SORT_DIR) {
498 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode); 498 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
499 /* } else if (sort_opts == SORT_VERSION) { */ 499 /* } else if (sort_opts == SORT_VERSION) { */
500 /* } else if (sort_opts == SORT_EXT) { */ 500 /* } else if (sort_opts == SORT_EXT) { */
501 } 501 }
502
503 if (dif == 0) { 502 if (dif == 0) {
504 /* sort by name - may be a tie_breaker for time or size cmp */ 503 /* sort by name, or tie_breaker for other sorts */
505 if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name); 504 if (ENABLE_LOCALE_SUPPORT)
506 else dif = strcmp(d1->name, d2->name); 505 dif = strcoll(d1->name, d2->name);
506 else
507 dif = strcmp(d1->name, d2->name);
507 } 508 }
508 509
509 if (all_fmt & SORT_REVERSE) { 510 /* Make dif fit into an int */
510 dif = -dif; 511 if (sizeof(dif) > sizeof(int)) {
512 if (sizeof(dif) == sizeof(int)*2) {
513 /* typical on many arches */
514 if (dif != 0) {
515 dif = 1 | (int)((uoff_t)dif >> (sizeof(int)*8));
516 }
517 } else {
518 while ((dif & ~(off_t)INT_MAX) != 0) {
519 dif >>= (sizeof(int)*8 / 2);
520 }
521 }
511 } 522 }
512 return dif; 523
524 return (all_fmt & SORT_REVERSE) ? -(int)dif : (int)dif;
513} 525}
514 526
515static void dnsort(struct dnode **dn, int size) 527static void dnsort(struct dnode **dn, int size)