lastlogin.c revision 1.5 1 /* $NetBSD: lastlogin.c,v 1.5 2000/07/04 20:27:37 matt Exp $ */
2 /*
3 * Copyright (c) 1996 John M. Vinopal
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed for the NetBSD Project
17 * by John M. Vinopal.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: lastlogin.c,v 1.5 2000/07/04 20:27:37 matt Exp $");
37 #endif
38
39 #include <sys/types.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <time.h>
46 #include <utmp.h>
47 #include <unistd.h>
48
49 extern char *__progname;
50 static char *logfile = _PATH_LASTLOG;
51
52 int main __P((int, char **));
53 static void output __P((struct passwd *, struct lastlog *));
54 static void usage __P((void));
55
56 int
57 main(argc, argv)
58 int argc;
59 char *argv[];
60 {
61 int ch, i;
62 FILE *fp;
63 struct passwd *passwd;
64 struct lastlog last;
65
66 while ((ch = getopt(argc, argv, "")) != -1) {
67 usage();
68 }
69
70 fp = fopen(logfile, "r");
71 if (fp == NULL)
72 err(1, "%s", logfile);
73
74 setpassent(1); /* Keep passwd file pointers open */
75
76 /* Process usernames given on the command line. */
77 if (argc > 1) {
78 long offset;
79 for (i = 1; i < argc; ++i) {
80 if ((passwd = getpwnam(argv[i])) == NULL) {
81 warnx("user '%s' not found", argv[i]);
82 continue;
83 }
84 /* Calculate the offset into the lastlog file. */
85 offset = (long)(passwd->pw_uid * sizeof(last));
86 if (fseek(fp, offset, SEEK_SET)) {
87 warn("fseek error");
88 continue;
89 }
90 if (fread(&last, sizeof(last), 1, fp) != 1) {
91 warnx("fread error on '%s'", passwd->pw_name);
92 clearerr(fp);
93 continue;
94 }
95 output(passwd, &last);
96 }
97 }
98 /* Read all lastlog entries, looking for active ones */
99 else {
100 for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) {
101 if (last.ll_time == 0)
102 continue;
103 if ((passwd = getpwuid(i)) != NULL)
104 output(passwd, &last);
105 }
106 if (ferror(fp))
107 warnx("fread error");
108 }
109
110 setpassent(0); /* Close passwd file pointers */
111
112 fclose(fp);
113 exit(0);
114 }
115
116 /* Duplicate the output of last(1) */
117 static void
118 output(p, l)
119 struct passwd *p;
120 struct lastlog *l;
121 {
122 printf("%-*.*s %-*.*s %-*.*s %s",
123 UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
124 UT_LINESIZE, UT_LINESIZE, l->ll_line,
125 UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
126 (l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
127 }
128
129 static void
130 usage()
131 {
132 fprintf(stderr, "usage: %s [user ...]\n", __progname);
133 exit(1);
134 }
135