fix silly indirect-through-zero bug
[unix-history] / usr / src / usr.sbin / sa / sa.c
index 337be4e..93c28e2 100644 (file)
@@ -1,6 +1,19 @@
+/*-
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.proprietary.c%
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)sa.c        4.8 (Berkeley) 84/07/18";
-#endif
+static char sccsid[] = "@(#)sa.c       4.14 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  *     Extensive modifications to internal data structures
 
 /*
  *     Extensive modifications to internal data structures
@@ -24,6 +37,7 @@ static        char *sccsid = "@(#)sa.c        4.8 (Berkeley) 84/07/18";
 #include <signal.h>
 #include <utmp.h>
 #include <pwd.h>
 #include <signal.h>
 #include <utmp.h>
 #include <pwd.h>
+#include "pathnames.h"
 
 /* interpret command time accounting */
 
 
 /* interpret command time accounting */
 
@@ -250,12 +264,15 @@ char      *getname();
 #define        SAVACCT "./savacct"
 #define        ACCT    "./acct"
 #else
 #define        SAVACCT "./savacct"
 #define        ACCT    "./acct"
 #else
-#define        USRACCT "/usr/adm/usracct"
-#define        SAVACCT "/usr/adm/savacct"
-#define        ACCT    "/usr/adm/acct"
+#define        USRACCT _PATH_USRACCT
+#define        SAVACCT _PATH_SAVACCT
+#define        ACCT    _PATH_ACCT
 #endif DEBUG
 \f
 
 #endif DEBUG
 \f
 
+char *usracct = USRACCT;
+char *savacct = SAVACCT;
+
 int    cellcmp();
 cell   *junkp = 0;
 /*
 int    cellcmp();
 cell   *junkp = 0;
 /*
@@ -272,9 +289,9 @@ int (*cmp)();
 
 /* we assume pagesize is at least 1k */
 int    pgdiv;
 
 /* we assume pagesize is at least 1k */
 int    pgdiv;
-#define        pgtok(x)        ((x) / pgdiv)
+#define        pgtok(x)        ((x) * pgdiv)
 
 
-extern tcmp(), ncmp(), bcmp(), dcmp(), Dcmp(), kcmp(), Kcmp();
+extern tcmp(), ncmp(), bflgcmp(), dcmp(), Dcmp(), kcmp(), Kcmp();
 extern double sum();
 
 main(argc, argv)
 extern double sum();
 
 main(argc, argv)
@@ -311,7 +328,7 @@ main(argc, argv)
 
                case 'b':
                        bflg++;
 
                case 'b':
                        bflg++;
-                       cmp = bcmp;
+                       cmp = bflgcmp;
                        break;
 
                case 'l':
                        break;
 
                case 'l':
@@ -396,16 +413,51 @@ main(argc, argv)
                case 'm':
                        mflg++;
                        break;
                case 'm':
                        mflg++;
                        break;
+
+               case 'U':
+               case 'S':
+                       if (i != 1 || argv[0][2]) {     /* gross! */
+                               fprintf(stderr, "-U and -S options must be separate\n");
+                               exit(1);
+                       }
+                       argc++, argv--;                 /* backup - yuk */
+                       goto doUS;
+
+               default:
+                       fprintf(stderr, "Invalid option %c\n", argv[0][1]);
+                       exit(1);
+               }
+       }
+
+#define optfile(f) {if (argc < 2) \
+                       { fprintf(stderr, "Missing filename\n"); exit(1); } \
+                       argc--, argv++; f = argv[0]; }
+
+doUS:
+       for (argc--, argv++; argc && argv[0][0] == '-'; argc--, argv++) {
+               switch(argv[0][1]) {
+                   case 'U':
+                       optfile(usracct);
+                       break;
+
+                   case 'S':
+                       optfile(savacct);
+                       break;
+
+                   default:
+                       fprintf(stderr, "Invalid option %c\n", argv[0][1]);
+                       exit(1);
                }
        }
                }
        }
+
        if (thres == 0)
                thres = 1;
        if (iflg==0)
                init();
        if (thres == 0)
                thres = 1;
        if (iflg==0)
                init();
