1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/file.h> 46 #include <protocols/rwhod.h> 47 #include <dirent.h> 48 #include <stdio.h> 49 #include <time.h> 50 #include <string.h> 51 #include <locale.h> 52 53 DIR *dirp; 54 55 struct whod wd; 56 int utmpcmp(); 57 #define NUSERS 1000 58 struct myutmp { 59 char myhost[MAXHOSTNAMELEN]; 60 int myidle; 61 struct outmp myutmp; 62 } myutmp[NUSERS]; 63 int nusers; 64 65 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we)) 66 /* 67 * this macro should be shared with ruptime. 68 */ 69 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60) 70 71 time_t now; 72 int aflg; 73 74 main(argc, argv) 75 int argc; 76 char **argv; 77 { 78 extern char *optarg; 79 extern int optind; 80 int ch; 81 struct dirent *dp; 82 int cc, width; 83 register struct whod *w = &wd; 84 register struct whoent *we; 85 register struct myutmp *mp; 86 int f, n, i; 87 time_t time(); 88 89 (void) setlocale(LC_TIME, ""); 90 91 while ((ch = getopt(argc, argv, "a")) != EOF) 92 switch((char)ch) { 93 case 'a': 94 aflg = 1; 95 break; 96 case '?': 97 default: 98 fprintf(stderr, "usage: rwho [-a]\n"); 99 exit(1); 100 } 101 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) { 102 perror(_PATH_RWHODIR); 103 exit(1); 104 } 105 mp = myutmp; 106 (void)time(&now); 107 while (dp = readdir(dirp)) { 108 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5)) 109 continue; 110 f = open(dp->d_name, O_RDONLY); 111 if (f < 0) 112 continue; 113 cc = read(f, (char *)&wd, sizeof (struct whod)); 114 if (cc < WHDRSIZE) { 115 (void) close(f); 116 continue; 117 } 118 if (down(w,now)) { 119 (void) close(f); 120 continue; 121 } 122 cc -= WHDRSIZE; 123 we = w->wd_we; 124 for (n = cc / sizeof (struct whoent); n > 0; n--) { 125 if (aflg == 0 && we->we_idle >= 60*60) { 126 we++; 127 continue; 128 } 129 if (nusers >= NUSERS) { 130 printf("too many users\n"); 131 exit(1); 132 } 133 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle; 134 (void) strcpy(mp->myhost, w->wd_hostname); 135 nusers++; we++; mp++; 136 } 137 (void) close(f); 138 } 139 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp); 140 mp = myutmp; 141 width = 0; 142 for (i = 0; i < nusers; i++) { 143 /* append one for the blank and use 8 for the out_line */ 144 int j = strlen(mp->myhost) + 1 + 8; 145 if (j > width) 146 width = j; 147 mp++; 148 } 149 mp = myutmp; 150 for (i = 0; i < nusers; i++) { 151 char buf[BUFSIZ], cbuf[80]; 152 strftime(cbuf, sizeof(cbuf), "%c", localtime((time_t *)&mp->myutmp.out_time)); 153 (void)sprintf(buf, "%s:%-.8s", mp->myhost, mp->myutmp.out_line); 154 printf("%-8.8s %-*s %.12s", 155 mp->myutmp.out_name, 156 width, 157 buf, 158 cbuf + 4); 159 mp->myidle /= 60; 160 if (mp->myidle) { 161 if (aflg) { 162 if (mp->myidle >= 100*60) 163 mp->myidle = 100*60 - 1; 164 if (mp->myidle >= 60) 165 printf(" %2d", mp->myidle / 60); 166 else 167 printf(" "); 168 } else 169 printf(" "); 170 printf(":%02d", mp->myidle % 60); 171 } 172 printf("\n"); 173 mp++; 174 } 175 exit(0); 176 } 177 178 utmpcmp(u1, u2) 179 struct myutmp *u1, *u2; 180 { 181 int rc; 182 183 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8); 184 if (rc) 185 return (rc); 186 rc = strncmp(u1->myhost, u2->myhost, 8); 187 if (rc) 188 return (rc); 189 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8)); 190 } 191