get screen width ioctl added and hold passwd file open
[unix-history] / usr / src / bin / ps / ps.c
index dc480c9..3f17c09 100644 (file)
@@ -1,29 +1,33 @@
-static char *sccsid = "@(#)ps.c        4.10+ (Berkeley) 7/2/81";
+#ifndef lint
+static char *sccsid = "@(#)ps.c        4.30 (Berkeley) %G%";
+#endif
+
 /*
 /*
- * ps; VAX 4BSD version
+ * ps
  */
  */
-
 #include <stdio.h>
 #include <ctype.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <sys/param.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <sys/param.h>
+#include <sys/ioctl.h>
 #include <sys/tty.h>
 #include <sys/dir.h>
 #include <sys/user.h>
 #include <sys/proc.h>
 #include <sys/tty.h>
 #include <sys/dir.h>
 #include <sys/user.h>
 #include <sys/proc.h>
-#include <sys/pte.h>
+#include <machine/pte.h>
 #include <sys/vm.h>
 #include <sys/text.h>
 #include <sys/stat.h>
 #include <sys/vm.h>
 #include <sys/text.h>
 #include <sys/stat.h>
+#include <sys/mbuf.h>
 #include <math.h>
 #include <math.h>
-#include <sys/vlimit.h>
+#include <errno.h>
 
 struct nlist nl[] = {
        { "_proc" },
 #define        X_PROC          0
        { "_Usrptmap" },
 
 struct nlist nl[] = {
        { "_proc" },
 #define        X_PROC          0
        { "_Usrptmap" },
-#define        X_USRPTMA       1
+#define        X_USRPTMAP      1
        { "_usrpt" },
 #define        X_USRPT         2
        { "_text" },
        { "_usrpt" },
 #define        X_USRPT         2
        { "_text" },
@@ -40,9 +44,15 @@ struct nlist nl[] = {
 #define        X_NPROC         8
        { "_ntext" },
 #define        X_NTEXT         9
 #define        X_NPROC         8
        { "_ntext" },
 #define        X_NTEXT         9
-       { "_hz" },
-#define        X_HZ            10
-       { 0 },
+       { "_dmmin" },
+#define        X_DMMIN         10
+       { "_dmmax" },
+#define        X_DMMAX         11
+       { "_Sysmap" },
+#define        X_SYSMAP        12
+       { "_Syssize" },
+#define        X_SYSSIZE       13
+       { "" },
 };
 
 struct savcom {
 };
 
 struct savcom {
@@ -51,7 +61,7 @@ struct        savcom {
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
-       } sun;
+       } s_un;
        struct  asav *ap;
 } *savcom;
 
        struct  asav *ap;
 } *savcom;
 
@@ -61,7 +71,7 @@ struct        asav {
        short   a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time;
        size_t  a_size, a_rss, a_tsiz, a_txtrss;
        short   a_xccount;
        short   a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time;
        size_t  a_size, a_rss, a_tsiz, a_txtrss;
        short   a_xccount;
-       char    a_tty[DIRSIZ+1];
+       char    a_tty[MAXNAMLEN+1];
        dev_t   a_ttyd;
        time_t  a_cpu;
        size_t  a_maxrss;
        dev_t   a_ttyd;
        time_t  a_cpu;
        size_t  a_maxrss;
@@ -85,52 +95,72 @@ struct      vsav {
        float   v_pctcpu;
 };
 
        float   v_pctcpu;
 };
 
-struct proc proc[8];           /* 8 = a few, for less syscalls */
+#define        NPROC   16
+
+struct proc proc[NPROC];               /* a few, for less syscalls */
 struct proc *mproc;
 struct text *text;
 
 struct proc *mproc;
 struct text *text;
 
-int    paduser1;               /* avoid hardware mem clobbering botch */
 union {
        struct  user user;
        char    upages[UPAGES][NBPG];
 } user;
 #define u      user.user
 union {
        struct  user user;
        char    upages[UPAGES][NBPG];
 } user;
 #define u      user.user
-int    paduser2;               /* avoid hardware mem clobbering botch */
 
 
-#define clear(x)       ((int)x & 0x7fffffff)
+#ifndef        PSFILE
+char   *psdb   = "/etc/psdatabase";
+#else
+char   *psdb   = PSFILE;
+#endif
 
 int    chkpid;
 
 int    chkpid;
-int    aflg, cflg, eflg, gflg, kflg, lflg, sflg, uflg, vflg, xflg;
+int    aflg, cflg, eflg, gflg, kflg, lflg, sflg,
+       uflg, vflg, xflg, Uflg;
 char   *tptr;
 char   *tptr;
-char   *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state();
+char   *gettty(), *getcmd(), *getname(), *savestr(), *state();
+char   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
+char   *strncpy(), *index(), *ttyname(), mytty[16];
+long   lseek();
+off_t  vtophys();
 double pcpu(), pmem();
 int    pscomp();
 int    nswap, maxslp;
 struct text *atext;
 double ccpu;
 int    ecmx;
 double pcpu(), pmem();
 int    pscomp();
 int    nswap, maxslp;
 struct text *atext;
 double ccpu;
 int    ecmx;
-struct pte *Usrptma, *usrpt;
-int    nproc, ntext, hz;
+struct pte *Usrptmap, *usrpt;
+int    nproc, ntext;
+int    dmmin, dmmax;
+struct pte *Sysmap;
+int    Syssize;
+
+#ifndef        MAXTTYS
+#define        MAXTTYS         256
+#endif
+
+int    nttys;
 
 struct ttys {
 
 struct ttys {
-       char    name[DIRSIZ+1];
        dev_t   ttyd;
        dev_t   ttyd;
-       struct  ttys *next;
        struct  ttys *cand;
        struct  ttys *cand;
-} *allttys, *cand[16];
+       char    name[MAXNAMLEN+1];
+} allttys[MAXTTYS], *cand[16];
 
 int    npr;
 
 int    cmdstart;
 int    twidth;
 
 int    npr;
 
 int    cmdstart;
 int    twidth;
+struct winsize win;
 char   *kmemf, *memf, *swapf, *nlistf;
 char   *kmemf, *memf, *swapf, *nlistf;
-int    kmem, mem, swap;
+int    kmem, mem, swap = -1;
 int    rawcpu, sumcpu;
 
 int    pcbpf;
 int    argaddr;
 extern char _sobuf[];
 
 int    rawcpu, sumcpu;
 
 int    pcbpf;
 int    argaddr;
 extern char _sobuf[];
 
