From owner-svn-src-all@FreeBSD.ORG Sun Apr 11 12:02:14 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 497B01065670; Sun, 11 Apr 2010 12:02:14 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 377628FC1C; Sun, 11 Apr 2010 12:02:14 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o3BC2EWc091078; Sun, 11 Apr 2010 12:02:14 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o3BC2ED7091075; Sun, 11 Apr 2010 12:02:14 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201004111202.o3BC2ED7091075@svn.freebsd.org> From: Ed Schouten Date: Sun, 11 Apr 2010 12:02:14 +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: r206471 - head/usr.sbin/lastlogin X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 11 Apr 2010 12:02:14 -0000 Author: ed Date: Sun Apr 11 12:02:13 2010 New Revision: 206471 URL: http://svn.freebsd.org/changeset/base/206471 Log: Alphabetically sort the output of lastlogin(8). According to the manpage, the entries have to be sorted by uid. This is no longer possible, since our utmpx implementation is completely unaware of user IDs. You can safely add entries for multiple users sharing the same uid. Make the output less random by sorting everything by name. Modified: head/usr.sbin/lastlogin/lastlogin.8 head/usr.sbin/lastlogin/lastlogin.c Modified: head/usr.sbin/lastlogin/lastlogin.8 ============================================================================== --- head/usr.sbin/lastlogin/lastlogin.8 Sun Apr 11 11:51:44 2010 (r206470) +++ head/usr.sbin/lastlogin/lastlogin.8 Sun Apr 11 12:02:13 2010 (r206471) @@ -55,7 +55,7 @@ If more than one is given, the session information for each user is printed in the order given on the command line. Otherwise, information -for all users is printed, sorted by uid. +for all users is printed, sorted by name. .Pp The .Nm Modified: head/usr.sbin/lastlogin/lastlogin.c ============================================================================== --- head/usr.sbin/lastlogin/lastlogin.c Sun Apr 11 11:51:44 2010 (r206470) +++ head/usr.sbin/lastlogin/lastlogin.c Sun Apr 11 12:02:13 2010 (r206471) @@ -39,6 +39,7 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998 #include #include #include +#include #include #include #include @@ -47,11 +48,19 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998 static void output(struct utmpx *); static void usage(void); +static int +utcmp(const void *u1, const void *u2) +{ + + return (strcmp(((const struct utmpx *)u1)->ut_user, + ((const struct utmpx *)u2)->ut_user)); +} + int main(int argc, char *argv[]) { - int ch, i; - struct utmpx *u; + int ch, i, ulistsize; + struct utmpx *u, *ulist; while ((ch = getopt(argc, argv, "")) != -1) { usage(); @@ -74,12 +83,21 @@ main(int argc, char *argv[]) else { if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) errx(1, "failed to open lastlog database"); + ulist = NULL; + ulistsize = 0; while ((u = getutxent()) != NULL) { if (u->ut_type != USER_PROCESS) continue; - output(u); + if ((ulistsize % 16) == 0) + ulist = realloc(ulist, + (ulistsize + 16) * sizeof(struct utmpx)); + ulist[ulistsize++] = *u; } endutxent(); + + qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp); + for (i = 0; i < ulistsize; i++) + output(&ulist[i]); } exit(0);