1 /* $NetBSD: last.c,v 1.15 2000/06/30 06:19:58 simonb Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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 by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, 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
35 static struct utmp *buf;
37 static void onintr(int);
38 static int want(struct utmp *, int);
42 * read through the wtmp file
45 wtmp(const char *file, int namesz, int linesz, int hostsz)
47 struct utmp *bp; /* current structure */
48 TTY *T; /* tty list entry */
49 struct stat stb; /* stat of file for sz */
50 time_t delta; /* time difference */
55 size_t len = sizeof(*buf) * MAXUTMP;
57 if ((buf = malloc(len)) == NULL)
58 err(1, "Cannot allocate utmp buffer");
62 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
64 bl = (stb.st_size + len - 1) / len;
66 buf[FIRSTVALID].ut_timefld = time(NULL);
67 (void)signal(SIGINT, onintr);
68 (void)signal(SIGQUIT, onintr);
71 if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
72 (bytes = read(wfd, buf, len)) == -1)
74 for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
76 * if the terminal line is '~', the machine stopped.
77 * see utmp(5) for more info.
79 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
80 /* everybody just logged out */
81 for (T = ttylist; T; T = T->next)
82 T->logout = -bp->ut_timefld;
83 currentout = -bp->ut_timefld;
84 crmsg = strncmp(bp->ut_name, "shutdown",
85 namesz) ? "crash" : "shutdown";
87 ct = fmttime(bp->ut_timefld, fulltime);
88 printf("%-*.*s %-*.*s %-*.*s %s\n",
89 namesz, namesz, bp->ut_name,
90 linesz, linesz, bp->ut_line,
91 hostsz, hostsz, bp->ut_host, ct);
92 if (maxrec != -1 && !--maxrec)
98 * if the line is '{' or '|', date got set; see
99 * utmp(5) for more info.
101 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
102 && !bp->ut_line[1]) {
104 ct = fmttime(bp->ut_timefld, fulltime);
105 printf("%-*.*s %-*.*s %-*.*s %s\n",
113 if (maxrec && !--maxrec)
118 /* find associated tty */
119 for (T = ttylist;; T = T->next) {
122 T = addtty(bp->ut_line);
125 if (!strncmp(T->tty, bp->ut_line, LINESIZE))
128 if (TYPE(bp) == SIGNATURE)
130 if (bp->ut_name[0] && want(bp, YES)) {
131 ct = fmttime(bp->ut_timefld, fulltime);
132 printf("%-*.*s %-*.*s %-*.*s %s ",
133 namesz, namesz, bp->ut_name,
134 linesz, linesz, bp->ut_line,
135 hostsz, hostsz, bp->ut_host,
138 puts(" still logged in");
141 T->logout = -T->logout;
142 printf("- %s", crmsg);
147 fulltime | TIMEONLY));
148 delta = T->logout - bp->ut_timefld;
149 if (delta < SECSPERDAY)
152 fulltime | TIMEONLY | GMT));
154 printf(" (%ld+%s)\n",
157 fulltime | TIMEONLY | GMT));
159 if (maxrec != -1 && !--maxrec)
162 T->logout = bp->ut_timefld;
165 fulltime = 1; /* show full time */
166 crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
167 if ((ct = strrchr(file, '/')) != NULL)
169 printf("\n%s begins %s\n", ct ? ct : file, crmsg);
174 * see if want this entry
177 want(struct utmp *bp, int check)
183 * when uucp and ftp log in over a network, the entry in
184 * the utmp file is the name plus their process id. See
185 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
187 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
188 bp->ut_line[3] = '\0';
189 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
190 bp->ut_line[4] = '\0';
195 for (step = arglist; step; step = step->next)
198 if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
202 if (!strncmp(step->name, bp->ut_line, LINESIZE))
206 if (!strncmp(step->name, bp->ut_name, NAMESIZE))
215 * on interrupt, we inform the user how far we've gotten
221 printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
225 (void)fflush(stdout); /* fix required for rsh */