Annotation of src/usr.bin/rwho/rwho.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: rwho.c,v 1.12 2001/02/17 17:35:14 pjanzen Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1983 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.13 ! millert 37: static const char copyright[] =
1.1 deraadt 38: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
39: All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
1.13 ! millert 43: #if 0
! 44: static const char sccsid[] = "from: @(#)rwho.c 5.5 (Berkeley) 6/1/90";
! 45: #else
! 46: static const char rcsid[] = "$OpenBSD: rwho.c,v 1.12 2001/02/17 17:35:14 pjanzen Exp $";
! 47: #endif
1.1 deraadt 48: #endif /* not lint */
49:
1.12 pjanzen 50: #include <sys/types.h>
1.1 deraadt 51: #include <sys/param.h>
52: #include <sys/file.h>
53: #include <dirent.h>
54: #include <protocols/rwhod.h>
55: #include <stdio.h>
56: #include <string.h>
1.3 deraadt 57: #include <unistd.h>
58: #include <stdlib.h>
1.12 pjanzen 59: #include <time.h>
1.11 deraadt 60: #include <utmp.h>
1.8 deraadt 61: #include <vis.h>
1.9 deraadt 62: #include <err.h>
1.1 deraadt 63:
64: DIR *dirp;
65:
66: struct whod wd;
67: #define NUSERS 1000
68: struct myutmp {
69: char myhost[MAXHOSTNAMELEN];
70: int myidle;
1.13 ! millert 71: struct {
! 72: char out_line[9]; /* tty name + NUL */
! 73: char out_name[9]; /* user id + NUL */
! 74: int32_t out_time; /* time on */
! 75: } myutmp;
1.1 deraadt 76: } myutmp[NUSERS];
77: int nusers;
78:
1.13 ! millert 79: int utmpcmp(const void *, const void *);
! 80: __dead void usage(void);
1.3 deraadt 81:
1.13 ! millert 82: #define WHDRSIZE (sizeof(wd) - sizeof(wd.wd_we))
1.1 deraadt 83: /*
84: * this macro should be shared with ruptime.
85: */
86: #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
87:
88: time_t now;
89: int aflg;
90:
1.3 deraadt 91: int
1.13 ! millert 92: main(int argc, char **argv)
1.1 deraadt 93: {
94: int ch;
95: struct dirent *dp;
96: int cc, width;
1.13 ! millert 97: struct whod *w = &wd;
! 98: struct whoent *we;
! 99: struct myutmp *mp;
1.1 deraadt 100: int f, n, i;
1.6 deraadt 101: int nhosts = 0;
1.1 deraadt 102:
1.5 millert 103: while ((ch = getopt(argc, argv, "a")) != -1)
1.1 deraadt 104: switch((char)ch) {
105: case 'a':
106: aflg = 1;
107: break;
108: default:
1.10 deraadt 109: usage();
1.1 deraadt 110: }
1.10 deraadt 111: argc -= optind;
112: argv += optind;
113: if (argc != 0)
114: usage();
115:
1.13 ! millert 116: if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
! 117: err(1, _PATH_RWHODIR);
1.1 deraadt 118: mp = myutmp;
119: (void)time(&now);
1.3 deraadt 120: while ((dp = readdir(dirp))) {
1.1 deraadt 121: if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
122: continue;
123: f = open(dp->d_name, O_RDONLY);
124: if (f < 0)
125: continue;
1.13 ! millert 126: cc = read(f, &wd, sizeof(struct whod));
1.1 deraadt 127: if (cc < WHDRSIZE) {
1.13 ! millert 128: (void)close(f);
1.1 deraadt 129: continue;
130: }
1.6 deraadt 131: nhosts++;
1.1 deraadt 132: if (down(w,now)) {
1.13 ! millert 133: (void)close(f);
1.1 deraadt 134: continue;
135: }
136: cc -= WHDRSIZE;
137: we = w->wd_we;
1.13 ! millert 138: for (n = cc / sizeof(struct whoent); n > 0; n--) {
1.1 deraadt 139: if (aflg == 0 && we->we_idle >= 60*60) {
140: we++;
141: continue;
142: }
1.12 pjanzen 143: if (nusers >= NUSERS)
144: errx(1, "too many users");
1.13 ! millert 145: memcpy(mp->myhost, w->wd_hostname,
1.3 deraadt 146: sizeof(mp->myhost)-1);
147: mp->myhost[sizeof(mp->myhost)-1] = '\0';
1.13 ! millert 148: mp->myidle = we->we_idle;
! 149: /*
! 150: * Copy we->we_utmp by hand since the name and line
! 151: * variables in myutmp have room for NUL (unlike outmp).
! 152: */
! 153: memcpy(mp->myutmp.out_line, we->we_utmp.out_line,
! 154: sizeof(mp->myutmp.out_line)-1);
! 155: mp->myutmp.out_line[sizeof(mp->myutmp.out_line)-1] = 0;
! 156: memcpy(mp->myutmp.out_name, we->we_utmp.out_name,
! 157: sizeof(mp->myutmp.out_name)-1);
! 158: mp->myutmp.out_name[sizeof(mp->myutmp.out_name)-1] = 0;
! 159: mp->myutmp.out_time = we->we_utmp.out_time;
1.1 deraadt 160: nusers++; we++; mp++;
161: }
1.13 ! millert 162: (void)close(f);
1.1 deraadt 163: }
1.6 deraadt 164: if (nhosts == 0)
165: errx(0, "no hosts in %s.", _PATH_RWHODIR);
1.13 ! millert 166: qsort(myutmp, nusers, sizeof(struct myutmp), utmpcmp);
1.1 deraadt 167: mp = myutmp;
168: width = 0;
169: for (i = 0; i < nusers; i++) {
170: int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
171: if (j > width)
172: width = j;
173: mp++;
174: }
175: mp = myutmp;
176: for (i = 0; i < nusers; i++) {
1.13 ! millert 177: char buf[BUFSIZ], vis_user[4 * sizeof(mp->myutmp.out_name)];
! 178:
! 179: (void)snprintf(buf, sizeof(buf), "%s:%s", mp->myhost,
1.7 deraadt 180: mp->myutmp.out_line);
1.8 deraadt 181: strvis(vis_user, mp->myutmp.out_name, VIS_CSTYLE);
1.11 deraadt 182: printf("%-*.*s %-*s %.12s",
1.13 ! millert 183: UT_NAMESIZE, UT_NAMESIZE, vis_user, width, buf,
1.1 deraadt 184: ctime((time_t *)&mp->myutmp.out_time)+4);
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:
1.3 deraadt 204: int
1.13 ! millert 205: utmpcmp(const void *v1, const void *v2)
1.1 deraadt 206: {
207: int rc;
1.13 ! millert 208: const struct myutmp *u1 = (struct myutmp *)v1;
! 209: const struct myutmp *u2 = (struct myutmp *)v2;
1.1 deraadt 210:
211: rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
212: if (rc)
213: return (rc);
214: rc = strncmp(u1->myhost, u2->myhost, 8);
215: if (rc)
216: return (rc);
217: return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
1.13 ! millert 218: }
! 219:
! 220: void
! 221: usage(void)
! 222: {
! 223: extern char *__progname;
! 224:
! 225: fprintf(stderr, "usage: %s [-a]\n", __progname);
! 226: exit(1);
1.1 deraadt 227: }