hosts which are down were sorted incorrectly
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 6 Apr 1994 04:51:45 +0000 (20:51 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 6 Apr 1994 04:51:45 +0000 (20:51 -0800)
From: "Chris G. Demetriou" <cgd@postgres.Berkeley.EDU>
prettiness police

SCCS-vsn: usr.bin/ruptime/ruptime.c 8.2

usr/src/usr.bin/ruptime/ruptime.c

index dcb2d11..7528e7f 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 1983, 1993
+ * Copyright (c) 1983, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
@@ -7,24 +7,29 @@
 
 #ifndef lint
 static char copyright[] =
 
 #ifndef lint
 static char copyright[] =
-"@(#) Copyright (c) 1983, 1993\n\
+"@(#) Copyright (c) 1983, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ruptime.c  8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)ruptime.c  8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
-#include <sys/dir.h>
-#include <sys/file.h>
-#include <sys/errno.h>
+
 #include <protocols/rwhod.h>
 #include <protocols/rwhod.h>
+
+#include <dirent.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
+#include <tzfile.h>
+#include <unistd.h>
 
 
-size_t nhosts, hspace = 20;
 struct hs {
        struct  whod *hs_wd;
        int     hs_nusers;
 struct hs {
        struct  whod *hs_wd;
        int     hs_nusers;
@@ -34,30 +39,38 @@ struct      whod awhod;
 #define        ISDOWN(h)               (now - (h)->hs_wd->wd_recvtime > 11 * 60)
 #define        WHDRSIZE        (sizeof (awhod) - sizeof (awhod.wd_we))
 
 #define        ISDOWN(h)               (now - (h)->hs_wd->wd_recvtime > 11 * 60)
 #define        WHDRSIZE        (sizeof (awhod) - sizeof (awhod.wd_we))
 
+size_t nhosts;
 time_t now;
 int rflg = 1;
 time_t now;
 int rflg = 1;
-int hscmp(), ucmp(), lcmp(), tcmp();
 
 
+int     hscmp __P((const void *, const void *));
+char   *interval __P((time_t, char *));
+int     lcmp __P((const void *, const void *));
+void    morehosts __P((void));
+int     tcmp __P((const void *, const void *));
+int     ucmp __P((const void *, const void *));
+void    usage __P((void));
+
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       extern char *optarg;
        extern int optind;
        extern int optind;
-       register struct hs *hsp;
-       register struct whod *wd;
-       register struct whoent *we;
-       register DIR *dirp;
-       struct direct *dp;
-       int aflg, cc, ch, f, i, maxloadav;
+       struct dirent *dp;
+       struct hs *hsp;
+       struct whod *wd;
+       struct whoent *we;
+       DIR *dirp;
+       size_t hspace;
+       int aflg, cc, ch, fd, i, maxloadav;
        char buf[sizeof(struct whod)];
        char buf[sizeof(struct whod)];
-       int (*cmp)() = hscmp;
-       time_t time();
-       char *interval();
+       int (*cmp) __P((const void *, const void *));
 
        aflg = 0;
 
        aflg = 0;
+       cmp = hscmp;
        while ((ch = getopt(argc, argv, "alrut")) != EOF)
        while ((ch = getopt(argc, argv, "alrut")) != EOF)
-               switch((char)ch) {
+               switch (ch) {
                case 'a':
                        aflg = 1;
                        break;
                case 'a':
                        aflg = 1;
                        break;
@@ -74,55 +87,57 @@ main(argc, argv)
                        cmp = ucmp;
                        break;
                default: 
                        cmp = ucmp;
                        break;
                default: 
-                       (void)fprintf(stderr, "usage: ruptime [-alrut]\n");
-                       exit(1);
+                       usage();
                }
                }
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 0)
+               usage();
+
+       if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
+               err(1, "%s", _PATH_RWHODIR);
 
 
-       if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
-               (void)fprintf(stderr, "ruptime: %s: %s.\n",
-                   _PATH_RWHODIR, strerror(errno));
-               exit(1);
-       }
-       morehosts();
-       hsp = hs;
        maxloadav = -1;
        maxloadav = -1;
-       while (dp = readdir(dirp)) {
+       for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
                if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
                        continue;
                if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
                        continue;
-               if ((f = open(dp->d_name, O_RDONLY, 0)) < 0) {
-                       (void)fprintf(stderr, "ruptime: %s: %s\n",
-                           dp->d_name, strerror(errno));
+               if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
+                       warn("%s", dp->d_name);
                        continue;
                }
                        continue;
                }
-               cc = read(f, buf, sizeof(struct whod));
-               (void)close(f);
+               cc = read(fd, buf, sizeof(struct whod));
+               (void)close(fd);
+
                if (cc < WHDRSIZE)
                        continue;
                if (nhosts == hspace) {
                if (cc < WHDRSIZE)
                        continue;
                if (nhosts == hspace) {
-                       morehosts();
+                       if ((hs =
+                           realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
+                               err(1, NULL);
                        hsp = hs + nhosts;
                }
                        hsp = hs + nhosts;
                }
-               /* NOSTRICT */
-               hsp->hs_wd = malloc((size_t)WHDRSIZE);
-               wd = (struct whod *)buf;
-               bcopy((char *)wd, (char *)hsp->hs_wd, (size_t)WHDRSIZE);
-               hsp->hs_nusers = 0;
-               for (i = 0; i < 2; i++)
+
+               if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
+                       err(1, NULL);
+               memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
+
+               for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
                        if (wd->wd_loadav[i] > maxloadav)
                                maxloadav = wd->wd_loadav[i];
                        if (wd->wd_loadav[i] > maxloadav)
                                maxloadav = wd->wd_loadav[i];
-               we = (struct whoent *)(buf+cc);
-               while (--we >= wd->wd_we)
+
+               for (hsp->hs_nusers = 0,
+                   we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
                        if (aflg || we->we_idle < 3600)
                        if (aflg || we->we_idle < 3600)
-                               hsp->hs_nusers++;
-               nhosts++;
-               hsp++;
-       }
-       if (!nhosts) {
-               (void)printf("ruptime: no hosts in %s.\n", _PATH_RWHODIR);
-               exit(1);
+                               ++hsp->hs_nusers;
+               ++hsp;
+               ++nhosts;
        }
        }
-       qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
+       if (nhosts == 0)
+               errx(0, "no hosts in %s.", _PATH_RWHODIR);
+
        (void)time(&now);
        (void)time(&now);
+       qsort(hs, nhosts, sizeof(hs[0]), cmp);
        for (i = 0; i < nhosts; i++) {
                hsp = &hs[i];
                if (ISDOWN(hsp)) {
        for (i = 0; i < nhosts; i++) {
                hsp = &hs[i];
                if (ISDOWN(hsp)) {
@@ -143,7 +158,6 @@ main(argc, argv)
                        hsp->hs_wd->wd_loadav[1] / 100.0,
                    maxloadav >= 1000 ? 5 : 4,
                        hsp->hs_wd->wd_loadav[2] / 100.0);
                        hsp->hs_wd->wd_loadav[1] / 100.0,
                    maxloadav >= 1000 ? 5 : 4,
                        hsp->hs_wd->wd_loadav[2] / 100.0);
-               free((void *)hsp->hs_wd);
        }
        exit(0);
 }
        }
        exit(0);
 }
@@ -156,86 +170,86 @@ interval(tval, updown)
        static char resbuf[32];
        int days, hours, minutes;
 
        static char resbuf[32];
        int days, hours, minutes;
 
-       if (tval < 0 || tval > 365*24*60*60) {
-               (void)sprintf(resbuf, "   %s ??:??", updown);
-               return(resbuf);
+       if (tval < 0 || tval > DAYSPERNYEAR * SECSPERDAY) {
+               (void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
+               return (resbuf);
        }
        }
-       minutes = (tval + 59) / 60;             /* round to minutes */
-       hours = minutes / 60; minutes %= 60;
-       days = hours / 24; hours %= 24;
+                                               /* round to minutes. */
+       minutes = (tval + (SECSPERMIN - 1)) / SECSPERMIN;
+       hours = minutes / MINSPERHOUR;
+       minutes %= MINSPERHOUR;
+       days = hours / HOURSPERDAY;
+       hours %= HOURSPERDAY;
        if (days)
        if (days)
-               (void)sprintf(resbuf, "%s %2d+%02d:%02d",
-                   updown, days, hours, minutes);
+               (void)snprintf(resbuf, sizeof(resbuf),
+                   "%s %2d+%02d:%02d", updown, days, hours, minutes);
        else
        else
-               (void)sprintf(resbuf, "%s    %2d:%02d",
-                   updown, hours, minutes);
-       return(resbuf);
+               (void)snprintf(resbuf, sizeof(resbuf),
+                   "%s    %2d:%02d", updown, hours, minutes);
+       return (resbuf);
 }
 
 }
 
