install correct aliases file
[unix-history] / usr / src / usr.sbin / sa / sa.c
index 3a44d77..9e65ff0 100644 (file)
@@ -1,4 +1,6 @@
-static char *sccsid = "@(#)sa.c        4.2 (Berkeley) 81/02/28";
+#ifndef lint
+static char *sccsid = "@(#)sa.c        4.10 (Berkeley) %G%";
+#endif
 
 /*
  *     Extensive modifications to internal data structures
 
 /*
  *     Extensive modifications to internal data structures
@@ -16,6 +18,7 @@ static        char *sccsid = "@(#)sa.c        4.2 (Berkeley) 81/02/28";
  *     31jan81
  */
 #include <stdio.h>
  *     31jan81
  */
 #include <stdio.h>
+#include <ctype.h>
 #include <sys/types.h>
 #include <sys/acct.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/acct.h>
 #include <signal.h>
@@ -253,6 +256,9 @@ char        *getname();
 #endif DEBUG
 \f
 
 #endif DEBUG
 \f
 
+char *usracct = USRACCT;
+char *savacct = SAVACCT;
+
 int    cellcmp();
 cell   *junkp = 0;
 /*
 int    cellcmp();
 cell   *junkp = 0;
 /*
@@ -267,21 +273,26 @@ int       htabinstall = 1;
 int    maxuser = -1;
 int    (*cmp)();
 
 int    maxuser = -1;
 int    (*cmp)();
 
+/* we assume pagesize is at least 1k */
+int    pgdiv;
+#define        pgtok(x)        ((x) / pgdiv)
+
+extern tcmp(), ncmp(), bcmp(), dcmp(), Dcmp(), kcmp(), Kcmp();
+extern double sum();
+
 main(argc, argv)
 main(argc, argv)
-char **argv;
+       char **argv;
 {
        FILE *ff;
 {
        FILE *ff;
-       int i, j;
-       extern  tcmp(), ncmp(), bcmp(), dcmp(), Dcmp(), kcmp(), Kcmp();
-       extern  double sum();
-       double  ft;
-       register        struct  allocbox        *allocwalk;
-       register        cell    *tp, *ub;
-       int     size;
-       int     nchunks;
-       struct  chunkdesc       *chunkvector;
-       int     smallest;
-
+       double ft;
+       register struct allocbox *allocwalk;
+       register cell *tp, *ub;
+       int i, j, size, nchunks, smallest;
+       struct chunkdesc *chunkvector;
+
+       pgdiv = getpagesize() / 1024;
+       if (pgdiv == 0)
+               pgdiv = 1;
        maxuser = USERSLOP + getmaxuid();
 
        tabinit();
        maxuser = USERSLOP + getmaxuid();
 
        tabinit();
@@ -388,16 +399,51 @@ char **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;
        }
@@ -429,7 +475,7 @@ char **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;
@@ -449,7 +495,7 @@ char **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);
                }
@@ -530,16 +576,16 @@ printmoney()
                                else 
                                        printf("%-8d", i);
                                printf("%7u %9.2fcpu %10.0ftio %12.0fk*sec\n",
                                else 
                                        printf("%-8d", i);
                                printf("%7u %9.2fcpu %10.0ftio %12.0fk*sec\n",
-                                       up->us_cnt, up->us_ctime/60,
+                                       up->us_cnt, up->us_ctime / 60,
                                        up->us_io,
                                        up->us_io,
-                                       up->us_imem / (60 * 2));
+                                       up->us_imem / AHZ);
                        }
                }
        }
 }
 
 column(n, a, b, c, d, e)
                        }
                }
        }
 }
 
 column(n, a, b, c, d, e)
-double n, a, b, c, d, e;
+       double n, a, b, c, d, e;
 {
 
        printf("%8.0f", n);
 {
 
        printf("%8.0f", n);
@@ -550,32 +596,32 @@ double n, a, b, c, d, e;
        }
        col(n, a, treal, "re");
        if (oflg)
        }
        col(n, a, treal, "re");
        if (oflg)
