Module Name:    src
Committed By:   rillig
Date:           Sun Oct  7 23:17:52 UTC 2018

Modified Files:
        src/bin/sh: alias.c

Log Message:
When listing aliases, sort them alphabetically.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/bin/sh/alias.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/alias.c
diff -u src/bin/sh/alias.c:1.16 src/bin/sh/alias.c:1.17
--- src/bin/sh/alias.c:1.16	Mon Jul 24 12:34:45 2017
+++ src/bin/sh/alias.c	Sun Oct  7 23:17:52 2018
@@ -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 @@ __RCSID("$NetBSD: alias.c,v 1.16 2017/07
 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 @@ alias_text(void *dummy __unused, const c
 	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 @@ aliascmd(int argc, char **argv)
 	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) {

Reply via email to