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