+#define        pgtok(a)        ((a)/(1024/NBPG))
+
 main(argc, argv)
        char **argv;
 {
 main(argc, argv)
        char **argv;
 {
@@ -138,13 +168,12 @@ main(argc, argv)
        register char *ap;
        int uid;
        off_t procp;
        register char *ap;
        int uid;
        off_t procp;
+       int width;
 
 
-       if (chdir("/dev") < 0) {
-               perror("/dev");
-               exit(1);
-       }
-       twidth = 80;
-       setbuf(stdout, _sobuf);
+       if (ioctl(0, TIOCGWINSZ, &win) == -1)
+               twidth = 80;
+       else
+               twidth = (win.ws_col == 0 ? 80 : win.ws_col);
        argc--, argv++;
        if (argc > 0) {
                ap = argv[0];
        argc--, argv++;
        if (argc > 0) {
                ap = argv[0];
@@ -156,6 +185,11 @@ main(argc, argv)
                case 'S':
                        sumcpu++;
                        break;
                case 'S':
                        sumcpu++;
                        break;
+
+               case 'U':
+                       Uflg++;
+                       break;
+
                case 'a':
                        aflg++;
                        break;
                case 'a':
                        aflg++;
                        break;
@@ -180,14 +214,19 @@ main(argc, argv)
                case 't':
                        if (*ap)
                                tptr = ap;
                case 't':
                        if (*ap)
                                tptr = ap;
+                       else if ((tptr = ttyname(2)) != 0) {
+                               strcpy(mytty, tptr);
+                               if ((tptr = index(mytty,'y')) != 0)
+                                       tptr++;
+                       }
                        aflg++;
                        gflg++;
                        aflg++;
                        gflg++;
-                       if (*tptr == '?')
+                       if (tptr && *tptr == '?')
                                xflg++;
                        while (*ap)
                                ap++;
                        break;
                                xflg++;
                        while (*ap)
                                ap++;
                        break;
-               case 'u':
+               case 'u': 
                        uflg++;
                        break;
                case 'v':
                        uflg++;
                        break;
                case 'v':
@@ -195,7 +234,7 @@ main(argc, argv)
                        vflg++;
                        break;
                case 'w':
                        vflg++;
                        break;
                case 'w':
-                       if (twidth == 80)
+                       if (twidth < 132)
                                twidth = 132;
                        else
                                twidth = BUFSIZ;
                                twidth = 132;
                        else
                                twidth = BUFSIZ;
@@ -215,21 +254,21 @@ main(argc, argv)
        }
        openfiles(argc, argv);
        getkvars(argc, argv);
        }
        openfiles(argc, argv);
        getkvars(argc, argv);
-       getdev();
        uid = getuid();
        printhdr();
        procp = getw(nl[X_PROC].n_value);
        nproc = getw(nl[X_NPROC].n_value);
        uid = getuid();
        printhdr();
        procp = getw(nl[X_PROC].n_value);
        nproc = getw(nl[X_NPROC].n_value);
-       hz = getw(nl[X_HZ].n_value);
-       savcom = (struct savcom *)calloc(nproc, sizeof (*savcom));
-       for (i=0; i<nproc; i += 8) {
-               lseek(kmem, (char *)procp, 0);
+       savcom = (struct savcom *)calloc((unsigned) nproc, sizeof (*savcom));
+       for (i=0; i<nproc; i += NPROC) {
+               klseek(kmem, (long)procp, 0);
                j = nproc - i;
                j = nproc - i;
-               if (j > 8)
-                       j = 8;
+               if (j > NPROC)
+                       j = NPROC;
                j *= sizeof (struct proc);
                j *= sizeof (struct proc);
-               if (read(kmem, (char *)proc, j) != j)
+               if (read(kmem, (char *)proc, j) != j) {
                        cantread("proc table", kmemf);
                        cantread("proc table", kmemf);
+                       exit(1);
+               }
                procp += j;
                for (j = j / sizeof (struct proc) - 1; j >= 0; j--) {
                        mproc = &proc[j];
                procp += j;
                for (j = j / sizeof (struct proc) - 1; j >= 0; j--) {
                        mproc = &proc[j];
@@ -237,10 +276,11 @@ main(argc, argv)
                            mproc->p_pgrp == 0 && xflg == 0)
                                continue;
                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
                            mproc->p_pgrp == 0 && xflg == 0)
                                continue;
                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
-                           mproc->p_ppid == 1 && (mproc->p_flag&SDETACH) == 0)
+                           mproc->p_ppid == 1)
+                               continue;
+                       if (uid != mproc->p_uid && aflg==0)
                                continue;
                                continue;
-                       if (uid != mproc->p_uid && aflg==0 ||
-                           chkpid != 0 && chkpid != mproc->p_pid)
+                       if (chkpid != 0 && chkpid != mproc->p_pid)
                                continue;
                        if (vflg && gflg == 0 && xflg == 0) {
                                if (mproc->p_stat == SZOMB ||
                                continue;
                        if (vflg && gflg == 0 && xflg == 0) {
                                if (mproc->p_stat == SZOMB ||
@@ -254,7 +294,10 @@ main(argc, argv)
                        save();
                }
        }
                        save();
                }
        }
-       qsort(savcom, npr, sizeof(savcom[0]), pscomp);
+       width = twidth - cmdstart - 2;
+       if (width < 0)
+               width = 0;
+       qsort((char *) savcom, npr, sizeof(savcom[0]), pscomp);
        for (i=0; i<npr; i++) {
                register struct savcom *sp = &savcom[i];
                if (lflg)
        for (i=0; i<npr; i++) {
                register struct savcom *sp = &savcom[i];
                if (lflg)
@@ -283,23 +326,82 @@ main(argc, argv)
 }
 
 getw(loc)
 }
 
 getw(loc)
-       off_t loc;
+       unsigned long loc;
 {
 {
-       long word;
+       int word;
 
 
-       lseek(kmem, loc, 0);
-       if (read(kmem, &word, sizeof (word)) != sizeof (word))
+       klseek(kmem, (long)loc, 0);
+       if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
                printf("error reading kmem at %x\n", loc);
        return (word);
 }
 
                printf("error reading kmem at %x\n", loc);
        return (word);
 }
 
+klseek(fd, loc, off)
+       int fd;
+       long loc;
+       int off;
+{
+       if (kflg) {
+               if ((loc = vtophys(loc)) == -1)
+                       return;
+       }
+       (void) lseek(fd, (long)loc, off);
+}
+
+writepsdb(unixname)
+       char *unixname;
+{
+       int nllen;
+       register FILE *fp;
+
+       setuid(getuid());
+       if ((fp = fopen(psdb, "w")) == NULL) {
+               perror(psdb);
+               exit(1);
+       } else
+               fchmod(fileno(fp), 0644);
+       nllen = sizeof nl / sizeof (struct nlist);
+       fwrite((char *) &nllen, sizeof nllen, 1, fp);
+       fwrite((char *) nl, sizeof (struct nlist), nllen, fp);
+       fwrite((char *) &nttys, sizeof nttys, 1, fp);
+       fwrite((char *) allttys, sizeof (struct ttys), nttys, fp);
+       fwrite(unixname, strlen(unixname) + 1, 1, fp);
+       fclose(fp);
+}
+
+readpsdb(unixname)
+       char *unixname;
+{
+       int nllen;
+       register i;
+       register FILE *fp;
+       char unamebuf[BUFSIZ];
+       char *p = unamebuf;
+       extern int errno;
+
+       if ((fp = fopen(psdb, "r")) == NULL) {
+               if (errno == ENOENT)
+                       return (0);
+               perror(psdb);
+               exit(1);
+       }
+
+       fread((char *) &nllen, sizeof nllen, 1, fp);
+       fread((char *) nl, sizeof (struct nlist), nllen, fp);
+       fread((char *) &nttys, sizeof nttys, 1, fp);
+       fread((char *) allttys, sizeof (struct ttys), nttys, fp);
+       while ((*p = getc(fp)) != '\0')
+               p++;
+       return (strcmp(unixname, unamebuf) == 0);
+}
+
 openfiles(argc, argv)
        char **argv;
 {
 
 openfiles(argc, argv)
        char **argv;
 {
 
-       kmemf = "kmem";
+       kmemf = "/dev/kmem";
        if (kflg)
        if (kflg)
-               kmemf = argc > 1 ? argv[1] : "/vmcore";
+               kmemf = argc > 2 ? argv[2] : "/vmcore";
        kmem = open(kmemf, 0);
        if (kmem < 0) {
                perror(kmemf);
        kmem = open(kmemf, 0);
        if (kmem < 0) {
                perror(kmemf);
@@ -309,72 +411,101 @@ openfiles(argc, argv)
                mem = kmem;
                memf = kmemf;
        } else {
                mem = kmem;
                memf = kmemf;
        } else {
-               memf = "mem";
+               memf = "/dev/mem";
                mem = open(memf, 0);
                if (mem < 0) {
                        perror(memf);
                        exit(1);
                }
        }
                mem = open(memf, 0);
                if (mem < 0) {
                        perror(memf);
                        exit(1);
                }
        }
-       swapf = argc>2 ? argv[2]: "drum";
-       swap = open(swapf, 0);
-       if (swap < 0) {
-               perror(swapf);
-               exit(1);
+       if (kflg == 0 || argc > 3) {
+               swapf = argc>3 ? argv[3]: "/dev/drum";
+               swap = open(swapf, 0);
+               if (swap < 0) {
+                       perror(swapf);
+                       exit(1);
+               }
        }
 }
 
 getkvars(argc, argv)
        char **argv;
 {
        }
 }
 
 getkvars(argc, argv)
        char **argv;
 {
-       register struct nlist *nlp;
 
 
-       nlistf = argc > 3 ? argv[3] : "/vmunix";
-       nlist(nlistf, nl);
+       nlistf = argc > 1 ? argv[1] : "/vmunix";
+       if (Uflg) {
+               nlist(nlistf, nl);
+               getdev();
+               writepsdb(nlistf);
+               exit (0);
+       } else if (!readpsdb(nlistf)) {
+               if (!kflg)
+                       nl[X_SYSMAP].n_name = "";
+               nlist(nlistf, nl);
+               nttys =  0;
+               getdev();
+       }
+
        if (nl[0].n_type == 0) {
                fprintf(stderr, "%s: No namelist\n", nlistf);
                exit(1);
        }
        if (nl[0].n_type == 0) {
                fprintf(stderr, "%s: No namelist\n", nlistf);
                exit(1);
        }
-       if (kflg)
-               for (nlp = nl; nlp < &nl[sizeof (nl)/sizeof (nl[0])]; nlp++)
-                       nlp->n_value = clear(nlp->n_value);
-       Usrptma = (struct pte *)nl[X_USRPTMA].n_value;
+       if (kflg) {
+               /* We must do the sys map first because klseek uses it */
+               long    addr;
+
+               Syssize = nl[X_SYSSIZE].n_value;
+               Sysmap = (struct pte *)
+                       calloc((unsigned) Syssize, sizeof (struct pte));
+               if (Sysmap == NULL) {
+                       fprintf(stderr, "Out of space for Sysmap\n");
+                       exit(1);
+               }
+               addr = (long) nl[X_SYSMAP].n_value;
+               addr &= ~0x80000000;
+               (void) lseek(kmem, addr, 0);
+               read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte));
+       }
        usrpt = (struct pte *)nl[X_USRPT].n_value;
        usrpt = (struct pte *)nl[X_USRPT].n_value;
-       lseek(kmem, (long)nl[X_NSWAP].n_value, 0);
-       if (read(kmem, &nswap, sizeof (nswap)) != sizeof (nswap)) {
+       Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
+       klseek(kmem, (long)nl[X_NSWAP].n_value, 0);
+       if (read(kmem, (char *)&nswap, sizeof (nswap)) != sizeof (nswap)) {
                cantread("nswap", kmemf);
                exit(1);
        }
                cantread("nswap", kmemf);
                exit(1);
        }
-       lseek(kmem, (long)nl[X_MAXSLP].n_value, 0);
-       if (read(kmem, &maxslp, sizeof (maxslp)) != sizeof (maxslp)) {
+       klseek(kmem, (long)nl[X_MAXSLP].n_value, 0);
+       if (read(kmem, (char *)&maxslp, sizeof (maxslp)) != sizeof (maxslp)) {
                cantread("maxslp", kmemf);
                exit(1);
        }
                cantread("maxslp", kmemf);
                exit(1);
        }
-       lseek(kmem, (long)nl[X_CCPU].n_value, 0);
-       if (read(kmem, &ccpu, sizeof (ccpu)) != sizeof (ccpu)) {
+       klseek(kmem, (long)nl[X_CCPU].n_value, 0);
+       if (read(kmem, (char *)&ccpu, sizeof (ccpu)) != sizeof (ccpu)) {
                cantread("ccpu", kmemf);
                exit(1);
        }
                cantread("ccpu", kmemf);
                exit(1);
        }
-       lseek(kmem, (long)nl[X_ECMX].n_value, 0);
-       if (read(kmem, &ecmx, sizeof (ecmx)) != sizeof (ecmx)) {
+       klseek(kmem, (long)nl[X_ECMX].n_value, 0);
+       if (read(kmem, (char *)&ecmx, sizeof (ecmx)) != sizeof (ecmx)) {
                cantread("ecmx", kmemf);
                exit(1);
        }
        if (uflg || vflg) {
                ntext = getw(nl[X_NTEXT].n_value);
                cantread("ecmx", kmemf);
                exit(1);
        }
        if (uflg || vflg) {
                ntext = getw(nl[X_NTEXT].n_value);
-               text = (struct text *)alloc(ntext * sizeof (struct text));
+               text = (struct text *)
+                       calloc((unsigned) ntext, sizeof (struct text));
                if (text == 0) {
                        fprintf(stderr, "no room for text table\n");
                        exit(1);
                }
                atext = (struct text *)getw(nl[X_TEXT].n_value);
                if (text == 0) {
                        fprintf(stderr, "no room for text table\n");
                        exit(1);
                }
                atext = (struct text *)getw(nl[X_TEXT].n_value);
-               lseek(kmem, (int)atext, 0);
+               klseek(kmem, (long)atext, 0);
                if (read(kmem, (char *)text, ntext * sizeof (struct text))
                    != ntext * sizeof (struct text)) {
                        cantread("text table", kmemf);
                        exit(1);
                }
        }
                if (read(kmem, (char *)text, ntext * sizeof (struct text))
                    != ntext * sizeof (struct text)) {
                        cantread("text table", kmemf);
                        exit(1);
                }
        }
+       dmmin = getw(nl[X_DMMIN].n_value);
+       dmmax = getw(nl[X_DMMAX].n_value);
 }
 
 printhdr()
 }
 
 printhdr()
