Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/users utmpx support



details:   https://anonhg.NetBSD.org/src/rev/d6902df50da6
branches:  trunk
changeset: 534688:d6902df50da6
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Aug 01 23:51:20 2002 +0000

description:
utmpx support

diffstat:

 usr.bin/users/Makefile |   6 +++-
 usr.bin/users/users.c  |  73 +++++++++++++++++++------------------------------
 2 files changed, 34 insertions(+), 45 deletions(-)

diffs (137 lines):

diff -r 7f708c1f0eaa -r d6902df50da6 usr.bin/users/Makefile
--- a/usr.bin/users/Makefile    Thu Aug 01 23:47:46 2002 +0000
+++ b/usr.bin/users/Makefile    Thu Aug 01 23:51:20 2002 +0000
@@ -1,6 +1,10 @@
-#      $NetBSD: Makefile,v 1.3 1994/12/06 07:32:26 jtc Exp $
+#      $NetBSD: Makefile,v 1.4 2002/08/01 23:51:20 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 
 PROG=  users
+SRCS=  users.c utmpentry.c
+
+.PATH.c: ${.CURDIR}/../../usr.bin/who
+CPPFLAGS+=-I${.CURDIR}/../../usr.bin/who -DSUPPORT_UTMPX -DSUPPORT_UTMP
 
 .include <bsd.prog.mk>
diff -r 7f708c1f0eaa -r d6902df50da6 usr.bin/users/users.c
--- a/usr.bin/users/users.c     Thu Aug 01 23:47:46 2002 +0000
+++ b/usr.bin/users/users.c     Thu Aug 01 23:51:20 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: users.c,v 1.9 1998/12/19 23:35:24 christos Exp $       */
+/*     $NetBSD: users.c,v 1.10 2002/08/01 23:51:20 christos Exp $      */
 
 /*
  * Copyright (c) 1980, 1987, 1993
@@ -43,84 +43,69 @@
 #if 0
 static char sccsid[] = "@(#)users.c    8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: users.c,v 1.9 1998/12/19 23:35:24 christos Exp $");
+__RCSID("$NetBSD: users.c,v 1.10 2002/08/01 23:51:20 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
 
 #include <err.h>
+#include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <unistd.h>
-#include <utmp.h>
 
-typedef char   namebuf[UT_NAMESIZE];
+#include "utmpentry.h"
 
 int    main __P((int, char **));
-int scmp __P((const void *, const void *));
 
 int
 main(argc, argv)
        int argc;
        char **argv;
 {
-       namebuf *names = NULL;
        int ncnt = 0;
-       int nmax = 0;
-       int cnt;
-       struct utmp utmp;
        int ch;
+       struct utmpentry *from, *ehead, *save, **nextp;
 
        while ((ch = getopt(argc, argv, "")) != -1)
                switch(ch) {
                case '?':
                default:
-                       (void)fprintf(stderr, "usage: users\n");
+                       (void)fprintf(stderr, "Usage: %s\n", getprogname());
                        exit(1);
                }
        argc -= optind;
        argv += optind;
 
-       if (!freopen(_PATH_UTMP, "r", stdin)) {
-               err(1, "can't open %s", _PATH_UTMP);
-               /* NOTREACHED */
+       ncnt = getutentries(NULL, &ehead);
+
+       if (ncnt == 0)
+               return 0;
+
+       from = ehead;
+       ehead = NULL;
+       while (from != NULL) {
+               for (nextp = &ehead;
+                   (*nextp) && strcmp(from->name, (*nextp)->name) > 0;
+                   nextp = &(*nextp)->next)
+                       continue;
+               save = from;
+               from = from->next;
+               save->next = *nextp;
+               *nextp = save;
        }
 
-       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) {
-                                       err(1, "realloc");
-                                       /* NOTREACHED */
-                               }
-                       }
+       save = ehead;
+       (void)printf("%s", ehead->name);
 
-                       (void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
-                       ++ncnt;
+       for (from = ehead->next; from; from = from->next)
+               if (strcmp(save->name, from->name) != 0) {
+                       (void)printf(" %s", from->name);
+                       save = from;
                }
-       }
 
-       if (ncnt) {
-               qsort(names, ncnt, UT_NAMESIZE, scmp);
-               (void)printf("%.*s", (int)UT_NAMESIZE, names[0]);
-               for (cnt = 1; cnt < ncnt; ++cnt)
-                       if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE))
-                               (void)printf(" %.*s", (int)UT_NAMESIZE,
-                               names[cnt]);
-               (void)printf("\n");
-       }
+       (void)printf("\n");
        exit(0);
 }
-
-int
-scmp(p, q)
-       const void *p, *q;
-{
-       return(strncmp((char *) p, (char *) q, UT_NAMESIZE));
-}



Home | Main Index | Thread Index | Old Index