rwho.c revision 1.10 1 /* $NetBSD: rwho.c,v 1.10 1998/04/02 11:00:10 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 /*static char sccsid[] = "from: @(#)rwho.c 8.1 (Berkeley) 6/6/93";*/
44 __RCSID("$NetBSD: rwho.c,v 1.10 1998/04/02 11:00:10 kleink Exp $");
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/file.h>
49
50 #include <protocols/rwhod.h>
51
52 #include <dirent.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 DIR *dirp;
62
63 struct whod wd;
64 #define NUSERS 1000
65 struct myutmp {
66 char myhost[MAXHOSTNAMELEN];
67 int myidle;
68 struct outmp myutmp;
69 } myutmp[NUSERS];
70 int nusers;
71
72 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
73 /*
74 * this macro should be shared with ruptime.
75 */
76 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
77
78 int utmpcmp __P((const void *, const void *));
79 int main __P((int, char **));
80
81 time_t now;
82 int aflg;
83
84 int
85 main(argc, argv)
86 int argc;
87 char **argv;
88 {
89 extern char *optarg;
90 extern int optind;
91 int ch;
92 struct dirent *dp;
93 int cc, width;
94 struct whod *w = &wd;
95 struct whoent *we;
96 struct myutmp *mp;
97 int f, n, i, nhosts;
98
99 while ((ch = getopt(argc, argv, "a")) != -1)
100 switch((char)ch) {
101 case 'a':
102 aflg = 1;
103 break;
104 case '?':
105 default:
106 fprintf(stderr, "usage: rwho [-a]\n");
107 exit(1);
108 }
109 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
110 perror(_PATH_RWHODIR);
111 exit(1);
112 }
113 mp = myutmp;
114 nhosts = 0;
115 (void)time(&now);
116 while ((dp = readdir(dirp)) != NULL) {
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;
122 cc = read(f, (char *)&wd, sizeof (struct whod));
123 if (cc < WHDRSIZE) {
124 (void) close(f);
125 continue;
126 }
127 nhosts++;
128 if (down(w,now)) {
129 (void) close(f);
130 continue;
131 }
132 cc -= WHDRSIZE;
133 we = w->wd_we;
134 for (n = cc / sizeof (struct whoent); n > 0; n--) {
135 if (aflg == 0 && we->we_idle >= 60*60) {
136 we++;
137 continue;
138 }
139 if (nusers >= NUSERS) {
140 printf("too many users\n");
141 exit(1);
142 }
143 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
144 (void) strcpy(mp->myhost, w->wd_hostname);
145 nusers++; we++; mp++;
146 }
147 (void) close(f);
148 }
149 if (nhosts == 0)
150 errx(0, "no hosts in %s.", _PATH_RWHODIR);
151 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
152 mp = myutmp;
153 width = 0;
154 for (i = 0; i < nusers; i++) {
155 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
156 if (j > width)
157 width = j;
158 mp++;
159 }
160 mp = myutmp;
161 for (i = 0; i < nusers; i++) {
162 char buf[BUFSIZ];
163 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
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
188 int
189 utmpcmp(v1, v2)
190 const void *v1, *v2;
191 {
192 const struct myutmp *u1, *u2;
193 int rc;
194
195 u1 = v1;
196 u2 = v2;
197 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
198 if (rc)
199 return (rc);
200 rc = strncmp(u1->myhost, u2->myhost, 8);
201 if (rc)
202 return (rc);
203 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
204 }
205