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