@@ -385,40 +516,42 @@ printhdr()
                fprintf(stderr, "ps: specify only one of s,l,v and u\n");
                exit(1);
        }
                fprintf(stderr, "ps: specify only one of s,l,v and u\n");
                exit(1);
        }
-       hdr = lflg ? lhdr : (vflg ? vhdr : (uflg ? uhdr : shdr));
+       hdr = lflg ? lhdr : 
+                       (vflg ? vhdr : 
+                               (uflg ? uhdr : shdr));
        if (lflg+vflg+uflg+sflg == 0)
                hdr += strlen("SSIZ ");
        cmdstart = strlen(hdr);
        printf("%s COMMAND\n", hdr);
        if (lflg+vflg+uflg+sflg == 0)
                hdr += strlen("SSIZ ");
        cmdstart = strlen(hdr);
        printf("%s COMMAND\n", hdr);
-       fflush(stdout);
+       (void) fflush(stdout);
 }
 
 cantread(what, fromwhat)
        char *what, *fromwhat;
 {
 
 }
 
 cantread(what, fromwhat)
        char *what, *fromwhat;
 {
 
-       fprintf(stderr, "ps: error reading %s from %s", what, fromwhat);
+       fprintf(stderr, "ps: error reading %s from %s\n", what, fromwhat);
 }
 
 }
 
