include aculib in cleanup
[unix-history] / usr / src / usr.bin / ruptime / ruptime.c
index 3945c5f..a465932 100644 (file)
@@ -1,12 +1,11 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ruptime.c  4.4 82/05/09";
+static char sccsid[] = "@(#)ruptime.c  4.12 (Berkeley) 83/05/25";
 #endif
 
 #include <sys/param.h>
 #include <stdio.h>
 #include <dir.h>
 #endif
 
 #include <sys/param.h>
 #include <stdio.h>
 #include <dir.h>
-#include <utmp.h>
-#include "rwhod.h"
+#include <rwhod.h>
 
 DIR    *etc;
 
 
 DIR    *etc;
 
@@ -17,15 +16,18 @@ struct      hs {
        int     hs_nusers;
 } hs[NHOSTS];
 struct whod awhod;
        int     hs_nusers;
 } hs[NHOSTS];
 struct whod awhod;
-int    hscmp();
+int    hscmp(), ucmp(), lcmp(), tcmp();
 
 #define        WHDRSIZE        (sizeof (awhod) - sizeof (awhod.wd_we))
 
 #define        WHDRSIZE        (sizeof (awhod) - sizeof (awhod.wd_we))
+#define        RWHODIR         "/usr/spool/rwho"
 
 char   *interval();
 int    now;
 char   *malloc(), *sprintf();
 int    aflg;
 
 
 char   *interval();
 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;
 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;
        register struct whod *wd;
        register struct whoent *we;
        int maxloadav = 0;
+       int (*cmp)() = hscmp;
 
        time(&t);
        argc--, argv++;
 
        time(&t);
        argc--, argv++;
@@ -46,8 +49,23 @@ again:
                argc--, argv++;
                goto again;
        }
                argc--, argv++;
                goto again;
        }
-       if (chdir("/etc") < 0) {
-               perror("/etc");
+       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(RWHODIR) < 0) {
+               perror(RWHODIR);
                exit(1);
        }
        etc = opendir(".");
                exit(1);
        }
        etc = opendir(".");
@@ -84,23 +102,23 @@ again:
                }
                (void) close(f);
        }
                }
                (void) close(f);
        }
-       qsort((char *)hs, nhosts, sizeof (hs[0]), hscmp);
        (void) time(&now);
        (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 (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) {
-                       printf("%-8.8s%s\n", hsp->hs_wd->wd_hostname,
+               if (down(hsp)) {
+                       printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
                            interval(now - hsp->hs_wd->wd_recvtime, "down"));
                        continue;
                }
                            interval(now - hsp->hs_wd->wd_recvtime, "down"));
                        continue;
                }
-               printf("%-8.8s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
+               printf("%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
                    hsp->hs_wd->wd_hostname,
                    interval(hsp->hs_wd->wd_sendtime -
                    hsp->hs_wd->wd_hostname,
                    interval(hsp->hs_wd->wd_sendtime -
-                       hsp->hs_wd->wd_bootime, "  up"),
+                       hsp->hs_wd->wd_boottime, "  up"),
                    hsp->hs_nusers,
                    hsp->hs_nusers == 1 ? ", " : "s,",
                    maxloadav >= 1000 ? 5 : 4,
                    hsp->hs_nusers,
                    hsp->hs_nusers == 1 ? ", " : "s,",
                    maxloadav >= 1000 ? 5 : 4,
@@ -133,7 +151,7 @@ interval(time, updown)
                (void) sprintf(resbuf, "%s %2d+%02d:%02d",
                    updown, days, hours, minutes);
        else
                (void) sprintf(resbuf, "%s %2d+%02d:%02d",
                    updown, days, hours, minutes);
        else
-               (void) sprintf(resbuf, "   %s %2d:%02d",
+               (void) sprintf(resbuf, "%s    %2d:%02d",
                    updown, hours, minutes);
        return (resbuf);
 }
                    updown, hours, minutes);
        return (resbuf);
 }
@@ -144,3 +162,56 @@ hscmp(h1, h2)
 
        return (strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
 }
 
        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_boottime)
+               -
+               (down(h1) ? h1->hs_wd->wd_recvtime - now
+                         : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
+       );
+}