xref: /freebsd/usr.bin/rwho/rwho.c (revision 3bd65123)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  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 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)rwho.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <protocols/rwhod.h>
51 #include <dirent.h>
52 #include <err.h>
53 #include <langinfo.h>
54 #include <locale.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <utmp.h>
61 
62 DIR	*dirp;
63 
64 struct	whod wd;
65 int	utmpcmp();
66 #define	NUSERS	1000
67 struct	myutmp {
68 	char    myhost[sizeof(wd.wd_hostname)];
69 	int	myidle;
70 	struct	outmp myutmp;
71 } myutmp[NUSERS];
72 int	nusers;
73 
74 #define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
75 /*
76  * this macro should be shared with ruptime.
77  */
78 #define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
79 
80 time_t	now;
81 int	aflg;
82 
83 static void usage __P((void));
84 int utmpcmp __P((const void *, const void *));
85 
86 int
87 main(argc, argv)
88 	int argc;
89 	char **argv;
90 {
91 	int ch;
92 	struct dirent *dp;
93 	int cc, width;
94 	register struct whod *w = &wd;
95 	register struct whoent *we;
96 	register struct myutmp *mp;
97 	int f, n, i;
98 	int d_first;
99 
100 	(void) setlocale(LC_TIME, "");
101 	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
102 
103 	while ((ch = getopt(argc, argv, "a")) != -1)
104 		switch((char)ch) {
105 		case 'a':
106 			aflg = 1;
107 			break;
108 		case '?':
109 		default:
110 			usage();
111 		}
112 	argc -= optind;
113 	argv += optind;
114 
115 	if (argc != 0)
116 		usage();
117 
118 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
119 		err(1, "%s", _PATH_RWHODIR);
120 	mp = myutmp;
121 	(void)time(&now);
122 	while ((dp = readdir(dirp))) {
123 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
124 			continue;
125 		f = open(dp->d_name, O_RDONLY);
126 		if (f < 0)
127 			continue;
128 		cc = read(f, (char *)&wd, sizeof (struct whod));
129 		if (cc < WHDRSIZE) {
130 			(void) close(f);
131 			continue;
132 		}
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 				errx(1, "too many users");
146 			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
147 			(void) strcpy(mp->myhost, w->wd_hostname);
148 			nusers++; we++; mp++;
149 		}
150 		(void) close(f);
151 	}
152 	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
153 	mp = myutmp;
154 	width = 0;
155 	for (i = 0; i < nusers; i++) {
156 		/* append one for the blank and use 8 for the out_line */
157 		int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
158 		if (j > width)
159 			width = j;
160 		mp++;
161 	}
162 	mp = myutmp;
163 	for (i = 0; i < nusers; i++) {
164 		char buf[BUFSIZ], cbuf[80];
165 
166 		strftime(cbuf, sizeof(cbuf),
167 			 d_first ? "%e %b %R" : "%b %e %R",
168 			 localtime((time_t *)&mp->myutmp.out_time));
169 		(void)sprintf(buf, "%s:%-.*s", mp->myhost,
170 		   sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
171 		printf("%-*.*s %-*s %s",
172 		   UT_NAMESIZE, sizeof(mp->myutmp.out_name),
173 		   mp->myutmp.out_name,
174 		   width,
175 		   buf,
176 		   cbuf);
177 		mp->myidle /= 60;
178 		if (mp->myidle) {
179 			if (aflg) {
180 				if (mp->myidle >= 100*60)
181 					mp->myidle = 100*60 - 1;
182 				if (mp->myidle >= 60)
183 					printf(" %2d", mp->myidle / 60);
184 				else
185 					printf("   ");
186 			} else
187 				printf(" ");
188 			printf(":%02d", mp->myidle % 60);
189 		}
190 		printf("\n");
191 		mp++;
192 	}
193 	exit(0);
194 }
195 
196 
197 static void
198 usage()
199 {
200 	fprintf(stderr, "usage: rwho [-a]\n");
201 	exit(1);
202 }
203 
204 #define MYUTMP(a) ((struct myutmp *)(a))
205 
206 int
207 utmpcmp(u1, u2)
208 	const void *u1, *u2;
209 {
210 	int rc;
211 
212 	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
213 		sizeof(MYUTMP(u2)->myutmp.out_name));
214 	if (rc)
215 		return (rc);
216 	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
217 	if (rc)
218 		return (rc);
219 	return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line,
220 		sizeof(MYUTMP(u2)->myutmp.out_line)));
221 }
222