-struct direct dbuf;
+struct direct *dbuf;
 int    dialbase;
 
 getdev()
 {
 int    dialbase;
 
 getdev()
 {
-       register FILE *df;
-       register struct ttys *dp;
+       register DIR *df;
 
 
+       if (chdir("/dev") < 0) {
+               perror("/dev");
+               exit(1);
+       }
        dialbase = -1;
        dialbase = -1;
-       if ((df = fopen(".", "r")) == NULL) {
+       if ((df = opendir(".")) == NULL) {
                fprintf(stderr, "Can't open . in /dev\n");
                exit(1);
        }
                fprintf(stderr, "Can't open . in /dev\n");
                exit(1);
        }
-       while (fread((char *)&dbuf, sizeof(dbuf), 1, df) == 1) {
-               if (dbuf.d_ino == 0)
-                       continue;
-               maybetty(dp);
-       }
-       fclose(df);
+       while ((dbuf = readdir(df)) != NULL) 
+               maybetty();
+       closedir(df);
 }
 
 /*
 }
 
 /*
@@ -429,7 +562,7 @@ getdev()
  */
 maybetty()
 {
  */
 maybetty()
 {
-       register char *cp = dbuf.d_name;
+       register char *cp = dbuf->d_name;
        register struct ttys *dp;
        int x;
        struct stat stb;
        register struct ttys *dp;
        int x;
        struct stat stb;
@@ -446,12 +579,12 @@ maybetty()
 
        case 'd':
                if (!strcmp(cp, "drum"))
 
        case 'd':
                if (!strcmp(cp, "drum"))
-                       return (0);
+                       return;
                break;
 
        case 'f':
                if (!strcmp(cp, "floppy"))
                break;
 
        case 'f':
                if (!strcmp(cp, "floppy"))
-                       return (0);
+                       return;
                break;
 
        case 'k':
                break;
 
        case 'k':
@@ -462,40 +595,35 @@ maybetty()
 
        case 'r':
                cp++;
 
        case 'r':
                cp++;
-               if (*cp == 'r' || *cp == 'u' || *cp == 'h')
-                       cp++;
 #define is(a,b) cp[0] == 'a' && cp[1] == 'b'
 #define is(a,b) cp[0] == 'a' && cp[1] == 'b'
-               if (is(r,p) || is(u,p) || is(r,k) || is(r,m) || is(m,t)) {
+               if (is(h,p) || is(r,a) || is(u,p) || is(r,k) 
+                   || is(r,m) || is(m,t)) {
                        cp += 2;
                        if (isdigit(*cp) && cp[2] == 0)
                        cp += 2;
                        if (isdigit(*cp) && cp[2] == 0)
-                               return (0);
+                               return;
                }
                break;
 
        case 'm':
 trymem:
                if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
                }
                break;
 
        case 'm':
 trymem:
                if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
-                       return (0);
+                       return;
                if (cp[0] == 'm' && cp[1] == 't')
                if (cp[0] == 'm' && cp[1] == 't')
-                       return (0);
+                       return;
                break;
 
        case 'n':
                if (!strcmp(cp, "null"))
                break;
 
        case 'n':
                if (!strcmp(cp, "null"))
-                       return (0);
+                       return;
                break;
 
        case 'v':
                if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
                    cp[3] == 0)
                break;
 
        case 'v':
                if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
                    cp[3] == 0)
-                       return (0);
+                       return;
                break;
        }
                break;
        }
