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;
70 char *name; /* argument */
74 int type; /* type of arg */
75 struct arg *next; /* linked list pointer */
77 ARG *arglist; /* head of linked list */
79 LIST_HEAD(idlisthead, idtab) idlist;
82 time_t logout; /* log out time */
83 char id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
84 LIST_ENTRY(idtab) list;
87 static const char *crmsg; /* cause of last reboot */
88 static time_t currentout; /* current logout value */
89 static long maxrec; /* records to display */
90 static const char *file = NULL; /* wtmp file */
91 static int sflag = 0; /* show delta in seconds */
92 static int width = 5; /* show seconds in delta */
93 static int yflag; /* show year */
95 static int snapfound = 0; /* found snapshot entry? */
96 static time_t snaptime; /* if != 0, we will only
97 * report users logged in
98 * at this snapshot time
101 void addarg(int, char *);
102 time_t dateconv(char *);
103 void doentry(struct utmpx *);
104 void hostconv(char *);
105 void printentry(struct utmpx *, struct idtab *);
106 char *ttyconv(char *);
107 int want(struct utmpx *);
114 (void)fprintf(stderr,
115 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
116 " [-n maxrec] [-t tty] [user ...]\n");
121 main(int argc, char *argv[])
126 (void) setlocale(LC_TIME, "");
127 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
131 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
133 case '0': case '1': case '2': case '3': case '4':
134 case '5': case '6': case '7': case '8': case '9':
136 * kludge: last was originally designed to take
137 * a number after a dash.
140 p = strchr(argv[optind - 1], ch);
142 p = strchr(argv[optind], ch);
149 snaptime = dateconv(optarg);
156 addarg(HOST_TYPE, optarg);
160 maxrec = strtol(optarg, &p, 10);
161 if (p == optarg || *p != '\0' || errno != 0 ||
163 errx(1, "%s: bad line count", optarg);
166 sflag++; /* Show delta as seconds */
169 addarg(TTY_TYPE, ttyconv(optarg));
182 if (sflag && width == 8) usage();
186 for (argv += optind; *argv; ++argv) {
187 #define COMPATIBILITY
189 /* code to allow "last p5" to work */
190 addarg(TTY_TYPE, ttyconv(*argv));
192 addarg(USER_TYPE, *argv);
201 * read through the wtmp file
206 struct utmpx *buf = NULL;
208 static unsigned int amount = 0;
216 /* Load the last entries from the file. */
217 if (setutxdb(UTXDB_LOG, file) != 0)
219 while ((ut = getutxent()) != NULL) {
220 if (amount % 128 == 0) {
221 buf = realloc(buf, (amount + 128) * sizeof *ut);
225 memcpy(&buf[amount++], ut, sizeof *ut);
226 if (t > ut->ut_tv.tv_sec)
227 t = ut->ut_tv.tv_sec;
231 /* Display them in reverse order. */
233 doentry(&buf[--amount]);
236 (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm);
242 * process a single wtmp entry
245 doentry(struct utmpx *bp)
247 struct idtab *tt, *ttx; /* idlist entry */
249 /* the machine stopped */
250 if (bp->ut_type == BOOT_TIME || bp->ut_type == SHUTDOWN_TIME) {
251 /* everybody just logged out */
252 for (tt = LIST_FIRST(&idlist); tt;) {
253 LIST_REMOVE(tt, list);
255 tt = LIST_NEXT(tt, list);
258 currentout = -bp->ut_tv.tv_sec;
259 crmsg = bp->ut_type != SHUTDOWN_TIME ?
260 "crash" : "shutdown";
262 * if we're in snapshot mode, we want to exit if this
263 * shutdown/reboot appears while we we are tracking the
266 if (snaptime && snapfound)
269 * don't print shutdown/reboot entries unless flagged for
271 if (!snaptime && want(bp))
272 printentry(bp, NULL);
276 if (bp->ut_type == OLD_TIME || bp->ut_type == NEW_TIME) {
277 if (want(bp) && !snaptime)
278 printentry(bp, NULL);
282 if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS)
285 /* find associated identifier */
286 LIST_FOREACH(tt, &idlist, list)
287 if (!memcmp(tt->id, bp->ut_id, sizeof bp->ut_id))
292 tt = malloc(sizeof(struct idtab));
294 errx(1, "malloc failure");
295 tt->logout = currentout;
296 memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
297 LIST_INSERT_HEAD(&idlist, tt, list);
301 * print record if not in snapshot mode and wanted
302 * or in snapshot mode and in snapshot range
304 if (bp->ut_type == USER_PROCESS && (want(bp) ||
305 (bp->ut_tv.tv_sec < snaptime &&
306 (tt->logout > snaptime || tt->logout < 1)))) {
310 tt->logout = bp->ut_tv.tv_sec;
317 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
318 * logout type (crash/shutdown) as appropriate.
321 printentry(struct utmpx *bp, struct idtab *tt)
325 time_t delta; /* time difference */
328 if (maxrec != -1 && !maxrec--)
330 t = bp->ut_tv.tv_sec;
332 (void) strftime(ct, sizeof(ct), d_first ?
333 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
334 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
335 switch (bp->ut_type) {
337 printf("%-42s", "boot time");
340 printf("%-42s", "shutdown time");
343 printf("%-42s", "old time");
346 printf("%-42s", "new time");
349 printf("%-10s %-8s %-22.22s",
350 bp->ut_user, bp->ut_line, bp->ut_host);
353 printf(" %s%c", ct, tt == NULL ? '\n' : ' ');
357 puts(" still logged in");
360 if (tt->logout < 0) {
361 tt->logout = -tt->logout;
362 printf("- %s", crmsg);
364 tm = localtime(&tt->logout);
365 (void) strftime(ct, sizeof(ct), "%R", tm);
368 delta = tt->logout - bp->ut_tv.tv_sec;
370 printf(" (%8ld)\n", (long)delta);
373 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
375 printf(" (%s)\n", ct);
377 printf(" (%ld+%s)\n", (long)delta / 86400, ct);
383 * see if want this entry
386 want(struct utmpx *bp)
396 for (step = arglist; step; step = step->next)
399 if (!strcasecmp(step->name, bp->ut_host))
403 if (!strcmp(step->name, bp->ut_line))
407 if (!strcmp(step->name, bp->ut_user))
416 * add an entry to a linked list of arguments
419 addarg(int type, char *arg)
423 if ((cur = malloc(sizeof(ARG))) == NULL)
424 errx(1, "malloc failure");
433 * convert the hostname to search pattern; if the supplied host name
434 * has a domain attached that is the same as the current domain, rip
435 * off the domain suffix since that's what login(1) does.
440 static int first = 1;
441 static char *hostdot, name[MAXHOSTNAMELEN];
444 if (!(argdot = strchr(arg, '.')))
448 if (gethostname(name, sizeof(name)))
449 err(1, "gethostname");
450 hostdot = strchr(name, '.');
452 if (hostdot && !strcasecmp(hostdot, argdot))
458 * convert tty to correct name.
466 * kludge -- we assume that all tty's end with
467 * a two character suffix.
469 if (strlen(arg) == 2) {
470 /* either 6 for "ttyxx" or 8 for "console" */
471 if ((mval = malloc(8)) == NULL)
472 errx(1, "malloc failure");
473 if (!strcmp(arg, "co"))
474 (void)strcpy(mval, "console");
476 (void)strcpy(mval, "tty");
477 (void)strcpy(mval + 3, arg);
481 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
488 * Convert the snapshot time in command line given in the format
489 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
490 * Derived from atime_arg1() in usr.bin/touch/touch.c
500 /* Start with the current time. */
501 if (time(&timet) < 0)
503 if ((t = localtime(&timet)) == NULL)
506 /* [[CC]YY]MMDDhhmm[.SS] */
507 if ((p = strchr(arg, '.')) == NULL)
508 t->tm_sec = 0; /* Seconds defaults to 0. */
510 if (strlen(p + 1) != 2)
513 t->tm_sec = ATOI2(p);
517 switch (strlen(arg)) {
518 case 12: /* CCYYMMDDhhmm */
519 t->tm_year = ATOI2(arg);
523 case 10: /* YYMMDDhhmm */
525 yearset = ATOI2(arg);
526 t->tm_year += yearset;
528 yearset = ATOI2(arg);
530 t->tm_year = yearset + 2000;
532 t->tm_year = yearset + 1900;
534 t->tm_year -= 1900; /* Convert to UNIX time. */
536 case 8: /* MMDDhhmm */
537 t->tm_mon = ATOI2(arg);
538 --t->tm_mon; /* Convert from 01-12 to 00-11 */
539 t->tm_mday = ATOI2(arg);
540 t->tm_hour = ATOI2(arg);
541 t->tm_min = ATOI2(arg);
544 t->tm_hour = ATOI2(arg);
545 t->tm_min = ATOI2(arg);
550 t->tm_isdst = -1; /* Figure out DST. */
554 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");