| 1 | /* |
| 2 | * Copyright (c) 1987 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms are permitted |
| 6 | * provided that this notice is preserved and that due credit is given |
| 7 | * to the University of California at Berkeley. The name of the University |
| 8 | * may not be used to endorse or promote products derived from this |
| 9 | * software without specific written prior permission. This software |
| 10 | * is provided ``as is'' without express or implied warranty. |
| 11 | */ |
| 12 | |
| 13 | #ifndef lint |
| 14 | char copyright[] = |
| 15 | "@(#) Copyright (c) 1987 Regents of the University of California.\n\ |
| 16 | All rights reserved.\n"; |
| 17 | #endif /* not lint */ |
| 18 | |
| 19 | #ifndef lint |
| 20 | static char sccsid[] = "@(#)last.c 5.9 (Berkeley) %G%"; |
| 21 | #endif /* not lint */ |
| 22 | |
| 23 | /* |
| 24 | * last |
| 25 | */ |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/file.h> |
| 29 | #include <signal.h> |
| 30 | #include <time.h> |
| 31 | #include <utmp.h> |
| 32 | #include <stdio.h> |
| 33 | |
| 34 | #define SECDAY (24*60*60) /* seconds in a day */ |
| 35 | #define NO 0 /* false/no */ |
| 36 | #define YES 1 /* true/yes */ |
| 37 | |
| 38 | static struct utmp buf[1024]; /* utmp read buffer */ |
| 39 | |
| 40 | #define HMAX sizeof(buf[0].ut_host) /* size of utmp host field */ |
| 41 | #define LMAX sizeof(buf[0].ut_line) /* size of utmp tty field */ |
| 42 | #define NMAX sizeof(buf[0].ut_name) /* size of utmp name field */ |
| 43 | |
| 44 | typedef struct arg { |
| 45 | char *name; /* argument */ |
| 46 | #define HOST_TYPE -2 |
| 47 | #define TTY_TYPE -3 |
| 48 | #define USER_TYPE -4 |
| 49 | int type; /* type of arg */ |
| 50 | struct arg *next; /* linked list pointer */ |
| 51 | } ARG; |
| 52 | ARG *arglist; /* head of linked list */ |
| 53 | |
| 54 | typedef struct ttytab { |
| 55 | long logout; /* log out time */ |
| 56 | char tty[LMAX + 1]; /* terminal name */ |
| 57 | struct ttytab *next; /* linked list pointer */ |
| 58 | } TTY; |
| 59 | TTY *ttylist; /* head of linked list */ |
| 60 | |
| 61 | static long currentout, /* current logout value */ |
| 62 | maxrec; /* records to display */ |
| 63 | static char *file = "/usr/adm/wtmp"; /* wtmp file */ |
| 64 | |
| 65 | main(argc, argv) |
| 66 | int argc; |
| 67 | char **argv; |
| 68 | { |
| 69 | extern int optind; |
| 70 | extern char *optarg; |
| 71 | int ch; |
| 72 | long atol(); |
| 73 | char *ttyconv(); |
| 74 | |
| 75 | while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF) |
| 76 | switch((char)ch) { |
| 77 | case '0': case '1': case '2': case '3': case '4': |
| 78 | case '5': case '6': case '7': case '8': case '9': |
| 79 | /* |
| 80 | * kludge: last was originally designed to take |
| 81 | * a number after a dash. |
| 82 | */ |
| 83 | if (!maxrec) |
| 84 | maxrec = atol(argv[optind - 1] + 1); |
| 85 | break; |
| 86 | case 'f': |
| 87 | file = optarg; |
| 88 | break; |
| 89 | case 'h': |
| 90 | hostconv(optarg); |
| 91 | addarg(HOST_TYPE, optarg); |
| 92 | break; |
| 93 | case 't': |
| 94 | addarg(TTY_TYPE, ttyconv(optarg)); |
| 95 | break; |
| 96 | case '?': |
| 97 | default: |
| 98 | fputs("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n", stderr); |
| 99 | exit(1); |
| 100 | } |
| 101 | for (argv += optind; *argv; ++argv) { |
| 102 | #define COMPATIBILITY |
| 103 | #ifdef COMPATIBILITY |
| 104 | /* code to allow "last p5" to work */ |
| 105 | addarg(TTY_TYPE, ttyconv(*argv)); |
| 106 | #endif |
| 107 | addarg(USER_TYPE, *argv); |
| 108 | } |
| 109 | wtmp(); |
| 110 | exit(0); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * wtmp -- |
| 115 | * read through the wtmp file |
| 116 | */ |
| 117 | static |
| 118 | wtmp() |
| 119 | { |
| 120 | register struct utmp *bp; /* current structure */ |
| 121 | register TTY *T; /* tty list entry */ |
| 122 | struct stat stb; /* stat of file for size */ |
| 123 | long bl, delta, /* time difference */ |
| 124 | lseek(), time(); |
| 125 | int bytes, wfd, |
| 126 | onintr(); |
| 127 | char *ct, *crmsg, |
| 128 | *asctime(), *ctime(), *strcpy(); |
| 129 | TTY *addtty(); |
| 130 | |
| 131 | if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) { |
| 132 | perror(file); |
| 133 | exit(1); |
| 134 | } |
| 135 | bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); |
| 136 | |
| 137 | (void)time(&buf[0].ut_time); |
| 138 | (void)signal(SIGINT, onintr); |
| 139 | (void)signal(SIGQUIT, onintr); |
| 140 | |
| 141 | while (--bl >= 0) { |
| 142 | if (lseek(wfd, (long)(bl * sizeof(buf)), L_SET) == -1 || |
| 143 | (bytes = read(wfd, (char *)buf, sizeof(buf))) == -1) { |
| 144 | fprintf(stderr, "last: %s: ", file); |
| 145 | perror((char *)NULL); |
| 146 | exit(1); |
| 147 | } |
| 148 | for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) { |
| 149 | /* |
| 150 | * if the terminal line is '~', the machine stopped. |
| 151 | * see utmp(5) for more info. |
| 152 | */ |
| 153 | if (!strncmp(bp->ut_line, "~", LMAX)) { |
| 154 | /* everybody just logged out */ |
| 155 | for (T = ttylist; T; T = T->next) |
| 156 | T->logout = -bp->ut_time; |
| 157 | currentout = -bp->ut_time; |
| 158 | crmsg = strncmp(bp->ut_name, "shutdown", NMAX) ? "crash" : "down "; |
| 159 | if (!bp->ut_name[0]) |
| 160 | (void)strcpy(bp->ut_name, "reboot"); |
| 161 | if (want(bp, NO)) { |
| 162 | ct = ctime(&bp->ut_time); |
| 163 | printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", NMAX, NMAX, bp->ut_name, LMAX, LMAX, bp->ut_line, HMAX, HMAX, bp->ut_host, ct, ct + 11); |
| 164 | if (maxrec && !--maxrec) |
| 165 | return; |
| 166 | } |
| 167 | continue; |
| 168 | } |
| 169 | /* find associated tty */ |
| 170 | for (T = ttylist;; T = T->next) { |
| 171 | if (!T) { |
| 172 | /* add new one */ |
| 173 | T = addtty(bp->ut_line); |
| 174 | break; |
| 175 | } |
| 176 | if (!strncmp(T->tty, bp->ut_line, LMAX)) |
| 177 | break; |
| 178 | } |
| 179 | if (bp->ut_name[0] && want(bp, YES)) { |
| 180 | ct = ctime(&bp->ut_time); |
| 181 | printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", NMAX, NMAX, bp->ut_name, LMAX, LMAX, bp->ut_line, HMAX, HMAX, bp->ut_host, ct, ct + 11); |
| 182 | if (!T->logout) |
| 183 | puts(" still logged in"); |
| 184 | else { |
| 185 | if (T->logout < 0) { |
| 186 | T->logout = -T->logout; |
| 187 | printf("- %s", crmsg); |
| 188 | } |
| 189 | else |
| 190 | printf("- %5.5s", ctime(&T->logout)+11); |
| 191 | delta = T->logout - bp->ut_time; |
| 192 | if (delta < SECDAY) |
| 193 | printf(" (%5.5s)\n", asctime(gmtime(&delta))+11); |
| 194 | else |
| 195 | printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11); |
| 196 | } |
| 197 | if (maxrec != -1 && !--maxrec) |
| 198 | return; |
| 199 | } |
| 200 | T->logout = bp->ut_time; |
| 201 | } |
| 202 | } |
| 203 | ct = ctime(&buf[0].ut_time); |
| 204 | printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * want -- |
| 209 | * see if want this entry |
| 210 | */ |
| 211 | static |
| 212 | want(bp, check) |
| 213 | register struct utmp *bp; |
| 214 | int check; |
| 215 | { |
| 216 | register ARG *step; |
| 217 | |
| 218 | if (check) |
| 219 | /* |
| 220 | * when uucp and ftp log in over a network, the entry in |
| 221 | * the utmp file is the name plus their process id. See |
| 222 | * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. |
| 223 | */ |
| 224 | if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) |
| 225 | bp->ut_line[3] = '\0'; |
| 226 | else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) |
| 227 | bp->ut_line[4] = '\0'; |
| 228 | if (!arglist) |
| 229 | return(YES); |
| 230 | |
| 231 | for (step = arglist; step; step = step->next) |
| 232 | switch(step->type) { |
| 233 | case HOST_TYPE: |
| 234 | if (!strncasecmp(step->name, bp->ut_host, HMAX)) |
| 235 | return(YES); |
| 236 | break; |
| 237 | case TTY_TYPE: |
| 238 | if (!strncmp(step->name, bp->ut_line, LMAX)) |
| 239 | return(YES); |
| 240 | break; |
| 241 | case USER_TYPE: |
| 242 | if (!strncmp(step->name, bp->ut_name, NMAX)) |
| 243 | return(YES); |
| 244 | break; |
| 245 | } |
| 246 | return(NO); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * addarg -- |
| 251 | * add an entry to a linked list of arguments |
| 252 | */ |
| 253 | static |
| 254 | addarg(type, arg) |
| 255 | int type; |
| 256 | char *arg; |
| 257 | { |
| 258 | register ARG *cur; |
| 259 | char *malloc(); |
| 260 | |
| 261 | if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) { |
| 262 | fputs("last: malloc failure.\n", stderr); |
| 263 | exit(1); |
| 264 | } |
| 265 | cur->next = arglist; |
| 266 | cur->type = type; |
| 267 | cur->name = arg; |
| 268 | arglist = cur; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * addtty -- |
| 273 | * add an entry to a linked list of ttys |
| 274 | */ |
| 275 | static TTY * |
| 276 | addtty(ttyname) |
| 277 | char *ttyname; |
| 278 | { |
| 279 | register TTY *cur; |
| 280 | char *malloc(); |
| 281 | |
| 282 | if (!(cur = (TTY *)malloc((u_int)sizeof(TTY)))) { |
| 283 | fputs("last: malloc failure.\n", stderr); |
| 284 | exit(1); |
| 285 | } |
| 286 | cur->next = ttylist; |
| 287 | cur->logout = currentout; |
| 288 | bcopy(ttyname, cur->tty, LMAX); |
| 289 | return(ttylist = cur); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * hostconv -- |
| 294 | * convert the hostname to search pattern; if the supplied host name |
| 295 | * has a domain attached that is the same as the current domain, rip |
| 296 | * off the domain suffix since that's what login(1) does. |
| 297 | */ |
| 298 | static |
| 299 | hostconv(arg) |
| 300 | char *arg; |
| 301 | { |
| 302 | static int first = 1; |
| 303 | static char *hostdot, |
| 304 | name[MAXHOSTNAMELEN]; |
| 305 | char *argdot, *index(); |
| 306 | |
| 307 | if (!(argdot = index(arg, '.'))) |
| 308 | return; |
| 309 | if (first) { |
| 310 | first = 0; |
| 311 | if (gethostname(name, sizeof(name))) { |
| 312 | perror("last: gethostname"); |
| 313 | exit(1); |
| 314 | } |
| 315 | hostdot = index(name, '.'); |
| 316 | } |
| 317 | if (hostdot && !strcasecmp(hostdot, argdot)) |
| 318 | *argdot = '\0'; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * ttyconv -- |
| 323 | * convert tty to correct name. |
| 324 | */ |
| 325 | static char * |
| 326 | ttyconv(arg) |
| 327 | char *arg; |
| 328 | { |
| 329 | char *mval, |
| 330 | *malloc(), *strcpy(); |
| 331 | |
| 332 | /* |
| 333 | * kludge -- we assume that all tty's end with |
| 334 | * a two character suffix. |
| 335 | */ |
| 336 | if (strlen(arg) == 2) { |
| 337 | /* either 6 for "ttyxx" or 8 for "console" */ |
| 338 | if (!(mval = malloc((u_int)8))) { |
| 339 | fputs("last: malloc failure.\n", stderr); |
| 340 | exit(1); |
| 341 | } |
| 342 | if (!strcmp(optarg, "co")) |
| 343 | (void)strcpy(mval, "console"); |
| 344 | else { |
| 345 | (void)strcpy(mval, "tty"); |
| 346 | (void)strcpy(mval + 3, arg); |
| 347 | } |
| 348 | return(mval); |
| 349 | } |
| 350 | if (!strncmp(arg, "/dev/", sizeof("/dev/") - 1)) |
| 351 | return(arg + 5); |
| 352 | return(arg); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * onintr -- |
| 357 | * on interrupt, we inform the user how far we've gotten |
| 358 | */ |
| 359 | static |
| 360 | onintr(signo) |
| 361 | int signo; |
| 362 | { |
| 363 | char *ct, *ctime(); |
| 364 | |
| 365 | ct = ctime(&buf[0].ut_time); |
| 366 | printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11); |
| 367 | if (signo == SIGINT) |
| 368 | exit(1); |
| 369 | (void)fflush(stdout); /* fix required for rsh */ |
| 370 | } |