Deleted Added
full compact
ruptime.c (331722) ruptime.c (359798)
1/*
2 * Copyright (c) 1983, 1993, 1994
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

--- 24 unchanged lines hidden (view full) ---

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
38#endif /* not lint */
39
40#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1983, 1993, 1994
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

--- 24 unchanged lines hidden (view full) ---

33 The Regents of the University of California. All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
38#endif /* not lint */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/usr.bin/ruptime/ruptime.c 331722 2018-03-29 02:50:57Z eadler $");
41__FBSDID("$FreeBSD: stable/11/usr.bin/ruptime/ruptime.c 359798 2020-04-11 07:37:10Z nyan $");
42
43#include <sys/param.h>
44
45#include <protocols/rwhod.h>
46
47#include <dirent.h>
48#include <err.h>
49#include <errno.h>

--- 14 unchanged lines hidden (view full) ---

64
65static size_t nhosts;
66static time_t now;
67static int rflg = 1;
68static DIR *dirp;
69
70static int hscmp(const void *, const void *);
71static char *interval(time_t, const char *);
42
43#include <sys/param.h>
44
45#include <protocols/rwhod.h>
46
47#include <dirent.h>
48#include <err.h>
49#include <errno.h>

--- 14 unchanged lines hidden (view full) ---

64
65static size_t nhosts;
66static time_t now;
67static int rflg = 1;
68static DIR *dirp;
69
70static int hscmp(const void *, const void *);
71static char *interval(time_t, const char *);
72static int iwidth(int);
72static int lcmp(const void *, const void *);
73static void ruptime(const char *, int, int (*)(const void *, const void *));
74static int tcmp(const void *, const void *);
75static int ucmp(const void *, const void *);
76static void usage(void);
77
78int
79main(int argc, char *argv[])

--- 58 unchanged lines hidden (view full) ---

138 (void)snprintf(resbuf, sizeof(resbuf),
139 "%s %4d+%02d:%02d", updown, days, hours, minutes);
140 else
141 (void)snprintf(resbuf, sizeof(resbuf),
142 "%s %2d:%02d", updown, hours, minutes);
143 return (resbuf);
144}
145
73static int lcmp(const void *, const void *);
74static void ruptime(const char *, int, int (*)(const void *, const void *));
75static int tcmp(const void *, const void *);
76static int ucmp(const void *, const void *);
77static void usage(void);
78
79int
80main(int argc, char *argv[])

--- 58 unchanged lines hidden (view full) ---

139 (void)snprintf(resbuf, sizeof(resbuf),
140 "%s %4d+%02d:%02d", updown, days, hours, minutes);
141 else
142 (void)snprintf(resbuf, sizeof(resbuf),
143 "%s %2d:%02d", updown, hours, minutes);
144 return (resbuf);
145}
146
147/* Width to print a small nonnegative integer. */
148static int
149iwidth(int w)
150{
151 if (w < 10)
152 return (1);
153 if (w < 100)
154 return (2);
155 if (w < 1000)
156 return (3);
157 if (w < 10000)
158 return (4);
159 return (5);
160}
161
146#define HS(a) ((const struct hs *)(a))
147
148/* Alphabetical comparison. */
149static int
150hscmp(const void *a1, const void *a2)
151{
152 return (rflg *
153 strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));

--- 17 unchanged lines hidden (view full) ---

171
172static void
173ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
174{
175 struct hs *hsp;
176 struct whod *wd;
177 struct whoent *we;
178 struct dirent *dp;
162#define HS(a) ((const struct hs *)(a))
163
164/* Alphabetical comparison. */
165static int
166hscmp(const void *a1, const void *a2)
167{
168 return (rflg *
169 strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));

--- 17 unchanged lines hidden (view full) ---

187
188static void
189ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
190{
191 struct hs *hsp;
192 struct whod *wd;
193 struct whoent *we;
194 struct dirent *dp;
179 const char *hostname;
180 int fd, i, maxloadav;
195 int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
181 size_t hspace;
182 ssize_t cc;
183
184 rewinddir(dirp);
185 hsp = NULL;
196 size_t hspace;
197 ssize_t cc;
198
199 rewinddir(dirp);
200 hsp = NULL;
186 maxloadav = -1;
201 hostnamewidth = 0;
202 loadavwidth[0] = 4;
203 loadavwidth[1] = 4;
204 loadavwidth[2] = 4;
205 userswidth = 1;
187 (void)time(&now);
188 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
189 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
190 continue;
191 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
192 warn("%s", dp->d_name);
193 continue;
194 }

--- 6 unchanged lines hidden (view full) ---

201 }
202
203 wd = &hsp->hs_wd;
204 cc = read(fd, wd, sizeof(*wd));
205 (void)close(fd);
206 if (cc < (ssize_t)WHDRSIZE)
207 continue;
208
206 (void)time(&now);
207 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
208 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
209 continue;
210 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
211 warn("%s", dp->d_name);
212 continue;
213 }

