last.c revision 1.9 1 1.9 thorpej /* $NetBSD: last.c,v 1.9 1997/08/26 18:52:13 thorpej Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1987, 1993, 1994
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.8 kleink #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 kleink __COPYRIGHT(
39 1.5 jtc "@(#) Copyright (c) 1987, 1993, 1994\n\
40 1.8 kleink The Regents of the University of California. All rights reserved.\n");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.5 jtc #if 0
45 1.5 jtc static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
46 1.5 jtc #endif
47 1.9 thorpej __RCSID("$NetBSD: last.c,v 1.9 1997/08/26 18:52:13 thorpej Exp $");
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/stat.h>
52 1.5 jtc
53 1.5 jtc #include <err.h>
54 1.5 jtc #include <fcntl.h>
55 1.5 jtc #include <paths.h>
56 1.1 cgd #include <signal.h>
57 1.5 jtc #include <stdio.h>
58 1.5 jtc #include <stdlib.h>
59 1.5 jtc #include <string.h>
60 1.1 cgd #include <time.h>
61 1.5 jtc #include <tzfile.h>
62 1.5 jtc #include <unistd.h>
63 1.1 cgd #include <utmp.h>
64 1.1 cgd
65 1.1 cgd #define NO 0 /* false/no */
66 1.1 cgd #define YES 1 /* true/yes */
67 1.1 cgd
68 1.1 cgd static struct utmp buf[1024]; /* utmp read buffer */
69 1.1 cgd
70 1.1 cgd typedef struct arg {
71 1.1 cgd char *name; /* argument */
72 1.1 cgd #define HOST_TYPE -2
73 1.1 cgd #define TTY_TYPE -3
74 1.1 cgd #define USER_TYPE -4
75 1.1 cgd int type; /* type of arg */
76 1.1 cgd struct arg *next; /* linked list pointer */
77 1.1 cgd } ARG;
78 1.1 cgd ARG *arglist; /* head of linked list */
79 1.1 cgd
80 1.1 cgd typedef struct ttytab {
81 1.6 cgd time_t logout; /* log out time */
82 1.1 cgd char tty[UT_LINESIZE + 1]; /* terminal name */
83 1.1 cgd struct ttytab *next; /* linked list pointer */
84 1.1 cgd } TTY;
85 1.1 cgd TTY *ttylist; /* head of linked list */
86 1.1 cgd
87 1.6 cgd static time_t currentout; /* current logout value */
88 1.6 cgd static long maxrec; /* records to display */
89 1.1 cgd static char *file = _PATH_WTMP; /* wtmp file */
90 1.7 perry static int fulltime = 0; /* Display seconds? */
91 1.1 cgd
92 1.8 kleink int main __P((int, char *[]));
93 1.5 jtc void addarg __P((int, char *));
94 1.5 jtc TTY *addtty __P((char *));
95 1.5 jtc void hostconv __P((char *));
96 1.5 jtc void onintr __P((int));
97 1.5 jtc char *ttyconv __P((char *));
98 1.5 jtc int want __P((struct utmp *, int));
99 1.5 jtc void wtmp __P((void));
100 1.5 jtc
101 1.5 jtc int
102 1.1 cgd main(argc, argv)
103 1.1 cgd int argc;
104 1.5 jtc char *argv[];
105 1.1 cgd {
106 1.1 cgd extern int optind;
107 1.1 cgd extern char *optarg;
108 1.1 cgd int ch;
109 1.5 jtc char *p;
110 1.1 cgd
111 1.1 cgd maxrec = -1;
112 1.8 kleink while ((ch = getopt(argc, argv, "0123456789f:h:t:T")) != -1)
113 1.5 jtc switch (ch) {
114 1.1 cgd case '0': case '1': case '2': case '3': case '4':
115 1.1 cgd case '5': case '6': case '7': case '8': case '9':
116 1.1 cgd /*
117 1.1 cgd * kludge: last was originally designed to take
118 1.1 cgd * a number after a dash.
119 1.1 cgd */
120 1.1 cgd if (maxrec == -1) {
121 1.1 cgd p = argv[optind - 1];
122 1.1 cgd if (p[0] == '-' && p[1] == ch && !p[2])
123 1.1 cgd maxrec = atol(++p);
124 1.1 cgd else
125 1.1 cgd maxrec = atol(argv[optind] + 1);
126 1.1 cgd if (!maxrec)
127 1.1 cgd exit(0);
128 1.1 cgd }
129 1.1 cgd break;
130 1.1 cgd case 'f':
131 1.1 cgd file = optarg;
132 1.1 cgd break;
133 1.1 cgd case 'h':
134 1.1 cgd hostconv(optarg);
135 1.1 cgd addarg(HOST_TYPE, optarg);
136 1.1 cgd break;
137 1.1 cgd case 't':
138 1.1 cgd addarg(TTY_TYPE, ttyconv(optarg));
139 1.1 cgd break;
140 1.7 perry case 'T':
141 1.7 perry fulltime = 1;
142 1.7 perry break;
143 1.1 cgd case '?':
144 1.1 cgd default:
145 1.5 jtc (void)fprintf(stderr,
146 1.7 perry "usage: last [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
147 1.1 cgd exit(1);
148 1.1 cgd }
149 1.1 cgd
150 1.1 cgd if (argc) {
151 1.1 cgd setlinebuf(stdout);
152 1.1 cgd for (argv += optind; *argv; ++argv) {
153 1.1 cgd #define COMPATIBILITY
154 1.1 cgd #ifdef COMPATIBILITY
155 1.1 cgd /* code to allow "last p5" to work */
156 1.1 cgd addarg(TTY_TYPE, ttyconv(*argv));
157 1.1 cgd #endif
158 1.1 cgd addarg(USER_TYPE, *argv);
159 1.1 cgd }
160 1.1 cgd }
161 1.1 cgd wtmp();
162 1.1 cgd exit(0);
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd /*
166 1.1 cgd * wtmp --
167 1.1 cgd * read through the wtmp file
168 1.1 cgd */
169 1.5 jtc void
170 1.1 cgd wtmp()
171 1.1 cgd {
172 1.7 perry struct utmp *bp; /* current structure */
173 1.7 perry TTY *T; /* tty list entry */
174 1.7 perry struct stat stb; /* stat of file for size */
175 1.9 thorpej time_t delta; /* time difference */
176 1.9 thorpej off_t bl;
177 1.7 perry int timesize; /* how much of time string to print */
178 1.1 cgd int bytes, wfd;
179 1.5 jtc char *ct, *crmsg;
180 1.5 jtc
181 1.8 kleink crmsg = NULL;
182 1.8 kleink
183 1.5 jtc if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
184 1.5 jtc err(1, "%s", file);
185 1.1 cgd bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
186 1.1 cgd
187 1.7 perry if (fulltime)
188 1.7 perry timesize = 8; /* HH:MM:SS */
189 1.7 perry else
190 1.7 perry timesize = 5; /* HH:MM */
191 1.7 perry
192 1.1 cgd (void)time(&buf[0].ut_time);
193 1.1 cgd (void)signal(SIGINT, onintr);
194 1.1 cgd (void)signal(SIGQUIT, onintr);
195 1.1 cgd
196 1.1 cgd while (--bl >= 0) {
197 1.9 thorpej if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
198 1.5 jtc (bytes = read(wfd, buf, sizeof(buf))) == -1)
199 1.5 jtc err(1, "%s", file);
200 1.1 cgd for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
201 1.1 cgd /*
202 1.1 cgd * if the terminal line is '~', the machine stopped.
203 1.1 cgd * see utmp(5) for more info.
204 1.1 cgd */
205 1.1 cgd if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
206 1.1 cgd /* everybody just logged out */
207 1.1 cgd for (T = ttylist; T; T = T->next)
208 1.1 cgd T->logout = -bp->ut_time;
209 1.1 cgd currentout = -bp->ut_time;
210 1.1 cgd crmsg = strncmp(bp->ut_name, "shutdown",
211 1.1 cgd UT_NAMESIZE) ? "crash" : "shutdown";
212 1.1 cgd if (want(bp, NO)) {
213 1.1 cgd ct = ctime(&bp->ut_time);
214 1.7 perry printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
215 1.5 jtc UT_NAMESIZE, UT_NAMESIZE,
216 1.5 jtc bp->ut_name, UT_LINESIZE,
217 1.5 jtc UT_LINESIZE, bp->ut_line,
218 1.5 jtc UT_HOSTSIZE, UT_HOSTSIZE,
219 1.7 perry bp->ut_host, ct, timesize,
220 1.7 perry timesize, ct + 11);
221 1.1 cgd if (maxrec != -1 && !--maxrec)
222 1.1 cgd return;
223 1.1 cgd }
224 1.1 cgd continue;
225 1.1 cgd }
226 1.1 cgd /*
227 1.1 cgd * if the line is '{' or '|', date got set; see
228 1.1 cgd * utmp(5) for more info.
229 1.1 cgd */
230 1.1 cgd if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
231 1.1 cgd && !bp->ut_line[1]) {
232 1.1 cgd if (want(bp, NO)) {
233 1.1 cgd ct = ctime(&bp->ut_time);
234 1.7 perry printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
235 1.5 jtc UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
236 1.5 jtc UT_LINESIZE, UT_LINESIZE, bp->ut_line,
237 1.5 jtc UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
238 1.7 perry ct, timesize, timesize, ct + 11);
239 1.1 cgd if (maxrec && !--maxrec)
240 1.1 cgd return;
241 1.1 cgd }
242 1.1 cgd continue;
243 1.1 cgd }
244 1.1 cgd /* find associated tty */
245 1.1 cgd for (T = ttylist;; T = T->next) {
246 1.1 cgd if (!T) {
247 1.1 cgd /* add new one */
248 1.1 cgd T = addtty(bp->ut_line);
249 1.1 cgd break;
250 1.1 cgd }
251 1.1 cgd if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
252 1.1 cgd break;
253 1.1 cgd }
254 1.1 cgd if (bp->ut_name[0] && want(bp, YES)) {
255 1.1 cgd ct = ctime(&bp->ut_time);
256 1.7 perry printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
257 1.5 jtc UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
258 1.5 jtc UT_LINESIZE, UT_LINESIZE, bp->ut_line,
259 1.5 jtc UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
260 1.7 perry ct, timesize, timesize, ct + 11);
261 1.1 cgd if (!T->logout)
262 1.1 cgd puts(" still logged in");
263 1.1 cgd else {
264 1.1 cgd if (T->logout < 0) {
265 1.1 cgd T->logout = -T->logout;
266 1.1 cgd printf("- %s", crmsg);
267 1.1 cgd }
268 1.1 cgd else
269 1.7 perry printf("- %*.*s",
270 1.7 perry timesize, timesize,
271 1.5 jtc ctime(&T->logout)+11);
272 1.1 cgd delta = T->logout - bp->ut_time;
273 1.5 jtc if (delta < SECSPERDAY)
274 1.7 perry printf(" (%*.*s)\n",
275 1.7 perry timesize, timesize,
276 1.5 jtc asctime(gmtime(&delta))+11);
277 1.1 cgd else
278 1.7 perry printf(" (%ld+%*.*s)\n",
279 1.5 jtc delta / SECSPERDAY,
280 1.7 perry timesize, timesize,
281 1.5 jtc asctime(gmtime(&delta))+11);
282 1.1 cgd }
283 1.1 cgd if (maxrec != -1 && !--maxrec)
284 1.1 cgd return;
285 1.1 cgd }
286 1.1 cgd T->logout = bp->ut_time;
287 1.1 cgd }
288 1.1 cgd }
289 1.1 cgd ct = ctime(&buf[0].ut_time);
290 1.7 perry printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * want --
295 1.1 cgd * see if want this entry
296 1.1 cgd */
297 1.5 jtc int
298 1.1 cgd want(bp, check)
299 1.5 jtc struct utmp *bp;
300 1.1 cgd int check;
301 1.1 cgd {
302 1.5 jtc ARG *step;
303 1.1 cgd
304 1.1 cgd if (check)
305 1.1 cgd /*
306 1.1 cgd * when uucp and ftp log in over a network, the entry in
307 1.1 cgd * the utmp file is the name plus their process id. See
308 1.1 cgd * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
309 1.1 cgd */
310 1.1 cgd if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
311 1.1 cgd bp->ut_line[3] = '\0';
312 1.1 cgd else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
313 1.1 cgd bp->ut_line[4] = '\0';
314 1.1 cgd if (!arglist)
315 1.5 jtc return (YES);
316 1.1 cgd
317 1.1 cgd for (step = arglist; step; step = step->next)
318 1.1 cgd switch(step->type) {
319 1.1 cgd case HOST_TYPE:
320 1.1 cgd if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
321 1.5 jtc return (YES);
322 1.1 cgd break;
323 1.1 cgd case TTY_TYPE:
324 1.1 cgd if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
325 1.5 jtc return (YES);
326 1.1 cgd break;
327 1.1 cgd case USER_TYPE:
328 1.1 cgd if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
329 1.5 jtc return (YES);
330 1.1 cgd break;
331 1.1 cgd }
332 1.5 jtc return (NO);
333 1.1 cgd }
334 1.1 cgd
335 1.1 cgd /*
336 1.1 cgd * addarg --
337 1.1 cgd * add an entry to a linked list of arguments
338 1.1 cgd */
339 1.5 jtc void
340 1.1 cgd addarg(type, arg)
341 1.1 cgd int type;
342 1.1 cgd char *arg;
343 1.1 cgd {
344 1.5 jtc ARG *cur;
345 1.1 cgd
346 1.5 jtc if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
347 1.5 jtc err(1, "malloc failure");
348 1.1 cgd cur->next = arglist;
349 1.1 cgd cur->type = type;
350 1.1 cgd cur->name = arg;
351 1.1 cgd arglist = cur;
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd /*
355 1.1 cgd * addtty --
356 1.1 cgd * add an entry to a linked list of ttys
357 1.1 cgd */
358 1.1 cgd TTY *
359 1.1 cgd addtty(ttyname)
360 1.1 cgd char *ttyname;
361 1.1 cgd {
362 1.5 jtc TTY *cur;
363 1.1 cgd
364 1.5 jtc if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
365 1.5 jtc err(1, "malloc failure");
366 1.1 cgd cur->next = ttylist;
367 1.1 cgd cur->logout = currentout;
368 1.5 jtc memmove(cur->tty, ttyname, UT_LINESIZE);
369 1.5 jtc return (ttylist = cur);
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd /*
373 1.1 cgd * hostconv --
374 1.1 cgd * convert the hostname to search pattern; if the supplied host name
375 1.1 cgd * has a domain attached that is the same as the current domain, rip
376 1.1 cgd * off the domain suffix since that's what login(1) does.
377 1.1 cgd */
378 1.5 jtc void
379 1.1 cgd hostconv(arg)
380 1.1 cgd char *arg;
381 1.1 cgd {
382 1.1 cgd static int first = 1;
383 1.1 cgd static char *hostdot, name[MAXHOSTNAMELEN];
384 1.5 jtc char *argdot;
385 1.1 cgd
386 1.5 jtc if (!(argdot = strchr(arg, '.')))
387 1.1 cgd return;
388 1.1 cgd if (first) {
389 1.1 cgd first = 0;
390 1.5 jtc if (gethostname(name, sizeof(name)))
391 1.5 jtc err(1, "gethostname");
392 1.5 jtc hostdot = strchr(name, '.');
393 1.1 cgd }
394 1.1 cgd if (hostdot && !strcasecmp(hostdot, argdot))
395 1.1 cgd *argdot = '\0';
396 1.1 cgd }
397 1.1 cgd
398 1.1 cgd /*
399 1.1 cgd * ttyconv --
400 1.1 cgd * convert tty to correct name.
401 1.1 cgd */
402 1.1 cgd char *
403 1.1 cgd ttyconv(arg)
404 1.1 cgd char *arg;
405 1.1 cgd {
406 1.5 jtc char *mval;
407 1.1 cgd
408 1.1 cgd /*
409 1.1 cgd * kludge -- we assume that all tty's end with
410 1.1 cgd * a two character suffix.
411 1.1 cgd */
412 1.1 cgd if (strlen(arg) == 2) {
413 1.1 cgd /* either 6 for "ttyxx" or 8 for "console" */
414 1.5 jtc if (!(mval = malloc((u_int)8)))
415 1.5 jtc err(1, "malloc failure");
416 1.1 cgd if (!strcmp(arg, "co"))
417 1.1 cgd (void)strcpy(mval, "console");
418 1.1 cgd else {
419 1.1 cgd (void)strcpy(mval, "tty");
420 1.1 cgd (void)strcpy(mval + 3, arg);
421 1.1 cgd }
422 1.5 jtc return (mval);
423 1.1 cgd }
424 1.1 cgd if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
425 1.5 jtc return (arg + 5);
426 1.5 jtc return (arg);
427 1.1 cgd }
428 1.1 cgd
429 1.1 cgd /*
430 1.1 cgd * onintr --
431 1.1 cgd * on interrupt, we inform the user how far we've gotten
432 1.1 cgd */
433 1.1 cgd void
434 1.1 cgd onintr(signo)
435 1.1 cgd int signo;
436 1.1 cgd {
437 1.5 jtc char *ct;
438 1.1 cgd
439 1.1 cgd ct = ctime(&buf[0].ut_time);
440 1.7 perry printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
441 1.1 cgd if (signo == SIGINT)
442 1.1 cgd exit(1);
443 1.1 cgd (void)fflush(stdout); /* fix required for rsh */
444 1.1 cgd }
445