Annotation of src/usr.bin/rwho/rwho.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: rwho.c,v 1.6 1997/03/26 18:02:37 deraadt 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
37: char copyright[] =
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
43: /*static char sccsid[] = "from: @(#)rwho.c 5.5 (Berkeley) 6/1/90";*/
1.7 ! deraadt 44: static char rcsid[] = "$OpenBSD: rwho.c,v 1.6 1997/03/26 18:02:37 deraadt Exp $";
1.1 deraadt 45: #endif /* not lint */
46:
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.1 deraadt 55:
56: DIR *dirp;
57:
58: struct whod wd;
59: #define NUSERS 1000
60: struct myutmp {
61: char myhost[MAXHOSTNAMELEN];
62: int myidle;
63: struct outmp myutmp;
64: } myutmp[NUSERS];
65: int nusers;
66:
1.3 deraadt 67: int utmpcmp __P((struct myutmp *, struct myutmp *));
68:
1.1 deraadt 69: #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
70: /*
71: * this macro should be shared with ruptime.
72: */
73: #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
74:
1.3 deraadt 75: char *ctime();
1.1 deraadt 76: time_t now;
77: int aflg;
78:
1.3 deraadt 79: int
1.1 deraadt 80: main(argc, argv)
81: int argc;
82: char **argv;
83: {
84: extern char *optarg;
85: extern int optind;
86: int ch;
87: struct dirent *dp;
88: int cc, width;
89: register struct whod *w = &wd;
90: register struct whoent *we;
91: register struct myutmp *mp;
92: int f, n, i;
93: time_t time();
1.6 deraadt 94: int nhosts = 0;
1.1 deraadt 95:
1.5 millert 96: while ((ch = getopt(argc, argv, "a")) != -1)
1.1 deraadt 97: switch((char)ch) {
98: case 'a':
99: aflg = 1;
100: break;
101: case '?':
102: default:
103: fprintf(stderr, "usage: rwho [-a]\n");
104: exit(1);
105: }
106: if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
107: perror(_PATH_RWHODIR);
108: exit(1);
109: }
110: mp = myutmp;
111: (void)time(&now);
1.3 deraadt 112: while ((dp = readdir(dirp))) {
1.1 deraadt 113: if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
114: continue;
115: f = open(dp->d_name, O_RDONLY);
116: if (f < 0)
117: continue;
118: cc = read(f, (char *)&wd, sizeof (struct whod));
119: if (cc < WHDRSIZE) {
120: (void) close(f);
121: continue;
122: }
1.6 deraadt 123: nhosts++;
1.1 deraadt 124: if (down(w,now)) {
125: (void) close(f);
126: continue;
127: }
128: cc -= WHDRSIZE;
129: we = w->wd_we;
130: for (n = cc / sizeof (struct whoent); n > 0; n--) {
131: if (aflg == 0 && we->we_idle >= 60*60) {
132: we++;
133: continue;
134: }
135: if (nusers >= NUSERS) {
136: printf("too many users\n");
137: exit(1);
138: }
139: mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
1.3 deraadt 140: (void) strncpy(mp->myhost, w->wd_hostname,
141: sizeof(mp->myhost)-1);
142: mp->myhost[sizeof(mp->myhost)-1] = '\0';
1.1 deraadt 143: nusers++; we++; mp++;
144: }
145: (void) close(f);
146: }
1.6 deraadt 147: if (nhosts == 0)
148: errx(0, "no hosts in %s.", _PATH_RWHODIR);
1.4 deraadt 149: qsort((char *)myutmp, nusers, sizeof (struct myutmp),
150: (int (*)())utmpcmp);
1.1 deraadt 151: mp = myutmp;
152: width = 0;
153: for (i = 0; i < nusers; i++) {
154: int j = strlen(mp->myhost) + 1 + strlen(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];
1.7 ! deraadt 162: (void)snprintf(buf, sizeof buf, "%s:%s", mp->myhost,
! 163: mp->myutmp.out_line);
1.1 deraadt 164: printf("%-8.8s %-*s %.12s",
165: mp->myutmp.out_name,
166: width,
167: buf,
168: ctime((time_t *)&mp->myutmp.out_time)+4);
169: mp->myidle /= 60;
170: if (mp->myidle) {
171: if (aflg) {
172: if (mp->myidle >= 100*60)
173: mp->myidle = 100*60 - 1;
174: if (mp->myidle >= 60)
175: printf(" %2d", mp->myidle / 60);
176: else
177: printf(" ");
178: } else
179: printf(" ");
180: printf(":%02d", mp->myidle % 60);
181: }
182: printf("\n");
183: mp++;
184: }
185: exit(0);
186: }
187:
1.3 deraadt 188: int
1.1 deraadt 189: utmpcmp(u1, u2)
190: struct myutmp *u1, *u2;
191: {
192: int rc;
193:
194: rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
195: if (rc)
196: return (rc);
197: rc = strncmp(u1->myhost, u2->myhost, 8);
198: if (rc)
199: return (rc);
200: return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
201: }