-               col(n, 3600*(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
                printf("%10.0ftio", e);
        if (kflg || !Kflg)
        if(dflg || !Dflg)
                printf("%10.0favio", e/(n?n:1));
        else
                printf("%10.0ftio", e);
        if (kflg || !Kflg)
-               printf("%10.0fk", d/(2*((b+c)!=0.0?(b+c):1.0)));
+               printf("%10.0fk", d/((b+c)!=0.0?(b+c):1.0));
        else
        else
-               printf("%10.0fk*sec", d/(2*60));
+               printf("%10.0fk*sec", d/AHZ);
 }
 
 col(n, a, m, cp)
 }
 
 col(n, a, m, cp)
-double n, a, m;
-char *cp;
+       double n, a, m;
+       char *cp;
 {
 
        if(jflg)
 {
 
        if(jflg)
-               printf("%11.2f%s", a/(n*60.), cp); else
-               printf("%11.2f%s", a/3600., 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
@@ -591,8 +637,8 @@ char *f;
        struct acct fbuf;
        register char *cp;
        register int c;
        struct acct fbuf;
        register char *cp;
        register int c;
-       register        struct  user    *up;
-       register        cell    *tp;
+       register struct user *up;
+       register cell *tp;
 #ifdef DEBUG
        int     nrecords = 0;
 #endif DEBUG
 #ifdef DEBUG
        int     nrecords = 0;
 #endif DEBUG
@@ -613,36 +659,31 @@ char *f;
                        printf("Input record from %s number %d\n",
                                f, nrecords);
 #endif DEBUG
                        printf("Input record from %s number %d\n",
                                f, nrecords);
 #endif DEBUG
-               if (fbuf.ac_comm[0]==0) {
-                       fbuf.ac_comm[0] = '?';
-               }
-               for (cp = fbuf.ac_comm; cp < &fbuf.ac_comm[NC]; cp++) {
-                       c = *cp & 0377;
-                       if (c && (c < ' ' || c >= 0200)) {
+               for (cp = fbuf.ac_comm; *cp && cp < &fbuf.ac_comm[NC]; cp++)
+                       if (!isascii(*cp) || iscntrl(*cp))
                                *cp = '?';
                                *cp = '?';
-                       }
-               }
+               if (cp == fbuf.ac_comm)
+                       *cp++ = '?';
                if (fbuf.ac_flag&AFORK) {
                if (fbuf.ac_flag&AFORK) {
-                       for (cp=fbuf.ac_comm; cp < &fbuf.ac_comm[NC]; cp++)
-                               if (*cp==0) {
-                                       *cp = '*';
-                                       break;
-                               }
+                       if (cp >= &fbuf.ac_comm[NC])
+                               cp = &fbuf.ac_comm[NC-1];
+                       *cp++ = '*';
                }
                }
+               if (cp < &fbuf.ac_comm[NC])
+                       *cp = '\0';
                x = expand(fbuf.ac_utime) + expand(fbuf.ac_stime);
                x = expand(fbuf.ac_utime) + expand(fbuf.ac_stime);
-               y = fbuf.ac_mem;
-               z = expand(fbuf.ac_io);
+               y = pgtok((u_short)fbuf.ac_mem);
+               z = expand(fbuf.ac_io) / AHZ;
                if (uflg) {
                if (uflg) {
-                       printf("%3d%6.1fcp %6dmem %6dio %.14s\n",
-                           fbuf.ac_uid, x/60.0, y, z,
-                           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/60.;
+               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;
@@ -651,7 +692,7 @@ char *f;
                tp->p.imem += x * y;
                timem += x * y;
                tp->p.count++;
                tp->p.imem += x * y;
                timem += x * y;
                tp->p.count++;
-               x = expand(fbuf.ac_etime)*60;
+               x = expand(fbuf.ac_etime);
                tp->p.realt += x;
                treal += x;
                x = expand(fbuf.ac_utime);
                tp->p.realt += x;
                treal += x;
                x = expand(fbuf.ac_utime);
@@ -670,19 +711,20 @@ char *f;
  *     Generalized cell compare routine, to cast out users
  */
 cellcmp(p1, p2)
  *     Generalized cell compare routine, to cast out users
  */
 cellcmp(p1, p2)
-       cell    *p1, *p2;
+       cell *p1, *p2;
 {
        if (ISPROCESS(p1)){
                if (ISPROCESS(p2))
 {
        if (ISPROCESS(p1)){
                if (ISPROCESS(p2))
-                       return(cmp(p1, p2));
+                       return((*cmp)(p1, p2));
                return(-1);
        }
        if (ISPROCESS(p2))
                return(1);
        return(0);
 }
                return(-1);
        }
        if (ISPROCESS(p2))
                return(1);
        return(0);
 }
+
 ncmp(p1, p2)
 ncmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
 
        if(p1->p.count == p2->p.count)
 {
 
        if(p1->p.count == p2->p.count)
@@ -693,7 +735,7 @@ cell *p1, *p2;
 }
 
 bcmp(p1, p2)
 }
 
 bcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
        double f1, f2;
        double sum();
 {
        double f1, f2;
        double sum();
@@ -714,7 +756,7 @@ cell *p1, *p2;
 }
 
 Kcmp(p1, p2)
 }
 
 Kcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
 
        if (p1->p.imem < p2->p.imem) {
 {
 
        if (p1->p.imem < p2->p.imem) {
@@ -731,7 +773,7 @@ cell *p1, *p2;
 }
 
 kcmp(p1, p2)
 }
 
 kcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
        double a1, a2;
 
 {
        double a1, a2;
 
@@ -751,7 +793,7 @@ cell *p1, *p2;
 }
 
 dcmp(p1, p2)
 }
 
 dcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
        double a1, a2;
 
 {
        double a1, a2;
 
@@ -771,7 +813,7 @@ cell *p1, *p2;
 }
 
 Dcmp(p1, p2)
 }
 
 Dcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
 
        if (p1->p.io < p2->p.io) {
 {
 
        if (p1->p.io < p2->p.io) {
@@ -788,7 +830,7 @@ cell *p1, *p2;
 }
 
 tcmp(p1, p2)
 }
 
 tcmp(p1, p2)
-cell *p1, *p2;
+       cell *p1, *p2;
 {
        extern double sum();
        double f1, f2;
 {
        extern double sum();
        double f1, f2;
@@ -809,7 +851,7 @@ cell *p1, *p2;
 }
 
 double sum(p)
 }
 
 double sum(p)
-cell *p;
+       cell *p;
 {
 
        if(p->p.name[0] == 0)
 {
 
        if(p->p.name[0] == 0)
@@ -819,14 +861,14 @@ cell *p;
 
 init()
 {
 
 init()
 {
-       struct  user    userbuf;
-       struct  process tbuf;
-       register        cell    *tp;
-       register        struct  user    *up;
-       int             uid;
+       struct user userbuf;
+       struct process  tbuf;
+       register cell *tp;
+       register struct user *up;
+       int uid;
        FILE *f;
 
        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);
@@ -845,7 +887,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;
@@ -862,9 +904,9 @@ init()
 
 strip()
 {
 
 strip()
 {
-       int     c;
-       register        struct  allocbox        *allocwalk;
-       register        cell    *tp, *ub, *junkp;
+       int c;
+       register struct allocbox *allocwalk;
+       register cell *tp, *ub, *junkp;
 
        if (fflg)
                printf("Categorizing commands used %d times or fewer as **junk**\n",
 
        if (fflg)
                printf("Categorizing commands used %d times or fewer as **junk**\n",
@@ -892,7 +934,7 @@ strip()
 
 time_t
 expand(t)
 
 time_t
 expand(t)
-unsigned t;
+       unsigned t;
 {
        register time_t nt;
 
 {
        register time_t nt;
 
@@ -905,36 +947,42 @@ unsigned t;
        return(nt);
 }
 
        return(nt);
 }
 
-static char    UserKey[NAMELG + 2];
-char *makekey(uid)
-       int     uid;
+static char UserKey[NAMELG + 2];
+
+char *
+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);
 }
 
-struct user *wasuser(uid)
-       int     uid;
+struct user *
+wasuser(uid)
+       int uid;
 {
 {
-       struct  user    *tp;
+       struct user *tp;
+
        htabinstall = 0;
        tp = finduser(uid);
        htabinstall = 1;
        return(tp);
 }
        htabinstall = 0;
        tp = finduser(uid);
        htabinstall = 1;
        return(tp);
 }
+
 /*
  *     Only call this if you really want to insert it in the table!
  */
 /*
  *     Only call this if you really want to insert it in the table!
  */
-struct user *finduser(uid)
-       int     uid;
+struct user *
+finduser(uid)
+       int uid;
 {
 {
+
        if (uid > maxuser){
                fprintf(stderr, "Preposterous user id, %d: ignored\n", uid);
                return(0);
        if (uid > maxuser){
                fprintf(stderr, "Preposterous user id, %d: ignored\n", uid);
                return(0);
-       } else {
-               return((struct user*)enter(makekey(uid)));
        }
        }
+       return((struct user*)enter(makekey(uid)));
 }
 
 /*
 }
 
 /*
@@ -943,24 +991,26 @@ struct user *finduser(uid)
  */
 getnames()
 {
  */
 getnames()
 {
-       register        struct user     *tp;
+       register struct user *tp;
        register struct passwd *pw;
        struct passwd *getpwent();
 
        setpwent();
        while (pw = getpwent()){
        register struct passwd *pw;
        struct passwd *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();
 }
 
-int getmaxuid()
+int
+getmaxuid()
 {
 {
-       register        struct  user    *tp;
-       register        struct  passwd  *pw;
-       struct          passwd  *getpwent();
-       int             maxuid = -1;
+       register struct user *tp;
+       register struct passwd *pw;
+       struct passwd *getpwent();
+       int maxuid = -1;
 
        setpwent();
        while(pw = getpwent()){
 
        setpwent();
        while(pw = getpwent()){
@@ -983,8 +1033,10 @@ tabinit()
 }
 
 #define ALLOCQTY       sizeof (struct allocbox)
 }
 
 #define ALLOCQTY       sizeof (struct allocbox)
-cell *taballoc()
+cell *
+taballoc()
 {
 {
+
        if (tabsleft == 0){
                newbox = (struct allocbox *)calloc(1, ALLOCQTY);
                tabsleft = TABDALLOP;
        if (tabsleft == 0){
                newbox = (struct allocbox *)calloc(1, ALLOCQTY);
                tabsleft = TABDALLOP;
@@ -1007,9 +1059,10 @@ cell *taballoc()
 
 htaballoc()
 {
 
 htaballoc()
 {
-       register        struct  hashdallop      *new;
+       register struct hashdallop *new;
 #ifdef DEBUG
 #ifdef DEBUG
-       static  int     ntables = 0;
+       static int ntables = 0;
+
        printf("%%%New hash table chunk allocated, number %d\n", ++ntables);
 #endif DEBUG
        new = (struct hashdallop *)calloc(1, sizeof (struct hashdallop));
        printf("%%%New hash table chunk allocated, number %d\n", ++ntables);
 #endif DEBUG
        new = (struct hashdallop *)calloc(1, sizeof (struct hashdallop));
@@ -1029,19 +1082,16 @@ htaballoc()
  *     is called quite frequently, and the calling overhead
  *     contributes significantly to the overall execution speed of sa.
  */
  *     is called quite frequently, and the calling overhead
  *     contributes significantly to the overall execution speed of sa.
  */
-cell *enter(name)
-       char    *name;  
+cell *
+enter(name)
+       char *name;     
 {
 {
-       static   int            initialprobe;
-       register cell           **hp;
-       register char           *from;
-       register char           *to;
-       register        int     len;
-       register        int     nprobes;
-       static   struct hashdallop *hdallop;
-       static   cell           **emptyslot;
-       static   struct hashdallop *emptyhd;
-       static   cell           **hp_ub;
+       static int initialprobe;
+       register cell **hp;
+       register char *from, *to;
+       register int len, nprobes;
+       static struct hashdallop *hdallop, *emptyhd;
+       static cell **emptyslot, **hp_ub;
 
        emptyslot = 0;
        for (nprobes = 0, from = name, len = 0;
 
        emptyslot = 0;
        for (nprobes = 0, from = name, len = 0;
@@ -1098,4 +1148,4 @@ cell *enter(name)
                return(*hp);
        }
        return(0);
                return(*hp);
        }
        return(0);
-}      /*end of lookup*/
+}