Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh When listing aliases, sort them alphabetically.



details:   https://anonhg.NetBSD.org/src/rev/922e686bf59f
branches:  trunk
changeset: 433848:922e686bf59f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Oct 07 23:17:52 2018 +0000

description:
When listing aliases, sort them alphabetically.

diffstat:

 bin/sh/alias.c |  62 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 47 insertions(+), 15 deletions(-)

diffs (99 lines):

diff -r cd1e1e94466c -r 922e686bf59f bin/sh/alias.c
--- a/bin/sh/alias.c    Sun Oct 07 20:30:50 2018 +0000
+++ b/bin/sh/alias.c    Sun Oct 07 23:17:52 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: alias.c,v 1.16 2017/07/24 12:34:45 kre Exp $   */
+/*     $NetBSD: alias.c,v 1.17 2018/10/07 23:17:52 rillig Exp $        */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)alias.c    8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: alias.c,v 1.16 2017/07/24 12:34:45 kre Exp $");
+__RCSID("$NetBSD: alias.c,v 1.17 2018/10/07 23:17:52 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -58,6 +58,8 @@
 struct alias *atab[ATABSIZE];
 
 STATIC void setalias(char *, char *);
+STATIC int by_name(const void *, const void *);
+STATIC void list_aliases(void);
 STATIC int unalias(char *);
 STATIC struct alias **hashalias(const char *);
 
@@ -204,9 +206,47 @@
        return ap->val;
 }
 
-/*
- * TODO - sort output
- */
+STATIC int
+by_name(const void *a, const void *b)
+{
+
+       return strcmp(
+               (*(const struct alias * const *)a)->name,
+               (*(const struct alias * const *)b)->name);
+}
+
+STATIC void
+list_aliases(void)
+{
+       size_t i, j, n;
+       const struct alias **aliases;
+       const struct alias *ap;
+
+       n = 0;
+       for (i = 0; i < ATABSIZE; i++)
+               for (ap = atab[i]; ap != NULL; ap = ap->next)
+                       if (ap->name[0] != '\0')
+                               n++;
+
+       aliases = ckmalloc(n * sizeof aliases[0]);
+
+       j = 0;
+       for (i = 0; i < ATABSIZE; i++)
+               for (ap = atab[i]; ap != NULL; ap = ap->next)
+                       if (ap->name[0] != '\0')
+                               aliases[j++] = ap;
+
+       qsort(aliases, n, sizeof aliases[0], by_name);
+
+       for (i = 0; i < n; i++) {
+               out1fmt("alias %s=", aliases[i]->name);
+               print_quoted(aliases[i]->val);
+               out1c('\n');
+       }
+
+       ckfree(aliases);
+}
+
 int
 aliascmd(int argc, char **argv)
 {
@@ -215,18 +255,10 @@
        struct alias *ap;
 
        if (argc == 1) {
-               int i;
-
-               for (i = 0; i < ATABSIZE; i++)
-                       for (ap = atab[i]; ap; ap = ap->next) {
-                               if (*ap->name != '\0') {
-                                       out1fmt("alias %s=", ap->name);
-                                       print_quoted(ap->val);
-                                       out1c('\n');
-                               }
-                       }
+               list_aliases();
                return (0);
        }
+
        while ((n = *++argv) != NULL) {
                if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
                        if ((ap = lookupalias(n, 0)) == NULL) {



Home | Main Index | Thread Index | Old Index