mirror of
https://github.com/freebsd/freebsd-src.git
synced 2024-11-28 06:52:45 +00:00
Port lastlogin(8) to utmpx.
While there, fix a bug I introduced previously. We must reopen the database for each username passed on the command line. We must rewind the database and search from the beginning.
This commit is contained in:
parent
e4ce5cb26c
commit
b5207b31c4
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=202205
@ -3,7 +3,4 @@
|
||||
PROG= lastlogin
|
||||
MAN= lastlogin.8
|
||||
|
||||
DPADD= ${LIBULOG}
|
||||
LDADD= -lulog
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -41,62 +41,62 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <ulog.h>
|
||||
#include <unistd.h>
|
||||
#include <utmpx.h>
|
||||
|
||||
int main(int, char **);
|
||||
static void output(struct ulog_utmpx *);
|
||||
static void output(struct utmpx *);
|
||||
static void usage(void);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int ch, i;
|
||||
struct ulog_utmpx *u;
|
||||
struct utmpx *u;
|
||||
|
||||
while ((ch = getopt(argc, argv, "")) != -1) {
|
||||
usage();
|
||||
}
|
||||
|
||||
if (ulog_setutxfile(UTXI_USER, NULL) != 0)
|
||||
errx(1, "failed to open lastlog database");
|
||||
|
||||
setpassent(1); /* Keep passwd file pointers open */
|
||||
|
||||
/* Process usernames given on the command line. */
|
||||
if (argc > 1) {
|
||||
for (i = 1; i < argc; ++i) {
|
||||
if ((u = ulog_getutxuser(argv[i])) == NULL) {
|
||||
if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
|
||||
errx(1, "failed to open lastlog database");
|
||||
if ((u = getutxuser(argv[i])) == NULL) {
|
||||
warnx("user '%s' not found", argv[i]);
|
||||
continue;
|
||||
}
|
||||
output(u);
|
||||
endutxent();
|
||||
}
|
||||
}
|
||||
/* Read all lastlog entries, looking for active ones */
|
||||
else {
|
||||
while ((u = ulog_getutxent()) != NULL) {
|
||||
if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
|
||||
errx(1, "failed to open lastlog database");
|
||||
while ((u = getutxent()) != NULL) {
|
||||
if (u->ut_type != USER_PROCESS)
|
||||
continue;
|
||||
output(u);
|
||||
}
|
||||
endutxent();
|
||||
}
|
||||
|
||||
setpassent(0); /* Close passwd file pointers */
|
||||
|
||||
ulog_endutxent();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* Duplicate the output of last(1) */
|
||||
static void
|
||||
output(struct ulog_utmpx *u)
|
||||
output(struct utmpx *u)
|
||||
{
|
||||
time_t t = u->ut_tv.tv_sec;
|
||||
|
||||
printf("%-16s %-8s %-16s %s",
|
||||
u->ut_user, u->ut_line, u->ut_host,
|
||||
(u->ut_type == USER_PROCESS) ? ctime(&t) : "Never logged in\n");
|
||||
printf("%-10s %-8s %-22s %s",
|
||||
u->ut_user, u->ut_line, u->ut_host, ctime(&t));
|
||||
}
|
||||
|
||||
static void
|
||||
|
Loading…
Reference in New Issue
Block a user