typo
[unix-history] / usr / src / usr.bin / ruptime / ruptime.c
index 3945c5f..1c2e737 100644 (file)
@@ -1,14 +1,13 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ruptime.c  4.4 82/05/09";
+static char sccsid[] = "@(#)ruptime.c  4.16 (Berkeley) 83/11/17";
 #endif
 
 #include <sys/param.h>
 #include <stdio.h>
 #endif
 
 #include <sys/param.h>
 #include <stdio.h>
-#include <dir.h>
-#include <utmp.h>
-#include "rwhod.h"
+#include <sys/dir.h>
+#include "../etc/rwhod/rwhod.h"
 
 
-DIR    *etc;
+DIR    *dirp;
 
 #define        NHOSTS  100
 int    nhosts;
 
 #define        NHOSTS  100
 int    nhosts;
@@ -17,14 +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;
+int    rflg = 1;
+
+#define down(h)                (now - (h)->hs_wd->wd_recvtime > 11 * 60)
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -33,29 +36,50 @@ main(argc, argv)
        struct direct *dp;
        int f, i, t;
        char buf[BUFSIZ]; int cc;
        struct direct *dp;
        int f, i, t;
        char buf[BUFSIZ]; int cc;
+       char *name;
        register struct hs *hsp = hs;
        register struct whod *wd;
        register struct whoent *we;
        int maxloadav = 0;
        register struct hs *hsp = hs;
        register struct whod *wd;
        register struct whoent *we;
        int maxloadav = 0;
+       int (*cmp)() = hscmp;
 
 
+       name = *argv;
+       while (*++argv) 
+               while (**argv)
+                       switch (*(*argv)++) {
+                       case 'a':
+                               aflg++;
+                               break;
+                       case 'l':
+                               cmp = lcmp;
+                               break;
+                       case 'u':
+                               cmp = ucmp;
+                               break;
+                       case 't':
+                               cmp = tcmp;
+                               break;
+                       case 'r':
+                               rflg = -rflg;
+                               break;
+                       case '-':
+                               break;
+                       default: 
+                               fprintf(stderr, "Usage: %s [ -ar [ lut ] ]\n",
+                                       name);
+                               exit (1);
+                       }
        time(&t);
        time(&t);
-       argc--, argv++;
-again:
-       if (!strcmp(*argv, "-a")) {
-               aflg++;
-               argc--, argv++;
-               goto again;
-       }
-       if (chdir("/etc") < 0) {
-               perror("/etc");
+       if (chdir(RWHODIR) < 0) {
+               perror(RWHODIR);
                exit(1);
        }
                exit(1);
        }
-       etc = opendir(".");
-       if (etc == NULL) {
-               perror("/etc");
+       dirp = opendir(".");
+       if (dirp == NULL) {
+               perror(RWHODIR);
                exit(1);
        }
                exit(1);
        }
-       while (dp = readdir(etc)) {
+       while (dp = readdir(dirp)) {
                if (dp->d_ino == 0)
                        continue;
                if (strncmp(dp->d_name, "whod.", 5))
                if (dp->d_ino == 0)
                        continue;
                if (strncmp(dp->d_name, "whod.", 5))
@@ -84,23 +108,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 +157,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);
 }
@@ -142,5 +166,59 @@ hscmp(h1, h2)
        struct hs *h1, *h2;
 {
 
        struct hs *h1, *h2;
 {
 
-       return (strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
+       return (rflg * 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 (rflg);
+       else if (down(h2))
+               return (-rflg);
+       else
+               return (rflg * 
+                       (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 (rflg);
+       else if (down(h2))
+               return (-rflg);
+       else
+               return (rflg * (h2->hs_nusers - h1->hs_nusers));
+}
+
+/*
+ * Compare according to uptime.
+ */
+tcmp(h1, h2)
+       struct hs *h1, *h2;
+{
+       long t1, t2;
+
+       return (rflg * (
+               (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)
+       ));
 }
 }