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