1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1983, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 06/06/93"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/dir.h> 20 #include <sys/file.h> 21 #include <protocols/rwhod.h> 22 #include <stdio.h> 23 24 DIR *dirp; 25 26 struct whod wd; 27 int utmpcmp(); 28 #define NUSERS 1000 29 struct myutmp { 30 char myhost[MAXHOSTNAMELEN]; 31 int myidle; 32 struct outmp myutmp; 33 } myutmp[NUSERS]; 34 int nusers; 35 36 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we)) 37 /* 38 * this macro should be shared with ruptime. 39 */ 40 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60) 41 42 char *ctime(), *strcpy(); 43 time_t now; 44 int aflg; 45 46 main(argc, argv) 47 int argc; 48 char **argv; 49 { 50 extern char *optarg; 51 extern int optind; 52 int ch; 53 struct direct *dp; 54 int cc, width; 55 register struct whod *w = &wd; 56 register struct whoent *we; 57 register struct myutmp *mp; 58 int f, n, i; 59 time_t time(); 60 61 while ((ch = getopt(argc, argv, "a")) != EOF) 62 switch((char)ch) { 63 case 'a': 64 aflg = 1; 65 break; 66 case '?': 67 default: 68 fprintf(stderr, "usage: rwho [-a]\n"); 69 exit(1); 70 } 71 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) { 72 perror(_PATH_RWHODIR); 73 exit(1); 74 } 75 mp = myutmp; 76 (void)time(&now); 77 while (dp = readdir(dirp)) { 78 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5)) 79 continue; 80 f = open(dp->d_name, O_RDONLY); 81 if (f < 0) 82 continue; 83 cc = read(f, (char *)&wd, sizeof (struct whod)); 84 if (cc < WHDRSIZE) { 85 (void) close(f); 86 continue; 87 } 88 if (down(w,now)) { 89 (void) close(f); 90 continue; 91 } 92 cc -= WHDRSIZE; 93 we = w->wd_we; 94 for (n = cc / sizeof (struct whoent); n > 0; n--) { 95 if (aflg == 0 && we->we_idle >= 60*60) { 96 we++; 97 continue; 98 } 99 if (nusers >= NUSERS) { 100 printf("too many users\n"); 101 exit(1); 102 } 103 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle; 104 (void) strcpy(mp->myhost, w->wd_hostname); 105 nusers++; we++; mp++; 106 } 107 (void) close(f); 108 } 109 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp); 110 mp = myutmp; 111 width = 0; 112 for (i = 0; i < nusers; i++) { 113 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line); 114 if (j > width) 115 width = j; 116 mp++; 117 } 118 mp = myutmp; 119 for (i = 0; i < nusers; i++) { 120 char buf[BUFSIZ]; 121 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line); 122 printf("%-8.8s %-*s %.12s", 123 mp->myutmp.out_name, 124 width, 125 buf, 126 ctime((time_t *)&mp->myutmp.out_time)+4); 127 mp->myidle /= 60; 128 if (mp->myidle) { 129 if (aflg) { 130 if (mp->myidle >= 100*60) 131 mp->myidle = 100*60 - 1; 132 if (mp->myidle >= 60) 133 printf(" %2d", mp->myidle / 60); 134 else 135 printf(" "); 136 } else 137 printf(" "); 138 printf(":%02d", mp->myidle % 60); 139 } 140 printf("\n"); 141 mp++; 142 } 143 exit(0); 144 } 145 146 utmpcmp(u1, u2) 147 struct myutmp *u1, *u2; 148 { 149 int rc; 150 151 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8); 152 if (rc) 153 return (rc); 154 rc = strncmp(u1->myhost, u2->myhost, 8); 155 if (rc) 156 return (rc); 157 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8)); 158 } 159