-mightbe:
-       cp = dbuf.d_name;
-       while (cp < &dbuf.d_name[DIRSIZ] && *cp)
-               cp++;
-       --cp;
+       cp = dbuf->d_name + dbuf->d_namlen - 1;
        x = 0;
        if (cp[-1] == 'd') {
                if (dialbase == -1) {
        x = 0;
        if (cp[-1] == 'd') {
                if (dialbase == -1) {
@@ -509,7 +637,7 @@ mightbe:
                else
                        x = 11;
        }
                else
                        x = 11;
        }
-       if (cp > dbuf.d_name && isdigit(cp[-1]) && isdigit(*cp))
+       if (cp > dbuf->d_name && isdigit(cp[-1]) && isdigit(*cp))
                x += 10 * (cp[-1] - ' ') + cp[0] - '0';
        else if (*cp >= 'a' && *cp <= 'f')
                x += 10 + *cp - 'a';
                x += 10 * (cp[-1] - ' ') + cp[0] - '0';
        else if (*cp >= 'a' && *cp <= 'f')
                x += 10 + *cp - 'a';
@@ -518,11 +646,22 @@ mightbe:
        else
                x = -1;
 donecand:
        else
                x = -1;
 donecand:
-       dp = (struct ttys *)alloc(sizeof (struct ttys));
-       strncpy(dp->name, dbuf.d_name, DIRSIZ);
-       dp->next = allttys;
-       dp->ttyd = -1;
-       allttys = dp;
+       if (nttys >= MAXTTYS) {
+               fprintf(stderr, "ps: tty table overflow\n");
+               exit(1);
+       }
+       dp = &allttys[nttys++];
+       (void) strcpy(dp->name, dbuf->d_name);
+       if (Uflg) {
+               if (stat(dp->name, &stb) == 0 &&
+                  (stb.st_mode&S_IFMT)==S_IFCHR)
+                       dp->ttyd = x = stb.st_rdev;
+               else {
+                       nttys--;
+                       return;
+               }
+       } else
+               dp->ttyd = -1;
        if (x == -1)
                return;
        x &= 017;
        if (x == -1)
                return;
        x &= 017;
@@ -553,7 +692,7 @@ gettty()
                        goto found;
        }
        /* ick */
                        goto found;
        }
        /* ick */
-       for (dp = allttys; dp; dp = dp->next) {
+       for (dp = allttys; dp < &allttys[nttys]; dp++) {
                if (dp->ttyd == -1) {
                        if (stat(dp->name, &stb) == 0 &&
                           (stb.st_mode&S_IFMT)==S_IFCHR)
                if (dp->ttyd == -1) {
                        if (stat(dp->name, &stb) == 0 &&
                           (stb.st_mode&S_IFMT)==S_IFCHR)
@@ -583,13 +722,13 @@ save()
        if (mproc->p_stat != SZOMB && getu() == 0)
                return;
        ttyp = gettty();
        if (mproc->p_stat != SZOMB && getu() == 0)
                return;
        ttyp = gettty();
-       if (xflg == 0 && ttyp[0] == '?' || tptr && strcmpn(tptr, ttyp, 2))
+       if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2))
                return;
        sp = &savcom[npr];
        cmdp = getcmd();
        if (cmdp == 0)
                return;
                return;
        sp = &savcom[npr];
        cmdp = getcmd();
        if (cmdp == 0)
                return;
-       sp->ap = ap = (struct asav *)alloc(sizeof (struct asav));
+       sp->ap = ap = (struct asav *)calloc(1, sizeof (struct asav));
        sp->ap->a_cmdp = cmdp;
 #define e(a,b) ap->a = mproc->b
        e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice);
        sp->ap->a_cmdp = cmdp;
 #define e(a,b) ap->a = mproc->b
        e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice);
@@ -598,16 +737,14 @@ save()
        ap->a_tty[0] = ttyp[0];
        ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' ';
        if (ap->a_stat == SZOMB) {
        ap->a_tty[0] = ttyp[0];
        ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' ';
        if (ap->a_stat == SZOMB) {
-               register struct xproc *xp = (struct xproc *)mproc;
-
-               ap->a_cpu = xp->xp_vm.vm_utime + xp->xp_vm.vm_stime;
+               ap->a_cpu = 0;
        } else {
                ap->a_size = mproc->p_dsize + mproc->p_ssize;
                e(a_rss, p_rssize); 
                ap->a_ttyd = u.u_ttyd;
        } else {
                ap->a_size = mproc->p_dsize + mproc->p_ssize;
                e(a_rss, p_rssize); 
                ap->a_ttyd = u.u_ttyd;
-               ap->a_cpu = u.u_vm.vm_utime + u.u_vm.vm_stime;
+               ap->a_cpu = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec;
                if (sumcpu)
                if (sumcpu)
-                       ap->a_cpu += u.u_cvm.vm_utime + u.u_cvm.vm_stime;
+                       ap->a_cpu += u.u_cru.ru_utime.tv_sec + u.u_cru.ru_stime.tv_sec;
                if (mproc->p_textp && text) {
                        xp = &text[mproc->p_textp - atext];
                        ap->a_tsiz = xp->x_size;
                if (mproc->p_textp && text) {
                        xp = &text[mproc->p_textp - atext];
                        ap->a_tsiz = xp->x_size;
@@ -616,12 +753,12 @@ save()
                }
        }
 #undef e
                }
        }
 #undef e
-       ap->a_cpu /= hz;
        ap->a_maxrss = mproc->p_maxrss;
        if (lflg) {
                register struct lsav *lp;
 
        ap->a_maxrss = mproc->p_maxrss;
        if (lflg) {
                register struct lsav *lp;
 
-               sp->sun.lp = lp = (struct lsav *)alloc(sizeof (struct lsav));
+               sp->s_un.lp = lp = (struct lsav *)
+                       calloc(1, sizeof (struct lsav));
 #define e(a,b) lp->a = mproc->b
                e(l_ppid, p_ppid); e(l_cpu, p_cpu);
                if (ap->a_stat != SZOMB)
 #define e(a,b) lp->a = mproc->b
                e(l_ppid, p_ppid); e(l_cpu, p_cpu);
                if (ap->a_stat != SZOMB)
@@ -631,27 +768,29 @@ save()
        } else if (vflg) {
                register struct vsav *vp;
 
        } else if (vflg) {
                register struct vsav *vp;
 
-               sp->sun.vp = vp = (struct vsav *)alloc(sizeof (struct vsav));
+               sp->s_un.vp = vp = (struct vsav *)
+                       calloc(1, sizeof (struct vsav));
 #define e(a,b) vp->a = mproc->b
                if (ap->a_stat != SZOMB) {
                        e(v_swrss, p_swrss);
 #define e(a,b) vp->a = mproc->b
                if (ap->a_stat != SZOMB) {
                        e(v_swrss, p_swrss);
-                       vp->v_majflt = u.u_vm.vm_majflt;
+                       vp->v_majflt = u.u_ru.ru_majflt;
                        if (mproc->p_textp)
                                vp->v_txtswrss = xp->x_swrss;
                }
                vp->v_pctcpu = pcpu();
 #undef e
        } else if (uflg)
                        if (mproc->p_textp)
                                vp->v_txtswrss = xp->x_swrss;
                }
                vp->v_pctcpu = pcpu();
 #undef e
        } else if (uflg)
