aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/ls.c36
1 files changed, 10 insertions, 26 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index 9cbb4cab7..882eab8e7 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -396,8 +396,10 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
396 396
397/*----------------------------------------------------------------------*/ 397/*----------------------------------------------------------------------*/
398#ifdef CONFIG_FEATURE_LS_SORTFILES 398#ifdef CONFIG_FEATURE_LS_SORTFILES
399static int sortcmp(struct dnode *d1, struct dnode *d2) 399static int sortcmp(const void *a, const void *b)
400{ 400{
401 struct dnode *d1 = *(struct dnode **)a;
402 struct dnode *d2 = *(struct dnode **)b;
401 unsigned int sort_opts = all_fmt & SORT_MASK; 403 unsigned int sort_opts = all_fmt & SORT_MASK;
402 int dif; 404 int dif;
403 405
@@ -432,27 +434,9 @@ static int sortcmp(struct dnode *d1, struct dnode *d2)
432} 434}
433 435
434/*----------------------------------------------------------------------*/ 436/*----------------------------------------------------------------------*/
435static void shellsort(struct dnode **dn, int size) 437static void dnsort(struct dnode **dn, int size)
436{ 438{
437 struct dnode *temp; 439 qsort(dn, size, sizeof *dn, sortcmp);
438 int gap, i, j;
439
440 /* shell short the array */
441 if (dn == NULL || size < 2)
442 return;
443
444 for (gap = size / 2; gap > 0; gap /= 2) {
445 for (i = gap; i < size; i++) {
446 for (j = i - gap; j >= 0; j -= gap) {
447 if (sortcmp(dn[j], dn[j + gap]) <= 0)
448 break;
449 /* they are out of order, swap them */
450 temp = dn[j];
451 dn[j] = dn[j + gap];
452 dn[j + gap] = temp;
453 }
454 }
455 }
456} 440}
457#endif 441#endif
458 442
@@ -543,7 +527,7 @@ static void showdirs(struct dnode **dn, int ndirs, int first)
543 if (nfiles > 0) { 527 if (nfiles > 0) {
544 /* list all files at this level */ 528 /* list all files at this level */
545#ifdef CONFIG_FEATURE_LS_SORTFILES 529#ifdef CONFIG_FEATURE_LS_SORTFILES
546 shellsort(subdnp, nfiles); 530 dnsort(subdnp, nfiles);
547#endif 531#endif
548 showfiles(subdnp, nfiles); 532 showfiles(subdnp, nfiles);
549#ifdef CONFIG_FEATURE_LS_RECURSIVE 533#ifdef CONFIG_FEATURE_LS_RECURSIVE
@@ -553,7 +537,7 @@ static void showdirs(struct dnode **dn, int ndirs, int first)
553 dndirs = countsubdirs(subdnp, nfiles); 537 dndirs = countsubdirs(subdnp, nfiles);
554 if (dndirs > 0) { 538 if (dndirs > 0) {
555#ifdef CONFIG_FEATURE_LS_SORTFILES 539#ifdef CONFIG_FEATURE_LS_SORTFILES
556 shellsort(dnd, dndirs); 540 dnsort(dnd, dndirs);
557#endif 541#endif
558 showdirs(dnd, dndirs, 0); 542 showdirs(dnd, dndirs, 0);
559 free(dnd); /* free the array of dnode pointers to the dirs */ 543 free(dnd); /* free the array of dnode pointers to the dirs */
@@ -1135,7 +1119,7 @@ int ls_main(int argc, char **argv)
1135 1119
1136 if (all_fmt & DISP_NOLIST) { 1120 if (all_fmt & DISP_NOLIST) {
1137#ifdef CONFIG_FEATURE_LS_SORTFILES 1121#ifdef CONFIG_FEATURE_LS_SORTFILES
1138 shellsort(dnp, nfiles); 1122 dnsort(dnp, nfiles);
1139#endif 1123#endif
1140 if (nfiles > 0) 1124 if (nfiles > 0)
1141 showfiles(dnp, nfiles); 1125 showfiles(dnp, nfiles);
@@ -1146,7 +1130,7 @@ int ls_main(int argc, char **argv)
1146 dnfiles = nfiles - dndirs; 1130 dnfiles = nfiles - dndirs;
1147 if (dnfiles > 0) { 1131 if (dnfiles > 0) {
1148#ifdef CONFIG_FEATURE_LS_SORTFILES 1132#ifdef CONFIG_FEATURE_LS_SORTFILES
1149 shellsort(dnf, dnfiles); 1133 dnsort(dnf, dnfiles);
1150#endif 1134#endif
1151 showfiles(dnf, dnfiles); 1135 showfiles(dnf, dnfiles);
1152 if (ENABLE_FEATURE_CLEAN_UP) 1136 if (ENABLE_FEATURE_CLEAN_UP)
@@ -1154,7 +1138,7 @@ int ls_main(int argc, char **argv)
1154 } 1138 }
1155 if (dndirs > 0) { 1139 if (dndirs > 0) {
1156#ifdef CONFIG_FEATURE_LS_SORTFILES 1140#ifdef CONFIG_FEATURE_LS_SORTFILES
1157 shellsort(dnd, dndirs); 1141 dnsort(dnd, dndirs);
1158#endif 1142#endif
1159 showdirs(dnd, dndirs, dnfiles == 0); 1143 showdirs(dnd, dndirs, dnfiles == 0);
1160 if (ENABLE_FEATURE_CLEAN_UP) 1144 if (ENABLE_FEATURE_CLEAN_UP)