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