sigh, I give up making this fit on a line (need to squish flags)
[unix-history] / usr / src / bin / ps / ps.c
index 11414d9..5d1dcbc 100644 (file)
-#ifndef lint
-static char *sccsid = "@(#)ps.c        4.17 (Berkeley) %G%";
-#endif
-
 /*
 /*
- * ps
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
  */
 
  */
 
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)ps.c       5.14 (Berkeley) %G%";
+#endif not lint
+
 #include <stdio.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <ctype.h>
-#include <nlist.h>
+#include <a.out.h>
 #include <pwd.h>
 #include <sys/param.h>
 #include <pwd.h>
 #include <sys/param.h>
+#include <sys/ioctl.h>
 #include <sys/tty.h>
 #include <sys/tty.h>
-#include <dir.h>
+#include <sys/dir.h>
 #include <sys/user.h>
 #include <sys/proc.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" },
+char *nl_names[] = {
+       "_proc",
 #define        X_PROC          0
 #define        X_PROC          0
-       { "_Usrptmap" },
-#define        X_USRPTMA       1
-       { "_usrpt" },
+       "_Usrptmap",
+#define        X_USRPTMAP      1
+       "_usrpt",
 #define        X_USRPT         2
 #define        X_USRPT         2
-       { "_text" },
+       "_text",
 #define        X_TEXT          3
 #define        X_TEXT          3
-       { "_nswap" },
+       "_nswap",
 #define        X_NSWAP         4
 #define        X_NSWAP         4
-       { "_maxslp" },
+       "_maxslp",
 #define        X_MAXSLP        5
 #define        X_MAXSLP        5
-       { "_ccpu" },
+       "_ccpu",
 #define        X_CCPU          6
 #define        X_CCPU          6
-       { "_ecmx" },
+       "_ecmx",
 #define        X_ECMX          7
 #define        X_ECMX          7
-       { "_nproc" },
+       "_nproc",
 #define        X_NPROC         8
 #define        X_NPROC         8
-       { "_ntext" },
+       "_ntext",
 #define        X_NTEXT         9
 #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
+       "_inode",
+#define X_INODE                14
+       "_file",
+#define X_FILE         15
+       "_cfree",
+#define X_CFREE                16
+       "_callout",
+#define X_CALLOUT      17
+       "_swapmap",
+#define X_SWAPMAP      18
+       "_argmap",
+#define X_ARGMAP       19
+       "_kernelmap",
+#define X_KERNELMAP    20
+       "_mbmap",
+#define X_MBMAP                21
+       "_namecache",
+#define X_NCH          22
+       "_quota",
+#define X_QUOTA                23
+       "_dquot",
+#define X_DQUOT                24
+       "_swbuf",
+#define X_SWBUF                25
+       "_buf",
+#define X_BUF          26
+       "_cmap",
+#define X_CMAP         27
+       "_buffers",
+#define X_BUFFERS      28
+       ""
 };
 
 };
 
+struct nlist *nl;                      /* all because we can't init unions */
+int nllen;                             /* # of nlist entries */
+
 struct savcom {
        union {
                struct  lsav *lp;
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
 struct savcom {
        union {
                struct  lsav *lp;
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
-               struct  sssav *ssp;
-       } sun;
+       } s_un;
        struct  asav *ap;
 } *savcom;
 
        struct  asav *ap;
 } *savcom;
 
@@ -72,20 +120,14 @@ struct     asav {
 };
 
 char   *lhdr;
 };
 
 char   *lhdr;
+int    wcwidth;                /* width of the wchan field for sprintf*/
 struct lsav {
        short   l_ppid;
 struct lsav {
        short   l_ppid;
-       char    l_cpu;
+       u_char  l_cpu;
        int     l_addr;
        caddr_t l_wchan;
 };
 
        int     l_addr;
        caddr_t l_wchan;
 };
 
-char   *sshdr;
-struct sssav {
-       short   ss_ppid;
-       short   ss_brother;
-       short   ss_sons;
-};
-
 char   *uhdr;
 char   *shdr;
 
 char   *uhdr;
 char   *shdr;
 
@@ -96,7 +138,9 @@ 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;
 
@@ -106,42 +150,94 @@ union {
 } user;
 #define u      user.user
 
 } user;
 #define u      user.user
 
-#define clear(x)       ((int)x & 0x7fffffff)
+#ifndef        PSFILE
+char   *psdb   = "/etc/psdatabase";
+#else
+char   *psdb   = PSFILE;
+#endif
 
 
-int    chkpid;
-int    aflg, cflg, eflg, gflg, kflg, lflg, sflg, ssflg,
-       nonssflg, uflg, vflg, xflg;
+int    chkpid = -1;
+int    aflg, cflg, eflg, gflg, kflg, lflg, nflg, sflg,
+       uflg, vflg, xflg, Uflg;
+int    nchans;                         /* total # of wait channels */
 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   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
+char   *strncpy(), *index(), *ttyname(), mytty[MAXPATHLEN+1];
+char   *malloc(), *getchan();
 long   lseek();
 long   lseek();
+off_t  vtophys();
 double pcpu(), pmem();
 double pcpu(), pmem();
+int    wchancomp();
 int    pscomp();
 int    nswap, maxslp;
 struct text *atext;
 double ccpu;
 int    ecmx;
 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;
+
+int    nttys;
 
 struct ttys {
 
 struct ttys {
-       char    name[MAXNAMLEN+1];
        dev_t   ttyd;
        dev_t   ttyd;
-       struct  ttys *next;
-       struct  ttys *cand;
-} *allttys, *cand[16];
+       int cand;
+       char    name[MAXNAMLEN+1];
+} *allttys;
+int cand[16] = {-1, -1, -1, -1, -1, -1, -1, -1,
+               -1, -1, -1, -1, -1, -1, -1, -1};
+struct lttys {
+       struct ttys ttys;
+       struct lttys *next;
+} *lallttys;
+
+/*
+ * struct for the symbolic wait channel info
+ *
+ * WNAMESIZ is the max # of chars saved of the symbolic wchan gleaned
+ * from the namelist.  Normally, only WSNAMESIZ are printed in the long
+ * format, unless the terminal width is greater than WTSIZ wide.
+ */
+#define WNAMESIZ       12
+#define WSNAMESIZ      6
+#define WTSIZ          95
+
+struct wchan {
+       char    wc_name[WNAMESIZ+1];    /* symbolic name */
+       caddr_t wc_caddr;               /* addr in kmem */
+} *wchanhd;                            /* an array sorted by wc_caddr */
+
+#define NWCINDEX       10              /* the size of the index array */
+
+caddr_t wchan_index[NWCINDEX];         /* used to speed searches */
+/*
+ * names listed here are not kept as wait channels -- this is used to 
+ * remove names that confuse ps, like symbols that define the end of an
+ * array that happen to be equal to the next symbol.
+ */
+char *wchan_stop_list[] = {
+       "umbabeg",
+       "umbaend",
+       "calimit",
+       NULL
+};
 
 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;
 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;
