Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Sep 2018 09:58:13 +0000 (UTC)
From:      Philip Paeps <philip@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r338452 - stable/11/usr.sbin/lastlogin
Message-ID:  <201809040958.w849wDuF082810@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: philip
Date: Tue Sep  4 09:58:13 2018
New Revision: 338452
URL: https://svnweb.freebsd.org/changeset/base/338452

Log:
  MFC r338353:
    Add libxo(3) support to lastlogin(8).

Modified:
  stable/11/usr.sbin/lastlogin/Makefile
  stable/11/usr.sbin/lastlogin/lastlogin.8
  stable/11/usr.sbin/lastlogin/lastlogin.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.sbin/lastlogin/Makefile
==============================================================================
--- stable/11/usr.sbin/lastlogin/Makefile	Tue Sep  4 09:53:45 2018	(r338451)
+++ stable/11/usr.sbin/lastlogin/Makefile	Tue Sep  4 09:58:13 2018	(r338452)
@@ -2,5 +2,6 @@
 
 PROG=	lastlogin
 MAN=	lastlogin.8
+LIBADD=	xo
 
 .include <bsd.prog.mk>

Modified: stable/11/usr.sbin/lastlogin/lastlogin.8
==============================================================================
--- stable/11/usr.sbin/lastlogin/lastlogin.8	Tue Sep  4 09:53:45 2018	(r338451)
+++ stable/11/usr.sbin/lastlogin/lastlogin.8	Tue Sep  4 09:58:13 2018	(r338452)
@@ -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 delete
 .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.

Modified: stable/11/usr.sbin/lastlogin/lastlogin.c
==============================================================================
--- stable/11/usr.sbin/lastlogin/lastlogin.c	Tue Sep  4 09:53:45 2018	(r338451)
+++ stable/11/usr.sbin/lastlogin/lastlogin.c	Tue Sep  4 09:58:13 2018	(r338452)
@@ -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:3
 #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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201809040958.w849wDuF082810>