2 * Berkeley last for Linux. Currently maintained by poe@daimi.aau.dk at
3 * ftp://ftp.daimi.aau.dk/pub/linux/poe/admutil*
5 * Copyright (c) 1987 Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
22 * - added Native Language Support
25 /* 2001-02-14 Marek Zelem <marek@fornax.sk>
26 * - using mmap() on Linux - great speed improvement
32 #include <sys/param.h>
35 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
50 #include "pathnames.h"
53 #define SECDAY (24*60*60) /* seconds in a day */
54 #define NO 0 /* false/no */
55 #define YES 1 /* true/yes */
57 static struct utmp utmpbuf;
59 #define HMAX (int)sizeof(utmpbuf.ut_host) /* size of utmp host field */
60 #define LMAX (int)sizeof(utmpbuf.ut_line) /* size of utmp tty field */
61 #define NMAX (int)sizeof(utmpbuf.ut_name) /* size of utmp name field */
64 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
67 /* maximum sizes used for printing */
68 /* probably we want a two-pass version that computes the right length */
69 int hmax = MIN(HMAX, 16);
70 int lmax = MIN(LMAX, 8);
71 int nmax = MIN(NMAX, 16);
74 char *name; /* argument */
79 int type; /* type of arg */
80 struct arg *next; /* linked list pointer */
82 ARG *arglist; /* head of linked list */
84 typedef struct ttytab {
85 long logout; /* log out time */
86 char tty[LMAX + 1]; /* terminal name */
87 struct ttytab *next; /* linked list pointer */
89 TTY *ttylist; /* head of linked list */
91 static long currentout, /* current logout value */
92 maxrec; /* records to display */
93 static char *file = _PATH_WTMP; /* wtmp file */
95 static int doyear = 0; /* output year in dates */
96 static int dolong = 0; /* print also ip-addr */
98 static void wtmp(void);
99 static void addarg(int, char *);
100 static void hostconv(char *);
101 static void onintr(int);
102 static int want(struct utmp *, int);
104 static char *ttyconv(char *);
107 main(int argc, char **argv) {
112 setlocale(LC_ALL, "");
113 bindtextdomain(PACKAGE, LOCALEDIR);
116 while ((ch = getopt(argc, argv, "0123456789yli:f:h:t:")) != -1)
118 case '0': case '1': case '2': case '3': case '4':
119 case '5': case '6': case '7': case '8': case '9':
121 * kludge: last was originally designed to take
122 * a number after a dash.
125 maxrec = atol(argv[optind - 1] + 1);
132 addarg(HOST_TYPE, optarg);
135 addarg(TTY_TYPE, ttyconv(optarg));
144 addarg(INET_TYPE, optarg);
148 fputs(_("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"), stderr);
151 for (argv += optind; *argv; ++argv) {
152 #define COMPATIBILITY
154 /* code to allow "last p5" to work */
155 addarg(TTY_TYPE, ttyconv(*argv));
157 addarg(USER_TYPE, *argv);
164 * print_partial_line --
165 * print the first part of each output line according to specified format
168 print_partial_line(struct utmp *bp) {
171 ct = ctime(&bp->ut_time);
172 printf("%-*.*s %-*.*s ", nmax, nmax, bp->ut_name,
173 lmax, lmax, bp->ut_line);
178 foo.s_addr = bp->ut_addr;
179 printf("%-*.*s ", hmax, hmax, inet_ntoa(foo));
181 printf("%-*.*s ", hmax, hmax, "");
184 printf("%-*.*s ", hmax, hmax, bp->ut_host);
188 printf("%10.10s %4.4s %5.5s ", ct, ct + 20, ct + 11);
190 printf("%10.10s %5.5s ", ct, ct + 11);
196 * read through the wtmp file
200 register struct utmp *bp; /* current structure */
201 register TTY *T; /* tty list entry */
202 long delta; /* time difference */
214 (void)time(&utmpbuf.ut_time);
215 (void)signal(SIGINT, onintr);
216 (void)signal(SIGQUIT, onintr);
218 if ((fd = open(file,O_RDONLY)) < 0)
221 utl_len = st.st_size;
222 utl = mmap(NULL, utl_len, PROT_READ|PROT_WRITE,
223 MAP_PRIVATE|MAP_FILE, fd, 0);
226 listnr = utl_len/sizeof(struct utmp);
229 ct = ctime(&utl[0].ut_time);
231 for(i = listnr - 1; i >= 0; i--) {
234 * if the terminal line is '~', the machine stopped.
235 * see utmp(5) for more info.
237 if (!strncmp(bp->ut_line, "~", LMAX)) {
239 * utmp(5) also mentions that the user
240 * name should be 'shutdown' or 'reboot'.
241 * Not checking the name causes e.g. runlevel
242 * changes to be displayed as 'crash'. -thaele
244 if (!strncmp(bp->ut_user, "reboot", NMAX) ||
245 !strncmp(bp->ut_user, "shutdown", NMAX)) {
246 /* everybody just logged out */
247 for (T = ttylist; T; T = T->next)
248 T->logout = -bp->ut_time;
251 currentout = -bp->ut_time;
252 crmsg = (strncmp(bp->ut_name, "shutdown", NMAX)
253 ? "crash" : "down ");
255 (void)strcpy(bp->ut_name, "reboot");
257 ct = ctime(&bp->ut_time);
258 if(bp->ut_type != LOGIN_PROCESS) {
259 print_partial_line(bp);
262 if (maxrec && !--maxrec)
267 /* find associated tty */
268 for (T = ttylist;; T = T->next) {
271 T = addtty(bp->ut_line);
274 if (!strncmp(T->tty, bp->ut_line, LMAX))
277 if (bp->ut_name[0] && bp->ut_type != LOGIN_PROCESS
278 && bp->ut_type != DEAD_PROCESS
281 print_partial_line(bp);
284 puts(_(" still logged in"));
287 T->logout = -T->logout;
288 printf("- %s", crmsg);
291 printf("- %5.5s", ctime(&T->logout)+11);
292 delta = T->logout - bp->ut_time;
294 printf(" (%5.5s)\n", asctime(gmtime(&delta))+11);
296 printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
298 if (maxrec != -1 && !--maxrec)
301 T->logout = bp->ut_time;
302 utmpbuf.ut_time = bp->ut_time;
306 if(ct) printf(_("\nwtmp begins %s"), ct); /* ct already ends in \n */
311 * see if want this entry
314 want(struct utmp *bp, int check) {
319 * when uucp and ftp log in over a network, the entry in
320 * the utmp file is the name plus their process id. See
321 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
323 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
324 bp->ut_line[3] = '\0';
325 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
326 bp->ut_line[4] = '\0';
331 for (step = arglist; step; step = step->next)
334 if (!strncmp(step->name, bp->ut_host, HMAX))
338 if (!strncmp(step->name, bp->ut_line, LMAX))
342 if (!strncmp(step->name, bp->ut_name, NMAX))
346 if (bp->ut_addr == inet_addr(step->name))
355 * add an entry to a linked list of arguments
358 addarg(int type, char *arg) {
361 if (!(cur = (ARG *)malloc((unsigned int)sizeof(ARG)))) {
362 fputs(_("last: malloc failure.\n"), stderr);
373 * add an entry to a linked list of ttys
376 addtty(char *ttyname) {
379 if (!(cur = (TTY *)malloc((unsigned int)sizeof(TTY)))) {
380 fputs(_("last: malloc failure.\n"), stderr);
384 cur->logout = currentout;
385 memcpy(cur->tty, ttyname, LMAX);
386 return(ttylist = cur);
391 * convert the hostname to search pattern; if the supplied host name
392 * has a domain attached that is the same as the current domain, rip
393 * off the domain suffix since that's what login(1) does.
396 hostconv(char *arg) {
397 static int first = 1;
398 static char *hostdot,
399 name[MAXHOSTNAMELEN];
402 if (!(argdot = strchr(arg, '.')))
406 if (gethostname(name, sizeof(name))) {
407 perror(_("last: gethostname"));
410 hostdot = strchr(name, '.');
412 if (hostdot && !strcmp(hostdot, argdot))
418 * convert tty to correct name.
425 * kludge -- we assume that all tty's end with
426 * a two character suffix.
428 if (strlen(arg) == 2) {
429 /* either 6 for "ttyxx" or 8 for "console" */
430 if (!(mval = malloc((unsigned int)8))) {
431 fputs(_("last: malloc failure.\n"), stderr);
434 if (!strcmp(arg, "co"))
435 (void)strcpy(mval, "console");
437 (void)strcpy(mval, "tty");
438 (void)strcpy(mval + 3, arg);
442 if (!strncmp(arg, "/dev/", sizeof("/dev/") - 1))
449 * on interrupt, we inform the user how far we've gotten
455 ct = ctime(&utmpbuf.ut_time);
456 printf(_("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11);
459 (void)fflush(stdout); /* fix required for rsh */