@@ -150,71 +246,78 @@ 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;
-
-       if (ap = rindex(argv[0], '/'))
-               ap++;
+       if (ioctl(1, TIOCGWINSZ, &win) == -1)
+               twidth = 80;
        else
        else
-               ap = argv[0];
-       if (*ap == 's')                         /* If name starts with 's' */
-               ssflg++;
-
+               twidth = (win.ws_col == 0 ? 80 : win.ws_col);
        argc--, argv++;
        if (argc > 0) {
                ap = argv[0];
                while (*ap) switch (*ap++) {
 
                case 'C':
        argc--, argv++;
        if (argc > 0) {
                ap = argv[0];
                while (*ap) switch (*ap++) {
 
                case 'C':
-                       rawcpu++; nonssflg++;
+                       rawcpu++;
                        break;
                case 'S':
                        sumcpu++;
                        break;
                        break;
                case 'S':
                        sumcpu++;
                        break;
+
+               case 'U':
+                       Uflg++;
+                       break;
+
                case 'a':
                        aflg++;
                        break;
                case 'c':
                case 'a':
                        aflg++;
                        break;
                case 'c':
-                       cflg = !cflg; nonssflg++;
+                       cflg = !cflg;
                        break;
                case 'e':
                        break;
                case 'e':
-                       eflg++; nonssflg++;
+                       eflg++;
                        break;
                case 'g':
                        break;
                case 'g':
-                       gflg++; nonssflg++;
+                       gflg++;
                        break;
                case 'k':
                        break;
                case 'k':
-                       kflg++; nonssflg++;
+                       kflg++;
                        break;
                case 'l':
                        break;
                case 'l':
-                       lflg++; nonssflg++;
+                       lflg++;
+                       break;
+               case 'n':
+                       nflg++;
                        break;
                case 's':
                        break;
                case 's':
-                       sflg++; nonssflg++;
+                       sflg++;
                        break;
                case 't':
                        if (*ap)
                                tptr = ap;
                        break;
                case 't':
                        if (*ap)
                                tptr = ap;
-                       aflg++; nonssflg++;
+                       else if ((tptr = ttyname(0)) != 0) {
+                               tptr = strcpy(mytty, tptr);
+                               if (strncmp(tptr, "/dev/", 5) == 0)
+                                       tptr += 5;
+                       }
+                       if (strncmp(tptr, "tty", 3) == 0)
+                               tptr += 3;
+                       aflg++;
                        gflg++;
                        gflg++;
-                       if (*tptr == '?')
+                       if (tptr && *tptr == '?')
                                xflg++;
                        while (*ap)
                                ap++;
                        break;
                case 'u': 
                                xflg++;
                        while (*ap)
                                ap++;
                        break;
                case 'u': 
-                       uflg++; nonssflg++;
+                       uflg++;
                        break;
                case 'v':
                        cflg = 1;
                        break;
                case 'v':
                        cflg = 1;
-                       vflg++; nonssflg++;
+                       vflg++;
                        break;
                case 'w':
                        break;
                case 'w':
-                       if (twidth == 80)
+                       if (twidth < 132)
                                twidth = 132;
                        else
                                twidth = BUFSIZ;
                                twidth = 132;
                        else
                                twidth = BUFSIZ;
@@ -222,45 +325,33 @@ main(argc, argv)
                case 'x':
                        xflg++;
                        break;
                case 'x':
                        xflg++;
                        break;
-               case 'y':               /* Rand 2/81 */
-                       ssflg++;
-                       break;
                default:
                        if (!isdigit(ap[-1]))
                                break;
                        chkpid = atoi(--ap);
                        *ap = 0;
                default:
                        if (!isdigit(ap[-1]))
                                break;
                        chkpid = atoi(--ap);
                        *ap = 0;
-                       aflg++; nonssflg++;
+                       aflg++;
                        xflg++;
                        break;
                }
        }
                        xflg++;
                        break;
                }
        }
-       if (ssflg) {
-               if (nonssflg) {
-                       fprintf (stderr, "Usage: ss [axwS]\n");
-                       exit(1);
-               }
-               uflg++;
-               gflg++;
-       }
-
        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) {
+       savcom = (struct savcom *)calloc((unsigned) nproc, sizeof (*savcom));
+       for (i=0; i<nproc; i += NPROC) {
                klseek(kmem, (long)procp, 0);
                j = nproc - i;
                klseek(kmem, (long)procp, 0);
                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];
@@ -270,8 +361,9 @@ main(argc, argv)
                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
                            mproc->p_ppid == 1)
                                continue;
                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
                            mproc->p_ppid == 1)
                                continue;
-                       if (uid != mproc->p_uid && aflg==0 ||
-                           chkpid != 0 && chkpid != mproc->p_pid)
+                       if (uid != mproc->p_uid && aflg==0)
+                               continue;
+                       if (chkpid != -1 && 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 ||
@@ -285,11 +377,10 @@ main(argc, argv)
                        save();
                }
        }
                        save();
                }
        }
-       qsort(savcom, npr, sizeof(savcom[0]), pscomp);
-       if (ssflg) {
-               walk(npr);
-               exit (npr == 0);
-       }
+       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)
@@ -300,16 +391,14 @@ main(argc, argv)
                        upr(sp);
                else
                        spr(sp);
                        upr(sp);
                else
                        spr(sp);