-       if (argc<2)
+       if (argc<1)
                doacct(ACCT);
                doacct(ACCT);
-       else while (--argc)
-               doacct(*++argv);
+       else while (argc--)
+               doacct(*argv++);
        if (uflg) {
                return;
        }
        if (uflg) {
                return;
        }
@@ -437,7 +489,7 @@ main(argc, argv)
        }
        if (sflg) {
                signal(SIGINT, SIG_IGN);
        }
        if (sflg) {
                signal(SIGINT, SIG_IGN);
-               if ((ff = fopen(USRACCT, "w")) != NULL) {
+               if ((ff = fopen(usracct, "w")) != NULL) {
                        static  struct  user ZeroUser = {0};
                        struct  user    *up;
                        int     uid;
                        static  struct  user ZeroUser = {0};
                        struct  user    *up;
                        int     uid;
@@ -457,7 +509,7 @@ main(argc, argv)
                                                sizeof(struct Olduser),1,ff);
                        }
                }
                                                sizeof(struct Olduser),1,ff);
                        }
                }
-               if ((ff = fopen(SAVACCT, "w")) == NULL) {
+               if ((ff = fopen(savacct, "w")) == NULL) {
                        printf("Can't save\n");
                        exit(0);
                }
                        printf("Can't save\n");
                        exit(0);
                }
@@ -540,7 +592,7 @@ printmoney()
                                printf("%7u %9.2fcpu %10.0ftio %12.0fk*sec\n",
                                        up->us_cnt, up->us_ctime / 60,
                                        up->us_io,
                                printf("%7u %9.2fcpu %10.0ftio %12.0fk*sec\n",
                                        up->us_cnt, up->us_ctime / 60,
                                        up->us_io,
-                                       up->us_imem / 60);
+                                       up->us_imem / AHZ);
                        }
                }
        }
                        }
                }
        }
@@ -558,14 +610,14 @@ column(n, a, b, c, d, e)
        }
        col(n, a, treal, "re");
        if (oflg)
        }
        col(n, a, treal, "re");
        if (oflg)
-               col(n, 60*64*(b/(b+c)), tcpu+tsys, "u/s");
+               col(n, 60*AHZ*(b/(b+c)), tcpu+tsys, "u/s");
        else if(lflg) {
                col(n, b, tcpu, "u");
                col(n, c, tsys, "s");
        } else
                col(n, b+c, tcpu+tsys, "cp");
        if(tflg)
        else if(lflg) {
                col(n, b, tcpu, "u");
                col(n, c, tsys, "s");
        } else
                col(n, b+c, tcpu+tsys, "cp");
        if(tflg)
-               printf("%8.1f", a/(b+c), "re/cp");
+               printf("%8.1fre/cp", a/(b+c));
        if(dflg || !Dflg)
                printf("%10.0favio", e/(n?n:1));
        else
        if(dflg || !Dflg)
                printf("%10.0favio", e/(n?n:1));
        else
@@ -573,7 +625,7 @@ column(n, a, b, c, d, e)
        if (kflg || !Kflg)
                printf("%10.0fk", d/((b+c)!=0.0?(b+c):1.0));
        else
        if (kflg || !Kflg)
                printf("%10.0fk", d/((b+c)!=0.0?(b+c):1.0));
        else
-               printf("%10.0fk*sec", d/60);
+               printf("%10.0fk*sec", d/AHZ);
 }
 
 col(n, a, m, cp)
 }
 
 col(n, a, m, cp)
