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