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
35static 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
42static 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: src/usr.bin/rwho/rwho.c,v 1.18 2002/07/01 16:40:33 markm Exp $");
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#ifdef __APPLE__
65#define UT_NAMESIZE 8
66#else
67#include <utmp.h>
68#endif
69
70DIR	*dirp;
71
72struct	whod wd;
73#define	NUSERS	1000
74struct	myutmp {
75	char    myhost[sizeof(wd.wd_hostname)];
76	int	myidle;
77	struct	outmp myutmp;
78} myutmp[NUSERS];
79int	nusers;
80
81#define	WHDRSIZE	(ssize_t)(sizeof (wd) - sizeof (wd.wd_we))
82/*
83 * this macro should be shared with ruptime.
84 */
85#define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
86
87time_t	now;
88int	aflg;
89
90static void usage(void);
91int utmpcmp(const void *, const void *);
92
93int
94main(int argc, char *argv[])
95{
96	int ch;
97	struct dirent *dp;
98	int width;
99	ssize_t cc;
100	register struct whod *w = &wd;
101	register struct whoent *we;
102	register struct myutmp *mp;
103	int f, n, i;
104	int d_first;
105
106	(void) setlocale(LC_TIME, "");
107	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
108
109	while ((ch = getopt(argc, argv, "a")) != -1)
110		switch((char)ch) {
111		case 'a':
112			aflg = 1;
113			break;
114		case '?':
115		default:
116			usage();
117		}
118	argc -= optind;
119	argv += optind;
120
121	if (argc != 0)
122		usage();
123
124	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
125		err(1, "%s", _PATH_RWHODIR);
126	mp = myutmp;
127	(void)time(&now);
128	while ((dp = readdir(dirp))) {
129		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
130			continue;
131		f = open(dp->d_name, O_RDONLY);
132		if (f < 0)
133			continue;
134		cc = read(f, (char *)&wd, sizeof (struct whod));
135		if (cc < WHDRSIZE) {
136			(void) close(f);
137			continue;
138		}
139		if (down(w,now)) {
140			(void) close(f);
141			continue;
142		}
143		cc -= WHDRSIZE;
144		we = w->wd_we;
145		for (n = cc / sizeof (struct whoent); n > 0; n--) {
146			if (aflg == 0 && we->we_idle >= 60*60) {
147				we++;
148				continue;
149			}
150			if (nusers >= NUSERS)
151				errx(1, "too many users");
152			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
153			(void) strcpy(mp->myhost, w->wd_hostname);
154			nusers++; we++; mp++;
155		}
156		(void) close(f);
157	}
158	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
159	mp = myutmp;
160	width = 0;
161	for (i = 0; i < nusers; i++) {
162		/* append one for the blank and use 8 for the out_line */
163		int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
164		if (j > width)
165			width = j;
166		mp++;
167	}
168	mp = myutmp;
169	for (i = 0; i < nusers; i++) {
170		char buf[BUFSIZ], cbuf[80];
171		time_t t = _int_to_time(mp->myutmp.out_time);
172
173		strftime(cbuf, sizeof(cbuf),
174			 d_first ? "%e %b %R" : "%b %e %R",
175			 localtime(&t));
176		(void)sprintf(buf, "%s:%-.*s", mp->myhost,
177		   (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
178		printf("%-*.*s %-*s %s",
179		   UT_NAMESIZE, (int)sizeof(mp->myutmp.out_name),
180		   mp->myutmp.out_name,
181		   width,
182		   buf,
183		   cbuf);
184		mp->myidle /= 60;
185		if (mp->myidle) {
186			if (aflg) {
187				if (mp->myidle >= 100*60)
188					mp->myidle = 100*60 - 1;
189				if (mp->myidle >= 60)
190					printf(" %2d", mp->myidle / 60);
191				else
192					printf("   ");
193			} else
194				printf(" ");
195			printf(":%02d", mp->myidle % 60);
196		}
197		printf("\n");
198		mp++;
199	}
200	exit(0);
201}
202
203
204static void
205usage(void)
206{
207	fprintf(stderr, "usage: rwho [-a]\n");
208	exit(1);
209}
210
211#define MYUTMP(a) ((const struct myutmp *)(a))
212
213int
214utmpcmp(const void *u1, const void *u2)
215{
216	int rc;
217
218	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
219		sizeof(MYUTMP(u2)->myutmp.out_name));
220	if (rc)
221		return (rc);
222	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
223	if (rc)
224		return (rc);
225	return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line,
226		sizeof(MYUTMP(u2)->myutmp.out_line)));
227}
228