@@ -582,8 +634,8 @@ col(n, a, m, cp)
 {
 
        if(jflg)
 {
 
        if(jflg)
-               printf("%11.2f%s", a/(n*64.), cp); else
-               printf("%11.2f%s", a/(60*64.), cp);
+               printf("%11.2f%s", a/(n*(double)AHZ), cp); else
+               printf("%11.2f%s", a/(60.*(double)AHZ), cp);
        if(cflg) {
                if(a == m)
                        printf("%9s", ""); else
        if(cflg) {
                if(a == m)
                        printf("%9s", ""); else
@@ -635,17 +687,17 @@ char *f;
                        *cp = '\0';
                x = expand(fbuf.ac_utime) + expand(fbuf.ac_stime);
                y = pgtok((u_short)fbuf.ac_mem);
                        *cp = '\0';
                x = expand(fbuf.ac_utime) + expand(fbuf.ac_stime);
                y = pgtok((u_short)fbuf.ac_mem);
-               z = expand(fbuf.ac_io) >> 6;
+               z = expand(fbuf.ac_io) / AHZ;
                if (uflg) {
                if (uflg) {
-                       printf("%3d %6.2f cpu %8uk mem %6d io %.*s\n",
-                           fbuf.ac_uid, x / 64.0, y, z, NC, fbuf.ac_comm);
+                       printf("%3d %6.2f cpu %8luk mem %6ld io %.*s\n",
+                           fbuf.ac_uid, x/(double)AHZ, y, z, NC, fbuf.ac_comm);
                        continue;
                }
                up = finduser(fbuf.ac_uid);
                if (up == 0)
                        continue;       /* preposterous user id */
                up->us_cnt++;
                        continue;
                }
                up = finduser(fbuf.ac_uid);
                if (up == 0)
                        continue;       /* preposterous user id */
                up->us_cnt++;
-               up->us_ctime += x;
+               up->us_ctime += x/(double)AHZ;
                up->us_imem += x * y;
                up->us_io += z;
                ncom += 1.0;
                up->us_imem += x * y;
                up->us_io += z;
                ncom += 1.0;
@@ -696,7 +748,7 @@ ncmp(p1, p2)
        return(p2->p.count - p1->p.count);
 }
 
        return(p2->p.count - p1->p.count);
 }
 
-bcmp(p1, p2)
+bflgcmp(p1, p2)
        cell *p1, *p2;
 {
        double f1, f2;
        cell *p1, *p2;
 {
        double f1, f2;
@@ -830,7 +882,7 @@ init()
        int uid;
        FILE *f;
 
        int uid;
        FILE *f;
 
-       if ((f = fopen(SAVACCT, "r")) == NULL)
+       if ((f = fopen(savacct, "r")) == NULL)
                goto gshm;
        while (fread((char *)&tbuf, sizeof(struct process), 1, f) == 1) {
                tp = enter(tbuf.name);
                goto gshm;
        while (fread((char *)&tbuf, sizeof(struct process), 1, f) == 1) {
                tp = enter(tbuf.name);
@@ -849,7 +901,7 @@ init()
        }
        fclose(f);
  gshm:
        }
        fclose(f);
  gshm:
-       if ((f = fopen(USRACCT, "r")) == NULL)
+       if ((f = fopen(usracct, "r")) == NULL)
                return;
        for(uid = 0;
            fread((char *)&(userbuf.oldu), sizeof(struct Olduser), 1, f) == 1;
                return;
        for(uid = 0;
            fread((char *)&(userbuf.oldu), sizeof(struct Olduser), 1, f) == 1;
@@ -915,7 +967,7 @@ char *
 makekey(uid)
        int uid;
 {
 makekey(uid)
        int uid;
 {
-       sprintf(UserKey+1, "%04x", uid);
+       (void)sprintf(UserKey+1, "%04x", uid);
        UserKey[0] = USERKEY;
        return(UserKey);
 }
        UserKey[0] = USERKEY;
        return(UserKey);
 }
@@ -959,7 +1011,8 @@ getnames()
 
        setpwent();
        while (pw = getpwent()){
 
        setpwent();
        while (pw = getpwent()){
-               if ( (tp = wasuser(pw->pw_uid)) != 0)
+               /* use first name in passwd file for duplicate uid's */
+               if ((tp = wasuser(pw->pw_uid)) != 0 && !isalpha(tp->us_name[0]))
                        strncpy(tp->us_name, pw->pw_name, NAMELG);
        }
        endpwent();
                        strncpy(tp->us_name, pw->pw_name, NAMELG);
        }
        endpwent();