Add libxo(3) support to last(1).
Reviewed by: kp Approved by: re (gjb) MFC after: 1 week Relnotes: yes Differential Revision: https://reviews.freebsd.org/D16922
This commit is contained in:
parent
0b5cab4e0f
commit
c7c5d8e387
@ -2,6 +2,7 @@
|
||||
# $FreeBSD$
|
||||
|
||||
PROG= last
|
||||
LIBADD= xo
|
||||
|
||||
NO_WFORMAT=
|
||||
|
||||
|
@ -28,7 +28,7 @@
|
||||
.\" @(#)last.1 8.1 (Berkeley) 6/6/93
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd June 1, 2018
|
||||
.Dd August 28, 2018
|
||||
.Dt LAST 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
@ -36,6 +36,7 @@
|
||||
.Nd indicate last logins of users and ttys
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Fl -libxo
|
||||
.Op Fl swy
|
||||
.Oo
|
||||
.Fl d
|
||||
@ -72,6 +73,13 @@ will so indicate.
|
||||
.Pp
|
||||
The following options are available:
|
||||
.Bl -tag -width indent-two
|
||||
.It Fl -libxo
|
||||
Generate output via
|
||||
.Xr libxo 3
|
||||
in a selection of different human and machine readable formats.
|
||||
See
|
||||
.Xr xo_parse_args 3
|
||||
for details on command line arguments.
|
||||
.It Fl d Ar date
|
||||
Specify the snapshot date and time.
|
||||
All users logged in at the snapshot date and time will
|
||||
@ -201,6 +209,8 @@ login data base
|
||||
.Xr getutxent 3 ,
|
||||
.Xr ac 8 ,
|
||||
.Xr lastlogin 8
|
||||
.Xr libxo 3 ,
|
||||
.Xr xo_parse_args 3
|
||||
.Sh HISTORY
|
||||
.Nm
|
||||
utility first appeared in
|
||||
@ -211,6 +221,10 @@ The original version was written by
|
||||
.An Howard P. Katseff ;
|
||||
.An Keith Bostic
|
||||
rewrote it in 1986/87 to add functionality and to improve code quality.
|
||||
.An Philip Paeps
|
||||
added
|
||||
.Xr libxo 3
|
||||
support in August 2018.
|
||||
.Sh BUGS
|
||||
If a login shell should terminate abnormally for some reason, it is likely
|
||||
that a logout record will not be written to the
|
||||
|
@ -3,6 +3,7 @@
|
||||
*
|
||||
* Copyright (c) 1987, 1993, 1994
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
* Copyright (c) 2018 Philip Paeps
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -62,6 +63,8 @@ __FBSDID("$FreeBSD$");
|
||||
#include <unistd.h>
|
||||
#include <utmpx.h>
|
||||
|
||||
#include <libxo/xo.h>
|
||||
|
||||
#define NO 0 /* false/no */
|
||||
#define YES 1 /* true/yes */
|
||||
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
|
||||
@ -112,7 +115,7 @@ static void wtmp(void);
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
(void)fprintf(stderr,
|
||||
xo_error(
|
||||
"usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
|
||||
" [-n maxrec] [-t tty] [user ...]\n");
|
||||
exit(1);
|
||||
@ -127,6 +130,11 @@ main(int argc, char *argv[])
|
||||
(void) setlocale(LC_TIME, "");
|
||||
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
||||
|
||||
argc = xo_parse_args(argc, argv);
|
||||
if (argc < 0)
|
||||
exit(1);
|
||||
atexit(xo_finish_atexit);
|
||||
|
||||
maxrec = -1;
|
||||
snaptime = 0;
|
||||
while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
|
||||
@ -161,7 +169,7 @@ main(int argc, char *argv[])
|
||||
maxrec = strtol(optarg, &p, 10);
|
||||
if (p == optarg || *p != '\0' || errno != 0 ||
|
||||
maxrec <= 0)
|
||||
errx(1, "%s: bad line count", optarg);
|
||||
xo_errx(1, "%s: bad line count", optarg);
|
||||
break;
|
||||
case 's':
|
||||
sflag++; /* Show delta as seconds */
|
||||
@ -181,17 +189,17 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (caph_limit_stdio() < 0)
|
||||
err(1, "can't limit stdio rights");
|
||||
xo_err(1, "can't limit stdio rights");
|
||||
|
||||
caph_cache_catpages();
|
||||
caph_cache_tzdata();
|
||||
|
||||
/* Cache UTX database. */
|
||||
if (setutxdb(UTXDB_LOG, file) != 0)
|
||||
err(1, "%s", file != NULL ? file : "(default utx db)");
|
||||
xo_err(1, "%s", file != NULL ? file : "(default utx db)");
|
||||
|
||||
if (caph_enter() < 0)
|
||||
err(1, "cap_enter");
|
||||
xo_err(1, "cap_enter");
|
||||
|
||||
if (sflag && width == 8) usage();
|
||||
|
||||
@ -229,12 +237,14 @@ wtmp(void)
|
||||
SLIST_INIT(&idlist);
|
||||
(void)time(&t);
|
||||
|
||||
xo_open_container("last-information");
|
||||
|
||||
/* Load the last entries from the file. */
|
||||
while ((ut = getutxent()) != NULL) {
|
||||
if (amount % 128 == 0) {
|
||||
buf = realloc(buf, (amount + 128) * sizeof *ut);
|
||||
if (buf == NULL)
|
||||
err(1, "realloc");
|
||||
xo_err(1, "realloc");
|
||||
}
|
||||
memcpy(&buf[amount++], ut, sizeof *ut);
|
||||
if (t > ut->ut_tv.tv_sec)
|
||||
@ -243,12 +253,17 @@ wtmp(void)
|
||||
endutxent();
|
||||
|
||||
/* Display them in reverse order. */
|
||||
xo_open_list("last");
|
||||
while (amount > 0)
|
||||
doentry(&buf[--amount]);
|
||||
xo_close_list("last");
|
||||
free(buf);
|
||||
tm = localtime(&t);
|
||||
(void) strftime(ct, sizeof(ct), "%+", tm);
|
||||
printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct);
|
||||
xo_emit("\n{:utxdb/%s}", (file == NULL) ? "utx.log" : file);
|
||||
xo_attr("seconds", "%lu", (unsigned long) t);
|
||||
xo_emit(" begins {:begins/%s}\n", ct);
|
||||
xo_close_container("last-information");
|
||||
}
|
||||
|
||||
/*
|
||||
@ -303,7 +318,7 @@ doentry(struct utmpx *bp)
|
||||
/* add new one */
|
||||
tt = malloc(sizeof(struct idtab));
|
||||
if (tt == NULL)
|
||||
errx(1, "malloc failure");
|
||||
xo_errx(1, "malloc failure");
|
||||
tt->logout = currentout;
|
||||
memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
|
||||
SLIST_INSERT_HEAD(&idlist, tt, list);
|
||||
@ -339,6 +354,7 @@ printentry(struct utmpx *bp, struct idtab *tt)
|
||||
|
||||
if (maxrec != -1 && !maxrec--)
|
||||
exit(0);
|
||||
xo_open_instance("last");
|
||||
t = bp->ut_tv.tv_sec;
|
||||
tm = localtime(&t);
|
||||
(void) strftime(ct, sizeof(ct), d_first ?
|
||||
@ -346,48 +362,55 @@ printentry(struct utmpx *bp, struct idtab *tt)
|
||||
(yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
|
||||
switch (bp->ut_type) {
|
||||
case BOOT_TIME:
|
||||
printf("%-42s", "boot time");
|
||||
xo_emit("{:user/%-42s/%s}", "boot time");
|
||||
break;
|
||||
case SHUTDOWN_TIME:
|
||||
printf("%-42s", "shutdown time");
|
||||
xo_emit("{:user/%-42s/%s}", "shutdown time");
|
||||
break;
|
||||
case OLD_TIME:
|
||||
printf("%-42s", "old time");
|
||||
xo_emit("{:user/%-42s/%s}", "old time");
|
||||
break;
|
||||
case NEW_TIME:
|
||||
printf("%-42s", "new time");
|
||||
xo_emit("{:user/%-42s/%s}", "new time");
|
||||
break;
|
||||
case USER_PROCESS:
|
||||
printf("%-10s %-8s %-22.22s",
|
||||
xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
|
||||
bp->ut_user, bp->ut_line, bp->ut_host);
|
||||
break;
|
||||
}
|
||||
printf(" %s%c", ct, tt == NULL ? '\n' : ' ');
|
||||
xo_attr("seconds", "%lu", (unsigned long)t);
|
||||
xo_emit(" {:login-time/%s%c/%s}", ct, tt == NULL ? '\n' : ' ');
|
||||
if (tt == NULL)
|
||||
return;
|
||||
goto end;
|
||||
if (!tt->logout) {
|
||||
puts(" still logged in");
|
||||
return;
|
||||
xo_emit(" {:logout-time/still logged in}\n");
|
||||
goto end;
|
||||
}
|
||||
if (tt->logout < 0) {
|
||||
tt->logout = -tt->logout;
|
||||
printf("- %s", crmsg);
|
||||
xo_emit("- {:logout-reason/%s}", crmsg);
|
||||
} else {
|
||||
tm = localtime(&tt->logout);
|
||||
(void) strftime(ct, sizeof(ct), "%R", tm);
|
||||
printf("- %s", ct);
|
||||
xo_attr("seconds", "%lu", (unsigned long)tt->logout);
|
||||
xo_emit("- {:logout-time/%s}", ct);
|
||||
}
|
||||
delta = tt->logout - bp->ut_tv.tv_sec;
|
||||
xo_attr("seconds", "%ld", (long)delta);
|
||||
if (sflag) {
|
||||
printf(" (%8ld)\n", (long)delta);
|
||||
xo_emit(" ({:session-length/%8ld})\n", (long)delta);
|
||||
} else {
|
||||
tm = gmtime(&delta);
|
||||
(void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
|
||||
if (delta < 86400)
|
||||
printf(" (%s)\n", ct);
|
||||
xo_emit(" ({:session-length/%s})\n", ct);
|
||||
else
|
||||
printf(" (%ld+%s)\n", (long)delta / 86400, ct);
|
||||
xo_emit(" ({:session-length/%ld+%s})\n",
|
||||
(long)delta / 86400, ct);
|
||||
}
|
||||
|
||||
end:
|
||||
xo_close_instance("last");
|
||||
}
|
||||
|
||||
/*
|
||||
@ -438,7 +461,7 @@ addarg(int type, char *arg)
|
||||
ARG *cur;
|
||||
|
||||
if ((cur = malloc(sizeof(ARG))) == NULL)
|
||||
errx(1, "malloc failure");
|
||||
xo_errx(1, "malloc failure");
|
||||
cur->next = arglist;
|
||||
cur->type = type;
|
||||
cur->name = arg;
|
||||
@ -463,7 +486,7 @@ hostconv(char *arg)
|
||||
if (first) {
|
||||
first = 0;
|
||||
if (gethostname(name, sizeof(name)))
|
||||
err(1, "gethostname");
|
||||
xo_err(1, "gethostname");
|
||||
hostdot = strchr(name, '.');
|
||||
}
|
||||
if (hostdot && !strcasecmp(hostdot, argdot))
|
||||
@ -486,7 +509,7 @@ ttyconv(char *arg)
|
||||
if (strlen(arg) == 2) {
|
||||
/* either 6 for "ttyxx" or 8 for "console" */
|
||||
if ((mval = malloc(8)) == NULL)
|
||||
errx(1, "malloc failure");
|
||||
xo_errx(1, "malloc failure");
|
||||
if (!strcmp(arg, "co"))
|
||||
(void)strcpy(mval, "console");
|
||||
else {
|
||||
@ -516,9 +539,9 @@ dateconv(char *arg)
|
||||
|
||||
/* Start with the current time. */
|
||||
if (time(&timet) < 0)
|
||||
err(1, "time");
|
||||
xo_err(1, "time");
|
||||
if ((t = localtime(&timet)) == NULL)
|
||||
err(1, "localtime");
|
||||
xo_err(1, "localtime");
|
||||
|
||||
/* [[CC]YY]MMDDhhmm[.SS] */
|
||||
if ((p = strchr(arg, '.')) == NULL)
|
||||
@ -567,7 +590,7 @@ dateconv(char *arg)
|
||||
t->tm_isdst = -1; /* Figure out DST. */
|
||||
timet = mktime(t);
|
||||
if (timet == -1)
|
||||
terr: errx(1,
|
||||
terr: xo_errx(1,
|
||||
"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
|
||||
return timet;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user