1: #ifndef lint 2: static char *sccsid = "@(#)last.c 4.3 (Berkeley) 2/28/81"; 3: #endif 4: /* 5: * last 6: */ 7: #include <sys/types.h> 8: #include <stdio.h> 9: #include <signal.h> 10: #include <stat.h> 11: #include <utmp.h> 12: 13: #define NMAX sizeof(buf[0].ut_name) 14: #define LMAX sizeof(buf[0].ut_line) 15: #define SECDAY (24*60*60) 16: 17: #define lineq(a,b) (!strncmp(a,b,LMAX)) 18: #define nameq(a,b) (!strncmp(a,b,NMAX)) 19: 20: #define MAXTTYS 256 21: 22: char **argv; 23: int argc; 24: 25: struct utmp buf[128]; 26: char ttnames[MAXTTYS][LMAX+1]; 27: long logouts[MAXTTYS]; 28: 29: char *ctime(), *strspl(); 30: int onintr(); 31: 32: main(ac, av) 33: char **av; 34: { 35: register int i; 36: long bl; 37: int wtmp; 38: char *ct; 39: register struct utmp *bp; 40: long otime; 41: struct stat stb; 42: int print; 43: char * crmsg = (char *)0; 44: 45: time(&buf[0].ut_time); 46: ac--, av++; 47: argc = ac; 48: argv = av; 49: for (i = 0; i < argc; i++) { 50: if (strlen(argv[i])>2) 51: continue; 52: if (!strcmp(argv[i], "~")) 53: continue; 54: if (getpwnam(argv[i])) 55: continue; 56: argv[i] = strspl("tty", argv[i]); 57: } 58: wtmp = open("/usr/adm/wtmp", 0); 59: if (wtmp < 0) { 60: perror("/usr/adm/wtmp"); 61: exit(1); 62: } 63: fstat(wtmp, &stb); 64: bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf); 65: if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 66: signal(SIGINT, onintr); 67: signal(SIGQUIT, onintr); 68: } 69: for (bl--; bl >= 0; bl--) { 70: lseek(wtmp, (off_t)bl * sizeof (buf), 0); 71: bp = &buf[read(wtmp, (char *) buf, sizeof (buf)) / sizeof(buf[0]) - 1]; 72: for ( ; bp >= buf; bp--) { 73: print = want(bp); 74: if (print) { 75: ct = ctime(&bp->ut_time); 76: printf("%-*.*s %-*.*s %10.10s %5.5s ", 77: NMAX, NMAX, bp->ut_name, 78: LMAX, LMAX, bp->ut_line, ct, 11+ct); 79: } 80: for (i = 0; i < MAXTTYS; i++) { 81: if (ttnames[i][0] == 0) { 82: strncpy(ttnames[i], bp->ut_line, 83: sizeof(bp->ut_line)); 84: otime = logouts[i]; 85: logouts[i] = bp->ut_time; 86: break; 87: } 88: if (lineq(ttnames[i], bp->ut_line)) { 89: otime = logouts[i]; 90: logouts[i] = bp->ut_time; 91: break; 92: } 93: } 94: if (print) { 95: if (lineq(bp->ut_line, "~")) 96: printf("\n"); 97: else if (otime == 0) 98: printf(" still logged in\n"); 99: else { 100: long delta; 101: if (otime < 0) { 102: otime = -otime; 103: printf("- %s", crmsg); 104: } else 105: printf("- %5.5s", 106: ctime(&otime)+11); 107: delta = otime - bp->ut_time; 108: if (delta < SECDAY) 109: printf(" (%5.5s)\n", 110: asctime(gmtime(&delta))+11); 111: else 112: printf(" (%ld+%5.5s)\n", 113: delta / SECDAY, 114: asctime(gmtime(&delta))+11); 115: } 116: fflush(stdout); 117: } 118: if (lineq(bp->ut_line, "~")) { 119: for (i = 0; i < MAXTTYS; i++) 120: logouts[i] = -bp->ut_time; 121: if (nameq(bp->ut_name, "shutdown")) 122: crmsg = "down "; 123: else 124: crmsg = "crash"; 125: } 126: } 127: } 128: ct = ctime(&buf[0].ut_time); 129: printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11); 130: exit(0); 131: } 132: 133: onintr(signo) 134: int signo; 135: { 136: char *ct; 137: 138: if (signo == SIGQUIT) 139: signal(SIGQUIT, onintr); 140: ct = ctime(&buf[0].ut_time); 141: printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11); 142: if (signo == SIGINT) 143: exit(1); 144: } 145: 146: want(bp) 147: struct utmp *bp; 148: { 149: register char **av; 150: register int ac; 151: 152: if (bp->ut_line[0] == '~' && bp->ut_name[0] == '\0') 153: strcpy(bp->ut_name, "reboot"); /* bandaid */ 154: if (bp->ut_name[0] == 0) 155: return (0); 156: if (argc == 0) 157: return (1); 158: av = argv; 159: for (ac = 0; ac < argc; ac++) { 160: if (nameq(*av, bp->ut_name) || lineq(*av, bp->ut_line)) 161: return (1); 162: av++; 163: } 164: return (0); 165: } 166: 167: char * 168: strspl(left, right) 169: char *left, *right; 170: { 171: char *res = (char *)malloc(strlen(left)+strlen(right)+1); 172: 173: strcpy(res, left); 174: strcat(res, right); 175: return (res); 176: }