-               if (sp->ap->a_flag & SWEXIT)
-                       printf(" <exiting>");
-               else if (sp->ap->a_stat == SZOMB)
-                       printf(" <defunct>");
+               if (sp->ap->a_stat == SZOMB)
+                       printf(" %.*s", twidth - cmdstart - 2, "<defunct>");
+               else if (sp->ap->a_flag & SWEXIT)
+                       printf(" %.*s", twidth - cmdstart - 2, "<exiting>");
                else if (sp->ap->a_pid == 0)
                else if (sp->ap->a_pid == 0)
-                       printf(" swapper");
+                       printf(" %.*s", twidth - cmdstart - 2, "swapper");
                else if (sp->ap->a_pid == 2)
                else if (sp->ap->a_pid == 2)
-                       printf(" pagedaemon");
-               else if (sp->ap->a_pid == 3 && sp->ap->a_flag & SSYS)
-                       printf(" ip input");
+                       printf(" %.*s", twidth - cmdstart - 2, "pagedaemon");
                else
                        printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp);
                printf("\n");
                else
                        printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp);
                printf("\n");
@@ -320,7 +409,7 @@ main(argc, argv)
 getw(loc)
        unsigned long loc;
 {
 getw(loc)
        unsigned long loc;
 {
-       long word;
+       int word;
 
        klseek(kmem, (long)loc, 0);
        if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
 
        klseek(kmem, (long)loc, 0);
        if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
@@ -333,19 +422,121 @@ klseek(fd, loc, off)
        long loc;
        int off;
 {
        long loc;
        int off;
 {
-
-       if (kflg)
-               loc &= 0x7fffffff;
+       if (kflg) {
+               if ((loc = vtophys(loc)) == -1)
+                       return;
+       }
        (void) lseek(fd, (long)loc, off);
 }
 
        (void) lseek(fd, (long)loc, off);
 }
 
+/*
+ * Version allows change of db format w/o temporarily bombing ps's
+ */
+char thisversion[4] = "V2";            /* length must remain 4 */
+
+writepsdb(unixname)
+       char *unixname;
+{
+       register FILE *fp;
+       struct lttys *lt;
+       struct stat stb;
+
+       setgid(getgid());
+       setuid(getuid());
+       if ((fp = fopen(psdb, "w")) == NULL) {
+               perror(psdb);
+               exit(1);
+       } else
+               fchmod(fileno(fp), 0644);
+
+       fwrite(thisversion, sizeof thisversion, 1, fp);
+       fwrite(unixname, strlen(unixname) + 1, 1, fp);
+       if (stat(unixname, &stb) < 0)
+               stb.st_mtime = 0;
+       fwrite((char *) &stb.st_mtime, sizeof stb.st_mtime, 1, fp);
+
+       fwrite((char *) &nllen, sizeof nllen, 1, fp);
+       fwrite((char *) nl, sizeof (struct nlist), nllen, fp);
+       fwrite((char *) cand, sizeof (cand), 1, fp);
+       fwrite((char *) &nttys, sizeof nttys, 1, fp);
+       for (lt = lallttys ; lt ; lt = lt->next)
+               fwrite((char *)&lt->ttys, sizeof (struct ttys), 1, fp);
+       fwrite((char *) &nchans, sizeof nchans, 1, fp);
+       fwrite((char *) wchanhd, sizeof (struct wchan), nchans, fp);
+       fwrite((char *) wchan_index, sizeof (caddr_t), NWCINDEX, fp);
+       fclose(fp);
+}
+
+readpsdb(unixname)
+       char *unixname;
+{
+       register i;
+       register FILE *fp;
+       char unamebuf[BUFSIZ];
+       char *p = unamebuf;
+       char dbversion[sizeof thisversion];
+       struct stat stb;
+       time_t dbmtime;
+       extern int errno;
+
+       if ((fp = fopen(psdb, "r")) == NULL) {
+               if (errno == ENOENT)
+                       return (0);
+               perror(psdb);
+               exit(1);
+       }
+
+       /*
+        * Does the db file match this unix?
+        */
+       fread(dbversion, sizeof dbversion, 1, fp);
+       if (bcmp(thisversion, dbversion, sizeof thisversion))
+               goto bad;
+       while ((*p = getc(fp)) != '\0')
+               p++;
+       if (strcmp(unixname, unamebuf))
+               goto bad;
+       fread((char *) &dbmtime, sizeof dbmtime, 1, fp);
+       if (stat(unixname, &stb) < 0)
+               stb.st_mtime = 0;
+       if (stb.st_mtime != dbmtime)
+               goto bad;
+
+       fread((char *) &nllen, sizeof nllen, 1, fp);
+       nl = (struct nlist *) malloc (nllen * sizeof (struct nlist));
+       fread((char *) nl, sizeof (struct nlist), nllen, fp);
+       fread((char *) cand, sizeof (cand), 1, fp);
+       fread((char *) &nttys, sizeof nttys, 1, fp);
+       allttys = (struct ttys *)malloc(sizeof(struct ttys)*nttys);
+       if (allttys == NULL) {
+               fprintf(stderr, "ps: Can't malloc space for tty table\n");
+               exit(1);
+       }
+       fread((char *) allttys, sizeof (struct ttys), nttys, fp);
+       fread((char *) &nchans, sizeof nchans, 1, fp);
+       wchanhd = (struct wchan *) malloc(nchans * sizeof (struct wchan));
+       if (wchanhd == NULL) {
+               fprintf(stderr, "ps: Can't malloc space for wait channels\n");
+               nflg++;
+               fseek(fp, (long) nchans * sizeof (struct wchan), 1);
+       } else
+               fread((char *) wchanhd, sizeof (struct wchan), nchans, fp);
+       fread((char *) wchan_index, sizeof (caddr_t), NWCINDEX, fp);
+       fclose(fp);
+       return(1);
+
+bad:
+       fclose(fp);
+       return(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);
@@ -355,37 +546,71 @@ 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;
+       int faildb = 0;                 /* true if psdatabase init failed */
+       int i;
+
+       nlistf = argc > 1 ? argv[1] : "/vmunix";
+       if (Uflg) {
+               init_nlist();
+               nlist(nlistf, nl);
+               getvchans();
+               getdev();
+               writepsdb(nlistf);
+               exit (0);
+       } else if (!readpsdb(nlistf)) {
+               init_nlist();
+               if (!kflg)
+                       nl[X_SYSMAP].n_un.n_name = "";
+               faildb = 1;
+               nlist(nlistf, nl);
+               nttys = 0;
+               getdev();
+       }
 
 
-       nlistf = argc > 3 ? argv[3] : "/vmunix";
-       nlist(nlistf, nl);
        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 &= ~KERNBASE;
+               (void) lseek(kmem, addr, 0);
+               read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte));
+       }
+       if (faildb)
+               getvchans();
        usrpt = (struct pte *)nl[X_USRPT].n_value;
        usrpt = (struct pte *)nl[X_USRPT].n_value;
