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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
63 #include <sys/queue.h>
65 #define NO 0 /* false/no */
66 #define YES 1 /* true/yes */
67 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
69 static struct utmp buf[1024]; /* utmp read buffer */
72 char *name; /* argument */
76 int type; /* type of arg */
77 struct arg *next; /* linked list pointer */
79 ARG *arglist; /* head of linked list */
81 LIST_HEAD(ttylisthead, ttytab) ttylist;
84 time_t logout; /* log out time */
85 char tty[UT_LINESIZE + 1]; /* terminal name */
86 LIST_ENTRY(ttytab) list;
89 static const char *crmsg; /* cause of last reboot */
90 static long currentout, /* current logout value */
91 maxrec; /* records to display */
92 static const char *file = _PATH_WTMP; /* wtmp file */
93 static int sflag = 0; /* show delta in seconds */
94 static int width = 5; /* show seconds in delta */
95 static int yflag; /* show year */
97 static int snapfound = 0; /* found snapshot entry? */
98 static time_t snaptime; /* if != 0, we will only
99 * report users logged in
100 * at this snapshot time
103 void addarg(int, char *);
104 time_t dateconv(char *);
105 void doentry(struct utmp *);
106 void hostconv(char *);
108 void printentry(struct utmp *, struct ttytab *);
109 char *ttyconv(char *);
110 int want(struct utmp *);
117 (void)fprintf(stderr,
118 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
119 " [-n maxrec] [-t tty] [user ...]\n");
124 main(int argc, char *argv[])
129 (void) setlocale(LC_TIME, "");
130 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
134 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
136 case '0': case '1': case '2': case '3': case '4':
137 case '5': case '6': case '7': case '8': case '9':
139 * kludge: last was originally designed to take
140 * a number after a dash.
143 p = strchr(argv[optind - 1], ch);
145 p = strchr(argv[optind], ch);
152 snaptime = dateconv(optarg);
159 addarg(HOST_TYPE, optarg);
163 maxrec = strtol(optarg, &p, 10);
164 if (p == optarg || *p != '\0' || errno != 0 ||
166 errx(1, "%s: bad line count", optarg);
169 sflag++; /* Show delta as seconds */
172 addarg(TTY_TYPE, ttyconv(optarg));
185 if (sflag && width == 8) usage();
189 for (argv += optind; *argv; ++argv) {
190 #define COMPATIBILITY
192 /* code to allow "last p5" to work */
193 addarg(TTY_TYPE, ttyconv(*argv));
195 addarg(USER_TYPE, *argv);
204 * read through the wtmp file
209 struct utmp *bp; /* current structure */
210 struct stat stb; /* stat of file for size */
219 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
221 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
224 buf[0].ut_time = _time_to_int(t);
225 (void)signal(SIGINT, onintr);
226 (void)signal(SIGQUIT, onintr);
229 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
230 (bytes = read(wfd, buf, sizeof(buf))) == -1)
232 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp)
235 t = _int_to_time(buf[0].ut_time);
237 (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm);
243 * process a single wtmp entry
246 doentry(struct utmp *bp)
248 struct ttytab *tt, *ttx; /* ttylist entry */
251 * if the terminal line is '~', the machine stopped.
252 * see utmp(5) for more info.
254 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
255 /* everybody just logged out */
256 for (tt = LIST_FIRST(&ttylist); tt;) {
257 LIST_REMOVE(tt, list);
259 tt = LIST_NEXT(tt, list);
262 currentout = -bp->ut_time;
263 crmsg = strncmp(bp->ut_name, "shutdown", UT_NAMESIZE) ?
264 "crash" : "shutdown";
266 * if we're in snapshot mode, we want to exit if this
267 * shutdown/reboot appears while we we are tracking the
270 if (snaptime && snapfound)
273 * don't print shutdown/reboot entries unless flagged for
275 if (!snaptime && want(bp))
276 printentry(bp, NULL);
280 * if the line is '{' or '|', date got set; see
281 * utmp(5) for more info.
283 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') &&
285 if (want(bp) && !snaptime)
286 printentry(bp, NULL);
289 /* find associated tty */
290 LIST_FOREACH(tt, &ttylist, list)
291 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE))
296 tt = malloc(sizeof(struct ttytab));
298 errx(1, "malloc failure");
299 tt->logout = currentout;
300 strncpy(tt->tty, bp->ut_line, UT_LINESIZE);
301 LIST_INSERT_HEAD(&ttylist, tt, list);
305 * print record if not in snapshot mode and wanted
306 * or in snapshot mode and in snapshot range
308 if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime &&
309 (tt->logout > snaptime || tt->logout < 1)))) {
312 * when uucp and ftp log in over a network, the entry in
313 * the utmp file is the name plus their process id. See
314 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
316 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
317 bp->ut_line[3] = '\0';
318 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
319 bp->ut_line[4] = '\0';
322 tt->logout = bp->ut_time;
329 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
330 * logout type (crash/shutdown) as appropriate.
333 printentry(struct utmp *bp, struct ttytab *tt)
337 time_t delta; /* time difference */
340 if (maxrec != -1 && !maxrec--)
342 t = _int_to_time(bp->ut_time);
344 (void) strftime(ct, sizeof(ct), d_first ?
345 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
346 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
347 printf("%-*.*s %-*.*s %-*.*s %s%c",
348 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
349 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
350 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
351 ct, tt == NULL ? '\n' : ' ');
355 puts(" still logged in");
358 if (tt->logout < 0) {
359 tt->logout = -tt->logout;
360 printf("- %s", crmsg);
362 tm = localtime(&tt->logout);
363 (void) strftime(ct, sizeof(ct), "%R", tm);
366 delta = tt->logout - bp->ut_time;
368 printf(" (%8ld)\n", (long)delta);
371 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
373 printf(" (%s)\n", ct);
375 printf(" (%ld+%s)\n", (long)delta / 86400, ct);
381 * see if want this entry
384 want(struct utmp *bp)
394 for (step = arglist; step; step = step->next)
397 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
401 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
405 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
414 * add an entry to a linked list of arguments
417 addarg(int type, char *arg)
421 if ((cur = malloc(sizeof(ARG))) == NULL)
422 errx(1, "malloc failure");
431 * convert the hostname to search pattern; if the supplied host name
432 * has a domain attached that is the same as the current domain, rip
433 * off the domain suffix since that's what login(1) does.
438 static int first = 1;
439 static char *hostdot, name[MAXHOSTNAMELEN];
442 if (!(argdot = strchr(arg, '.')))
446 if (gethostname(name, sizeof(name)))
447 err(1, "gethostname");
448 hostdot = strchr(name, '.');
450 if (hostdot && !strcasecmp(hostdot, argdot))
456 * convert tty to correct name.
464 * kludge -- we assume that all tty's end with
465 * a two character suffix.
467 if (strlen(arg) == 2) {
468 /* either 6 for "ttyxx" or 8 for "console" */
469 if ((mval = malloc(8)) == NULL)
470 errx(1, "malloc failure");
471 if (!strcmp(arg, "co"))
472 (void)strcpy(mval, "console");
474 (void)strcpy(mval, "tty");
475 (void)strcpy(mval + 3, arg);
479 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
486 * Convert the snapshot time in command line given in the format
487 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
488 * Derived from atime_arg1() in usr.bin/touch/touch.c
498 /* Start with the current time. */
499 if (time(&timet) < 0)
501 if ((t = localtime(&timet)) == NULL)
504 /* [[CC]YY]MMDDhhmm[.SS] */
505 if ((p = strchr(arg, '.')) == NULL)
506 t->tm_sec = 0; /* Seconds defaults to 0. */
508 if (strlen(p + 1) != 2)
511 t->tm_sec = ATOI2(p);
515 switch (strlen(arg)) {
516 case 12: /* CCYYMMDDhhmm */
517 t->tm_year = ATOI2(arg);
521 case 10: /* YYMMDDhhmm */
523 yearset = ATOI2(arg);
524 t->tm_year += yearset;
526 yearset = ATOI2(arg);
528 t->tm_year = yearset + 2000;
530 t->tm_year = yearset + 1900;
532 t->tm_year -= 1900; /* Convert to UNIX time. */
534 case 8: /* MMDDhhmm */
535 t->tm_mon = ATOI2(arg);
536 --t->tm_mon; /* Convert from 01-12 to 00-11 */
537 t->tm_mday = ATOI2(arg);
538 t->tm_hour = ATOI2(arg);
539 t->tm_min = ATOI2(arg);
542 t->tm_hour = ATOI2(arg);
543 t->tm_min = ATOI2(arg);
548 t->tm_isdst = -1; /* Figure out DST. */
552 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
559 * on interrupt, we inform the user how far we've gotten
566 time_t t = _int_to_time(buf[0].ut_time);
569 (void) strftime(ct, sizeof(ct),
570 d_first ? "%a %e %b %R" : "%a %b %e %R",
572 printf("\ninterrupted %s\n", ct);
575 (void)fflush(stdout); /* fix required for rsh */