xref: /dragonfly/usr.bin/rwho/rwho.c (revision f746689a)
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  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)rwho.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/rwho/rwho.c,v 1.13.2.1 2002/03/12 19:49:09 phantom Exp $
36  * $DragonFly: src/usr.bin/rwho/rwho.c,v 1.5 2008/10/16 01:52:33 swildner Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/file.h>
41 
42 #include <protocols/rwhod.h>
43 
44 #include <dirent.h>
45 #include <err.h>
46 #include <langinfo.h>
47 #include <locale.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include <utmp.h>
54 
55 struct	whod wd;
56 
57 #define	NUSERS	1000
58 
59 struct	myutmp {
60 	char    myhost[sizeof(wd.wd_hostname)];
61 	int	myidle;
62 	struct	outmp myutmp;
63 } myutmp[NUSERS];
64 
65 #define	WHDRSIZE	(sizeof (wd) - sizeof (wd.wd_we))
66 /*
67  * this macro should be shared with ruptime.
68  */
69 #define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
70 
71 static void	usage(void);
72 static int	utmpcmp(const void *, const void *);
73 
74 int
75 main(int argc, char **argv)
76 {
77 	struct dirent *dp;
78 	struct whod *w = &wd;
79 	struct whoent *we;
80 	struct myutmp *mp;
81 	int f, n, i, ch;
82 	int width, d_first, aflg;
83 	int nusers;
84 	size_t cc;
85 	time_t now;
86 	DIR *dirp;
87 
88 	aflg = nusers = 0;
89 	setlocale(LC_TIME, "");
90 	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
91 
92 	while ((ch = getopt(argc, argv, "a")) != -1)
93 		switch(ch) {
94 		case 'a':
95 			aflg = 1;
96 			break;
97 		case '?':
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	if (argc != 0)
105 		usage();
106 
107 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
108 		err(1, "%s", _PATH_RWHODIR);
109 	mp = myutmp;
110 	time(&now);
111 	while ((dp = readdir(dirp))) {
112 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
113 			continue;
114 		f = open(dp->d_name, O_RDONLY);
115 		if (f < 0)
116 			continue;
117 		cc = read(f, (char *)&wd, sizeof (struct whod));
118 		if (cc < WHDRSIZE) {
119 			close(f);
120 			continue;
121 		}
122 		if (down(w,now)) {
123 			close(f);
124 			continue;
125 		}
126 		cc -= WHDRSIZE;
127 		we = w->wd_we;
128 		for (n = cc / sizeof (struct whoent); n > 0; n--) {
129 			if (aflg == 0 && we->we_idle >= 60*60) {
130 				we++;
131 				continue;
132 			}
133 			if (nusers >= NUSERS)
134 				errx(1, "too many users");
135 			mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
136 			strcpy(mp->myhost, w->wd_hostname);
137 			nusers++; we++; mp++;
138 		}
139 		close(f);
140 	}
141 	qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
142 	mp = myutmp;
143 	width = 0;
144 	for (i = 0; i < nusers; i++) {
145 		/* append one for the blank and use 8 for the out_line */
146 		int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
147 		if (j > width)
148 			width = j;
149 		mp++;
150 	}
151 	mp = myutmp;
152 	for (i = 0; i < nusers; i++) {
153 		char buf[BUFSIZ], cbuf[80];
154 
155 		strftime(cbuf, sizeof(cbuf),
156 			 d_first ? "%e %b %R" : "%b %e %R",
157 			 localtime((time_t *)&mp->myutmp.out_time));
158 		sprintf(buf, "%s:%-.*s", mp->myhost,
159 		   (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
160 		printf("%-*.*s %-*s %s",
161 		   UT_NAMESIZE, (int)sizeof(mp->myutmp.out_name),
162 		   mp->myutmp.out_name,
163 		   width,
164 		   buf,
165 		   cbuf);
166 		mp->myidle /= 60;
167 		if (mp->myidle) {
168 			if (aflg) {
169 				if (mp->myidle >= 100*60)
170 					mp->myidle = 100*60 - 1;
171 				if (mp->myidle >= 60)
172 					printf(" %2d", mp->myidle / 60);
173 				else
174 					printf("   ");
175 			} else
176 				printf(" ");
177 			printf(":%02d", mp->myidle % 60);
178 		}
179 		printf("\n");
180 		mp++;
181 	}
182 	exit(0);
183 }
184 
185 
186 static void
187 usage(void)
188 {
189 	fprintf(stderr, "usage: rwho [-a]\n");
190 	exit(1);
191 }
192 
193 #define MYUTMP(a) ((const struct myutmp *)(a))
194 
195 static int
196 utmpcmp(const void *u1, const void *u2)
197 {
198 	int rc;
199 
200 	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
201 		sizeof(MYUTMP(u2)->myutmp.out_name));
202 	if (rc)
203 		return (rc);
204 	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
205 	if (rc)
206 		return (rc);
207 	return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line,
208 		sizeof(MYUTMP(u2)->myutmp.out_line)));
209 }
210