+       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);
        klseek(kmem, (long)nl[X_NSWAP].n_value, 0);
        if (read(kmem, (char *)&nswap, sizeof (nswap)) != sizeof (nswap)) {
                cantread("nswap", kmemf);
@@ -408,7 +633,8 @@ getkvars(argc, argv)
        }
        if (uflg || vflg) {
                ntext = getw(nl[X_NTEXT].n_value);
        }
        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);
                if (text == 0) {
                        fprintf(stderr, "no room for text table\n");
                        exit(1);
@@ -421,8 +647,47 @@ getkvars(argc, argv)
                        exit(1);
                }
        }
                        exit(1);
                }
        }
+       dmmin = getw(nl[X_DMMIN].n_value);
+       dmmax = getw(nl[X_DMMAX].n_value);
 }
 
 }
 
+/*
+ * get the valloc'ed kernel variables for symbolic wait channels
+ */
+getvchans()
+{
+       int i, tmp;
+
+       if (nflg)
+               return;
+
+#define addv(i)        addchan(&nl[i].n_un.n_name[1], getw(nl[i].n_value))
+       addv(X_INODE);
+       addv(X_FILE);
+       addv(X_PROC);
+       addv(X_TEXT);
+       addv(X_CFREE);
+       addv(X_CALLOUT);
+       addv(X_SWAPMAP);
+       addv(X_ARGMAP);
+       addv(X_KERNELMAP);
+       addv(X_MBMAP);
+       addv(X_NCH);
+       if (nl[X_QUOTA].n_value != 0) { /* these are #ifdef QUOTA */
+               addv(X_QUOTA);
+               addv(X_DQUOT);
+       }
+       addv(X_SWBUF);
+       addv(X_BUF);
+       addv(X_CMAP);
+       addv(X_BUFFERS);
+       qsort(wchanhd, nchans, sizeof (struct wchan), wchancomp);
+       for (i = 0; i < NWCINDEX; i++) {
+               tmp = i * nchans;
+               wchan_index[i] = wchanhd[tmp / NWCINDEX].wc_caddr;
+       }
+#undef addv
+}
 printhdr()
 {
        char *hdr;
 printhdr()
 {
        char *hdr;
@@ -431,10 +696,29 @@ 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 = ssflg ? sshdr :
-               (lflg ? lhdr : 
-                       (vflg ? vhdr : 
-                               (uflg ? uhdr : shdr))); 
+       if (lflg) {
+               if (nflg)
+                       wcwidth = 6;
+               else if (twidth > WTSIZ)
+                       wcwidth = -WNAMESIZ;
+               else
+                       wcwidth = -WSNAMESIZ;
+               if ((hdr = malloc(strlen(lhdr) + WNAMESIZ)) == NULL) {
+                       fprintf(stderr, "ps: out of memory\n");
+                       exit(1);
+               }
+               (void)sprintf(hdr, lhdr, wcwidth, "WCHAN");
+       } else if (vflg)
+               hdr = vhdr;
+       else if (uflg) {
+               /* add enough on so that it can hold the sprintf below */
+               if ((hdr = malloc(strlen(uhdr) + 10)) == NULL) {
+                       fprintf(stderr, "ps: out of memory\n");
+                       exit(1);
+               }
+               (void)sprintf(hdr, uhdr, nflg ? " UID" : "USER    ");
+       } else
+               hdr = shdr;
        if (lflg+vflg+uflg+sflg == 0)
                hdr += strlen("SSIZ ");
        cmdstart = strlen(hdr);
        if (lflg+vflg+uflg+sflg == 0)
                hdr += strlen("SSIZ ");
        cmdstart = strlen(hdr);
@@ -446,7 +730,7 @@ cantread(what, fromwhat)
        char *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;
@@ -455,7 +739,13 @@ int        dialbase;
 getdev()
 {
        register DIR *df;
 getdev()
 {
        register DIR *df;
+       struct ttys *t;
+       struct lttys *lt;
 
 
+       if (chdir("/dev") < 0) {
+               perror("/dev");
+               exit(1);
+       }
        dialbase = -1;
        if ((df = opendir(".")) == NULL) {
                fprintf(stderr, "Can't open . in /dev\n");
        dialbase = -1;
        if ((df = opendir(".")) == NULL) {
                fprintf(stderr, "Can't open . in /dev\n");
@@ -464,6 +754,13 @@ getdev()
        while ((dbuf = readdir(df)) != NULL) 
                maybetty();
        closedir(df);
        while ((dbuf = readdir(df)) != NULL) 
                maybetty();
        closedir(df);
+       allttys = (struct ttys *)malloc(sizeof(struct ttys)*nttys);
+       if (allttys == NULL) {
+               fprintf(stderr, "ps: Can't malloc space for tty table\n");
+               exit(1);
+       }
+       for (lt = lallttys, t = allttys; lt ; lt = lt->next, t++)
+               *t = lt->ttys;
 }
 
 /*
 }
 
 /*
@@ -475,7 +772,8 @@ getdev()
 maybetty()
 {
        register char *cp = dbuf->d_name;
 maybetty()
 {
        register char *cp = dbuf->d_name;
-       register struct ttys *dp;
+       static struct lttys *dp;
+       struct lttys *olddp;
        int x;
        struct stat stb;
 
        int x;
        struct stat stb;
 
@@ -507,10 +805,9 @@ 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(h,k) 
+                   || is(r,b) || is(m,t)) {
                        cp += 2;
                        if (isdigit(*cp) && cp[2] == 0)
                                return;
                        cp += 2;
                        if (isdigit(*cp) && cp[2] == 0)
                                return;
@@ -528,6 +825,13 @@ trymem:
        case 'n':
                if (!strcmp(cp, "null"))
                        return;
        case 'n':
                if (!strcmp(cp, "null"))
                        return;
+               if (!strncmp(cp, "nrmt", 4))
+                       return;
+               break;
+
+       case 'p':
+               if (cp[1] && cp[1] == 't' && cp[2] == 'y')
+                       return;
                break;
 
        case 'v':
                break;
 
        case 'v':
@@ -559,16 +863,40 @@ trymem:
        else
                x = -1;
 donecand:
        else
                x = -1;
 donecand:
-       dp = (struct ttys *)alloc(sizeof (struct ttys));
-       (void) strcpy(dp->name, dbuf->d_name);
-       dp->next = allttys;
-       dp->ttyd = -1;
-       allttys = dp;
+       olddp = dp;
+       dp = (struct lttys *)malloc(sizeof(struct lttys));
+       if (dp == NULL) {
+               fprintf(stderr, "ps: Can't malloc space for tty table\n");
+               exit(1);
+       }
+       if (lallttys == NULL)
+               lallttys = dp;
+       nttys++;
+       if (olddp)
+               olddp->next = dp;
+       dp->next = NULL;
+       (void) strcpy(dp->ttys.name, dbuf->d_name);
+       if (Uflg) {
+               if (stat(dp->ttys.name, &stb) == 0 &&
+                  (stb.st_mode&S_IFMT)==S_IFCHR)
+                       dp->ttys.ttyd = x = stb.st_rdev;
+               else {
+                       nttys--;
+                       if (lallttys == dp)
+                               lallttys = NULL;
+                       free(dp);
+                       dp = olddp;
+                       if (dp)
+                               dp->next = NULL;
+                       return;
+               }
+       } else
+               dp->ttys.ttyd = -1;
        if (x == -1)
                return;
        x &= 017;
        if (x == -1)
                return;
        x &= 017;
-       dp->cand = cand[x];
-       cand[x] = dp;
+       dp->ttys.cand = cand[x];
+       cand[x] = nttys-1;
 }
 
 char *
 }
 
 char *
@@ -582,7 +910,8 @@ gettty()
        if (u.u_ttyp == 0)
                return("?");
        x = u.u_ttyd & 017;
        if (u.u_ttyp == 0)
                return("?");
        x = u.u_ttyd & 017;
-       for (dp = cand[x]; dp; dp = dp->cand) {
+       for (dp = &allttys[cand[x]]; dp != &allttys[-1];
+            dp = &allttys[dp->cand]) {
                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)
@@ -594,7 +923,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)
@@ -630,7 +959,7 @@ save()
        cmdp = getcmd();
        if (cmdp == 0)
                return;
        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);
@@ -639,16 +968,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;
@@ -657,12 +984,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)
@@ -672,32 +999,26 @@ 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
                        if (mproc->p_textp)
                                vp->v_txtswrss = xp->x_swrss;
                }
                vp->v_pctcpu = pcpu();
 #undef e
-       } else if (uflg) {
-               if (!ssflg)
-                       sp->sun.u_pctcpu = pcpu();
-               else {
-                       register struct sssav *ssp;
-
-                       sp->sun.ssp =ssp= (struct sssav *)alloc(sizeof (struct sssav));
-                       ssp->ss_ppid = mproc->p_ppid;
-               }
-       } else if (sflg) {
+       } else if (uflg)
+               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;
                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);
                }
        }
 
                }
        }
 
@@ -745,7 +1066,9 @@ getu()
 
        size = sflg ? ctob(UPAGES) : sizeof (struct user);
        if ((mproc->p_flag & SLOAD) == 0) {
 
        size = sflg ? ctob(UPAGES) : sizeof (struct user);
        if ((mproc->p_flag & SLOAD) == 0) {
-               (void) lseek(swap, (long)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);
@@ -755,21 +1078,19 @@ getu()
                argaddr = 0;
                return (1);
        }
                argaddr = 0;
                return (1);
        }
-       if (kflg)
-               mproc->p_p0br = (struct pte *)clear(mproc->p_p0br);
-       pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
+       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",
        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",
-                   mproc->p_pid, swapf);
+                   mproc->p_pid, kmemf);
                return (0);
        }
                return (0);
        }
-       (void) lseek(mem,
+       lseek(mem,
            (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",
            (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",
-                   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)
@@ -780,7 +1101,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;
-               (void) lseek(mem, (long)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);
@@ -793,7 +1114,7 @@ getu()
 char *
 getcmd()
 {
 char *
 getcmd()
 {
-       char cmdbuf[BUFSIZ];
+       char cmdbuf[CLSIZE*NBPG];
        union {
                char    argc[CLSIZE*NBPG];
                int     argi[CLSIZE*NBPG/sizeof (int)];
        union {
                char    argc[CLSIZE*NBPG];
                int     argi[CLSIZE*NBPG/sizeof (int)];
@@ -803,6 +1124,7 @@ getcmd()
        char c;
        int nbad;
        struct dblock db;
        char c;
        int nbad;
        struct dblock db;
+       char *file;
 
        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                return ("");
 
        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                return ("");
@@ -811,16 +1133,20 @@ getcmd()
                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);
-               (void) lseek(swap, (long)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 {
-               (void) lseek(mem, (long)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 */
@@ -857,15 +1183,11 @@ getcmd()
                (void) strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
                (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:
        (void) strcpy(cmdbuf, " (");
        (void) strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
 retucomm:
        (void) strcpy(cmdbuf, " (");
        (void) strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
@@ -874,19 +1196,25 @@ retucomm:
 }
 
 char   *lhdr =
 }
 
 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 %*sSTAT 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;
-
-       printf("%6x%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_nice-NZERO, lp->l_addr, ap->a_size/2, ap->a_rss/2);
-       printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
-       printf(" %4.4s ", state(ap));
+       register struct lsav *lp = sp->s_un.lp;
+
+       printf("%6x %4d %5u %5u %2d %3d %2d %4x %5d %4d",
+           (ap->a_flag &~ SPTECHG),                            /* XXX */
+           ap->a_uid, ap->a_pid, lp->l_ppid,
+           lp->l_cpu > 99 ? 99 : lp->l_cpu, ap->a_pri-PZERO,
+           ap->a_nice, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss));
+       if (lp->l_wchan == 0)
+               printf(" %*s", wcwidth, "");
+       else if (nflg)
+               printf(" %*x", wcwidth, (int)lp->l_wchan&~KERNBASE);
+       else
+               printf(" %*.*s", wcwidth, abs(wcwidth), getchan(lp->l_wchan));
+       printf(" %-3.3s ", state(ap));
        ptty(ap->a_tty);
        ptime(ap);
 }
        ptty(ap->a_tty);
        ptime(ap);
 }
@@ -902,52 +1230,55 @@ ptime(ap)
        struct asav *ap;
 {
 
        struct asav *ap;
 {
 
-       printf("%3ld:%02ld", ap->a_cpu / hz, ap->a_cpu % hz);
+       printf(" %2ld:%02ld", ap->a_cpu / 60, ap->a_cpu % 60);
 }
 
 char   *uhdr =
 }
 
 char   *uhdr =
-"USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME";
+"%s   PID %%CPU %%MEM    SZ   RSS TT STAT TIME";
 upr(sp)
        struct savcom *sp;
 {
        register struct asav *ap = sp->ap;
        int vmsize, rmsize;
 
 upr(sp)
        struct savcom *sp;
 {
        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;
-       printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
-           getname(ap->a_uid), ap->a_pid, sp->sun.u_pctcpu, pmem(ap),
-           vmsize, rmsize);
+               rmsize += pgtok(ap->a_txtrss/ap->a_xccount);
+       if (nflg)
+               printf("%4d ", ap->a_uid);
+       else
+               printf("%-8.8s ", getname(ap->a_uid));
+       printf("%5d %4.1f %4.1f %5d %5d",
+           ap->a_pid, sp->s_un.u_pctcpu, pmem(ap), vmsize, rmsize);
        putchar(' ');
        ptty(ap->a_tty);
        putchar(' ');
        ptty(ap->a_tty);
-       printf(" %4.4s", state(ap));
+       printf(" %-3.3s", state(ap));
        ptime(ap);
 }
 
 char *vhdr =
        ptime(ap);
 }
 
 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);
        ptty(ap->a_tty);
        register struct asav *ap = sp->ap;
 
        printf("%5u ", ap->a_pid);
        ptty(ap->a_tty);