-               sp->sun.u_pctcpu = pcpu();
+               sp->s_un.u_pctcpu = pcpu();
        else if (sflg) {
                if (ap->a_stat != SZOMB) {
                        for (cp = (char *)u.u_stack;
                            cp < &user.upages[UPAGES][0]; )
                                if (*cp++)
                                        break;
        else if (sflg) {
                if (ap->a_stat != SZOMB) {
                        for (cp = (char *)u.u_stack;
                            cp < &user.upages[UPAGES][0]; )
                                if (*cp++)
                                        break;
-                       sp->sun.s_ssiz = (&user.upages[UPAGES][0] - cp);
+                       sp->s_un.s_ssiz = (&user.upages[UPAGES][0] - cp);
                }
        }
                }
        }
+
        npr++;
 }
 
        npr++;
 }
 
@@ -690,15 +829,15 @@ pcpu()
 getu()
 {
        struct pte *pteaddr, apte;
 getu()
 {
        struct pte *pteaddr, apte;
-       int pad1;       /* avoid hardware botch */
        struct pte arguutl[UPAGES+CLSIZE];
        struct pte arguutl[UPAGES+CLSIZE];
-       int pad2;       /* avoid hardware botch */
        register int i;
        int ncl, size;
 
        size = sflg ? ctob(UPAGES) : sizeof (struct user);
        if ((mproc->p_flag & SLOAD) == 0) {
        register int i;
        int ncl, size;
 
        size = sflg ? ctob(UPAGES) : sizeof (struct user);
        if ((mproc->p_flag & SLOAD) == 0) {
-               lseek(swap, ctob(mproc->p_swaddr), 0);
+               if (swap < 0)
+                       return (0);
+               (void) lseek(swap, (long)dtob(mproc->p_swaddr), 0);
                if (read(swap, (char *)&user.user, size) != size) {
                        fprintf(stderr, "ps: cant read u for pid %d from %s\n",
                            mproc->p_pid, swapf);
                if (read(swap, (char *)&user.user, size) != size) {
                        fprintf(stderr, "ps: cant read u for pid %d from %s\n",
                            mproc->p_pid, swapf);
@@ -708,18 +847,19 @@ getu()
                argaddr = 0;
                return (1);
        }
                argaddr = 0;
                return (1);
        }
-       pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
-       lseek(kmem, kflg ? clear(pteaddr) : (int)pteaddr, 0);
+       pteaddr = &Usrptmap[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
+       klseek(kmem, (long)pteaddr, 0);
        if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
                printf("ps: cant read indir pte to get u for pid %d from %s\n",
        if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
                printf("ps: cant read indir pte to get u for pid %d from %s\n",
-                   mproc->p_pid, swapf);
+                   mproc->p_pid, kmemf);
                return (0);
        }
        lseek(mem,
                return (0);
        }
        lseek(mem,
-           ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte), 0);
+           (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
+               0);
        if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
                printf("ps: cant read page table for u of pid %d from %s\n",
        if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
                printf("ps: cant read page table for u of pid %d from %s\n",
-                   mproc->p_pid, swapf);
+                   mproc->p_pid, memf);
                return (0);
        }
        if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
                return (0);
        }
        if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
@@ -730,7 +870,7 @@ getu()
        ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
        while (--ncl >= 0) {
                i = ncl * CLSIZE;
        ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
        while (--ncl >= 0) {
                i = ncl * CLSIZE;
-               lseek(mem, ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
+               lseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
                if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
                        printf("ps: cant read page %d of u of pid %d from %s\n",
                            arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
                if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
                        printf("ps: cant read page %d of u of pid %d from %s\n",
                            arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
@@ -743,36 +883,39 @@ getu()
 char *
 getcmd()
 {
 char *
 getcmd()
 {
-       char cmdbuf[BUFSIZ];
-       int pad1;               /* avoid hardware botch */
+       char cmdbuf[CLSIZE*NBPG];
        union {
                char    argc[CLSIZE*NBPG];
                int     argi[CLSIZE*NBPG/sizeof (int)];
        } argspac;
        union {
                char    argc[CLSIZE*NBPG];
                int     argi[CLSIZE*NBPG/sizeof (int)];
        } argspac;
-       int pad2;               /* avoid hardware botch */
        register char *cp;
        register int *ip;
        char c;
        int nbad;
        struct dblock db;
        register char *cp;
        register int *ip;
        char c;
        int nbad;
        struct dblock db;
+       char *file;
 
        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                return ("");
        if (cflg) {
 
        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                return ("");
        if (cflg) {
-               strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm));
+               (void) strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm));
                return (savestr(cmdbuf));
        }
        if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) {
                return (savestr(cmdbuf));
        }
        if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) {
+               if (swap < 0)
+                       goto retucomm;
                vstodb(0, CLSIZE, &u.u_smap, &db, 1);
                vstodb(0, CLSIZE, &u.u_smap, &db, 1);
-               lseek(swap, ctob(db.db_base), 0);
+               (void) lseek(swap, (long)dtob(db.db_base), 0);
                if (read(swap, (char *)&argspac, sizeof(argspac))
                    != sizeof(argspac))
                        goto bad;
                if (read(swap, (char *)&argspac, sizeof(argspac))
                    != sizeof(argspac))
                        goto bad;
+               file = swapf;
        } else {
        } else {
-               lseek(mem, argaddr, 0);
+               lseek(mem, (long)argaddr, 0);
                if (read(mem, (char *)&argspac, sizeof (argspac))
                    != sizeof (argspac))
                        goto bad;
                if (read(mem, (char *)&argspac, sizeof (argspac))
                    != sizeof (argspac))
                        goto bad;
+               file = memf;
        }
        ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)];
        ip -= 2;                /* last arg word and .long 0 */
        }
        ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)];
        ip -= 2;                /* last arg word and .long 0 */
@@ -803,41 +946,37 @@ getcmd()
        while (*--cp == ' ')
                *cp = 0;
        cp = (char *)ip;
        while (*--cp == ' ')
                *cp = 0;
        cp = (char *)ip;
-       strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp);
+       (void) strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp);
        if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
        if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
-               strcat(cmdbuf, " (");
-               strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
-               strcat(cmdbuf, ")");
+               (void) strcat(cmdbuf, " (");
+               (void) strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
+               (void) strcat(cmdbuf, ")");
        }
        }
