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.
This commit is contained in:
parent
0451afacb6
commit
1404374a45
@ -3,4 +3,7 @@
|
|||||||
|
|
||||||
PROG= users
|
PROG= users
|
||||||
|
|
||||||
|
DPADD= ${LIBULOG}
|
||||||
|
LDADD= -lulog
|
||||||
|
|
||||||
.include <bsd.prog.mk>
|
.include <bsd.prog.mk>
|
||||||
|
@ -45,15 +45,16 @@ static const char rcsid[] =
|
|||||||
"$FreeBSD$";
|
"$FreeBSD$";
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ulog.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <utmp.h>
|
|
||||||
|
|
||||||
typedef char namebuf[UT_NAMESIZE];
|
typedef char namebuf[MAXLOGNAME];
|
||||||
|
|
||||||
int scmp(const void *, const void *);
|
int scmp(const void *, const void *);
|
||||||
static void usage(void);
|
static void usage(void);
|
||||||
@ -65,7 +66,7 @@ main(int argc, char **argv)
|
|||||||
int ncnt = 0;
|
int ncnt = 0;
|
||||||
int nmax = 0;
|
int nmax = 0;
|
||||||
int cnt;
|
int cnt;
|
||||||
struct utmp utmp;
|
struct ulog_utmpx *ut;
|
||||||
int ch;
|
int ch;
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "")) != -1)
|
while ((ch = getopt(argc, argv, "")) != -1)
|
||||||
@ -77,28 +78,28 @@ main(int argc, char **argv)
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (!freopen(_PATH_UTMP, "r", stdin))
|
ulog_setutxent();
|
||||||
errx(1, "can't open %s", _PATH_UTMP);
|
while ((ut = ulog_getutxent()) != NULL) {
|
||||||
while (fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1) {
|
if (ut->ut_type != LOGIN_PROCESS)
|
||||||
if (*utmp.ut_name) {
|
continue;
|
||||||
if (ncnt >= nmax) {
|
if (ncnt >= nmax) {
|
||||||
nmax += 32;
|
nmax += 32;
|
||||||
names = realloc(names, sizeof (*names) * nmax);
|
names = realloc(names, sizeof(*names) * nmax);
|
||||||
if (!names) {
|
if (!names) {
|
||||||
errx(1, "realloc");
|
errx(1, "realloc");
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
|
|
||||||
++ncnt;
|
|
||||||
}
|
}
|
||||||
|
(void)strlcpy(names[ncnt], ut->ut_user, sizeof(*names));
|
||||||
|
++ncnt;
|
||||||
}
|
}
|
||||||
if (ncnt) {
|
ulog_endutxent();
|
||||||
qsort(names, ncnt, UT_NAMESIZE, scmp);
|
if (ncnt > 0) {
|
||||||
(void)printf("%.*s", UT_NAMESIZE, names[0]);
|
qsort(names, ncnt, sizeof(namebuf), scmp);
|
||||||
|
(void)printf("%s", names[0]);
|
||||||
for (cnt = 1; cnt < ncnt; ++cnt)
|
for (cnt = 1; cnt < ncnt; ++cnt)
|
||||||
if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE))
|
if (strcmp(names[cnt], names[cnt - 1]) != 0)
|
||||||
(void)printf(" %.*s", UT_NAMESIZE, names[cnt]);
|
(void)printf(" %s", names[cnt]);
|
||||||
(void)printf("\n");
|
(void)printf("\n");
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -114,5 +115,6 @@ usage(void)
|
|||||||
int
|
int
|
||||||
scmp(const void *p, const void *q)
|
scmp(const void *p, const void *q)
|
||||||
{
|
{
|
||||||
return(strncmp(p, q, UT_NAMESIZE));
|
|
||||||
|
return (strcmp(p, q));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user