just warn branches to undef externals not local defined globals
[unix-history] / usr / src / usr.bin / ruptime / ruptime.c
CommitLineData
be0e2d23 1#ifndef lint
44bbb118 2static char sccsid[] = "@(#)ruptime.c 4.16 (Berkeley) 83/11/17";
be0e2d23
BJ
3#endif
4
ed0072ae 5#include <sys/param.h>
be0e2d23 6#include <stdio.h>
7123e2dc
SL
7#include <sys/dir.h>
8#include "../etc/rwhod/rwhod.h"
be0e2d23 9
b9484359 10DIR *dirp;
be0e2d23
BJ
11
12#define NHOSTS 100
13int nhosts;
14struct hs {
15 struct whod *hs_wd;
16 int hs_nusers;
17} hs[NHOSTS];
18struct whod awhod;
508c25be 19int hscmp(), ucmp(), lcmp(), tcmp();
be0e2d23
BJ
20
21#define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
fe77431f 22#define RWHODIR "/usr/spool/rwho"
be0e2d23
BJ
23
24char *interval();
25int now;
26char *malloc(), *sprintf();
27int aflg;
44bbb118 28int rflg = 1;
be0e2d23 29
b9484359 30#define down(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
508c25be 31
be0e2d23
BJ
32main(argc, argv)
33 int argc;
34 char **argv;
35{
36 struct direct *dp;
37 int f, i, t;
38 char buf[BUFSIZ]; int cc;
44bbb118 39 char *name;
be0e2d23
BJ
40 register struct hs *hsp = hs;
41 register struct whod *wd;
42 register struct whoent *we;
acf8f1b2 43 int maxloadav = 0;
508c25be 44 int (*cmp)() = hscmp;
be0e2d23 45
44bbb118
RC
46 name = *argv;
47 while (*++argv)
48 while (**argv)
49 switch (*(*argv)++) {
50 case 'a':
51 aflg++;
52 break;
53 case 'l':
54 cmp = lcmp;
55 break;
56 case 'u':
57 cmp = ucmp;
58 break;
59 case 't':
60 cmp = tcmp;
61 break;
62 case 'r':
63 rflg = -rflg;
64 break;
65 case '-':
66 break;
67 default:
68 fprintf(stderr, "Usage: %s [ -ar [ lut ] ]\n",
69 name);
70 exit (1);
71 }
be0e2d23 72 time(&t);
a82aa56a
SL
73 if (chdir(RWHODIR) < 0) {
74 perror(RWHODIR);
be0e2d23
BJ
75 exit(1);
76 }
b9484359
RC
77 dirp = opendir(".");
78 if (dirp == NULL) {
79 perror(RWHODIR);
be0e2d23
BJ
80 exit(1);
81 }
b9484359 82 while (dp = readdir(dirp)) {
be0e2d23
BJ
83 if (dp->d_ino == 0)
84 continue;
85 if (strncmp(dp->d_name, "whod.", 5))
86 continue;
87 if (nhosts == NHOSTS) {
88 fprintf(stderr, "too many hosts\n");
89 exit(1);
90 }
91 f = open(dp->d_name, 0);
92 if (f > 0) {
93 cc = read(f, buf, BUFSIZ);
94 if (cc >= WHDRSIZE) {
95 hsp->hs_wd = (struct whod *)malloc(WHDRSIZE);
96 wd = (struct whod *)buf;
97 bcopy(buf, hsp->hs_wd, WHDRSIZE);
98 hsp->hs_nusers = 0;
acf8f1b2
BJ
99 for (i = 0; i < 2; i++)
100 if (wd->wd_loadav[i] > maxloadav)
101 maxloadav = wd->wd_loadav[i];
be0e2d23
BJ
102 we = (struct whoent *)(buf+cc);
103 while (--we >= wd->wd_we)
104 if (aflg || we->we_idle < 3600)
105 hsp->hs_nusers++;
106 nhosts++; hsp++;
107 }
108 }
109 (void) close(f);
110 }
be0e2d23 111 (void) time(&now);
508c25be 112 qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
be0e2d23
BJ
113 if (nhosts == 0) {
114 printf("no hosts!?!\n");
115 exit(1);
116 }
117 for (i = 0; i < nhosts; i++) {
118 hsp = &hs[i];
508c25be 119 if (down(hsp)) {
cad6a03e 120 printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
be0e2d23
BJ
121 interval(now - hsp->hs_wd->wd_recvtime, "down"));
122 continue;
123 }
cad6a03e 124 printf("%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
be0e2d23
BJ
125 hsp->hs_wd->wd_hostname,
126 interval(hsp->hs_wd->wd_sendtime -
de3b21e8 127 hsp->hs_wd->wd_boottime, " up"),
be0e2d23
BJ
128 hsp->hs_nusers,
129 hsp->hs_nusers == 1 ? ", " : "s,",
acf8f1b2
BJ
130 maxloadav >= 1000 ? 5 : 4,
131 hsp->hs_wd->wd_loadav[0] / 100.0,
132 maxloadav >= 1000 ? 5 : 4,
133 hsp->hs_wd->wd_loadav[1] / 100.0,
134 maxloadav >= 1000 ? 5 : 4,
135 hsp->hs_wd->wd_loadav[2] / 100.0);
be0e2d23
BJ
136 cfree(hsp->hs_wd);
137 }
138 exit(0);
139}
140
141char *
142interval(time, updown)
143 int time;
144 char *updown;
145{
146 static char resbuf[32];
147 int days, hours, minutes;
148
149 if (time < 0 || time > 3*30*24*60*60) {
150 (void) sprintf(resbuf, " %s ??:??", updown);
151 return (resbuf);
152 }
153 minutes = (time + 59) / 60; /* round to minutes */
154 hours = minutes / 60; minutes %= 60;
155 days = hours / 24; hours %= 24;
156 if (days)
157 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
158 updown, days, hours, minutes);
159 else
1518ea81 160 (void) sprintf(resbuf, "%s %2d:%02d",
be0e2d23
BJ
161 updown, hours, minutes);
162 return (resbuf);
163}
164
165hscmp(h1, h2)
166 struct hs *h1, *h2;
167{
168
44bbb118 169 return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
be0e2d23 170}
508c25be
EC
171
172/*
173 * Compare according to load average.
174 */
175lcmp(h1, h2)
176 struct hs *h1, *h2;
177{
178
179 if (down(h1))
180 if (down(h2))
181 return (tcmp(h1, h2));
182 else
44bbb118 183 return (rflg);
508c25be 184 else if (down(h2))
44bbb118 185 return (-rflg);
508c25be 186 else
44bbb118
RC
187 return (rflg *
188 (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
508c25be
EC
189}
190
191/*
192 * Compare according to number of users.
193 */
194ucmp(h1, h2)
195 struct hs *h1, *h2;
196{
197
198 if (down(h1))
199 if (down(h2))
200 return (tcmp(h1, h2));
201 else
44bbb118 202 return (rflg);
508c25be 203 else if (down(h2))
44bbb118 204 return (-rflg);
508c25be 205 else
44bbb118 206 return (rflg * (h2->hs_nusers - h1->hs_nusers));
508c25be
EC
207}
208
209/*
210 * Compare according to uptime.
211 */
212tcmp(h1, h2)
213 struct hs *h1, *h2;
214{
215 long t1, t2;
216
44bbb118 217 return (rflg * (
508c25be 218 (down(h2) ? h2->hs_wd->wd_recvtime - now
de3b21e8 219 : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
508c25be
EC
220 -
221 (down(h1) ? h1->hs_wd->wd_recvtime - now
de3b21e8 222 : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
44bbb118 223 ));
508c25be 224}