-/*
-       if (xflg == 0 && gflg == 0 && tptr == 0 && cp[0] == '-')
-               return (0);
-*/
        return (savestr(cmdbuf));
 
 bad:
        return (savestr(cmdbuf));
 
 bad:
-       fprintf(stderr, "ps: error locating command name for pid %d\n",
-           mproc->p_pid);
+       fprintf(stderr, "ps: error locating command name for pid %d from %s\n",
+           mproc->p_pid, file);
 retucomm:
 retucomm:
-       strcpy(cmdbuf, " (");
-       strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
-       strcat(cmdbuf, ")");
+       (void) strcpy(cmdbuf, " (");
+       (void) strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
+       (void) strcat(cmdbuf, ")");
        return (savestr(cmdbuf));
 }
 
 char   *lhdr =
        return (savestr(cmdbuf));
 }
 
 char   *lhdr =
-"     F UID   PID  PPID CP PRI NI ADDR  SZ  RSS WCHAN STAT TT  TIME";
+"      F UID   PID  PPID CP PRI NI ADDR  SZ  RSS WCHAN  STAT TT  TIME";
 lpr(sp)
        struct savcom *sp;
 {
        register struct asav *ap = sp->ap;
 lpr(sp)
        struct savcom *sp;
 {
        register struct asav *ap = sp->ap;
-       register struct lsav *lp = sp->sun.lp;
+       register struct lsav *lp = sp->s_un.lp;
 
 
-       printf("%6x%4d%6u%6u%3d%4d%3d%5x%4d%5d",
+       printf("%7x%4d%6u%6u%3d%4d%3d%5x%4d%5d",
            ap->a_flag, ap->a_uid,
            ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO,
            ap->a_flag, ap->a_uid,
            ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO,
-           ap->a_nice-NZERO, lp->l_addr, ap->a_size/2, ap->a_rss/2);
-       printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
+           ap->a_nice-NZERO, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss));
+       printf(lp->l_wchan ? " %6x" : "       ", (int)lp->l_wchan&0xffffff);
        printf(" %4.4s ", state(ap));
        ptty(ap->a_tty);
        ptime(ap);
        printf(" %4.4s ", state(ap));
        ptty(ap->a_tty);
        ptime(ap);
@@ -854,7 +993,7 @@ ptime(ap)
        struct asav *ap;
 {
 
        struct asav *ap;
 {
 
-       printf("%3ld:%02ld", ap->a_cpu / hz, ap->a_cpu % hz);
+       printf("%3ld:%02ld", ap->a_cpu / 60, ap->a_cpu % 60);
 }
 
 char   *uhdr =
 }
 
 char   *uhdr =
@@ -865,12 +1004,12 @@ upr(sp)
        register struct asav *ap = sp->ap;
        int vmsize, rmsize;
 
        register struct asav *ap = sp->ap;
        int vmsize, rmsize;
 
-       vmsize = (ap->a_size + ap->a_tsiz)/2;
-       rmsize = ap->a_rss/2;
+       vmsize = pgtok((ap->a_size + ap->a_tsiz));
+       rmsize = pgtok(ap->a_rss);
        if (ap->a_xccount)
        if (ap->a_xccount)
-               rmsize += ap->a_txtrss/ap->a_xccount/2;
+               rmsize += pgtok(ap->a_txtrss/ap->a_xccount);
        printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
        printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
-           getname(ap->a_uid), ap->a_pid, sp->sun.u_pctcpu, pmem(ap),
+           getname(ap->a_uid), ap->a_pid, sp->s_un.u_pctcpu, pmem(ap),
            vmsize, rmsize);
        putchar(' ');
        ptty(ap->a_tty);
            vmsize, rmsize);
        putchar(' ');
        ptty(ap->a_tty);
@@ -879,11 +1018,11 @@ upr(sp)
 }
 
 char *vhdr =
 }
 
 char *vhdr =
-"  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM";
+" SIZE  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM"+5;
 vpr(sp)
        struct savcom *sp;
 {
 vpr(sp)
        struct savcom *sp;
 {
-       register struct vsav *vp = sp->sun.vp;
+       register struct vsav *vp = sp->s_un.vp;
        register struct asav *ap = sp->ap;
 
        printf("%5u ", ap->a_pid);
        register struct asav *ap = sp->ap;
 
        printf("%5u ", ap->a_pid);
@@ -893,13 +1032,13 @@ vpr(sp)
        printf("%3d%3d%7d%5d%5d",
           ap->a_slptime > 99 ? 99 : ap-> a_slptime,
           ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt,
        printf("%3d%3d%7d%5d%5d",
           ap->a_slptime > 99 ? 99 : ap-> a_slptime,
           ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt,
-          ap->a_size/2, ap->a_rss/2);
-       if (ap->a_maxrss == (INFINITY/NBPG))
+          pgtok(ap->a_size), pgtok(ap->a_rss));
+       if (ap->a_maxrss == (RLIM_INFINITY/NBPG))
                printf("   xx");
        else
                printf("   xx");
        else
-               printf("%5d", ap->a_maxrss/2);
+               printf("%5d", pgtok(ap->a_maxrss));
        printf("%5d%4d%5.1f%5.1f",
        printf("%5d%4d%5.1f%5.1f",
-          ap->a_tsiz/2, ap->a_txtrss/2, vp->v_pctcpu, pmem(ap));
+          pgtok(ap->a_tsiz), pgtok(ap->a_txtrss), vp->v_pctcpu, pmem(ap));
 }
 
 char   *shdr =
 }
 
 char   *shdr =
@@ -910,7 +1049,7 @@ spr(sp)
        register struct asav *ap = sp->ap;
 
        if (sflg)
        register struct asav *ap = sp->ap;
 
        if (sflg)
-               printf("%4d ", sp->sun.s_ssiz);
+               printf("%4d ", sp->s_un.s_ssiz);
        printf("%5u", ap->a_pid);
        putchar(' ');
        ptty(ap->a_tty);
        printf("%5u", ap->a_pid);
        putchar(' ');
        ptty(ap->a_tty);
