From 508c25be083c2938e922eec16fee9c9891d8cf72 Mon Sep 17 00:00:00 2001 From: "Eric C. Cooper" Date: Thu, 7 Oct 1982 23:30:17 -0800 Subject: [PATCH] added additional sorting options SCCS-vsn: usr.bin/ruptime/ruptime.c 4.5 --- usr/src/usr.bin/ruptime/ruptime.c | 79 +++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/usr/src/usr.bin/ruptime/ruptime.c b/usr/src/usr.bin/ruptime/ruptime.c index 3945c5f4c4..d54ab41580 100644 --- a/usr/src/usr.bin/ruptime/ruptime.c +++ b/usr/src/usr.bin/ruptime/ruptime.c @@ -1,5 +1,5 @@ #ifndef lint -static char sccsid[] = "@(#)ruptime.c 4.4 82/05/09"; +static char sccsid[] = "@(#)ruptime.c 4.5 82/10/07"; #endif #include @@ -17,7 +17,7 @@ struct hs { int hs_nusers; } hs[NHOSTS]; struct whod awhod; -int hscmp(); +int hscmp(), ucmp(), lcmp(), tcmp(); #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we)) @@ -26,6 +26,8 @@ int now; char *malloc(), *sprintf(); int aflg; +#define down(h) (now - (h)->hs_wd->wd_recvtime > 5 * 60) + main(argc, argv) int argc; char **argv; @@ -37,6 +39,7 @@ main(argc, argv) register struct whod *wd; register struct whoent *we; int maxloadav = 0; + int (*cmp)() = hscmp; time(&t); argc--, argv++; @@ -46,6 +49,21 @@ again: argc--, argv++; goto again; } + if (!strcmp(*argv, "-l")) { + cmp = lcmp; + argc--, argv++; + goto again; + } + if (!strcmp(*argv, "-u")) { + cmp = ucmp; + argc--, argv++; + goto again; + } + if (!strcmp(*argv, "-t")) { + cmp = tcmp; + argc--, argv++; + goto again; + } if (chdir("/etc") < 0) { perror("/etc"); exit(1); @@ -84,15 +102,15 @@ again: } (void) close(f); } - qsort((char *)hs, nhosts, sizeof (hs[0]), hscmp); (void) time(&now); + qsort((char *)hs, nhosts, sizeof (hs[0]), cmp); if (nhosts == 0) { printf("no hosts!?!\n"); exit(1); } for (i = 0; i < nhosts; i++) { hsp = &hs[i]; - if (now - hsp->hs_wd->wd_recvtime > 5 * 60) { + if (down(hsp)) { printf("%-8.8s%s\n", hsp->hs_wd->wd_hostname, interval(now - hsp->hs_wd->wd_recvtime, "down")); continue; @@ -144,3 +162,56 @@ hscmp(h1, h2) return (strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname)); } + +/* + * Compare according to load average. + */ +lcmp(h1, h2) + struct hs *h1, *h2; +{ + + if (down(h1)) + if (down(h2)) + return (tcmp(h1, h2)); + else + return (1); + else if (down(h2)) + return (-1); + else + return (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]); +} + +/* + * Compare according to number of users. + */ +ucmp(h1, h2) + struct hs *h1, *h2; +{ + + if (down(h1)) + if (down(h2)) + return (tcmp(h1, h2)); + else + return (1); + else if (down(h2)) + return (-1); + else + return (h2->hs_nusers - h1->hs_nusers); +} + +/* + * Compare according to uptime. + */ +tcmp(h1, h2) + struct hs *h1, *h2; +{ + long t1, t2; + + return ( + (down(h2) ? h2->hs_wd->wd_recvtime - now + : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_bootime) + - + (down(h1) ? h1->hs_wd->wd_recvtime - now + : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_bootime) + ); +} -- 2.20.1