Annotation of src/usr.bin/rwho/rwho.c, Revision 1.1.1.1
1.1 deraadt 1: /*
2: * Copyright (c) 1983 The Regents of the University of California.
3: * 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: char copyright[] =
36: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37: All rights reserved.\n";
38: #endif /* not lint */
39:
40: #ifndef lint
41: /*static char sccsid[] = "from: @(#)rwho.c 5.5 (Berkeley) 6/1/90";*/
42: static char rcsid[] = "$Id: rwho.c,v 1.4 1994/12/24 17:38:21 cgd Exp $";
43: #endif /* not lint */
44:
45: #include <sys/param.h>
46: #include <sys/file.h>
47: #include <dirent.h>
48: #include <protocols/rwhod.h>
49: #include <stdio.h>
50: #include <string.h>
51:
52: DIR *dirp;
53:
54: struct whod wd;
55: int utmpcmp();
56: #define NUSERS 1000
57: struct myutmp {
58: char myhost[MAXHOSTNAMELEN];
59: int myidle;
60: struct outmp myutmp;
61: } myutmp[NUSERS];
62: int nusers;
63:
64: #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
65: /*
66: * this macro should be shared with ruptime.
67: */
68: #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
69:
70: char *ctime(), *strcpy();
71: time_t now;
72: int aflg;
73:
74: main(argc, argv)
75: int argc;
76: char **argv;
77: {
78: extern char *optarg;
79: extern int optind;
80: int ch;
81: struct dirent *dp;
82: int cc, width;
83: register struct whod *w = &wd;
84: register struct whoent *we;
85: register struct myutmp *mp;
86: int f, n, i;
87: time_t time();
88:
89: while ((ch = getopt(argc, argv, "a")) != EOF)
90: switch((char)ch) {
91: case 'a':
92: aflg = 1;
93: break;
94: case '?':
95: default:
96: fprintf(stderr, "usage: rwho [-a]\n");
97: exit(1);
98: }
99: if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
100: perror(_PATH_RWHODIR);
101: exit(1);
102: }
103: mp = myutmp;
104: (void)time(&now);
105: while (dp = readdir(dirp)) {
106: if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
107: continue;
108: f = open(dp->d_name, O_RDONLY);
109: if (f < 0)
110: continue;
111: cc = read(f, (char *)&wd, sizeof (struct whod));
112: if (cc < WHDRSIZE) {
113: (void) close(f);
114: continue;
115: }
116: if (down(w,now)) {
117: (void) close(f);
118: continue;
119: }
120: cc -= WHDRSIZE;
121: we = w->wd_we;
122: for (n = cc / sizeof (struct whoent); n > 0; n--) {
123: if (aflg == 0 && we->we_idle >= 60*60) {
124: we++;
125: continue;
126: }
127: if (nusers >= NUSERS) {
128: printf("too many users\n");
129: exit(1);
130: }
131: mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
132: (void) strcpy(mp->myhost, w->wd_hostname);
133: nusers++; we++; mp++;
134: }
135: (void) close(f);
136: }
137: qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
138: mp = myutmp;
139: width = 0;
140: for (i = 0; i < nusers; i++) {
141: int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
142: if (j > width)
143: width = j;
144: mp++;
145: }
146: mp = myutmp;
147: for (i = 0; i < nusers; i++) {
148: char buf[BUFSIZ];
149: (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
150: printf("%-8.8s %-*s %.12s",
151: mp->myutmp.out_name,
152: width,
153: buf,
154: ctime((time_t *)&mp->myutmp.out_time)+4);
155: mp->myidle /= 60;
156: if (mp->myidle) {
157: if (aflg) {
158: if (mp->myidle >= 100*60)
159: mp->myidle = 100*60 - 1;
160: if (mp->myidle >= 60)
161: printf(" %2d", mp->myidle / 60);
162: else
163: printf(" ");
164: } else
165: printf(" ");
166: printf(":%02d", mp->myidle % 60);
167: }
168: printf("\n");
169: mp++;
170: }
171: exit(0);
172: }
173:
174: utmpcmp(u1, u2)
175: struct myutmp *u1, *u2;
176: {
177: int rc;
178:
179: rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
180: if (rc)
181: return (rc);
182: rc = strncmp(u1->myhost, u2->myhost, 8);
183: if (rc)
184: return (rc);
185: return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
186: }