From owner-svn-src-head@FreeBSD.ORG Thu Dec 3 16:42:19 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 4343B1065697; Thu, 3 Dec 2009 16:42:19 +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 279CB8FC1E; Thu, 3 Dec 2009 16:42:19 +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 nB3GgJYe019820; Thu, 3 Dec 2009 16:42:19 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id nB3GgJHh019817; Thu, 3 Dec 2009 16:42:19 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <200912031642.nB3GgJHh019817@svn.freebsd.org> From: Ed Schouten Date: Thu, 3 Dec 2009 16:42:19 +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: r200066 - head/usr.bin/users 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: Thu, 03 Dec 2009 16:42:19 -0000 Author: ed Date: Thu Dec 3 16:42:18 2009 New Revision: 200066 URL: http://svn.freebsd.org/changeset/base/200066 Log: Port users(1) to libulog. Instead of digging through the utmp database by hand, use proper API calls to do so. Instead of parsing entries with a non-empty ut_user, we now look at LOGIN_PROCESS entries. Modified: head/usr.bin/users/Makefile head/usr.bin/users/users.c Modified: head/usr.bin/users/Makefile ============================================================================== --- head/usr.bin/users/Makefile Thu Dec 3 16:33:47 2009 (r200065) +++ head/usr.bin/users/Makefile Thu Dec 3 16:42:18 2009 (r200066) @@ -3,4 +3,7 @@ PROG= users +DPADD= ${LIBULOG} +LDADD= -lulog + .include Modified: head/usr.bin/users/users.c ============================================================================== --- head/usr.bin/users/users.c Thu Dec 3 16:33:47 2009 (r200065) +++ head/usr.bin/users/users.c Thu Dec 3 16:42:18 2009 (r200066) @@ -45,15 +45,16 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ +#include #include #include #include #include #include +#include #include -#include -typedef char namebuf[UT_NAMESIZE]; +typedef char namebuf[MAXLOGNAME]; int scmp(const void *, const void *); static void usage(void); @@ -65,7 +66,7 @@ main(int argc, char **argv) int ncnt = 0; int nmax = 0; int cnt; - struct utmp utmp; + struct ulog_utmpx *ut; int ch; while ((ch = getopt(argc, argv, "")) != -1) @@ -77,28 +78,28 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (!freopen(_PATH_UTMP, "r", stdin)) - errx(1, "can't open %s", _PATH_UTMP); - while (fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1) { - if (*utmp.ut_name) { - if (ncnt >= nmax) { - nmax += 32; - names = realloc(names, sizeof (*names) * nmax); - if (!names) { - errx(1, "realloc"); - /* NOTREACHED */ - } + ulog_setutxent(); + while ((ut = ulog_getutxent()) != NULL) { + if (ut->ut_type != LOGIN_PROCESS) + continue; + if (ncnt >= nmax) { + nmax += 32; + names = realloc(names, sizeof(*names) * nmax); + if (!names) { + errx(1, "realloc"); + /* NOTREACHED */ } - (void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE); - ++ncnt; } + (void)strlcpy(names[ncnt], ut->ut_user, sizeof(*names)); + ++ncnt; } - if (ncnt) { - qsort(names, ncnt, UT_NAMESIZE, scmp); - (void)printf("%.*s", UT_NAMESIZE, names[0]); + ulog_endutxent(); + if (ncnt > 0) { + qsort(names, ncnt, sizeof(namebuf), scmp); + (void)printf("%s", names[0]); for (cnt = 1; cnt < ncnt; ++cnt) - if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE)) - (void)printf(" %.*s", UT_NAMESIZE, names[cnt]); + if (strcmp(names[cnt], names[cnt - 1]) != 0) + (void)printf(" %s", names[cnt]); (void)printf("\n"); } exit(0); @@ -114,5 +115,6 @@ usage(void) int scmp(const void *p, const void *q) { - return(strncmp(p, q, UT_NAMESIZE)); + + return (strcmp(p, q)); }