@@ -979,14 +1118,16 @@ vstodb(vsbase, vssize, dmp, dbp, rev)
        struct dmap *dmp;
        register struct dblock *dbp;
 {
        struct dmap *dmp;
        register struct dblock *dbp;
 {
-       register int blk = DMMIN;
+       register int blk = dmmin;
        register swblk_t *ip = dmp->dm_map;
 
        register swblk_t *ip = dmp->dm_map;
 
+       vsbase = ctod(vsbase);
+       vssize = ctod(vssize);
        if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
                panic("vstodb");
        while (vsbase >= blk) {
                vsbase -= blk;
        if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
                panic("vstodb");
        while (vsbase >= blk) {
                vsbase -= blk;
-               if (blk < DMMAX)
+               if (blk < dmmax)
                        blk *= 2;
                ip++;
        }
                        blk *= 2;
                ip++;
        }
@@ -1018,7 +1159,7 @@ pscomp(s1, s2)
        register int i;
 
        if (uflg)
        register int i;
 
        if (uflg)
-               return (s2->sun.u_pctcpu > s1->sun.u_pctcpu ? 1 : -1);
+               return (s2->s_un.u_pctcpu > s1->s_un.u_pctcpu ? 1 : -1);
        if (vflg)
                return (vsize(s2) - vsize(s1));
        i = s1->ap->a_ttyd - s2->ap->a_ttyd;
        if (vflg)
                return (vsize(s2) - vsize(s1));
        i = s1->ap->a_ttyd - s2->ap->a_ttyd;
@@ -1031,7 +1172,7 @@ vsize(sp)
        struct savcom *sp;
 {
        register struct asav *ap = sp->ap;
        struct savcom *sp;
 {
        register struct asav *ap = sp->ap;
-       register struct vsav *vp = sp->sun.vp;
+       register struct vsav *vp = sp->s_un.vp;
        
        if (ap->a_flag & SLOAD)
                return (ap->a_rss +
        
        if (ap->a_flag & SLOAD)
                return (ap->a_rss +
@@ -1039,76 +1180,126 @@ vsize(sp)
        return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss));
 }
 
        return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss));
 }
 
-#define        NMAX    8
-#define        NUID    2048
+#define        NMAX    8       /* sizeof loginname (should be sizeof (utmp.ut_name)) */
+#define NUID   2048    /* must not be a multiple of 5 */
 
 
-char   names[NUID][NMAX+1];
+struct nametable {
+       char    nt_name[NMAX+1];
+       int     nt_uid;
+} nametable[NUID];
 
 
-/*
- * Stolen from ls...
- */
-char *
-getname(uid)
+struct nametable *
+findslot(uid)
+int    uid;
 {
 {
-       register struct passwd *pw;
-       static init;
-       struct passwd *getpwent();
-
-       if (uid >= 0 && uid < NUID && names[uid][0])
-               return (&names[uid][0]);
-       if (init == 2)
-               return (0);
-       if (init == 0)
-               setpwent(), init = 1;
-       while (pw = getpwent()) {
-               if (pw->pw_uid >= NUID)
-                       continue;
-               if (names[pw->pw_uid][0])
-                       continue;
-               strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
-               if (pw->pw_uid == uid)
-                       return (&names[uid][0]);
+       register struct nametable       *n, *start;
+
+       /*
+        * find the uid or an empty slot.
+        * return NULL if neither found.
+        */
+
+       n = start = nametable + (uid % (NUID - 20));
+       while (n->nt_name[0] && n->nt_uid != uid) {
+               if ((n += 5) >= &nametable[NUID])
+                       n -= NUID;
+               if (n == start)
+                       return((struct nametable *)NULL);
        }
        }
-       init = 2;
-       endpwent();
-       return (0);
+       return(n);
 }
 
 }
 
-char   *freebase;
-int    nleft;
-
 char *
 char *
-alloc(size)
-       int size;
+getname(uid)
 {
 {
-       register char *cp;
-       register int i;
-
-       if (size > nleft) {
-               freebase = (char *)sbrk(i = size > 2048 ? size : 2048);
-               if (freebase == 0) {
-                       fprintf(stderr, "ps: ran out of memory\n");
-                       exit(1);
-               }
-               nleft = i - size;
-       } else
-               nleft -= size;
-       cp = freebase;
-       for (i = size; --i >= 0; )
-               *cp++ = 0;
-       freebase = cp;
-       return (cp - size);
+       register struct passwd          *pw;
+       static                          init = 0;
+       struct passwd                   *getpwent();
+       register struct nametable       *n;
+       extern int                      _pw_stayopen;
+
+       /*
+        * find uid in hashed table; add it if not found.
+        * return pointer to name.
+        */
+
+       if ((n = findslot(uid)) == NULL)
+               return((char *)NULL);
+
+       if (n->nt_name[0])      /* occupied? */
+               return(n->nt_name);
+
+       switch (init) {
+               case 0:
+                       setpwent();
+                       _pw_stayopen = 1;
+                       init = 1;
+                       /* intentional fall-thru */
+               case 1:
+                       while (pw = getpwent()) {
+                               if (pw->pw_uid < 0)
+                                       continue;
+                               if ((n = findslot(pw->pw_uid)) == NULL) {
+                                       endpwent();
+                                       init = 2;
+                                       return((char *)NULL);
+                               }
+                               if (n->nt_name[0])
+                                       continue;       /* duplicate, not uid */
+                               strncpy(n->nt_name, pw->pw_name, NMAX);
+                               n->nt_uid = pw->pw_uid;
+                               if (pw->pw_uid == uid)
+                                       return (n->nt_name);
+                       }
+                       endpwent();
+                       init = 2;
+                       /* intentional fall-thru */
+               case 2:
+                       return ((char *)NULL);
+       }
+       /* NOTREACHED */
 }
 
 char *
 savestr(cp)
        char *cp;
 {
 }
 
 char *
 savestr(cp)
        char *cp;
 {
-       register int len;
+       register unsigned len;
        register char *dp;
 
        len = strlen(cp);
        register char *dp;
 
        len = strlen(cp);
-       dp = (char *)alloc(len+1);
-       strcpy(dp, cp);
+       dp = (char *)calloc(len+1, sizeof (char));
+       (void) strcpy(dp, cp);
        return (dp);
 }
        return (dp);
 }
+
+/*
+ * This routine was stolen from adb to simulate memory management
+ * on the VAX.
+ */
+off_t
+vtophys(loc)
+long   loc;
+{
+       register        p;
+       off_t   newloc;
+
+       newloc = loc & ~0xc0000000;
+       p = btop(newloc);
+       if ((loc & 0xc0000000) == 0) {
+               fprintf(stderr, "Vtophys: translating non-kernel address\n");
+               return((off_t) -1);
+       }
+       if (p >= Syssize) {
+               fprintf(stderr, "Vtophys: page out of bound (%d>=%d)\n",
+                       p, Syssize);
+               return((off_t) -1);
+       }
+       if (Sysmap[p].pg_v == 0
+       && (Sysmap[p].pg_fod || Sysmap[p].pg_pfnum == 0)) {
+               fprintf(stderr, "Vtophys: page not valid\n");
+               return((off_t) -1);
+       }
+       loc = (long) (ptob(Sysmap[p].pg_pfnum) + (loc & PGOFSET));
+       return(loc);
+}