-       printf(" %4.4s", state(ap));
+       printf(" %-3.3s", state(ap));
        ptime(ap);
        ptime(ap);
-       printf("%3d%3d%7d%5d%5d",
+       printf(" %2d %2d %6d %5d %5d",
           ap->a_slptime > 99 ? 99 : ap-> a_slptime,
           ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt,
           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))
-               printf("   xx");
+          pgtok(ap->a_size), pgtok(ap->a_rss));
+       if (ap->a_maxrss == (RLIM_INFINITY/NBPG))
+               printf("    xx");
        else
        else
-               printf("%5d", ap->a_maxrss/2);
-       printf("%5d%4d%5.1f%5.1f",
-          ap->a_tsiz/2, ap->a_txtrss/2, vp->v_pctcpu, pmem(ap));
+               printf(" %5d", pgtok(ap->a_maxrss));
+       printf(" %4d %3d %4.1f %4.1f",
+          pgtok(ap->a_tsiz), pgtok(ap->a_txtrss), vp->v_pctcpu, pmem(ap));
 }
 
 char   *shdr =
 }
 
 char   *shdr =
@@ -958,11 +1289,11 @@ 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);
-       printf(" %4.4s", state(ap));
+       printf(" %-3.3s", state(ap));
        ptime(ap);
 }
 
        ptime(ap);
 }
 
