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 const 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 #if 0 42 static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$Id: rwho.c,v 1.11 1997/08/08 12:20:24 charnier Exp $"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/file.h> 50 #include <protocols/rwhod.h> 51 #include <dirent.h> 52 #include <err.h> 53 #include <locale.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <time.h> 58 #include <unistd.h> 59 #include <utmp.h> 60 61 DIR *dirp; 62 63 struct whod wd; 64 int utmpcmp(); 65 #define NUSERS 1000 66 struct myutmp { 67 char myhost[sizeof(wd.wd_hostname)]; 68 int myidle; 69 struct outmp myutmp; 70 } myutmp[NUSERS]; 71 int nusers; 72 73 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we)) 74 /* 75 * this macro should be shared with ruptime. 76 */ 77 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60) 78 79 time_t now; 80 int aflg; 81 82 static void usage __P((void)); 83 int utmpcmp __P((const void *, const void *)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char **argv; 89 { 90 int ch; 91 struct dirent *dp; 92 int cc, width; 93 register struct whod *w = &wd; 94 register struct whoent *we; 95 register struct myutmp *mp; 96 int f, n, i; 97 98 (void) setlocale(LC_TIME, ""); 99 100 while ((ch = getopt(argc, argv, "a")) != -1) 101 switch((char)ch) { 102 case 'a': 103 aflg = 1; 104 break; 105 case '?': 106 default: 107 usage(); 108 } 109 argc -= optind; 110 argv += optind; 111 112 if (argc != 0) 113 usage(); 114 115 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) 116 err(1, "%s", _PATH_RWHODIR); 117 mp = myutmp; 118 (void)time(&now); 119 while ((dp = readdir(dirp))) { 120 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5)) 121 continue; 122 f = open(dp->d_name, O_RDONLY); 123 if (f < 0) 124 continue; 125 cc = read(f, (char *)&wd, sizeof (struct whod)); 126 if (cc < WHDRSIZE) { 127 (void) close(f); 128 continue; 129 } 130 if (down(w,now)) { 131 (void) close(f); 132 continue; 133 } 134 cc -= WHDRSIZE; 135 we = w->wd_we; 136 for (n = cc / sizeof (struct whoent); n > 0; n--) { 137 if (aflg == 0 && we->we_idle >= 60*60) { 138 we++; 139 continue; 140 } 141 if (nusers >= NUSERS) 142 errx(1, "too many users"); 143 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle; 144 (void) strcpy(mp->myhost, w->wd_hostname); 145 nusers++; we++; mp++; 146 } 147 (void) close(f); 148 } 149 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp); 150 mp = myutmp; 151 width = 0; 152 for (i = 0; i < nusers; i++) { 153 /* append one for the blank and use 8 for the out_line */ 154 int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line); 155 if (j > width) 156 width = j; 157 mp++; 158 } 159 mp = myutmp; 160 for (i = 0; i < nusers; i++) { 161 char buf[BUFSIZ], cbuf[80]; 162 strftime(cbuf, sizeof(cbuf), "%c", localtime((time_t *)&mp->myutmp.out_time)); 163 (void)sprintf(buf, "%s:%-.*s", mp->myhost, 164 sizeof(mp->myutmp.out_line), mp->myutmp.out_line); 165 printf("%-*.*s %-*s %.12s", 166 UT_NAMESIZE, sizeof(mp->myutmp.out_name), 167 mp->myutmp.out_name, 168 width, 169 buf, 170 cbuf + 4); 171 mp->myidle /= 60; 172 if (mp->myidle) { 173 if (aflg) { 174 if (mp->myidle >= 100*60) 175 mp->myidle = 100*60 - 1; 176 if (mp->myidle >= 60) 177 printf(" %2d", mp->myidle / 60); 178 else 179 printf(" "); 180 } else 181 printf(" "); 182 printf(":%02d", mp->myidle % 60); 183 } 184 printf("\n"); 185 mp++; 186 } 187 exit(0); 188 } 189 190 191 static void 192 usage() 193 { 194 fprintf(stderr, "usage: rwho [-a]\n"); 195 exit(1); 196 } 197 198 #define MYUTMP(a) ((struct myutmp *)(a)) 199 200 int 201 utmpcmp(u1, u2) 202 const void *u1, *u2; 203 { 204 int rc; 205 206 rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name, 207 sizeof(MYUTMP(u2)->myutmp.out_name)); 208 if (rc) 209 return (rc); 210 rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost); 211 if (rc) 212 return (rc); 213 return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line, 214 sizeof(MYUTMP(u2)->myutmp.out_line))); 215 } 216