-/* alphabetical comparison */
+#define        HS(a)   ((struct hs *)(a))
+
+/* Alphabetical comparison. */
+int
 hscmp(a1, a2)
 hscmp(a1, a2)
-       void *a1, *a2;
+       const void *a1, *a2;
 {
 {
-       struct hs *h1 = a1, *h2 = a2;
-
-       return(rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
+       return (rflg *
+           strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
 }
 
 }
 
-/* load average comparison */
+/* Load average comparison. */
+int
 lcmp(a1, a2)
 lcmp(a1, a2)
-       void *a1, *a2;
+       const void *a1, *a2;
 {
 {
-       register struct hs *h1 = a1, *h2 = a2;
-
-       if (ISDOWN(h1))
-               if (ISDOWN(h2))
-                       return(tcmp(a1, a2));
+       if (ISDOWN(HS(a1)))
+               if (ISDOWN(HS(a2)))
+                       return (tcmp(a1, a2));
                else
                else
-                       return(rflg);
-       else if (ISDOWN(h2))
-               return(-rflg);
+                       return (rflg);
+       else if (ISDOWN(HS(a2)))
+               return (-rflg);
        else
        else
-               return(rflg *
-                       (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
+               return (rflg *
+                  (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
 }
 
 }
 
-/* number of users comparison */
+/* Number of users comparison. */
+int
 ucmp(a1, a2)
 ucmp(a1, a2)
-       void *a1, *a2;
+       const void *a1, *a2;
 {
 {
-       register struct hs *h1 = a1, *h2 = a2;
-
-       if (ISDOWN(h1))
-               if (ISDOWN(h2))
-                       return(tcmp(a1, a2));
+       if (ISDOWN(HS(a1)))
+               if (ISDOWN(HS(a2)))
+                       return (tcmp(a1, a2));
                else
                else
-                       return(rflg);
-       else if (ISDOWN(h2))
-               return(-rflg);
+                       return (rflg);
+       else if (ISDOWN(HS(a2)))
+               return (-rflg);
        else
        else
-               return(rflg * (h2->hs_nusers - h1->hs_nusers));
+               return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
 }
 
 }
 
-/* uptime comparison */
+/* Uptime comparison. */
+int
 tcmp(a1, a2)
 tcmp(a1, a2)
-       void *a1, *a2;
+       const void *a1, *a2;
 {
 {
-       register struct hs *h1 = a1, *h2 = a2;
-
-       return(rflg * (
-               (ISDOWN(h2) ? h2->hs_wd->wd_recvtime - now
-                         : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
+       return (rflg * (
+               (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
+                   : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
                -
                -
-               (ISDOWN(h1) ? h1->hs_wd->wd_recvtime - now
-                         : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
+               (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
+                   : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
        ));
 }
 
        ));
 }
 
-morehosts()
+void
+usage()
 {
 {
-       hs = realloc((char *)hs, (hspace *= 2) * sizeof(*hs));
-       if (hs == NULL) {
-               (void)fprintf(stderr, "ruptime: %s.\n", strerror(ENOMEM));
-               exit(1);
-       }
+       (void)fprintf(stderr, "usage: ruptime [-alrut]\n");
+       exit(1);
 }
 }