xref: /original-bsd/usr.bin/rwho/rwho.c (revision 92d3de31)
1 #ifndef lint
2 static char sccsid[] = "@(#)rwho.c	4.5 83/05/05";
3 #endif
4 
5 #include <sys/param.h>
6 #include <stdio.h>
7 #include <dir.h>
8 #include <utmp.h>
9 #include <rwhod.h>
10 
11 DIR	*etc;
12 
13 struct	whod wd;
14 int	utmpcmp();
15 #define	NUSERS	1000
16 struct	myutmp {
17 	char	myhost[32];
18 	int	myidle;
19 	struct	utmp myutmp;
20 } myutmp[NUSERS];
21 int	nusers;
22 
23 #define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
24 #define	RWHODIR		"/usr/spool/rwho"
25 
26 char	*ctime(), *strcpy();
27 int	now;
28 int	aflg;
29 
30 main(argc, argv)
31 	int argc;
32 	char **argv;
33 {
34 	struct direct *dp;
35 	int cc, width;
36 	register struct whod *w = &wd;
37 	register struct whoent *we;
38 	register struct myutmp *mp;
39 	int f, n, i;
40 
41 	argc--, argv++;
42 again:
43 	if (argc > 0 && !strcmp(argv[0], "-a")) {
44 		argc--, argv++;
45 		aflg++;
46 		goto again;
47 	}
48 	(void) time(&now);
49 	if (chdir(RWHODIR) < 0) {
50 		perror(RWHODIR);
51 		exit(1);
52 	}
53 	etc = opendir(".");
54 	if (etc == NULL) {
55 		perror("/etc");
56 		exit(1);
57 	}
58 	mp = myutmp;
59 	while (dp = readdir(etc)) {
60 		if (dp->d_ino == 0)
61 			continue;
62 		if (strncmp(dp->d_name, "whod.", 5))
63 			continue;
64 		f = open(dp->d_name, 0);
65 		if (f < 0)
66 			continue;
67 		cc = read(f, (char *)&wd, sizeof (struct whod));
68 		if (cc < WHDRSIZE) {
69 			(void) close(f);
70 			continue;
71 		}
72 		if (now - w->wd_recvtime > 5 * 60) {
73 			(void) close(f);
74 			continue;
75 		}
76 		cc -= WHDRSIZE;
77 		we = w->wd_we;
78 		for (n = cc / sizeof (struct whoent); n > 0; n--) {
79 			if (aflg == 0 && we->we_idle >= 60*60) {
80 				we++;
81 				continue;
82 			}
83 			if (nusers >= NUSERS) {
84 				printf("too many users\n");
85 				exit(1);
86 			}
87 			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
88 			(void) strcpy(mp->myhost, w->wd_hostname);
89 			nusers++; we++; mp++;
90 		}
91 		(void) close(f);
92 	}
93 	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
94 	mp = myutmp;
95 	width = 0;
96 	for (i = 0; i < nusers; i++) {
97 		int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.ut_line);
98 		if (j > width)
99 			width = j;
100 		mp++;
101 	}
102 	mp = myutmp;
103 	for (i = 0; i < nusers; i++) {
104 		char buf[22];
105 		sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.ut_line);
106 		printf("%-8.8s %-*s %.12s",
107 		   mp->myutmp.ut_name,
108 		   width,
109 		   buf,
110 		   ctime((time_t *)&mp->myutmp.ut_time)+4);
111 		mp->myidle /= 60;
112 		if (mp->myidle) {
113 			if (aflg) {
114 				if (mp->myidle >= 100*60)
115 					mp->myidle = 100*60 - 1;
116 				if (mp->myidle >= 60)
117 					printf(" %2d", mp->myidle / 60);
118 				else
119 					printf("   ");
120 			} else
121 				printf(" ");
122 			printf(":%02d", mp->myidle % 60);
123 		}
124 		printf("\n");
125 		mp++;
126 	}
127 	exit(0);
128 }
129 
130 utmpcmp(u1, u2)
131 	struct myutmp *u1, *u2;
132 {
133 	int rc;
134 
135 	rc = strncmp(u1->myutmp.ut_name, u2->myutmp.ut_name, 8);
136 	if (rc)
137 		return (rc);
138 	rc = strncmp(u1->myhost, u2->myhost, 8);
139 	if (rc)
140 		return (rc);
141 	return (strncmp(u1->myutmp.ut_line, u2->myutmp.ut_line, 8));
142 }
143