sbrk returns -1, not 0, on failure; from lepreau@utah-cs
[unix-history] / usr / src / bin / ps / ps.c
index 886dc79..f5721ee 100644 (file)
@@ -1,26 +1,25 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)ps.c        4.19 (Berkeley) %G%";
+static char *sccsid = "@(#)ps.c        4.26 (Berkeley) %G%";
 #endif
 
 /*
  * ps
  */
 #endif
 
 /*
  * ps
  */
-
 #include <stdio.h>
 #include <ctype.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <sys/param.h>
 #include <sys/tty.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <nlist.h>
 #include <pwd.h>
 #include <sys/param.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>
 
 struct nlist nl[] = {
        { "_proc" },
 
 struct nlist nl[] = {
        { "_proc" },
@@ -43,7 +42,11 @@ struct nlist nl[] = {
 #define        X_NPROC         8
        { "_ntext" },
 #define        X_NTEXT         9
 #define        X_NPROC         8
        { "_ntext" },
 #define        X_NTEXT         9
-       { 0 },
+       { "_dmmin" },
+#define        X_DMMIN         10
+       { "_dmmax" },
+#define        X_DMMAX         11
+       { "" },
 };
 
 struct savcom {
 };
 
 struct savcom {
@@ -52,8 +55,7 @@ struct        savcom {
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
                float   u_pctcpu;
                struct  vsav *vp;
                int     s_ssiz;
-               struct  sssav *ssp;
-       } sun;
+       } s_un;
        struct  asav *ap;
 } *savcom;
 
        struct  asav *ap;
 } *savcom;
 
@@ -77,13 +79,6 @@ struct       lsav {
        caddr_t l_wchan;
 };
 
        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;
 
@@ -107,11 +102,12 @@ union {
 #define clear(x)       ((int)x & 0x7fffffff)
 
 int    chkpid;
 #define clear(x)       ((int)x & 0x7fffffff)
 
 int    chkpid;
-int    aflg, cflg, eflg, gflg, kflg, lflg, sflg, ssflg,
-       nonssflg, uflg, vflg, xflg;
+int    aflg, cflg, eflg, gflg, kflg, lflg, sflg,
+       uflg, vflg, xflg;
 char   *tptr;
 char   *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state();
 char   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
 char   *tptr;
 char   *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state();
 char   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
+char   *index(), *ttyname(), mytty[16];
 long   lseek();
 double pcpu(), pmem();
 int    pscomp();
 long   lseek();
 double pcpu(), pmem();
 int    pscomp();
@@ -120,7 +116,8 @@ struct      text *atext;
 double ccpu;
 int    ecmx;
 struct pte *Usrptma, *usrpt;
 double ccpu;
 int    ecmx;
 struct pte *Usrptma, *usrpt;
-int    nproc, ntext, hz;
+int    nproc, ntext;
+int    dmmin, dmmax;
 
 struct ttys {
        char    name[MAXNAMLEN+1];
 
 struct ttys {
        char    name[MAXNAMLEN+1];
@@ -141,6 +138,8 @@ int pcbpf;
 int    argaddr;
 extern char _sobuf[];
 
 int    argaddr;
 extern char _sobuf[];
 
+#define        pgtok(a)        ((a)/(1024/NBPG))
+
 main(argc, argv)
        char **argv;
 {
 main(argc, argv)
        char **argv;
 {
@@ -150,20 +149,13 @@ main(argc, argv)
        off_t procp;
 
        twidth = 80;
        off_t procp;
 
        twidth = 80;
-       if (ap = rindex(argv[0], '/'))
-               ap++;
-       else
-               ap = argv[0];
-       if (*ap == 's')                         /* If name starts with 's' */
-               ssflg++;
-
        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;
                case 'S':
                        sumcpu++;
@@ -172,39 +164,44 @@ main(argc, argv)
                        aflg++;
                        break;
                case 'c':
                        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 '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(2)) != 0) {
+                               strcpy(mytty, tptr);
+                               if ((tptr = index(mytty,'y')) != 0)
+                                       tptr++;
+                       }
+                       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':
                        if (twidth == 80)
                        break;
                case 'w':
                        if (twidth == 80)
@@ -215,27 +212,16 @@ 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);
        if (chdir("/dev") < 0) {
        openfiles(argc, argv);
        getkvars(argc, argv);
        if (chdir("/dev") < 0) {
@@ -283,10 +269,6 @@ main(argc, argv)
                }
        }
        qsort(savcom, npr, sizeof(savcom[0]), pscomp);
                }
        }
        qsort(savcom, npr, sizeof(savcom[0]), pscomp);
-       if (ssflg) {
-               walk(npr);
-               exit (npr == 0);
-       }
        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)
