rwho.c revision 229209
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1983, 1993\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)rwho.c	8.1 (Berkeley) 6/6/93";
39#endif
40#endif /* not lint */
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: stable/9/usr.bin/rwho/rwho.c 229209 2012-01-01 21:21:57Z dim $");
44
45#include <sys/param.h>
46#include <sys/file.h>
47
48#include <protocols/rwhod.h>
49
50#include <dirent.h>
51#include <err.h>
52#include <langinfo.h>
53#include <locale.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <time.h>
58#include <timeconv.h>
59#include <unistd.h>
60
61DIR	*dirp;
62
63struct	whod wd;
64#define	NUSERS	1000
65struct	myutmp {
66	char    myhost[sizeof(wd.wd_hostname)];
67	int	myidle;
68	struct	outmp myutmp;
69} myutmp[NUSERS];
70int	nusers;
71
72#define	WHDRSIZE	(ssize_t)(sizeof (wd) - sizeof (wd.wd_we))
73/*
74 * this macro should be shared with ruptime.
75 */
76#define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
77
78time_t	now;
79int	aflg;
80
81static void usage(void);
82int utmpcmp(const void *, const void *);
83
84int
85main(int argc, char *argv[])
86{
87	int ch;
88	struct dirent *dp;
89	int width;
90	ssize_t cc;
91	register struct whod *w = &wd;
92	register struct whoent *we;
93	register struct myutmp *mp;
94	int f, n, i;
95	int d_first;
96
97	(void) setlocale(LC_TIME, "");
98	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
99
100	while ((ch = getopt(argc, argv, "a")) != -1)
101		switch((char)ch) {
102		case 'a':
103			aflg = 1;
104			break;
105		case '?':
106		default:
107			usage();
108		}
109	argc -= optind;
110	argv += optind;
111
112	if (argc != 0)
113		usage();
114
115	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
116		err(1, "%s", _PATH_RWHODIR);
117	mp = myutmp;
118	(void)time(&now);
119	while ((dp = readdir(dirp))) {
120		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
121			continue;
122		f = open(dp->d_name, O_RDONLY);
123		if (f < 0)
124			continue;
125		cc = read(f, (char *)&wd, sizeof (struct whod));
126		if (cc < WHDRSIZE) {
127			(void) close(f);
128			continue;
129		}
130		if (down(w,now)) {
131			(void) close(f);
132			continue;
133		}
134		cc -= WHDRSIZE;
135		we = w->wd_we;
136		for (n = cc / sizeof (struct whoent); n > 0; n--) {
137			if (aflg == 0 && we->we_idle >= 60*60) {
138				we++;
139				continue;
140			}
141			if (nusers >= NUSERS)
142				errx(1, "too many users");
143			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
144			(void) strcpy(mp->myhost, w->wd_hostname);
145			nusers++; we++; mp++;
146		}
147		(void) close(f);
148	}
149	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
150	mp = myutmp;
151	width = 0;
152	for (i = 0; i < nusers; i++) {
153		/* append one for the blank and use 8 for the out_line */
154		int j = strlen(mp->myhost) + 1 + sizeof(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], cbuf[80];
162		time_t t = _int_to_time(mp->myutmp.out_time);
163
164		strftime(cbuf, sizeof(cbuf),
165			 d_first ? "%e %b %R" : "%b %e %R",
166			 localtime(&t));
167		(void)sprintf(buf, "%s:%-.*s", mp->myhost,
168		   (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
169		printf("%-*.*s %-*s %s",
170		   (int)sizeof(mp->myutmp.out_name),
171		   (int)sizeof(mp->myutmp.out_name),
172		   mp->myutmp.out_name,
173		   width,
174		   buf,
175		   cbuf);
176		mp->myidle /= 60;
177		if (mp->myidle) {
178			if (aflg) {
179				if (mp->myidle >= 100*60)
180					mp->myidle = 100*60 - 1;
181				if (mp->myidle >= 60)
182					printf(" %2d", mp->myidle / 60);
183				else
184					printf("   ");
185			} else
186				printf(" ");
187			printf(":%02d", mp->myidle % 60);
188		}
189		printf("\n");
190		mp++;
191	}
192	exit(0);
193}
194
195
196static void
197usage(void)
198{
199	fprintf(stderr, "usage: rwho [-a]\n");
200	exit(1);
201}
202
203#define MYUTMP(a) ((const struct myutmp *)(a))
204
205int
206utmpcmp(const void *u1, const void *u2)
207{
208	int rc;
209
210	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
211		sizeof(MYUTMP(u2)->myutmp.out_name));
212	if (rc)
213		return (rc);
214	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
215	if (rc)
216		return (rc);
217	return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line,
218		sizeof(MYUTMP(u2)->myutmp.out_line)));
219}
220