lastlogin.c revision 1.5 1 1.5 matt /* $NetBSD: lastlogin.c,v 1.5 2000/07/04 20:27:37 matt 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.5 matt __RCSID("$NetBSD: lastlogin.c,v 1.5 2000/07/04 20:27:37 matt 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.5 matt #include <stdlib.h>
45 1.3 lukem #include <time.h>
46 1.3 lukem #include <utmp.h>
47 1.4 perry #include <unistd.h>
48 1.1 phil
49 1.1 phil extern char *__progname;
50 1.1 phil static char *logfile = _PATH_LASTLOG;
51 1.1 phil
52 1.3 lukem int main __P((int, char **));
53 1.3 lukem static void output __P((struct passwd *, struct lastlog *));
54 1.3 lukem static void usage __P((void));
55 1.1 phil
56 1.1 phil int
57 1.1 phil main(argc, argv)
58 1.1 phil int argc;
59 1.1 phil char *argv[];
60 1.1 phil {
61 1.1 phil int ch, i;
62 1.1 phil FILE *fp;
63 1.1 phil struct passwd *passwd;
64 1.1 phil struct lastlog last;
65 1.1 phil
66 1.1 phil while ((ch = getopt(argc, argv, "")) != -1) {
67 1.1 phil usage();
68 1.1 phil }
69 1.1 phil
70 1.1 phil fp = fopen(logfile, "r");
71 1.1 phil if (fp == NULL)
72 1.1 phil err(1, "%s", logfile);
73 1.1 phil
74 1.1 phil setpassent(1); /* Keep passwd file pointers open */
75 1.1 phil
76 1.1 phil /* Process usernames given on the command line. */
77 1.1 phil if (argc > 1) {
78 1.1 phil long offset;
79 1.1 phil for (i = 1; i < argc; ++i) {
80 1.1 phil if ((passwd = getpwnam(argv[i])) == NULL) {
81 1.1 phil warnx("user '%s' not found", argv[i]);
82 1.1 phil continue;
83 1.1 phil }
84 1.1 phil /* Calculate the offset into the lastlog file. */
85 1.1 phil offset = (long)(passwd->pw_uid * sizeof(last));
86 1.1 phil if (fseek(fp, offset, SEEK_SET)) {
87 1.1 phil warn("fseek error");
88 1.1 phil continue;
89 1.1 phil }
90 1.1 phil if (fread(&last, sizeof(last), 1, fp) != 1) {
91 1.1 phil warnx("fread error on '%s'", passwd->pw_name);
92 1.1 phil clearerr(fp);
93 1.1 phil continue;
94 1.1 phil }
95 1.1 phil output(passwd, &last);
96 1.1 phil }
97 1.1 phil }
98 1.1 phil /* Read all lastlog entries, looking for active ones */
99 1.1 phil else {
100 1.2 christos for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) {
101 1.2 christos if (last.ll_time == 0)
102 1.2 christos continue;
103 1.2 christos if ((passwd = getpwuid(i)) != NULL)
104 1.1 phil output(passwd, &last);
105 1.1 phil }
106 1.1 phil if (ferror(fp))
107 1.1 phil warnx("fread error");
108 1.1 phil }
109 1.1 phil
110 1.1 phil setpassent(0); /* Close passwd file pointers */
111 1.1 phil
112 1.1 phil fclose(fp);
113 1.1 phil exit(0);
114 1.1 phil }
115 1.1 phil
116 1.1 phil /* Duplicate the output of last(1) */
117 1.1 phil static void
118 1.1 phil output(p, l)
119 1.1 phil struct passwd *p;
120 1.1 phil struct lastlog *l;
121 1.1 phil {
122 1.1 phil printf("%-*.*s %-*.*s %-*.*s %s",
123 1.1 phil UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
124 1.1 phil UT_LINESIZE, UT_LINESIZE, l->ll_line,
125 1.1 phil UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
126 1.1 phil (l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
127 1.1 phil }
128 1.1 phil
129 1.1 phil static void
130 1.1 phil usage()
131 1.1 phil {
132 1.1 phil fprintf(stderr, "usage: %s [user ...]\n", __progname);
133 1.1 phil exit(1);
134 1.1 phil }
135