--- 6 unchanged lines hidden (view full) ---

220 }
221
222 wd = &hsp->hs_wd;
223 cc = read(fd, wd, sizeof(*wd));
224 (void)close(fd);
225 if (cc < (ssize_t)WHDRSIZE)
226 continue;
227
209 if (host != NULL) {
210 hostname = wd->wd_hostname;
211 if (strcasecmp(hostname, host) != 0)
212 continue;
213 }
228 if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
229 continue;
214 if (LEFTEARTH(wd->wd_recvtime))
215 continue;
216
230 if (LEFTEARTH(wd->wd_recvtime))
231 continue;
232
217 for (i = 0; i < 2; i++)
218 if (wd->wd_loadav[i] > maxloadav)
219 maxloadav = wd->wd_loadav[i];
233 if (hostnamewidth < (int)strlen(wd->wd_hostname))
234 hostnamewidth = (int)strlen(wd->wd_hostname);
235 for (i = 0; i < 3; i++) {
236 w = iwidth(wd->wd_loadav[i] / 100) + 3;
237 if (loadavwidth[i] < w)
238 loadavwidth[i] = w;
239 }
220
221 for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
222 (char *)(we + 1) <= (char *)wd + cc; we++)
223 if (aflg || we->we_idle < 3600)
224 ++hsp->hs_nusers;
240
241 for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
242 (char *)(we + 1) <= (char *)wd + cc; we++)
243 if (aflg || we->we_idle < 3600)
244 ++hsp->hs_nusers;
245 if (userswidth < iwidth(hsp->hs_nusers))
246 userswidth = iwidth(hsp->hs_nusers);
225 ++hsp;
226 ++nhosts;
227 }
228 if (nhosts == 0) {
229 if (host == NULL)
230 errx(1, "no hosts in %s", _PATH_RWHODIR);
231 else
232 warnx("host %s not in %s", host, _PATH_RWHODIR);
233 }
234
235 qsort(hs, nhosts, sizeof(hs[0]), cmp);
247 ++hsp;
248 ++nhosts;
249 }
250 if (nhosts == 0) {
251 if (host == NULL)
252 errx(1, "no hosts in %s", _PATH_RWHODIR);
253 else
254 warnx("host %s not in %s", host, _PATH_RWHODIR);
255 }
256
257 qsort(hs, nhosts, sizeof(hs[0]), cmp);
258 w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
259 if (hostnamewidth + w > 41)
260 hostnamewidth = 41 - w; /* limit to 79 cols */
236 for (i = 0; i < (int)nhosts; i++) {
237 hsp = &hs[i];
238 wd = &hsp->hs_wd;
239 if (ISDOWN(hsp)) {
261 for (i = 0; i < (int)nhosts; i++) {
262 hsp = &hs[i];
263 wd = &hsp->hs_wd;
264 if (ISDOWN(hsp)) {
240 (void)printf("%-25.25s%s\n", wd->wd_hostname,
265 (void)printf("%-*.*s%s\n",
266 hostnamewidth, hostnamewidth, wd->wd_hostname,
241 interval(now - hsp->hs_wd.wd_recvtime, "down"));
242 continue;
243 }
244 (void)printf(
267 interval(now - hsp->hs_wd.wd_recvtime, "down"));
268 continue;
269 }
270 (void)printf(
245 "%-25.25s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
246 wd->wd_hostname,
271 "%-*.*s %s, %*d user%s load %*.2f, %*.2f, %*.2f\n",
272 hostnamewidth, hostnamewidth, wd->wd_hostname,
247 interval((time_t)wd->wd_sendtime -
248 (time_t)wd->wd_boottime, " up"),
273 interval((time_t)wd->wd_sendtime -
274 (time_t)wd->wd_boottime, " up"),
249 hsp->hs_nusers,
275 userswidth, hsp->hs_nusers,
250 hsp->hs_nusers == 1 ? ", " : "s,",
276 hsp->hs_nusers == 1 ? ", " : "s,",
251 maxloadav >= 1000 ? 5 : 4,
252 wd->wd_loadav[0] / 100.0,
253 maxloadav >= 1000 ? 5 : 4,
254 wd->wd_loadav[1] / 100.0,
255 maxloadav >= 1000 ? 5 : 4,
256 wd->wd_loadav[2] / 100.0);
277 loadavwidth[0], wd->wd_loadav[0] / 100.0,
278 loadavwidth[1], wd->wd_loadav[1] / 100.0,
279 loadavwidth[2], wd->wd_loadav[2] / 100.0);
257 }
258 free(hs);
259 hs = NULL;
260}
261
262/* Number of users comparison. */
263static int
264ucmp(const void *a1, const void *a2)

--- 31 unchanged lines hidden ---
280 }
281 free(hs);
282 hs = NULL;
283}
284
285/* Number of users comparison. */
286static int
287ucmp(const void *a1, const void *a2)

--- 31 unchanged lines hidden ---