@@ -420,6 +402,8 @@ getkvars(argc, argv)
                        exit(1);
                }
        }
                        exit(1);
                }
        }
+       dmmin = getw(nl[X_DMMIN].n_value);
+       dmmax = getw(nl[X_DMMAX].n_value);
 }
 
 printhdr()
 }
 
 printhdr()
@@ -430,10 +414,9 @@ 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 : 
+       hdr = lflg ? lhdr : 
                        (vflg ? vhdr : 
                        (vflg ? vhdr : 
-                               (uflg ? uhdr : shdr))); 
+                               (uflg ? uhdr : 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);
@@ -638,8 +621,6 @@ 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 = 0;
        } else {
                ap->a_size = mproc->p_dsize + mproc->p_ssize;
                ap->a_cpu = 0;
        } else {
                ap->a_size = mproc->p_dsize + mproc->p_ssize;
@@ -660,7 +641,7 @@ save()
        if (lflg) {
                register struct lsav *lp;
 
        if (lflg) {
                register struct lsav *lp;
 
-               sp->sun.lp = lp = (struct lsav *)alloc(sizeof (struct lsav));
+               sp->s_un.lp = lp = (struct lsav *)alloc(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)
@@ -670,7 +651,7 @@ 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 *)alloc(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);
@@ -680,22 +661,15 @@ save()
                }
                vp->v_pctcpu = pcpu();
 #undef e
                }
                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 +719,7 @@ getu()
        if ((mproc->p_flag & SLOAD) == 0) {
                if (swap < 0)
                        return (0);
        if ((mproc->p_flag & SLOAD) == 0) {
                if (swap < 0)
                        return (0);
-               (void) lseek(swap, (long)ctob(mproc->p_swaddr), 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);
@@ -793,7 +767,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)];
@@ -815,7 +789,7 @@ getcmd()
                if (swap < 0)
                        goto retucomm;
                vstodb(0, CLSIZE, &u.u_smap, &db, 1);
                if (swap < 0)
                        goto retucomm;
                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;
@@ -879,17 +853,17 @@ 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 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);
+           ap->a_nice-NZERO, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss));
        printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
        printf(" %4.4s ", state(ap));
        ptty(ap->a_tty);
        printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
        printf(" %4.4s ", state(ap));
        ptty(ap->a_tty);
@@ -918,12 +892,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);
@@ -932,11 +906,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);
@@ -946,13 +920,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 =
@@ -963,7 +937,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);
@@ -1032,14 +1006,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++;
        }
@@ -1070,8 +1046,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;
@@ -1084,7 +1060,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 +
@@ -1092,39 +1068,81 @@ 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];
+
+struct nametable *
+findslot(uid)
+unsigned short uid;
+{
+       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);
+       }
+       return(n);
+}
 
 
-/*
- * Stolen from ls...
- */
 char *
 getname(uid)
 {
 char *
 getname(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;
-               (void) strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
-               if (pw->pw_uid == uid)
-                       return (&names[uid][0]);
+       register struct passwd          *pw;
+       static                          init = 0;
+       struct passwd                   *getpwent();
+       register struct nametable       *n;
+
+       /*
+        * 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();
+                       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);
        }
        }
-       init = 2;
-       endpwent();
-       return (0);
 }
 
 char   *freebase;
 }
 
 char   *freebase;
@@ -1137,9 +1155,12 @@ alloc(size)
        register char *cp;
        register int i;
 
        register char *cp;
        register int i;
 
+#ifdef sun
+       size = (size+1)&~1;
+#endif
        if (size > nleft) {
                freebase = (char *)sbrk((int)(i = size > 2048 ? size : 2048));
        if (size > nleft) {
                freebase = (char *)sbrk((int)(i = size > 2048 ? size : 2048));
-               if (freebase == 0) {
+               if (freebase == (char *)-1) {
                        fprintf(stderr, "ps: ran out of memory\n");
                        exit(1);
                }
                        fprintf(stderr, "ps: ran out of memory\n");
                        exit(1);
                }
@@ -1165,90 +1186,3 @@ savestr(cp)
        (void) strcpy(dp, cp);
        return (dp);
 }
        (void) strcpy(dp, cp);
        return (dp);
 }
-
-walk(np)
-       int np;
-{
-       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;
-                               }
-                       }
-               next: ;
-               }
-               walk1(i, 0);
-       }
-}
-
-walk1(pno, depth)
-       int pno, depth;
-{
-       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);
-}
-
-char *sshdr =
-"TTY User     SZ RSS  CPU  S      PID  ";
-
-walkpr(a, depth)
-       register struct savcom *a;
-       int depth;
-{
-
-       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");
-       else
-               printf(" %.*s", twidth - cmdstart - 2, a->ap->a_cmdp);
-       putchar('\n');
-}