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