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