xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/rwho.c (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #include <sys/param.h>
41 
42 #include <dirent.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <strings.h>
47 #include <time.h>
48 #include <unistd.h>
49 
50 #include <protocols/rwhod.h>
51 
52 static	DIR *dirp;
53 
54 static	struct whod wd;
55 #define	NUSERS	1000
56 static	struct myutmp {
57 	char	myhost[32];
58 	int	myidle;
59 	struct	outmp myutmp;
60 } myutmp[NUSERS];
61 static	int utmpcmp(const void *, const void *);
62 static	int nusers;
63 
64 #define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
65 #define	RWHODIR		"/var/spool/rwho"
66 /*
67  * this macro should be shared with ruptime.
68  */
69 #define	down(w, now)	((now) - (w)->wd_recvtime > 11 * 60)
70 
71 static	time_t now;
72 static	int aflg = 0;
73 
74 int
75 main(int argc, char **argv)
76 {
77 	struct dirent *dp;
78 	int cc, width;
79 	register struct whod *w = &wd;
80 	register struct whoent *we;
81 	register struct myutmp *mp;
82 	int f, n, i;
83 
84 	argc--, argv++;
85 again:
86 	if (argc > 0 && strcmp(argv[0], "-a") == 0) {
87 		argc--, argv++;
88 		aflg++;
89 		goto again;
90 	}
91 	(void) time(&now);
92 	if (chdir(RWHODIR) < 0) {
93 		perror(RWHODIR);
94 		return (EXIT_FAILURE);
95 	}
96 	dirp = opendir(".");
97 	if (dirp == NULL) {
98 		perror(RWHODIR);
99 		return (EXIT_FAILURE);
100 	}
101 	mp = myutmp;
102 	while (dp = readdir(dirp)) {
103 		if (dp->d_ino == 0)
104 			continue;
105 		if (strncmp(dp->d_name, "whod.", 5))
106 			continue;
107 		f = open(dp->d_name, 0);
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 				(void) printf("too many users\n");
128 				return (EXIT_FAILURE);
129 			}
130 			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
131 			(void) strncpy(mp->myhost, w->wd_hostname,
132 			    sizeof (mp->myhost));
133 			nusers++; we++; mp++;
134 		}
135 		(void) close(f);
136 	}
137 	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
138 	mp = myutmp;
139 	width = 0;
140 	for (i = 0; i < nusers; i++) {
141 		int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
142 		if (j > width)
143 			width = j;
144 		mp++;
145 	}
146 	mp = myutmp;
147 	for (i = 0; i < nusers; i++) {
148 		char buf[BUFSIZ];
149 		(void) snprintf(buf, BUFSIZ, "%.*s:%.*s",
150 		    sizeof (mp->myhost), mp->myhost,
151 		    sizeof (mp->myutmp.out_line), mp->myutmp.out_line);
152 		(void) printf("%-8.*s %-*s %.12s",
153 		    sizeof (mp->myutmp.out_name), mp->myutmp.out_name,
154 		    width, buf,
155 		    ctime((time_t *)&mp->myutmp.out_time) + 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 					(void) printf(" %2d", mp->myidle / 60);
163 				else
164 					(void) fputs("   ", stdout);
165 			} else
166 				(void) printf(" ");
167 			(void) printf(":%02d", mp->myidle % 60);
168 		}
169 		(void) puts("");
170 		mp++;
171 	}
172 	return (EXIT_SUCCESS);
173 }
174 
175 static int
176 utmpcmp(const void *p1, const void *p2)
177 {
178 	const struct myutmp *u1 = p1, *u2 = p2;
179 	int rc;
180 
181 	rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name,
182 	    sizeof (u1->myutmp.out_name));
183 	if (rc != 0)
184 		return (rc);
185 	rc = strncmp(u1->myhost, u2->myhost, sizeof (u1->myhost));
186 	if (rc != 0)
187 		return (rc);
188 	return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line,
189 	    sizeof (u1->myutmp.out_line)));
190 }
191