@@ -970,49 +1301,55 @@ char *
 state(ap)
        register struct asav *ap;
 {
 state(ap)
        register struct asav *ap;
 {
-       char stat, load, nice, anom;
        static char res[5];
        static char res[5];
+       char *cp = res;
 
        switch (ap->a_stat) {
 
        case SSTOP:
 
        switch (ap->a_stat) {
 
        case SSTOP:
-               stat = 'T';
+               *cp = 'T';
                break;
 
        case SSLEEP:
                if (ap->a_pri >= PZERO)
                        if (ap->a_slptime >= MAXSLP)
                break;
 
        case SSLEEP:
                if (ap->a_pri >= PZERO)
                        if (ap->a_slptime >= MAXSLP)
-                               stat = 'I';
+                               *cp = 'I';
                        else
                        else
-                               stat = 'S';
+                               *cp = 'S';
                else if (ap->a_flag & SPAGE)
                else if (ap->a_flag & SPAGE)
-                       stat = 'P';
+                       *cp = 'P';
                else
                else
-                       stat = 'D';
+                       *cp = 'D';
                break;
 
        case SWAIT:
        case SRUN:
        case SIDL:
                break;
 
        case SWAIT:
        case SRUN:
        case SIDL:
-               stat = 'R';
+               *cp = 'R';
                break;
 
        case SZOMB:
                break;
 
        case SZOMB:
-               stat = 'Z';
+               *cp = 'Z';
                break;
 
        default:
                break;
 
        default:
-               stat = '?';
+               *cp = '?';
        }
        }
-       load = ap->a_flag & SLOAD ? (ap->a_rss>ap->a_maxrss ? '>' : ' ') : 'W';
+       cp++;
+       if (ap->a_flag & SLOAD) {
+               if (ap->a_rss > ap->a_maxrss)
+                       *cp++ = '>';
+       } else
+               *cp++ = 'W';
        if (ap->a_nice < NZERO)
        if (ap->a_nice < NZERO)
-               nice = '<';
+               *cp++ = '<';
        else if (ap->a_nice > NZERO)
        else if (ap->a_nice > NZERO)
-               nice = 'N';
-       else
-               nice = ' ';
-       anom = (ap->a_flag&SUANOM) ? 'A' : ((ap->a_flag&SSEQL) ? 'S' : ' ');
-       res[0] = stat; res[1] = load; res[2] = nice; res[3] = anom;
+               *cp++ = 'N';
+       if (ap->a_flag & SUANOM)
+               *cp++ = 'A';
+       else if (ap->a_flag & SSEQL)
+               *cp++ = 'S';
+       *cp = '\0';
        return (res);
 }
 
        return (res);
 }
 
@@ -1027,14 +1364,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++;
        }
