No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / ruptime / ruptime.c
blob666b9f807e03280847af664deaf5c7d3216657d8
1 /* $NetBSD: ruptime.c,v 1.13 2008/07/21 14:19:25 lukem Exp $ */
3 /*
4 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 /*static char sccsid[] = "from: @(#)ruptime.c 8.2 (Berkeley) 4/5/94";*/
40 __RCSID("$NetBSD: ruptime.c,v 1.13 2008/07/21 14:19:25 lukem Exp $");
41 #endif /* not lint */
43 #include <sys/param.h>
45 #include <protocols/rwhod.h>
47 #include <dirent.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <tzfile.h>
56 #include <unistd.h>
58 struct hs {
59 struct whod *hs_wd;
60 int hs_nusers;
61 } *hs;
63 #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
64 #define WHDRSIZE (sizeof(struct whod) - \
65 sizeof (((struct whod *)0)->wd_we))
67 size_t nhosts;
68 time_t now;
69 int rflg = 1;
71 int hscmp(const void *, const void *);
72 char *interval(time_t, const char *);
73 int lcmp(const void *, const void *);
74 int main(int, char **);
75 void morehosts(void);
76 int tcmp(const void *, const void *);
77 int ucmp(const void *, const void *);
78 void usage(void);
80 int
81 main(int argc, char **argv)
83 struct dirent *dp;
84 struct hs *hsp;
85 struct whod *wd;
86 struct whoent *we;
87 DIR *dirp;
88 size_t hspace, i;
89 int aflg, cc, ch, fd, maxloadav;
90 char buf[sizeof(struct whod)];
91 int (*cmp)(const void *, const void *);
93 hsp = NULL;
94 aflg = 0;
95 cmp = hscmp;
96 while ((ch = getopt(argc, argv, "alrtu")) != -1)
97 switch (ch) {
98 case 'a':
99 aflg = 1;
100 break;
101 case 'l':
102 cmp = lcmp;
103 break;
104 case 'r':
105 rflg = -1;
106 break;
107 case 't':
108 cmp = tcmp;
109 break;
110 case 'u':
111 cmp = ucmp;
112 break;
113 default:
114 usage();
116 argc -= optind;
117 argv += optind;
119 if (argc != 0)
120 usage();
122 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
123 err(1, "%s", _PATH_RWHODIR);
125 maxloadav = -1;
126 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
127 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
128 continue;
129 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
130 warn("%s", dp->d_name);
131 continue;
133 cc = read(fd, buf, sizeof(struct whod));
134 (void)close(fd);
136 if (cc < (int)WHDRSIZE)
137 continue;
138 if (nhosts == hspace) {
139 if ((hs =
140 realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
141 err(1, "realloc");
142 hsp = hs + nhosts;
145 if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
146 err(1, "malloc");
147 memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
149 for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
150 if (wd->wd_loadav[i] > maxloadav)
151 maxloadav = wd->wd_loadav[i];
153 for (hsp->hs_nusers = 0,
154 we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
155 if (aflg || we->we_idle < 3600)
156 ++hsp->hs_nusers;
157 ++hsp;
158 ++nhosts;
160 if (nhosts == 0)
161 errx(0, "no hosts in %s", _PATH_RWHODIR);
163 (void)time(&now);
164 qsort(hs, nhosts, sizeof (hs[0]), cmp);
165 for (i = 0; i < nhosts; i++) {
166 hsp = &hs[i];
167 if (ISDOWN(hsp)) {
168 (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
169 interval(now - hsp->hs_wd->wd_recvtime, "down"));
170 continue;
172 (void)printf(
173 "%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
174 hsp->hs_wd->wd_hostname,
175 interval((time_t)hsp->hs_wd->wd_sendtime -
176 (time_t)hsp->hs_wd->wd_boottime, " up"),
177 hsp->hs_nusers,
178 hsp->hs_nusers == 1 ? ", " : "s,",
179 maxloadav >= 1000 ? 5 : 4,
180 hsp->hs_wd->wd_loadav[0] / 100.0,
181 maxloadav >= 1000 ? 5 : 4,
182 hsp->hs_wd->wd_loadav[1] / 100.0,
183 maxloadav >= 1000 ? 5 : 4,
184 hsp->hs_wd->wd_loadav[2] / 100.0);
186 exit(0);
189 char *
190 interval(time_t tval, const char *updown)
192 static char resbuf[32];
193 int days, hours, minutes;
195 if (tval < 0) {
196 (void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
197 return (resbuf);
199 /* round to minutes. */
200 minutes = (tval + (SECSPERMIN - 1)) / SECSPERMIN;
201 hours = minutes / MINSPERHOUR;
202 minutes %= MINSPERHOUR;
203 days = hours / HOURSPERDAY;
204 hours %= HOURSPERDAY;
205 if (days)
206 (void)snprintf(resbuf, sizeof(resbuf),
207 "%s%4d+%02d:%02d", updown, days, hours, minutes);
208 else
209 (void)snprintf(resbuf, sizeof(resbuf),
210 "%s %2d:%02d", updown, hours, minutes);
211 return (resbuf);
214 #define HS(a) ((const struct hs *)(a))
216 /* Alphabetical comparison. */
218 hscmp(a1, a2)
219 const void *a1, *a2;
221 return (rflg *
222 strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
225 /* Load average comparison. */
227 lcmp(const void *a1, const void *a2)
229 if (ISDOWN(HS(a1))) {
230 if (ISDOWN(HS(a2)))
231 return (tcmp(a1, a2));
232 else
233 return (rflg);
234 } else if (ISDOWN(HS(a2))) {
235 return (-rflg);
236 } else
237 return (rflg *
238 (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
241 /* Number of users comparison. */
243 ucmp(const void *a1, const void *a2)
245 if (ISDOWN(HS(a1))) {
246 if (ISDOWN(HS(a2)))
247 return (tcmp(a1, a2));
248 else
249 return (rflg);
250 } else if (ISDOWN(HS(a2))) {
251 return (-rflg);
252 } else
253 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
256 /* Uptime comparison. */
258 tcmp(const void *a1, const void *a2)
260 return (rflg * (
261 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
262 : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
264 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
265 : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
269 void
270 usage(void)
272 (void)fprintf(stderr, "usage: ruptime [-alrtu]\n");
273 exit(1);