2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1996 John M. Vinopal
5 * Copyright (c) 2018 Philip Paeps
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
52 int main(int, char **);
53 static void output(struct utmpx *);
54 static void usage(void);
55 static int utcmp_user(const void *, const void *);
58 static const char *file = NULL;
59 static int (*utcmp)(const void *, const void *) = utcmp_user;
62 utcmp_user(const void *u1, const void *u2)
65 return (order * strcmp(((const struct utmpx *)u1)->ut_user,
66 ((const struct utmpx *)u2)->ut_user));
70 utcmp_time(const void *u1, const void *u2)
74 t1 = ((const struct utmpx *)u1)->ut_tv.tv_sec;
75 t2 = ((const struct utmpx *)u2)->ut_tv.tv_sec;
76 return (t1 < t2 ? order : t1 > t2 ? -order : 0);
80 main(int argc, char *argv[])
83 struct utmpx *u, *ulist;
85 argc = xo_parse_args(argc, argv);
89 while ((ch = getopt(argc, argv, "f:rt")) != -1) {
107 xo_open_container("lastlogin-information");
108 xo_open_list("lastlogin");
111 /* Process usernames given on the command line. */
112 for (i = 0; i < argc; i++) {
113 if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
114 xo_err(1, "failed to open lastlog database");
115 if ((u = getutxuser(argv[i])) == NULL) {
116 xo_warnx("user '%s' not found", argv[i]);
123 /* Read all lastlog entries, looking for active ones. */
124 if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
125 xo_err(1, "failed to open lastlog database");
128 while ((u = getutxent()) != NULL) {
129 if (u->ut_type != USER_PROCESS)
131 if ((ulistsize % 16) == 0) {
132 ulist = realloc(ulist,
133 (ulistsize + 16) * sizeof(struct utmpx));
137 ulist[ulistsize++] = *u;
141 qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
142 for (i = 0; i < ulistsize; i++)
146 xo_close_list("lastlogin");
147 xo_close_container("lastlogin-information");
153 /* Duplicate the output of last(1) */
155 output(struct utmpx *u)
157 time_t t = u->ut_tv.tv_sec;
159 xo_open_instance("lastlogin");
160 xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
161 u->ut_user, u->ut_line, u->ut_host);
162 xo_attr("seconds", "%lu", (unsigned long)t);
163 xo_emit(" {:login-time/%.24s/%.24s}\n", ctime(&t));
164 xo_close_instance("lastlogin");
170 xo_error("usage: lastlogin [-f file] [-rt] [user ...]\n");