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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)rwho.c 8.1 (Berkeley) 6/6/93 31 * $FreeBSD: src/usr.bin/rwho/rwho.c,v 1.13.2.1 2002/03/12 19:49:09 phantom Exp $ 32 * $DragonFly: src/usr.bin/rwho/rwho.c,v 1.5 2008/10/16 01:52:33 swildner Exp $ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/file.h> 37 38 #include <protocols/rwhod.h> 39 40 #include <dirent.h> 41 #include <err.h> 42 #include <langinfo.h> 43 #include <locale.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <time.h> 48 #include <unistd.h> 49 #include <utmp.h> 50 51 struct whod wd; 52 53 #define NUSERS 1000 54 55 struct myutmp { 56 char myhost[sizeof(wd.wd_hostname)]; 57 int myidle; 58 struct outmp myutmp; 59 } myutmp[NUSERS]; 60 61 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we)) 62 /* 63 * this macro should be shared with ruptime. 64 */ 65 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60) 66 67 static void usage(void); 68 static int utmpcmp(const void *, const void *); 69 70 int 71 main(int argc, char **argv) 72 { 73 struct dirent *dp; 74 struct whod *w = &wd; 75 struct whoent *we; 76 struct myutmp *mp; 77 int f, n, i, ch; 78 int width, d_first, aflg; 79 int nusers; 80 size_t cc; 81 time_t now; 82 DIR *dirp; 83 84 aflg = nusers = 0; 85 setlocale(LC_TIME, ""); 86 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 87 88 while ((ch = getopt(argc, argv, "a")) != -1) 89 switch(ch) { 90 case 'a': 91 aflg = 1; 92 break; 93 case '?': 94 default: 95 usage(); 96 } 97 argc -= optind; 98 argv += optind; 99 100 if (argc != 0) 101 usage(); 102 103 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) 104 err(1, "%s", _PATH_RWHODIR); 105 mp = myutmp; 106 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 close(f); 116 continue; 117 } 118 if (down(w,now)) { 119 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 errx(1, "too many users"); 131 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle; 132 strcpy(mp->myhost, w->wd_hostname); 133 nusers++; we++; mp++; 134 } 135 close(f); 136 } 137 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp); 138 mp = myutmp; 139 width = 0; 140 for (i = 0; i < nusers; i++) { 141 /* append one for the blank and use 8 for the out_line */ 142 int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line); 143 if (j > width) 144 width = j; 145 mp++; 146 } 147 mp = myutmp; 148 for (i = 0; i < nusers; i++) { 149 char buf[BUFSIZ], cbuf[80]; 150 151 strftime(cbuf, sizeof(cbuf), 152 d_first ? "%e %b %R" : "%b %e %R", 153 localtime((time_t *)&mp->myutmp.out_time)); 154 sprintf(buf, "%s:%-.*s", mp->myhost, 155 (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line); 156 printf("%-*.*s %-*s %s", 157 UT_NAMESIZE, (int)sizeof(mp->myutmp.out_name), 158 mp->myutmp.out_name, 159 width, 160 buf, 161 cbuf); 162 mp->myidle /= 60; 163 if (mp->myidle) { 164 if (aflg) { 165 if (mp->myidle >= 100*60) 166 mp->myidle = 100*60 - 1; 167 if (mp->myidle >= 60) 168 printf(" %2d", mp->myidle / 60); 169 else 170 printf(" "); 171 } else 172 printf(" "); 173 printf(":%02d", mp->myidle % 60); 174 } 175 printf("\n"); 176 mp++; 177 } 178 exit(0); 179 } 180 181 182 static void 183 usage(void) 184 { 185 fprintf(stderr, "usage: rwho [-a]\n"); 186 exit(1); 187 } 188 189 #define MYUTMP(a) ((const struct myutmp *)(a)) 190 191 static int 192 utmpcmp(const void *u1, const void *u2) 193 { 194 int rc; 195 196 rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name, 197 sizeof(MYUTMP(u2)->myutmp.out_name)); 198 if (rc) 199 return (rc); 200 rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost); 201 if (rc) 202 return (rc); 203 return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line, 204 sizeof(MYUTMP(u2)->myutmp.out_line))); 205 } 206