Annotation of src/usr.bin/rwho/rwho.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: rwho.c,v 1.2 1996/06/26 05:39:00 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.3 ! deraadt 44: static char rcsid[] = "$OpenBSD: rwho.c,v 1.2 1996/06/26 05:39:00 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();
94:
95: while ((ch = getopt(argc, argv, "a")) != EOF)
96: switch((char)ch) {
97: case 'a':
98: aflg = 1;
99: break;
100: case '?':
101: default:
102: fprintf(stderr, "usage: rwho [-a]\n");
103: exit(1);
104: }
105: if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
106: perror(_PATH_RWHODIR);
107: exit(1);
108: }
109: mp = myutmp;
110: (void)time(&now);
1.3 ! deraadt 111: while ((dp = readdir(dirp))) {
1.1 deraadt 112: if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
113: continue;
114: f = open(dp->d_name, O_RDONLY);
115: if (f < 0)
116: continue;
117: cc = read(f, (char *)&wd, sizeof (struct whod));
118: if (cc < WHDRSIZE) {
119: (void) close(f);
120: continue;
121: }
122: if (down(w,now)) {
123: (void) close(f);
124: continue;
125: }
126: cc -= WHDRSIZE;
127: we = w->wd_we;
128: for (n = cc / sizeof (struct whoent); n > 0; n--) {
129: if (aflg == 0 && we->we_idle >= 60*60) {
130: we++;
131: continue;
132: }
133: if (nusers >= NUSERS) {
134: printf("too many users\n");
135: exit(1);
136: }
137: mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
1.3 ! deraadt 138: (void) strncpy(mp->myhost, w->wd_hostname,
! 139: sizeof(mp->myhost)-1);
! 140: mp->myhost[sizeof(mp->myhost)-1] = '\0';
1.1 deraadt 141: nusers++; we++; mp++;
142: }
143: (void) close(f);
144: }
145: qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
146: mp = myutmp;
147: width = 0;
148: for (i = 0; i < nusers; i++) {
149: int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
150: if (j > width)
151: width = j;
152: mp++;
153: }
154: mp = myutmp;
155: for (i = 0; i < nusers; i++) {
156: char buf[BUFSIZ];
157: (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
158: printf("%-8.8s %-*s %.12s",
159: mp->myutmp.out_name,
160: width,
161: buf,
162: ctime((time_t *)&mp->myutmp.out_time)+4);
163: mp->myidle /= 60;
164: if (mp->myidle) {
165: if (aflg) {
166: if (mp->myidle >= 100*60)
167: mp->myidle = 100*60 - 1;
168: if (mp->myidle >= 60)
169: printf(" %2d", mp->myidle / 60);
170: else
171: printf(" ");
172: } else
173: printf(" ");
174: printf(":%02d", mp->myidle % 60);
175: }
176: printf("\n");
177: mp++;
178: }
179: exit(0);
180: }
181:
1.3 ! deraadt 182: int
1.1 deraadt 183: utmpcmp(u1, u2)
184: struct myutmp *u1, *u2;
185: {
186: int rc;
187:
188: rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
189: if (rc)
190: return (rc);
191: rc = strncmp(u1->myhost, u2->myhost, 8);
192: if (rc)
193: return (rc);
194: return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
195: }