2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, 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
37 static char copyright[] =
38 "@(#) Copyright (c) 1987, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
46 #include <sys/param.h>
61 #include <sys/queue.h>
63 #define NO 0 /* false/no */
64 #define YES 1 /* true/yes */
66 static struct utmp buf[1024]; /* utmp read buffer */
69 char *name; /* argument */
73 int type; /* type of arg */
74 struct arg *next; /* linked list pointer */
76 ARG *arglist; /* head of linked list */
78 LIST_HEAD(ttylisthead, ttytab) ttylist;
81 time_t logout; /* log out time */
82 char tty[UT_LINESIZE + 1]; /* terminal name */
83 LIST_ENTRY(ttytab) list;
86 static long currentout, /* current logout value */
87 maxrec; /* records to display */
88 static char *file = _PATH_WTMP; /* wtmp file */
89 static int sflag = 0; /* show delta in seconds */
90 static int width = 5; /* show seconds in delta */
93 void addarg __P((int, char *));
94 void hostconv __P((char *));
95 void onintr __P((int));
96 char *ttyconv __P((char *));
97 int want __P((struct utmp *));
98 void wtmp __P((void));
103 (void)fprintf(stderr,
104 "usage: last [-#] [-f file] [-h hostname] [-t tty] [-s|w] [user ...]\n");
116 (void) setlocale(LC_TIME, "");
117 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
120 while ((ch = getopt(argc, argv, "0123456789f:h:st:w")) != -1)
122 case '0': case '1': case '2': case '3': case '4':
123 case '5': case '6': case '7': case '8': case '9':
125 * kludge: last was originally designed to take
126 * a number after a dash.
129 p = argv[optind - 1];
130 if (p[0] == '-' && p[1] == ch && !p[2])
133 maxrec = atol(argv[optind] + 1);
143 addarg(HOST_TYPE, optarg);
146 sflag++; /* Show delta as seconds */
149 addarg(TTY_TYPE, ttyconv(optarg));
159 if (sflag && width == 8) usage();
163 for (argv += optind; *argv; ++argv) {
164 #define COMPATIBILITY
166 /* code to allow "last p5" to work */
167 addarg(TTY_TYPE, ttyconv(*argv));
169 addarg(USER_TYPE, *argv);
178 * read through the wtmp file
183 struct utmp *bp; /* current structure */
184 struct ttytab *tt, *ttx; /* ttylist entry */
185 struct stat stb; /* stat of file for size */
187 time_t delta; /* time difference */
195 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
197 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
199 (void)time(&buf[0].ut_time);
200 (void)signal(SIGINT, onintr);
201 (void)signal(SIGQUIT, onintr);
204 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
205 (bytes = read(wfd, buf, sizeof(buf))) == -1)
207 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
209 * if the terminal line is '~', the machine stopped.
210 * see utmp(5) for more info.
212 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
213 /* everybody just logged out */
214 for (tt = LIST_FIRST(&ttylist); tt;) {
215 LIST_REMOVE(tt, list);
217 tt = LIST_NEXT(tt, list);
220 currentout = -bp->ut_time;
221 crmsg = strncmp(bp->ut_name, "shutdown",
222 UT_NAMESIZE) ? "crash" : "shutdown";
224 tm = localtime(&bp->ut_time);
225 (void) strftime(ct, sizeof(ct),
226 d_first ? "%a %e %b %R" :
229 printf("%-*.*s %-*.*s %-*.*s %s\n",
230 UT_NAMESIZE, UT_NAMESIZE,
231 bp->ut_name, UT_LINESIZE,
232 UT_LINESIZE, bp->ut_line,
233 UT_HOSTSIZE, UT_HOSTSIZE,
235 if (maxrec != -1 && !--maxrec)
241 * if the line is '{' or '|', date got set; see
242 * utmp(5) for more info.
244 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
245 && !bp->ut_line[1]) {
247 tm = localtime(&bp->ut_time);
248 (void) strftime(ct, sizeof(ct),
249 d_first ? "%a %e %b %R" :
252 printf("%-*.*s %-*.*s %-*.*s %s\n",
253 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
254 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
255 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
257 if (maxrec && !--maxrec)
262 if (bp->ut_name[0] == '\0' || want(bp)) {
263 /* find associated tty */
264 LIST_FOREACH(tt, &ttylist, list)
265 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE))
270 tt = malloc(sizeof(struct ttytab));
272 err(1, "malloc failure");
273 tt->logout = currentout;
274 strncpy(tt->tty, bp->ut_line, UT_LINESIZE);
275 LIST_INSERT_HEAD(&ttylist, tt, list);
278 if (bp->ut_name[0]) {
280 * when uucp and ftp log in over a network, the entry in
281 * the utmp file is the name plus their process id. See
282 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
284 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
285 bp->ut_line[3] = '\0';
286 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
287 bp->ut_line[4] = '\0';
288 tm = localtime(&bp->ut_time);
289 (void) strftime(ct, sizeof(ct),
290 d_first ? "%a %e %b %R" :
293 printf("%-*.*s %-*.*s %-*.*s %s ",
294 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
295 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
296 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
299 puts(" still logged in");
301 if (tt->logout < 0) {
302 tt->logout = -tt->logout;
303 printf("- %s", crmsg);
306 tm = localtime(&tt->logout);
307 (void) strftime(ct, sizeof(ct), "%R", tm);
310 delta = tt->logout - bp->ut_time;
316 (void) strftime(ct, sizeof(ct),
317 width >= 8 ? "%T" : "%R",
320 printf(" (%s)\n", ct);
322 printf(" (%ld+%s)\n",
326 LIST_REMOVE(tt, list);
328 if (maxrec != -1 && !--maxrec)
331 tt->logout = bp->ut_time;
336 tm = localtime(&buf[0].ut_time);
337 (void) strftime(ct, sizeof(ct), "\nwtmp begins %c\n", tm);
343 * see if want this entry
354 for (step = arglist; step; step = step->next)
357 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
361 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
365 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
374 * add an entry to a linked list of arguments
383 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
384 err(1, "malloc failure");
393 * convert the hostname to search pattern; if the supplied host name
394 * has a domain attached that is the same as the current domain, rip
395 * off the domain suffix since that's what login(1) does.
401 static int first = 1;
402 static char *hostdot, name[MAXHOSTNAMELEN];
405 if (!(argdot = strchr(arg, '.')))
409 if (gethostname(name, sizeof(name)))
410 err(1, "gethostname");
411 hostdot = strchr(name, '.');
413 if (hostdot && !strcasecmp(hostdot, argdot))
419 * convert tty to correct name.
428 * kludge -- we assume that all tty's end with
429 * a two character suffix.
431 if (strlen(arg) == 2) {
432 /* either 6 for "ttyxx" or 8 for "console" */
433 if (!(mval = malloc((u_int)8)))
434 err(1, "malloc failure");
435 if (!strcmp(arg, "co"))
436 (void)strcpy(mval, "console");
438 (void)strcpy(mval, "tty");
439 (void)strcpy(mval + 3, arg);
443 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
450 * on interrupt, we inform the user how far we've gotten
459 tm = localtime(&buf[0].ut_time);
460 (void) strftime(ct, sizeof(ct),
461 d_first ? "%a %e %b %R" : "%a %b %e %R",
463 printf("\ninterrupted %s\n", ct);
466 (void)fflush(stdout); /* fix required for rsh */