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