rwho.c revision 1.13 1 /* $NetBSD: rwho.c,v 1.13 2000/09/06 12:13:48 mjl 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.13 2000/09/06 12:13:48 mjl 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 <locale.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <fcntl.h>
61 #include <unistd.h>
62
63 void usage(void);
64 int utmpcmp(const void *, const void *);
65
66 DIR *dirp;
67
68 struct whod wd;
69 #define NUSERS 1000
70 struct myutmp {
71 char myhost[MAXHOSTNAMELEN];
72 int myidle;
73 struct outmp myutmp;
74 } myutmp[NUSERS];
75 int nusers;
76
77 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
78 /*
79 * this macro should be shared with ruptime.
80 */
81 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
82
83 int utmpcmp __P((const void *, const void *));
84 int main __P((int, char **));
85
86 time_t now;
87 int aflg;
88
89 int
90 main(int argc, char **argv)
91 {
92 int ch;
93 struct dirent *dp;
94 int cc, width;
95 struct whod *w = &wd;
96 struct whoent *we;
97 struct myutmp *mp;
98 int f, n, i, nhosts;
99
100 setlocale(LC_TIME, "");
101
102 while ((ch = getopt(argc, argv, "a")) != -1)
103 switch((char)ch) {
104 case 'a':
105 aflg = 1;
106 break;
107 case '?':
108 default:
109 usage();
110 }
111
112 if(optind != argc)
113 usage();
114
115 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
116 err(1, "%s", _PATH_RWHODIR);
117
118 mp = myutmp;
119 nhosts = 0;
120 (void)time(&now);
121 while ((dp = readdir(dirp)) != NULL) {
122 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
123 continue;
124 f = open(dp->d_name, O_RDONLY);
125 if (f < 0)
126 continue;
127 cc = read(f, (char *)&wd, sizeof (struct whod));
128 if (cc < WHDRSIZE) {
129 (void) close(f);
130 continue;
131 }
132 nhosts++;
133 if (down(w,now)) {
134 (void) close(f);
135 continue;
136 }
137 cc -= WHDRSIZE;
138 we = w->wd_we;
139 for (n = cc / sizeof (struct whoent); n > 0; n--) {
140 if (aflg == 0 && we->we_idle >= 60*60) {
141 we++;
142 continue;
143 }
144 if (nusers >= NUSERS)
145 err(1, "too many users");
146
147 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
148 (void) strcpy(mp->myhost, w->wd_hostname);
149 nusers++; we++; mp++;
150 }
151 (void) close(f);
152 }
153 if (nhosts == 0)
154 errx(0, "no hosts in %s.", _PATH_RWHODIR);
155 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
156 mp = myutmp;
157 width = 0;
158 for (i = 0; i < nusers; i++) {
159 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
160 if (j > width)
161 width = j;
162 mp++;
163 }
164 mp = myutmp;
165 for (i = 0; i < nusers; i++) {
166 char buf[BUFSIZ], cbuf[80];
167 strftime(cbuf, sizeof(cbuf), "%c", localtime((time_t *)&mp->myutmp.out_time));
168 /* cbuf = ctime((time_t *)&mp->myutmp.out_time); */
169 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
170 printf("%-8.8s %-*s %.12s",
171 mp->myutmp.out_name,
172 width,
173 buf,
174 cbuf + 4);
175 mp->myidle /= 60;
176 if (mp->myidle) {
177 if (aflg) {
178 if (mp->myidle >= 100*60)
179 mp->myidle = 100*60 - 1;
180 if (mp->myidle >= 60)
181 printf(" %2d", mp->myidle / 60);
182 else
183 printf(" ");
184 } else
185 printf(" ");
186 printf(":%02d", mp->myidle % 60);
187 }
188 printf("\n");
189 mp++;
190 }
191 exit(0);
192 }
193
194 int
195 utmpcmp(const void *v1, const void *v2)
196 {
197 const struct myutmp *u1, *u2;
198 int rc;
199
200 u1 = v1;
201 u2 = v2;
202 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
203 if (rc)
204 return (rc);
205 rc = strcmp(u1->myhost, u2->myhost);
206 if (rc)
207 return (rc);
208 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
209 }
210
211 void
212 usage(void)
213 {
214 fprintf(stderr, "usage: rwho [-a]\n");
215 exit(1);
216 }
217