summaryrefslogtreecommitdiffstats
path: root/usr.bin/users/users.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/users/users.c')
-rw-r--r--usr.bin/users/users.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/usr.bin/users/users.c b/usr.bin/users/users.c
index c90bfbd..ccf8006 100644
--- a/usr.bin/users/users.c
+++ b/usr.bin/users/users.c
@@ -45,15 +45,16 @@ static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
+#include <sys/param.h>
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <utmp.h>
+#include <utmpx.h>
-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 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 */
- }
+ setutxent();
+ while ((ut = getutxent()) != NULL) {
+ if (ut->ut_type != USER_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]);
+ 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));
}
OpenPOWER on IntegriCloud