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