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>
60 #include <sys/queue.h>
62 #define NO 0 /* false/no */
63 #define YES 1 /* true/yes */
65 static struct utmp buf[1024]; /* utmp read buffer */
68 char *name; /* argument */
72 int type; /* type of arg */
73 struct arg *next; /* linked list pointer */
75 ARG *arglist; /* head of linked list */
77 LIST_HEAD(ttylisthead, ttytab) ttylist;
80 time_t logout; /* log out time */
81 char tty[UT_LINESIZE + 1]; /* terminal name */
82 LIST_ENTRY(ttytab) list;
85 static long currentout, /* current logout value */
86 maxrec; /* records to display */
87 static char *file = _PATH_WTMP; /* wtmp file */
88 static int sflag = 0; /* show delta in seconds */
89 static int width = 5; /* show seconds in delta */
91 void addarg __P((int, char *));
92 void hostconv __P((char *));
93 void onintr __P((int));
94 char *ttyconv __P((char *));
95 int want __P((struct utmp *));
96 void wtmp __P((void));
101 (void)fprintf(stderr,
102 "usage: last [-#] [-f file] [-h hostname] [-t tty] [-s|w] [user ...]\n");
114 (void) setlocale(LC_TIME, "");
117 while ((ch = getopt(argc, argv, "0123456789f:h:st:w")) != -1)
119 case '0': case '1': case '2': case '3': case '4':
120 case '5': case '6': case '7': case '8': case '9':
122 * kludge: last was originally designed to take
123 * a number after a dash.
126 p = argv[optind - 1];
127 if (p[0] == '-' && p[1] == ch && !p[2])
130 maxrec = atol(argv[optind] + 1);
140 addarg(HOST_TYPE, optarg);
143 sflag++; /* Show delta as seconds */
146 addarg(TTY_TYPE, ttyconv(optarg));
156 if (sflag && width == 8) usage();
160 for (argv += optind; *argv; ++argv) {
161 #define COMPATIBILITY
163 /* code to allow "last p5" to work */
164 addarg(TTY_TYPE, ttyconv(*argv));
166 addarg(USER_TYPE, *argv);
175 * read through the wtmp file
180 struct utmp *bp; /* current structure */
181 struct ttytab *tt, *ttx; /* ttylist entry */
182 struct stat stb; /* stat of file for size */
184 time_t delta; /* time difference */
192 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
194 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
196 (void)time(&buf[0].ut_time);
197 (void)signal(SIGINT, onintr);
198 (void)signal(SIGQUIT, onintr);
201 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
202 (bytes = read(wfd, buf, sizeof(buf))) == -1)
204 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
206 * if the terminal line is '~', the machine stopped.
207 * see utmp(5) for more info.
209 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
210 /* everybody just logged out */
211 for (tt = ttylist.lh_first; tt;) {
212 LIST_REMOVE(tt, list);
214 tt = tt->list.le_next;
217 currentout = -bp->ut_time;
218 crmsg = strncmp(bp->ut_name, "shutdown",
219 UT_NAMESIZE) ? "crash" : "shutdown";
221 tm = localtime(&bp->ut_time);
222 (void) strftime(ct, sizeof(ct), "%c", tm);
223 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n",
224 UT_NAMESIZE, UT_NAMESIZE,
225 bp->ut_name, UT_LINESIZE,
226 UT_LINESIZE, bp->ut_line,
227 UT_HOSTSIZE, UT_HOSTSIZE,
228 bp->ut_host, ct, ct + 11);
229 if (maxrec != -1 && !--maxrec)
235 * if the line is '{' or '|', date got set; see
236 * utmp(5) for more info.
238 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
239 && !bp->ut_line[1]) {
241 tm = localtime(&bp->ut_time);
242 (void) strftime(ct, sizeof(ct), "%c", tm);
243 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n",
244 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
245 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
246 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
248 if (maxrec && !--maxrec)
253 if (bp->ut_name[0] == '\0' || want(bp)) {
254 /* find associated tty */
255 for (tt = ttylist.lh_first; ; tt = tt->list.le_next) {
258 tt = malloc(sizeof(struct ttytab));
260 err(1, "malloc failure");
261 tt->logout = currentout;
262 strncpy(tt->tty, bp->ut_line, UT_LINESIZE);
263 LIST_INSERT_HEAD(&ttylist, tt, list);
266 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE))
269 if (bp->ut_name[0]) {
271 * when uucp and ftp log in over a network, the entry in
272 * the utmp file is the name plus their process id. See
273 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
275 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
276 bp->ut_line[3] = '\0';
277 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
278 bp->ut_line[4] = '\0';
279 tm = localtime(&bp->ut_time);
280 (void) strftime(ct, sizeof(ct), "%c", tm);
281 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ",
282 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
283 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
284 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
287 puts(" still logged in");
289 if (tt->logout < 0) {
290 tt->logout = -tt->logout;
291 printf("- %s", crmsg);
294 tm = localtime(&tt->logout);
295 (void) strftime(ct, sizeof(ct), "%c", tm);
296 printf("- %5.5s", ct + 11);
298 delta = tt->logout - bp->ut_time;
304 (void) strftime(ct, sizeof(ct), "%c", tm);
306 printf(" (%*.*s)\n", width, width, ct + 11);
308 printf(" (%ld+%*.*s)\n",
309 delta / 86400, width, width, ct + 11);
312 LIST_REMOVE(tt, list);
314 if (maxrec != -1 && !--maxrec)
317 tt->logout = bp->ut_time;
322 tm = localtime(&buf[0].ut_time);
323 (void) strftime(ct, sizeof(ct), "\nwtmp begins %c\n", tm);
329 * see if want this entry
340 for (step = arglist; step; step = step->next)
343 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
347 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
351 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
360 * add an entry to a linked list of arguments
369 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
370 err(1, "malloc failure");
379 * convert the hostname to search pattern; if the supplied host name
380 * has a domain attached that is the same as the current domain, rip
381 * off the domain suffix since that's what login(1) does.
387 static int first = 1;
388 static char *hostdot, name[MAXHOSTNAMELEN];
391 if (!(argdot = strchr(arg, '.')))
395 if (gethostname(name, sizeof(name)))
396 err(1, "gethostname");
397 hostdot = strchr(name, '.');
399 if (hostdot && !strcasecmp(hostdot, argdot))
405 * convert tty to correct name.
414 * kludge -- we assume that all tty's end with
415 * a two character suffix.
417 if (strlen(arg) == 2) {
418 /* either 6 for "ttyxx" or 8 for "console" */
419 if (!(mval = malloc((u_int)8)))
420 err(1, "malloc failure");
421 if (!strcmp(arg, "co"))
422 (void)strcpy(mval, "console");
424 (void)strcpy(mval, "tty");
425 (void)strcpy(mval + 3, arg);
429 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
436 * on interrupt, we inform the user how far we've gotten
445 tm = localtime(&buf[0].ut_time);
446 (void) strftime(ct, sizeof(ct), "%c", tm);
447 printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
450 (void)fflush(stdout); /* fix required for rsh */