Add copyright
[unix-history] / usr / src / usr.bin / ruptime / ruptime.c
index 873231c..1df14ac 100644 (file)
@@ -1,14 +1,25 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ruptime.c  4.7 82/12/24";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)ruptime.c  5.1 (Berkeley) %G%";
+#endif not lint
 
 #include <sys/param.h>
 #include <stdio.h>
 
 #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;
@@ -20,13 +31,15 @@ struct      whod awhod;
 int    hscmp(), ucmp(), lcmp(), tcmp();
 
 #define        WHDRSIZE        (sizeof (awhod) - sizeof (awhod.wd_we))
 int    hscmp(), ucmp(), lcmp(), tcmp();
 
 #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 > 5 * 60)
+#define down(h)                (now - (h)->hs_wd->wd_recvtime > 11 * 60)
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -34,46 +47,51 @@ main(argc, argv)
 {
        struct direct *dp;
        int f, i, t;
 {
        struct direct *dp;
        int f, i, t;
-       char buf[BUFSIZ]; int cc;
+       char buf[sizeof(struct whod)]; int cc;
+       char *name;
        register struct hs *hsp = hs;
        register struct whod *wd;
        register struct whoent *we;
        int maxloadav = 0;
        int (*cmp)() = hscmp;
 
        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 (!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);
        }
        if (chdir(RWHODIR) < 0) {
                perror(RWHODIR);
                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,7 +102,7 @@ again:
                }
                f = open(dp->d_name, 0);
                if (f > 0) {
                }
                f = open(dp->d_name, 0);
                if (f > 0) {
-                       cc = read(f, buf, BUFSIZ);
+                       cc = read(f, buf, sizeof(struct whod));
                        if (cc >= WHDRSIZE) {
                                hsp->hs_wd = (struct whod *)malloc(WHDRSIZE);
                                wd = (struct whod *)buf;
                        if (cc >= WHDRSIZE) {
                                hsp->hs_wd = (struct whod *)malloc(WHDRSIZE);
                                wd = (struct whod *)buf;
@@ -111,11 +129,11 @@ again:
        for (i = 0; i < nhosts; i++) {
                hsp = &hs[i];
                if (down(hsp)) {
        for (i = 0; i < nhosts; i++) {
                hsp = &hs[i];
                if (down(hsp)) {
-                       printf("%-8.8s%s\n", hsp->hs_wd->wd_hostname,
+                       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_boottime, "  up"),
                    hsp->hs_wd->wd_hostname,
                    interval(hsp->hs_wd->wd_sendtime -
                        hsp->hs_wd->wd_boottime, "  up"),
@@ -151,7 +169,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);
 }
@@ -160,7 +178,7 @@ 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));
 }
 
 /*
 }
 
 /*
@@ -174,11 +192,12 @@ lcmp(h1, h2)
                if (down(h2))
                        return (tcmp(h1, h2));
                else
                if (down(h2))
                        return (tcmp(h1, h2));
                else
-                       return (1);
+                       return (rflg);
        else if (down(h2))
        else if (down(h2))
-               return (-1);
+               return (-rflg);
        else
        else
-               return (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]);
+               return (rflg * 
+                       (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
 }
 
 /*
 }
 
 /*
@@ -192,11 +211,11 @@ ucmp(h1, h2)
                if (down(h2))
                        return (tcmp(h1, h2));
                else
                if (down(h2))
                        return (tcmp(h1, h2));
                else
-                       return (1);
+                       return (rflg);
        else if (down(h2))
        else if (down(h2))
-               return (-1);
+               return (-rflg);
        else
        else
-               return (h2->hs_nusers - h1->hs_nusers);
+               return (rflg * (h2->hs_nusers - h1->hs_nusers));
 }
 
 /*
 }
 
 /*
@@ -207,11 +226,11 @@ tcmp(h1, h2)
 {
        long t1, t2;
 
 {
        long t1, t2;
 
-       return (
+       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)
                (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)
-       );
+       ));
 }
 }