From owner-svn-src-head@FreeBSD.ORG Sun Mar 22 21:09:22 2009 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 66FC3106566B; Sun, 22 Mar 2009 21:09:22 +0000 (UTC) (envelope-from stefanf@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 55E0C8FC0A; Sun, 22 Mar 2009 21:09:22 +0000 (UTC) (envelope-from stefanf@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n2ML9Mac083737; Sun, 22 Mar 2009 21:09:22 GMT (envelope-from stefanf@svn.freebsd.org) Received: (from stefanf@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n2ML9MX2083736; Sun, 22 Mar 2009 21:09:22 GMT (envelope-from stefanf@svn.freebsd.org) Message-Id: <200903222109.n2ML9MX2083736@svn.freebsd.org> From: Stefan Farfeleder Date: Sun, 22 Mar 2009 21:09:22 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r190284 - head/bin/sh X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Mar 2009 21:09:23 -0000 Author: stefanf Date: Sun Mar 22 21:09:22 2009 New Revision: 190284 URL: http://svn.freebsd.org/changeset/base/190284 Log: - Apply the r190270 changes to printing of single aliases too. - Sort the aliases before printing them. Modified: head/bin/sh/alias.c Modified: head/bin/sh/alias.c ============================================================================== --- head/bin/sh/alias.c Sun Mar 22 21:00:58 2009 (r190283) +++ head/bin/sh/alias.c Sun Mar 22 21:09:22 2009 (r190284) @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #define ATABSIZE 39 STATIC struct alias *atab[ATABSIZE]; +STATIC int aliases; STATIC void setalias(char *, char *); STATIC int unalias(const char *); @@ -106,6 +107,7 @@ setalias(char *name, char *val) ap->flag = 0; ap->next = *app; *app = ap; + aliases++; INTON; } @@ -135,6 +137,7 @@ unalias(const char *name) ckfree(ap); INTON; } + aliases--; return (0); } } @@ -168,6 +171,7 @@ rmaliases(void) ckfree(tmp); } } + aliases = 0; INTON; } @@ -187,9 +191,47 @@ lookupalias(char *name, int check) return (NULL); } -/* - * TODO - sort output - */ +static int +comparealiases(const void *p1, const void *p2) +{ + const struct alias *const *a1 = p1; + const struct alias *const *a2 = p2; + + return strcmp((*a1)->name, (*a2)->name); +} + +static void +printalias(const struct alias *a) +{ + char *p; + + out1fmt("%s=", a->name); + /* Don't print the space added above. */ + p = a->val + strlen(a->val) - 1; + *p = '\0'; + out1qstr(a->val); + *p = ' '; + out1c('\n'); +} + +static void +printaliases(void) +{ + int i, j; + struct alias **sorted, *ap; + + sorted = ckmalloc(aliases * sizeof(*sorted)); + j = 0; + for (i = 0; i < ATABSIZE; i++) + for (ap = atab[i]; ap; ap = ap->next) + if (*ap->name != '\0') + sorted[j++] = ap; + qsort(sorted, aliases, sizeof(*sorted), comparealiases); + for (i = 0; i < aliases; i++) + printalias(sorted[i]); + ckfree(sorted); +} + int aliascmd(int argc, char **argv) { @@ -198,21 +240,7 @@ 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("%s=", ap->name); - /* Don't print the space added - * above. */ - v = ap->val + strlen(ap->val) - 1; - *v = '\0'; - out1qstr(ap->val); - *v = ' '; - out1c('\n'); - } - } + printaliases(); return (0); } while ((n = *++argv) != NULL) { @@ -220,11 +248,8 @@ aliascmd(int argc, char **argv) if ((ap = lookupalias(n, 0)) == NULL) { outfmt(out2, "alias: %s not found\n", n); ret = 1; - } else { - out1fmt("alias %s=", n); - out1qstr(ap->val); - out1c('\n'); - } + } else + printalias(ap); else { *v++ = '\0'; setalias(n, v);