Add libxo(3) support to lastlogin(8).

Reviewed by:	kp
Approved by:	re (gjb)
MFC after:	1 week
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D16919
This commit is contained in:
Philip Paeps 2018-08-28 17:12:37 +00:00
parent c7c5d8e387
commit 427b88d77e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=338353
3 changed files with 42 additions and 8 deletions

View File

@ -2,5 +2,6 @@
PROG= lastlogin
MAN= lastlogin.8
LIBADD= xo
.include <bsd.prog.mk>

View File

@ -31,7 +31,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd June 6, 2011
.Dd August 28, 2018
.Dt LASTLOGIN 8
.Os
.Sh NAME
@ -39,6 +39,7 @@
.Nd indicate last login time of users
.Sh SYNOPSIS
.Nm
.Op Fl -libxo
.Op Fl f Ar file
.Op Fl rt
.Op Ar user ...
@ -68,6 +69,13 @@ The last login database is never turned over or deleted in standard usage.
.Pp
The following options are available:
.Bl -tag -width indent
.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 f Ar file
Open last login database
.Ar file
@ -86,9 +94,15 @@ last login database
.Xr last 1 ,
.Xr getutxent 3 ,
.Xr ac 8
.Xr libxo 3 ,
.Xr xo_parse_args 3
.Sh AUTHORS
.An -nosplit
.An John M. Vinopal
wrote this program in January 1996 and contributed it
to the
.Nx
project.
.An Philip Paeps added
.Xr libxo 3
support in August 2018.

View File

@ -2,6 +2,7 @@
* SPDX-License-Identifier: BSD-4-Clause
*
* Copyright (c) 1996 John M. Vinopal
* Copyright (c) 2018 Philip Paeps
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -46,6 +47,8 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
#include <unistd.h>
#include <utmpx.h>
#include <libxo/xo.h>
int main(int, char **);
static void output(struct utmpx *);
static void usage(void);
@ -79,6 +82,10 @@ main(int argc, char *argv[])
int ch, i, ulistsize;
struct utmpx *u, *ulist;
argc = xo_parse_args(argc, argv);
if (argc < 0)
exit(1);
while ((ch = getopt(argc, argv, "f:rt")) != -1) {
switch (ch) {
case 'f':
@ -97,13 +104,16 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
xo_open_container("lastlogin-information");
xo_open_list("lastlogin");
if (argc > 0) {
/* Process usernames given on the command line. */
for (i = 0; i < argc; i++) {
if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
err(1, "failed to open lastlog database");
xo_err(1, "failed to open lastlog database");
if ((u = getutxuser(argv[i])) == NULL) {
warnx("user '%s' not found", argv[i]);
xo_warnx("user '%s' not found", argv[i]);
continue;
}
output(u);
@ -112,7 +122,7 @@ main(int argc, char *argv[])
} else {
/* Read all lastlog entries, looking for active ones. */
if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
err(1, "failed to open lastlog database");
xo_err(1, "failed to open lastlog database");
ulist = NULL;
ulistsize = 0;
while ((u = getutxent()) != NULL) {
@ -122,7 +132,7 @@ main(int argc, char *argv[])
ulist = realloc(ulist,
(ulistsize + 16) * sizeof(struct utmpx));
if (ulist == NULL)
err(1, "malloc");
xo_err(1, "malloc");
}
ulist[ulistsize++] = *u;
}
@ -133,6 +143,10 @@ main(int argc, char *argv[])
output(&ulist[i]);
}
xo_close_list("lastlogin");
xo_close_container("lastlogin-information");
xo_finish();
exit(0);
}
@ -142,13 +156,18 @@ output(struct utmpx *u)
{
time_t t = u->ut_tv.tv_sec;
printf("%-10s %-8s %-22.22s %s",
u->ut_user, u->ut_line, u->ut_host, ctime(&t));
xo_open_instance("lastlogin");
xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
u->ut_user, u->ut_line, u->ut_host);
xo_attr("seconds", "%lu", (unsigned long)t);
xo_emit(" {:login-time/%.24s/%.24s}\n", ctime(&t));
xo_close_instance("lastlogin");
}
static void
usage(void)
{
fprintf(stderr, "usage: lastlogin [-f file] [-rt] [user ...]\n");
xo_error("usage: lastlogin [-f file] [-rt] [user ...]\n");
xo_finish();
exit(1);
}