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