@@ -1065,8 +1404,8 @@ pscomp(s1, s2)
 {
        register int i;
 
 {
        register int i;
 
-       if (uflg && !ssflg)
-               return (s2->sun.u_pctcpu > s1->sun.u_pctcpu ? 1 : -1);
+       if (uflg)
+               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;
@@ -1079,7 +1418,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 +
@@ -1087,163 +1426,299 @@ 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
+#include <utmp.h>
 
 
-char   names[NUID][NMAX+1];
+struct utmp utmp;
+#define        NMAX    (sizeof (utmp.ut_name))
+#define SCPYN(a, b)    strncpy(a, b, NMAX)
+
+#define NUID   64
+
+struct ncache {
+       int     uid;
+       char    name[NMAX+1];
+} nc[NUID];
 
 /*
 
 /*
- * Stolen from ls...
+ * This function assumes that the password file is hashed
+ * (or some such) to allow fast access based on a uid key.
  */
 char *
 getname(uid)
 {
        register struct passwd *pw;
  */
 char *
 getname(uid)
 {
        register struct passwd *pw;
-       static init;
        struct passwd *getpwent();
        struct passwd *getpwent();
+       register int cp;
+       extern int _pw_stayopen;
 
 
-       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;
-               (void) strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
-               if (pw->pw_uid == uid)
-                       return (&names[uid][0]);
-       }
-       init = 2;
-       endpwent();
-       return (0);
-}
-
-char   *freebase;
-int    nleft;
+       _pw_stayopen = 1;
 
 
-char *
-alloc(size)
-       int size;
-{
-       register char *cp;
-       register int i;
-
-       if (size > nleft) {
-               freebase = (char *)sbrk((int)(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);
+#if    (((NUID) & ((NUID) - 1)) != 0)
+       cp = uid % (NUID);
+#else
+       cp = uid & ((NUID) - 1);
+#endif
+       if (uid >= 0 && nc[cp].uid == uid && nc[cp].name[0])
+               return (nc[cp].name);
+       pw = getpwuid(uid);
+       if (!pw)
+               return (0);
+       nc[cp].uid = uid;
+       SCPYN(nc[cp].name, pw->pw_name);
+       return (nc[cp].name);
 }
 
 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);
+       dp = (char *)calloc(len+1, sizeof (char));
        (void) strcpy(dp, cp);
        return (dp);
 }
 
        (void) strcpy(dp, cp);
        return (dp);
 }
 
-walk(np)
-       int np;
+/*
+ * This routine was stolen from adb to simulate memory management
+ * on the VAX.
+ */
+off_t
+vtophys(loc)
+long   loc;
 {
 {
-       register int i, j, k, l, m;
-#undef afl
-#undef sfl
-#define afl(i,f) savcom[i].ap -> f
-#define sfl(i,f) savcom[i].sun.ssp -> f
-
-       for(i = 0; i < np; i = j) {
-               for(j = i; afl(j,a_ttyd) == afl(i,a_ttyd); j++) {
-                       sfl(j,ss_brother) = -1;
-                       sfl(j,ss_sons) = -1;
-               }
-               for(k = i+1; k < j; k++) {
-                       if(sfl(k,ss_ppid) == sfl(i,ss_ppid)) {
-                               for(l=i; sfl(l,ss_brother) != -1; 
-                                       l=sfl(l,ss_brother)) ;
-                               sfl(l,ss_brother) = k;
-                               goto next;
-                       }
-                       for(l = i; l < j; l++) {
-                               if(l == k) continue;
-                               if(sfl(k,ss_ppid) == afl(l,a_pid)) {
-                                       if(sfl(l,ss_sons) == -1)
-                                           sfl(l,ss_sons) = k;
-                                       else {
-                                           for(m = sfl(l,ss_sons);
-                                               sfl(m,ss_brother) != -1;
-                                               m = sfl(m,ss_brother)) ;
-                                           sfl(m,ss_brother) = k;
-                                       }
-                                       goto next;
-                               }
-                       }
-                       for(l = i; l < j; l++) {
-                               if(l == k) continue;
-                               if(sfl(k,ss_ppid) == sfl(l,ss_ppid)) {
-                                       for(m = k; sfl(m,ss_brother) != -1;
-                                           m = sfl(m,ss_brother)) ;
-                                       sfl(m,ss_brother) = l;
+       register        p;
+       off_t   newloc;
+
+       newloc = loc & ~KERNBASE;
+       p = btop(newloc);
+       if ((loc & KERNBASE) == 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);
+}
+
+/*
+ * since we can't init unions, the cleanest way to use a.out.h instead
+ * of nlist.h (required since nlist() uses some defines) is to do a
+ * runtime copy into the nl array -- sigh
+ */
+init_nlist()
+{
+       register struct nlist *np;
+       register char **namep;
+
+       nllen = sizeof nl_names / sizeof (char *);
+       np = nl = (struct nlist *) malloc(nllen * sizeof (struct nlist));
+       if (np == NULL) {
+               fprintf(stderr, "ps: out of memory allocating namelist\n");
+               exit(1);
+       }
+       namep = &nl_names[0];
+       while (nllen > 0) {
+               np->n_un.n_name = *namep;
+               if (**namep == '\0')
+                       break;
+               namep++;
+               np++;
+       }
+}
+
+/*
+ * nlist - retreive attributes from name list (string table version)
+ *     modified to add wait channels - Charles R. LaBrec 8/85
+ */
+nlist(name, list)
+       char *name;
+       struct nlist *list;
+{
+       register struct nlist *p, *q;
+       register char *s1, *s2;
+       register n, m;
+       int maxlen, nreq;
+       FILE *f;
+       FILE *sf;
+       off_t sa;               /* symbol address */
+       off_t ss;               /* start of strings */
+       int type;
+       struct exec buf;
+       struct nlist space[BUFSIZ/sizeof (struct nlist)];
+       char nambuf[BUFSIZ];
+
+       maxlen = 0;
+       for (q = list, nreq = 0; q->n_un.n_name && q->n_un.n_name[0]; q++, nreq++) {
+               q->n_type = 0;
+               q->n_value = 0;
+               q->n_desc = 0;
+               q->n_other = 0;
+               n = strlen(q->n_un.n_name);
+               if (n > maxlen)
+                       maxlen = n;
+       }
+       f = fopen(name, "r");
+       if (f == NULL)
+               return (-1);
+       fread((char *)&buf, sizeof buf, 1, f);
+       if (N_BADMAG(buf)) {
+               fclose(f);
+               return (-1);
+       }
+       sf = fopen(name, "r");
+       if (sf == NULL) {
+               /* ??? */
+               fclose(f);
+               return(-1);
+       }
+       sa = N_SYMOFF(buf);
+       ss = sa + buf.a_syms;
+       n = buf.a_syms;
+       fseek(f, sa, 0);
+       while (n) {
+               m = sizeof (space);
+               if (n < m)
+                       m = n;
+               if (fread((char *)space, m, 1, f) != 1)
+                       break;
+               n -= m;
+               for (q = space; (m -= sizeof(struct nlist)) >= 0; q++) {
+                       if (q->n_un.n_strx == 0 || q->n_type & N_STAB)
+                               continue;
+                       /*
+                        * since we know what type of symbols we will get,
+                        * we can make a quick check here -- crl
+                        */
+                       type = q->n_type & (N_TYPE | N_EXT);
+                       if ((q->n_type & N_TYPE) != N_ABS
+                           && type != (N_EXT | N_DATA)
+                           && type != (N_EXT | N_BSS))
+                               continue;
+                       fseek(sf, ss+q->n_un.n_strx, 0);
+                       fread(nambuf, maxlen+1, 1, sf);
+                       /* if using wchans, add it to the list of channels */
+                       if (!nflg)
+                               addchan(&nambuf[1], (caddr_t) q->n_value);
+                       for (p = list; p->n_un.n_name && p->n_un.n_name[0]; p++) {
+                               s1 = p->n_un.n_name;
+                               s2 = nambuf;
+                               if (strcmp(p->n_un.n_name, nambuf) == 0) {
+                                       p->n_value = q->n_value;
+                                       p->n_type = q->n_type;
+                                       p->n_desc = q->n_desc;
+                                       p->n_other = q->n_other;
+                                       --nreq;
+                                       break;
                                }
                        }
                                }
                        }
-               next: ;
                }
                }
-               walk1(i, 0);
        }
        }
+alldone:
+       fclose(f);
+       fclose(sf);
+       return (nreq);
 }
 
 }
 
-walk1(pno, depth)
-       int pno, depth;
+/*
+ * add the given channel to the channel list
+ */
+addchan(name, caddr)
+char *name;
+caddr_t caddr;
 {
 {
-       if(pno == -1)
-               return;
-/***    printf("%5d, %d\n",outargs[pno].o_pid, depth);  ***/
-       walkpr(&savcom[pno], depth);
-       walk1(sfl(pno,ss_sons), depth+1);
-       walk1(sfl(pno,ss_brother), depth);
-}
+       static int left = 0;
+       register struct wchan *wp;
+       register char **p;
 
 
-char *sshdr =
-"TTY User     SZ RSS  CPU  S      PID  ";
+       for (p = wchan_stop_list; *p; p++) {
+               if (**p != *name)       /* quick check first */
+                       continue;
+               if (strncmp(name, *p, WNAMESIZ) == 0)
+                       return;         /* if found, don't add */
+       }
+       if (left == 0) {
+               if (wchanhd) {
+                       left = 100;
+                       wchanhd = (struct wchan *) realloc(wchanhd,
+                               (nchans + left) * sizeof (struct wchan));
+               } else {
+                       left = 600;
+                       wchanhd = (struct wchan *) malloc(left
+                               * sizeof (struct wchan));
+               }
+               if (wchanhd == NULL) {
+                       fprintf(stderr, "ps: out of memory allocating wait channels\n");
+                       nflg++;
+                       return;
+               }
+       }
+       left--;
+       wp = &wchanhd[nchans++];
+       strncpy(wp->wc_name, name, WNAMESIZ);
+       wp->wc_name[WNAMESIZ] = '\0';
+       wp->wc_caddr = caddr;
+}
 
 
-walkpr(a, depth)
-       register struct savcom *a;
-       int depth;
+/*
+ * returns the symbolic wait channel corresponding to chan
+ */
+char *
+getchan(chan)
+register caddr_t chan;
 {
 {
+       register i, iend;
+       register char *prevsym;
+       register struct wchan *wp;
+
+       prevsym = "???";                /* nothing, to begin with */
+       if (chan) {
+               for (i = 0; i < NWCINDEX; i++)
+                       if ((unsigned) chan < (unsigned) wchan_index[i])
+                               break;
+               iend = i--;
+               if (i < 0)              /* can't be found */
+                       return prevsym;
+               iend *= nchans;
+               iend /= NWCINDEX;
+               i *= nchans;
+               i /= NWCINDEX;
+               wp = &wchanhd[i];
+               for ( ; i < iend; i++, wp++) {
+                       if ((unsigned) wp->wc_caddr > (unsigned) chan)
+                               break;
+                       prevsym = wp->wc_name;
+               }
+       }
+       return prevsym;
+}
 
 
-       if(!depth) {
-               printf("%-2.2s", a->ap->a_tty);
-               printf(" %-8.8s", getname(a->ap->a_uid));
-       } else
-               printf("   %-8s", &".......*"[8-(depth<=8?depth:8)]);
-       printf("%4d%4d", a->ap->a_size/2, a->ap->a_rss/2);
-       ptime(a->ap);
-       /* Once there was a "CCPU" field here.  Subsumed by -S now. */
-       printf(" %4.4s", state(a->ap));
-       printf("%6u ", a->ap->a_pid);
-       if (a->ap->a_pid == 0)
-               printf(" swapper");
-       else if (a->ap->a_pid == 2)
-               printf(" pagedaemon");
+/*
+ * used in sorting the wait channel array
+ */
+int
+wchancomp (w1, w2)
+struct wchan *w1, *w2;
+{
+       register unsigned c1, c2;
+
+       c1 = (unsigned) w1->wc_caddr;
+       c2 = (unsigned) w2->wc_caddr;
+       if (c1 > c2)
+               return 1;
+       else if (c1 == c2)
+               return 0;
        else
        else
-               printf(" %.*s", twidth - cmdstart - 2, a->ap->a_cmdp);
-       putchar('\n');
+               return -1;
 }
 }