2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1996 John M. Vinopal
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
49 int main(int, char **);
50 static void output(struct utmpx *);
51 static void usage(void);
52 static int utcmp_user(const void *, const void *);
55 static const char *file = NULL;
56 static int (*utcmp)(const void *, const void *) = utcmp_user;
59 utcmp_user(const void *u1, const void *u2)
62 return (order * strcmp(((const struct utmpx *)u1)->ut_user,
63 ((const struct utmpx *)u2)->ut_user));
67 utcmp_time(const void *u1, const void *u2)
71 t1 = ((const struct utmpx *)u1)->ut_tv.tv_sec;
72 t2 = ((const struct utmpx *)u2)->ut_tv.tv_sec;
73 return (t1 < t2 ? order : t1 > t2 ? -order : 0);
77 main(int argc, char *argv[])
80 struct utmpx *u, *ulist;
82 while ((ch = getopt(argc, argv, "f:rt")) != -1) {
101 /* Process usernames given on the command line. */
102 for (i = 0; i < argc; i++) {
103 if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
104 err(1, "failed to open lastlog database");
105 if ((u = getutxuser(argv[i])) == NULL) {
106 warnx("user '%s' not found", argv[i]);
113 /* Read all lastlog entries, looking for active ones. */
114 if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
115 err(1, "failed to open lastlog database");
118 while ((u = getutxent()) != NULL) {
119 if (u->ut_type != USER_PROCESS)
121 if ((ulistsize % 16) == 0) {
122 ulist = realloc(ulist,
123 (ulistsize + 16) * sizeof(struct utmpx));
127 ulist[ulistsize++] = *u;
131 qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
132 for (i = 0; i < ulistsize; i++)
139 /* Duplicate the output of last(1) */
141 output(struct utmpx *u)
143 time_t t = u->ut_tv.tv_sec;
145 printf("%-10s %-8s %-22.22s %s",
146 u->ut_user, u->ut_line, u->ut_host, ctime(&t));
152 fprintf(stderr, "usage: lastlogin [-f file] [-rt] [user ...]\n");