rwho.c revision 8874
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 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
41static char sccsid[] = "@(#)rwho.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/dir.h>
46#include <sys/file.h>
47#include <protocols/rwhod.h>
48#include <stdio.h>
49
50DIR	*dirp;
51
52struct	whod wd;
53int	utmpcmp();
54#define	NUSERS	1000
55struct	myutmp {
56	char	myhost[MAXHOSTNAMELEN];
57	int	myidle;
58	struct	outmp myutmp;
59} myutmp[NUSERS];
60int	nusers;
61
62#define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
63/*
64 * this macro should be shared with ruptime.
65 */
66#define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
67
68char	*ctime(), *strcpy();
69time_t	now;
70int	aflg;
71
72main(argc, argv)
73	int argc;
74	char **argv;
75{
76	extern char *optarg;
77	extern int optind;
78	int ch;
79	struct direct *dp;
80	int cc, width;
81	register struct whod *w = &wd;
82	register struct whoent *we;
83	register struct myutmp *mp;
84	int f, n, i;
85	time_t time();
86
87	while ((ch = getopt(argc, argv, "a")) != EOF)
88		switch((char)ch) {
89		case 'a':
90			aflg = 1;
91			break;
92		case '?':
93		default:
94			fprintf(stderr, "usage: rwho [-a]\n");
95			exit(1);
96		}
97	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
98		perror(_PATH_RWHODIR);
99		exit(1);
100	}
101	mp = myutmp;
102	(void)time(&now);
103	while (dp = readdir(dirp)) {
104		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
105			continue;
106		f = open(dp->d_name, O_RDONLY);
107		if (f < 0)
108			continue;
109		cc = read(f, (char *)&wd, sizeof (struct whod));
110		if (cc < WHDRSIZE) {
111			(void) close(f);
112			continue;
113		}
114		if (down(w,now)) {
115			(void) close(f);
116			continue;
117		}
118		cc -= WHDRSIZE;
119		we = w->wd_we;
120		for (n = cc / sizeof (struct whoent); n > 0; n--) {
121			if (aflg == 0 && we->we_idle >= 60*60) {
122				we++;
123				continue;
124			}
125			if (nusers >= NUSERS) {
126				printf("too many users\n");
127				exit(1);
128			}
129			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
130			(void) strcpy(mp->myhost, w->wd_hostname);
131			nusers++; we++; mp++;
132		}
133		(void) close(f);
134	}
135	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
136	mp = myutmp;
137	width = 0;
138	for (i = 0; i < nusers; i++) {
139		/* append one for the blank and use 8 for the out_line */
140		int j = strlen(mp->myhost) + 1 + 8;
141		if (j > width)
142			width = j;
143		mp++;
144	}
145	mp = myutmp;
146	for (i = 0; i < nusers; i++) {
147		char buf[BUFSIZ];
148		(void)sprintf(buf, "%s:%-.8s", mp->myhost, mp->myutmp.out_line);
149		printf("%-8.8s %-*s %.12s",
150		   mp->myutmp.out_name,
151		   width,
152		   buf,
153		   ctime((time_t *)&mp->myutmp.out_time)+4);
154		mp->myidle /= 60;
155		if (mp->myidle) {
156			if (aflg) {
157				if (mp->myidle >= 100*60)
158					mp->myidle = 100*60 - 1;
159				if (mp->myidle >= 60)
160					printf(" %2d", mp->myidle / 60);
161				else
162					printf("   ");
163			} else
164				printf(" ");
165			printf(":%02d", mp->myidle % 60);
166		}
167		printf("\n");
168		mp++;
169	}
170	exit(0);
171}
172
173utmpcmp(u1, u2)
174	struct myutmp *u1, *u2;
175{
176	int rc;
177
178	rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
179	if (rc)
180		return (rc);
181	rc = strncmp(u1->myhost, u2->myhost, 8);
182	if (rc)
183		return (rc);
184	return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
185}
186