rwho.c revision 18485
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1983, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes
3436150Scharnier#ifndef lint
3536150Scharnierstatic char copyright[] =
3636150Scharnier"@(#) Copyright (c) 1983, 1993\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3899110Sobrien#endif /* not lint */
3999110Sobrien
401556Srgrimes#ifndef lint
4117987Speterstatic char sccsid[] = "@(#)rwho.c	8.1 (Berkeley) 6/6/93";
4217987Speter#endif /* not lint */
43114763Sobrien
4417987Speter#include <sys/param.h>
451556Srgrimes#include <sys/file.h>
461556Srgrimes#include <protocols/rwhod.h>
471556Srgrimes#include <dirent.h>
481556Srgrimes#include <stdio.h>
4917525Sache#include <time.h>
5017525Sache#include <string.h>
511556Srgrimes#include <locale.h>
521556Srgrimes
531556SrgrimesDIR	*dirp;
541556Srgrimes
551556Srgrimesstruct	whod wd;
561556Srgrimesint	utmpcmp();
571556Srgrimes#define	NUSERS	1000
581556Srgrimesstruct	myutmp {
591556Srgrimes	char	myhost[MAXHOSTNAMELEN];
601556Srgrimes	int	myidle;
611556Srgrimes	struct	outmp myutmp;
621556Srgrimes} myutmp[NUSERS];
631556Srgrimesint	nusers;
6420425Ssteve
6517987Speter#define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
6617987Speter/*
6717987Speter * this macro should be shared with ruptime.
681556Srgrimes */
691556Srgrimes#define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
701556Srgrimes
711556Srgrimestime_t	now;
721556Srgrimesint	aflg;
731556Srgrimes
741556Srgrimesmain(argc, argv)
751556Srgrimes	int argc;
76201056Sjilles	char **argv;
7790111Simp{
781556Srgrimes	extern char *optarg;
791556Srgrimes	extern int optind;
801556Srgrimes	int ch;
8117987Speter	struct dirent *dp;
821556Srgrimes	int cc, width;
83208755Sjilles	register struct whod *w = &wd;
8417987Speter	register struct whoent *we;
851556Srgrimes	register struct myutmp *mp;
861556Srgrimes	int f, n, i;
871556Srgrimes	time_t time();
881556Srgrimes
8997689Stjr	(void) setlocale(LC_TIME, "");
901556Srgrimes
911556Srgrimes	while ((ch = getopt(argc, argv, "a")) != EOF)
92159632Sstefanf		switch((char)ch) {
931556Srgrimes		case 'a':
94213760Sobrien			aflg = 1;
951556Srgrimes			break;
96213760Sobrien		case '?':
9717987Speter		default:
98201056Sjilles			fprintf(stderr, "usage: rwho [-a]\n");
9920425Ssteve			exit(1);
10017987Speter		}
101201056Sjilles	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
10220425Ssteve		perror(_PATH_RWHODIR);
103201056Sjilles		exit(1);
10420425Ssteve	}
105201056Sjilles	mp = myutmp;
10620425Ssteve	(void)time(&now);
107201056Sjilles	while (dp = readdir(dirp)) {
10820425Ssteve		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
109201056Sjilles			continue;
11097689Stjr		f = open(dp->d_name, O_RDONLY);
1118855Srgrimes		if (f < 0)
1121556Srgrimes			continue;
1131556Srgrimes		cc = read(f, (char *)&wd, sizeof (struct whod));
114201056Sjilles		if (cc < WHDRSIZE) {
11520425Ssteve			(void) close(f);
116201056Sjilles			continue;
117159632Sstefanf		}
118208755Sjilles		if (down(w,now)) {
119208755Sjilles			(void) close(f);
120208755Sjilles			continue;
121208755Sjilles		}
122201056Sjilles		cc -= WHDRSIZE;
12320425Ssteve		we = w->wd_we;
12420425Ssteve		for (n = cc / sizeof (struct whoent); n > 0; n--) {
12520425Ssteve			if (aflg == 0 && we->we_idle >= 60*60) {
1261556Srgrimes				we++;
1271556Srgrimes				continue;
128213760Sobrien			}
1291556Srgrimes			if (nusers >= NUSERS) {
130213760Sobrien				printf("too many users\n");
131207678Sjilles				exit(1);
132207678Sjilles			}
133207678Sjilles			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
134213760Sobrien			(void) strcpy(mp->myhost, w->wd_hostname);
135207678Sjilles			nusers++; we++; mp++;
136207678Sjilles		}
137207678Sjilles		(void) close(f);
138213811Sobrien	}
139213811Sobrien	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
140213811Sobrien	mp = myutmp;
1411556Srgrimes	width = 0;
1421556Srgrimes	for (i = 0; i < nusers; i++) {
143155302Sschweikh		/* append one for the blank and use 8 for the out_line */
1441556Srgrimes		int j = strlen(mp->myhost) + 1 + 8;
1451556Srgrimes		if (j > width)
1461556Srgrimes			width = j;
1471556Srgrimes		mp++;
148201053Sjilles	}
1491556Srgrimes	mp = myutmp;
1501556Srgrimes	for (i = 0; i < nusers; i++) {
1511556Srgrimes		char buf[BUFSIZ], cbuf[80];
1521556Srgrimes		strftime(cbuf, sizeof(cbuf), "%c", localtime((time_t *)&mp->myutmp.out_time));
1531556Srgrimes		(void)sprintf(buf, "%s:%-.8s", mp->myhost, mp->myutmp.out_line);
1541556Srgrimes		printf("%-8.8s %-*s %.12s",
1551556Srgrimes		   mp->myutmp.out_name,
1561556Srgrimes		   width,
1571556Srgrimes		   buf,
1581556Srgrimes		   cbuf + 4);
1591556Srgrimes		mp->myidle /= 60;
1601556Srgrimes		if (mp->myidle) {
1611556Srgrimes			if (aflg) {
1621556Srgrimes				if (mp->myidle >= 100*60)
1631556Srgrimes					mp->myidle = 100*60 - 1;
1641556Srgrimes				if (mp->myidle >= 60)
1651556Srgrimes					printf(" %2d", mp->myidle / 60);
1661556Srgrimes				else
1671556Srgrimes					printf("   ");
16890111Simp			} else
16990111Simp				printf(" ");
17097689Stjr			printf(":%02d", mp->myidle % 60);
1711556Srgrimes		}
1721556Srgrimes		printf("\n");
1731556Srgrimes		mp++;
1741556Srgrimes	}
1751556Srgrimes	exit(0);
1761556Srgrimes}
1771556Srgrimes
1781556Srgrimesutmpcmp(u1, u2)
1791556Srgrimes	struct myutmp *u1, *u2;
180201056Sjilles{
181201056Sjilles	int rc;
18220425Ssteve
1831556Srgrimes	rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
1841556Srgrimes	if (rc)
1851556Srgrimes		return (rc);
1861556Srgrimes	rc = strncmp(u1->myhost, u2->myhost, 8);
1871556Srgrimes	if (rc)
1881556Srgrimes		return (rc);
1891556Srgrimes	return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
1901556Srgrimes}
1911556Srgrimes