---
 shell/ash.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index bbd730770..be9abf756 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3536,9 +3536,14 @@ printalias(const struct alias *ap)
        out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
 }
 
-/*
- * TODO - sort output
- */
+static int
+aliascmp(const void *a, const void *b)
+{
+       const struct alias *const *pa = a;
+       const struct alias *const *pb = b;
+       return strcmp((*pa)->name, (*pb)->name);
+}
+
 static int FAST_FUNC
 aliascmd(int argc UNUSED_PARAM, char **argv)
 {
@@ -3547,12 +3552,32 @@ aliascmd(int argc UNUSED_PARAM, char **argv)
        struct alias *ap;
 
        if (!argv[1]) {
-               int i;
+               int i, count;
+               struct alias **list;
 
+               /* Count aliases */
+               count = 0;
                for (i = 0; i < ATABSIZE; i++) {
                        for (ap = atab[i]; ap; ap = ap->next) {
-                               printalias(ap);
+                               count++;
+                       }
+               }
+               if (count > 0) {
+                       /* Collect into array */
+                       list = ckmalloc(count * sizeof(*list));
+                       count = 0;
+                       for (i = 0; i < ATABSIZE; i++) {
+                               for (ap = atab[i]; ap; ap = ap->next) {
+                                       list[count++] = ap;
+                               }
+                       }
+                       /* Sort alphabetically */
+                       qsort(list, count, sizeof(*list), aliascmp);
+                       /* Print sorted */
+                       for (i = 0; i < count; i++) {
+                               printalias(list[i]);
                        }
+                       free(list);
                }
                return 0;
        }
-- 
2.34.1

_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to