delete "rusage" alias; delete -s option; change "trs" to "trss"
[unix-history] / usr / src / bin / ps / ps.c
index 6414ed1..e740d47 100644 (file)
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)ps.c        4.30 (Berkeley) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)ps.c       5.34 (Berkeley) %G%";
+#endif /* not lint */
 
 
-/*
- * ps
- */
-#include <stdio.h>
-#include <ctype.h>
-#include <nlist.h>
-#include <pwd.h>
 #include <sys/param.h>
 #include <sys/param.h>
-#include <sys/tty.h>
-#include <sys/dir.h>
 #include <sys/user.h>
 #include <sys/user.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 #include <sys/proc.h>
 #include <sys/proc.h>
-#include <machine/pte.h>
-#include <sys/vm.h>
-#include <sys/text.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-#include <sys/mbuf.h>
-#include <math.h>
+#include <sys/ioctl.h>
+#include <sys/kinfo.h>
+#include <kvm.h>
 #include <errno.h>
 #include <errno.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <paths.h>
+#include "ps.h"
 
 
-struct nlist nl[] = {
-       { "_proc" },
-#define        X_PROC          0
-       { "_Usrptmap" },
-#define        X_USRPTMAP      1
-       { "_usrpt" },
-#define        X_USRPT         2
-       { "_text" },
-#define        X_TEXT          3
-       { "_nswap" },
-#define        X_NSWAP         4
-       { "_maxslp" },
-#define        X_MAXSLP        5
-       { "_ccpu" },
-#define        X_CCPU          6
-       { "_ecmx" },
-#define        X_ECMX          7
-       { "_nproc" },
-#define        X_NPROC         8
-       { "_ntext" },
-#define        X_NTEXT         9
-       { "_dmmin" },
-#define        X_DMMIN         10
-       { "_dmmax" },
-#define        X_DMMAX         11
-       { "_Sysmap" },
-#define        X_SYSMAP        12
-       { "_Syssize" },
-#define        X_SYSSIZE       13
-       { "" },
-};
-
-struct savcom {
-       union {
-               struct  lsav *lp;
-               float   u_pctcpu;
-               struct  vsav *vp;
-               int     s_ssiz;
-       } s_un;
-       struct  asav *ap;
-} *savcom;
-
-struct asav {
-       char    *a_cmdp;
-       int     a_flag;
-       short   a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time;
-       size_t  a_size, a_rss, a_tsiz, a_txtrss;
-       short   a_xccount;
-       char    a_tty[MAXNAMLEN+1];
-       dev_t   a_ttyd;
-       time_t  a_cpu;
-       size_t  a_maxrss;
-};
-
-char   *lhdr;
-struct lsav {
-       short   l_ppid;
-       char    l_cpu;
-       int     l_addr;
-       caddr_t l_wchan;
-};
-
-char   *uhdr;
-char   *shdr;
-
-char   *vhdr;
-struct vsav {
-       u_int   v_majflt;
-       size_t  v_swrss, v_txtswrss;
-       float   v_pctcpu;
-};
-
-#define        NPROC   16
-
-struct proc proc[NPROC];               /* a few, for less syscalls */
-struct proc *mproc;
-struct text *text;
-
-union {
-       struct  user user;
-       char    upages[UPAGES][NBPG];
-} user;
-#define u      user.user
-
-#ifndef        PSFILE
-char   *psdb   = "/etc/psdatabase";
-#else
-char   *psdb   = PSFILE;
-#endif
-
-int    chkpid;
-int    aflg, cflg, eflg, gflg, kflg, lflg, sflg,
-       uflg, vflg, xflg, Uflg;
-char   *tptr;
-char   *gettty(), *getcmd(), *getname(), *savestr(), *state();
-char   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
-char   *strncpy(), *index(), *ttyname(), mytty[16];
-long   lseek();
-off_t  vtophys();
-double pcpu(), pmem();
-int    pscomp();
-int    nswap, maxslp;
-struct text *atext;
-double ccpu;
-int    ecmx;
-struct pte *Usrptmap, *usrpt;
-int    nproc, ntext;
-int    dmmin, dmmax;
-struct pte *Sysmap;
-int    Syssize;
-
-#ifndef        MAXTTYS
-#define        MAXTTYS         256
-#endif
-
-int    nttys;
+KINFO *kinfo;
+VAR *vhead, *vtail;
 
 
-struct ttys {
-       dev_t   ttyd;
-       struct  ttys *cand;
-       char    name[MAXNAMLEN+1];
-} allttys[MAXTTYS], *cand[16];
+int    eval;                   /* exit value */
+int    rawcpu;                 /* -C */
+int    sumrusage;              /* -S */
+int    termwidth;              /* width of screen (0 == infinity) */
+int    totwidth;               /* calculated width of requested variables */
 
 
-int    npr;
+static int needuser, needcomm;
 
 
-int    cmdstart;
-int    twidth;
-char   *kmemf, *memf, *swapf, *nlistf;
-int    kmem, mem, swap = -1;
-int    rawcpu, sumcpu;
+enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
 
 
-int    pcbpf;
-int    argaddr;
-extern char _sobuf[];
+uid_t  getuid();
+char   *ttyname();
 
 
-#define        pgtok(a)        ((a)/(1024/NBPG))
+#define DFMT   "pid tt state time command"
+#define        JFMT    "user pid ppid pgid sess jobc state tt time command"
+#define LFMT \
+       "uid pid ppid cpu pri nice vsz rss wchan state tt time command"
+#define UFMT \
+       "user pid %cpu %mem vsz rss tt state start time command"
+#define        VFMT \
+       "pid state time sl re pagein vsz rss lim tsiz trs %cpu %mem command"
 
 main(argc, argv)
 
 main(argc, argv)
+       int argc;
        char **argv;
 {
        char **argv;
 {
-       register int i, j;
-       register char *ap;
-       int uid;
-       off_t procp;
+       extern char *optarg;
+       extern int optind;
+       register struct proc *p;
+       register size_t nentries;
+       register VAR *v;
+       register int i;
+       struct winsize ws;
+       dev_t ttydev;
+       int all, ch, flag, fmt, lineno, pid, prtheader, uid, what, xflg;
+       int pscomp();
+       char *kludge_oldps_options();
+
+       if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
+            ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
+            ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
+            ws.ws_col == 0)
+               termwidth = 79;
+       else
+               termwidth = ws.ws_col - 1;
 
 
-       twidth = 80;
-       argc--, argv++;
-       if (argc > 0) {
-               ap = argv[0];
-               while (*ap) switch (*ap++) {
+       if (argc > 1)
+               argv[1] = kludge_oldps_options(argv[1]);
 
 
+       fmt = 0;
+       all = xflg = 0;
+       pid = uid = -1;
+       ttydev = NODEV;
+       while ((ch = getopt(argc, argv, "aCghjLlmO:o:p:rSsTt:uvwx")) != EOF)
+               switch((char)ch) {
+               case 'a':
+                       all = 1;
+                       break;
                case 'C':
                case 'C':
-                       rawcpu++;
+                       rawcpu = 1;
                        break;
                        break;
-               case 'S':
-                       sumcpu++;
+               case 'g':
+                       break;  /* no-op */
+               case 'h':
+                       prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
                        break;
                        break;
-
-               case 'U':
-                       Uflg++;
+               case 'j':
+                       parsefmt(JFMT);
+                       fmt = 1;
                        break;
                        break;
-
-               case 'a':
-                       aflg++;
+               case 'L': 
+                       showkey();
+                       exit(0);
+               case 'l':
+                       parsefmt(LFMT);
+                       fmt = 1;
                        break;
                        break;
-               case 'c':
-                       cflg = !cflg;
+               case 'm':
+                       sortby = SORTMEM;
                        break;
                        break;
-               case 'e':
-                       eflg++;
+               case 'O':
+                       parsefmt("pid");
+                       parsefmt(optarg);
+                       parsefmt("tt state time command");
+                       fmt = 1;
                        break;
                        break;
-               case 'g':
-                       gflg++;
+               case 'o':
+                       parsefmt(optarg);
+                       fmt = 1;
                        break;
                        break;
-               case 'k':
-                       kflg++;
+               case 'p':
+                       pid = atoi(optarg);
+                       xflg = 1;
                        break;
                        break;
-               case 'l':
-                       lflg++;
+               case 'r':
+                       sortby = SORTCPU;
                        break;
                        break;
-               case 's':
-                       sflg++;
+               case 'S':
+                       sumrusage = 1;
                        break;
                        break;
-               case 't':
-                       if (*ap)
-                               tptr = ap;
-                       else if ((tptr = ttyname(2)) != 0) {
-                               strcpy(mytty, tptr);
-                               if ((tptr = index(mytty,'y')) != 0)
-                                       tptr++;
+               case 'T':
+                       if ((optarg = ttyname(STDIN_FILENO)) == NULL)
+                               error("stdin: not a terminal");
+                       /* FALLTHROUGH */
+               case 't': {
+                       char *ttypath;
+                       struct stat stbuf;
+                       char pathbuf[MAXPATHLEN];
+
+                       if (strcmp(optarg, "co") == 0)
+                               ttypath = _PATH_CONSOLE;
+                       else if (*optarg != '/')
+                               (void) sprintf(ttypath = pathbuf, "%s%s",
+                                   _PATH_TTY, optarg);
+                       else
+                               ttypath = optarg;
+                       if (stat(ttypath, &stbuf) == -1) {
+                               (void)fprintf(stderr,
+                                   "ps: %s: %s\n", ttypath, strerror(errno));
+                               exit(1);
                        }
                        }
-                       aflg++;
-                       gflg++;
-                       if (tptr && *tptr == '?')
-                               xflg++;
-                       while (*ap)
-                               ap++;
+                       if (!S_ISCHR(stbuf.st_mode))
+                               error("%s: not a terminal", ttypath);
+                       ttydev = stbuf.st_rdev;
                        break;
                        break;
-               case 'u': 
-                       uflg++;
+               }
+               case 'u':
+                       parsefmt(UFMT);
+                       sortby = SORTCPU;
+                       fmt = 1;
                        break;
                case 'v':
                        break;
                case 'v':
-                       cflg = 1;
-                       vflg++;
+                       parsefmt(VFMT);
+                       sortby = SORTMEM;
+                       fmt = 1;
                        break;
                case 'w':
                        break;
                case 'w':
-                       if (twidth == 80)
-                               twidth = 132;
+                       if (termwidth < 131)
+                               termwidth = 131;
                        else
                        else
-                               twidth = BUFSIZ;
+                               termwidth = UNLIMITED;
                        break;
                case 'x':
                        break;
                case 'x':
-                       xflg++;
+                       xflg = 1;
                        break;
                        break;
+               case '?':
                default:
                default:
-                       if (!isdigit(ap[-1]))
-                               break;
-                       chkpid = atoi(--ap);
-                       *ap = 0;
-                       aflg++;
-                       xflg++;
-                       break;
-               }
-       }
-       openfiles(argc, argv);
-       getkvars(argc, argv);
-       uid = getuid();
-       printhdr();
-       procp = getw(nl[X_PROC].n_value);
-       nproc = getw(nl[X_NPROC].n_value);
-       savcom = (struct savcom *)calloc((unsigned) nproc, sizeof (*savcom));
-       for (i=0; i<nproc; i += NPROC) {
-               klseek(kmem, (long)procp, 0);
-               j = nproc - i;
-               if (j > NPROC)
-                       j = NPROC;
-               j *= sizeof (struct proc);
-               if (read(kmem, (char *)proc, j) != j) {
-                       cantread("proc table", kmemf);
-                       exit(1);
-               }
-               procp += j;
-               for (j = j / sizeof (struct proc) - 1; j >= 0; j--) {
-                       mproc = &proc[j];
-                       if (mproc->p_stat == 0 ||
-                           mproc->p_pgrp == 0 && xflg == 0)
-                               continue;
-                       if (tptr == 0 && gflg == 0 && xflg == 0 &&
-                           mproc->p_ppid == 1)
-                               continue;
-                       if (uid != mproc->p_uid && aflg==0)
-                               continue;
-                       if (chkpid != 0 && chkpid != mproc->p_pid)
-                               continue;
-                       if (vflg && gflg == 0 && xflg == 0) {
-                               if (mproc->p_stat == SZOMB ||
-                                   mproc->p_flag&SWEXIT)
-                                       continue;
-                               if (mproc->p_slptime > MAXSLP &&
-                                   (mproc->p_stat == SSLEEP ||
-                                    mproc->p_stat == SSTOP))
-                               continue;
-                       }
-                       save();
+                       usage();
                }
                }
-       }
-       qsort((char *) savcom, npr, sizeof(savcom[0]), pscomp);
-       for (i=0; i<npr; i++) {
-               register struct savcom *sp = &savcom[i];
-               if (lflg)
-                       lpr(sp);
-               else if (vflg)
-                       vpr(sp);
-               else if (uflg)
-                       upr(sp);
-               else
-                       spr(sp);
-               if (sp->ap->a_flag & SWEXIT)
-                       printf(" <exiting>");
-               else if (sp->ap->a_stat == SZOMB)
-                       printf(" <defunct>");
-               else if (sp->ap->a_pid == 0)
-                       printf(" swapper");
-               else if (sp->ap->a_pid == 2)
-                       printf(" pagedaemon");
-               else if (sp->ap->a_pid == 3 && sp->ap->a_flag & SSYS)
-                       printf(" ip input");
-               else
-                       printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp);
-               printf("\n");
-       }
-       exit(npr == 0);
-}
+       argc -= optind;
+       argv += optind;
 
 
-getw(loc)
-       unsigned long loc;
-{
-       int word;
+       if (*argv) {
+               char *nlistf, *memf = NULL, *swapf = NULL;
 
 
-       klseek(kmem, (long)loc, 0);
-       if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
-               printf("error reading kmem at %x\n", loc);
-       return (word);
-}
-
-klseek(fd, loc, off)
-       int fd;
-       long loc;
-       int off;
-{
-       if (kflg) {
-               if ((loc = vtophys(loc)) == -1)
-                       return;
-       }
-       (void) lseek(fd, (long)loc, off);
-}
-
-writepsdb(unixname)
-       char *unixname;
-{
-       int nllen;
-       register FILE *fp;
-
-       setuid(getuid());
-       if ((fp = fopen(psdb, "w")) == NULL) {
-               perror(psdb);
-               exit(1);
-       } else
-               fchmod(fileno(fp), 0644);
-       nllen = sizeof nl / sizeof (struct nlist);
-       fwrite((char *) &nllen, sizeof nllen, 1, fp);
-       fwrite((char *) nl, sizeof (struct nlist), nllen, fp);
-       fwrite((char *) &nttys, sizeof nttys, 1, fp);
-       fwrite((char *) allttys, sizeof (struct ttys), nttys, fp);
-       fwrite(unixname, strlen(unixname) + 1, 1, fp);
-       fclose(fp);
-}
-
-readpsdb(unixname)
-       char *unixname;
-{
-       int nllen;
-       register i;
-       register FILE *fp;
-       char unamebuf[BUFSIZ];
-       char *p = unamebuf;
-       extern int errno;
-
-       if ((fp = fopen(psdb, "r")) == NULL) {
-               if (errno == ENOENT)
-                       return (0);
-               perror(psdb);
-               exit(1);
-       }
-
-       fread((char *) &nllen, sizeof nllen, 1, fp);
-       fread((char *) nl, sizeof (struct nlist), nllen, fp);
-       fread((char *) &nttys, sizeof nttys, 1, fp);
-       fread((char *) allttys, sizeof (struct ttys), nttys, fp);
-       while ((*p = getc(fp)) != '\0')
-               p++;
-       return (strcmp(unixname, unamebuf) == 0);
-}
-
-openfiles(argc, argv)
-       char **argv;
-{
-
-       kmemf = "/dev/kmem";
-       if (kflg)
-               kmemf = argc > 2 ? argv[2] : "/vmcore";
-       kmem = open(kmemf, 0);
-       if (kmem < 0) {
-               perror(kmemf);
-               exit(1);
-       }
-       if (kflg)  {
-               mem = kmem;
-               memf = kmemf;
-       } else {
-               memf = "/dev/mem";
-               mem = open(memf, 0);
-               if (mem < 0) {
-                       perror(memf);
-                       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);
+               nlistf = *argv++;
+               if (*argv) {
+                       memf = *argv++;
+                       if (*argv)
+                               swapf = *argv++;
                }
                }
+               if (kvm_openfiles(nlistf, memf, swapf) == -1)
+                       error("kvm_openfiles: %s", kvm_geterr());
        }
        }
-}
 
 
-getkvars(argc, argv)
-       char **argv;
-{
-
-       nlistf = argc > 1 ? argv[1] : "/vmunix";
-       if (Uflg) {
-               nlist(nlistf, nl);
-               getdev();
-               writepsdb(nlistf);
-               exit (0);
-       } else if (!readpsdb(nlistf)) {
-               if (!kflg)
-                       nl[X_SYSMAP].n_name = "";
-               nlist(nlistf, nl);
-               nttys =  0;
-               getdev();
-       }
+       if (!fmt)
+               parsefmt(DFMT);
 
 
-       if (nl[0].n_type == 0) {
-               fprintf(stderr, "%s: No namelist\n", nlistf);
-               exit(1);
-       }
-       if (kflg) {
-               /* We must do the sys map first because klseek uses it */
-               long    addr;
+       if (!all && ttydev == NODEV && pid == -1)  /* XXX - should be cleaner */
+               uid = getuid();
 
 
-               Syssize = nl[X_SYSSIZE].n_value;
-               Sysmap = (struct pte *)
-                       calloc((unsigned) Syssize, sizeof (struct pte));
-               if (Sysmap == NULL) {
-                       fprintf(stderr, "Out of space for Sysmap\n");
-                       exit(1);
-               }
-               addr = (long) nl[X_SYSMAP].n_value;
-               addr &= ~0x80000000;
-               (void) lseek(kmem, addr, 0);
-               read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte));
-       }
-       usrpt = (struct pte *)nl[X_USRPT].n_value;
-       Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
-       klseek(kmem, (long)nl[X_NSWAP].n_value, 0);
-       if (read(kmem, (char *)&nswap, sizeof (nswap)) != sizeof (nswap)) {
-               cantread("nswap", kmemf);
-               exit(1);
-       }
-       klseek(kmem, (long)nl[X_MAXSLP].n_value, 0);
-       if (read(kmem, (char *)&maxslp, sizeof (maxslp)) != sizeof (maxslp)) {
-               cantread("maxslp", kmemf);
+       /*
+        * scan requested variables, noting what structures are needed,
+        * and adjusting header widths as appropiate.
+        */
+       scanvars();
+       /*
+        * get proc list
+        */
+       if (uid != -1) {
+               what = KINFO_PROC_UID;
+               flag = uid;
+       } else if (ttydev != NODEV) {
+               what = KINFO_PROC_TTY;
+               flag = ttydev;
+       } else if (pid != -1) {
+               what = KINFO_PROC_PID;
+               flag = pid;
+       /*
+        * select procs
+        */
+       if ((nentries = kvm_getprocs(what, flag)) == -1) {
+               (void) fprintf(stderr, "ps: %s\n", kvm_geterr());
                exit(1);
        }
                exit(1);
        }
-       klseek(kmem, (long)nl[X_CCPU].n_value, 0);
-       if (read(kmem, (char *)&ccpu, sizeof (ccpu)) != sizeof (ccpu)) {
-               cantread("ccpu", kmemf);
+       kinfo = (KINFO *)malloc(nentries * sizeof(KINFO));
+       if (kinfo == NULL) {
+               (void)fprintf(stderr, "ps: %s\n", strerror(ENOMEM));
                exit(1);
        }
                exit(1);
        }
-       klseek(kmem, (long)nl[X_ECMX].n_value, 0);
-       if (read(kmem, (char *)&ecmx, sizeof (ecmx)) != sizeof (ecmx)) {
-               cantread("ecmx", kmemf);
-               exit(1);
+       for (nentries = 0; p = kvm_nextproc(); ++nentries) {
+               kinfo[nentries].ki_p = p;
+               kinfo[nentries].ki_e = kvm_geteproc(p);
+               if (needuser)
+                       saveuser(&kinfo[nentries]);
        }
        }
-       if (uflg || vflg) {
-               ntext = getw(nl[X_NTEXT].n_value);
-               text = (struct text *)
-                       calloc((unsigned) ntext, sizeof (struct text));
-               if (text == 0) {
-                       fprintf(stderr, "no room for text table\n");
-                       exit(1);
+       /*
+        * print header
+        */
+       printheader();
+       if (nentries == 0)
+               exit(0);
+       /*
+        * sort proc list
+        */
+       qsort((void *)kinfo, nentries, sizeof(KINFO), pscomp);
+       /*
+        * for each proc, call each variable output function.
+        */
+       for (i = lineno = 0; i < nentries; i++) {
+               if (xflg == 0 && (kinfo[i].ki_e->e_tdev == NODEV ||
+                   (kinfo[i].ki_p->p_flag & SCTTY ) == 0))
+                       continue;
+               for (v = vhead; v; v = v->next) {
+                       (*v->oproc)(&kinfo[i], v);
+                       if (v->next != NULL)
+                               (void) putchar(' ');
                }
                }
-               atext = (struct text *)getw(nl[X_TEXT].n_value);
-               klseek(kmem, (long)atext, 0);
-               if (read(kmem, (char *)text, ntext * sizeof (struct text))
-                   != ntext * sizeof (struct text)) {
-                       cantread("text table", kmemf);
-                       exit(1);
+               (void) putchar('\n');
+               if (prtheader && lineno++ == prtheader-4) {
+                       (void) putchar('\n');
+                       printheader();
+                       lineno = 0;
                }
        }
                }
        }
-       dmmin = getw(nl[X_DMMIN].n_value);
-       dmmax = getw(nl[X_DMMAX].n_value);
+       exit(eval);
 }
 
 }
 
-printhdr()
+scanvars()
 {
 {
-       char *hdr;
+       register VAR *v;
+       register int i;
 
 
-       if (sflg+lflg+vflg+uflg > 1) {
-               fprintf(stderr, "ps: specify only one of s,l,v and u\n");
-               exit(1);
+       for (v = vhead; v; v = v->next) {
+               i = strlen(v->header);
+               if (v->width < i)
+                       v->width = i;
+               totwidth += v->width + 1;       /* +1 for space */
+               if (v->flag & USER)
+                       needuser = 1;
+               if (v->flag & COMM)
+                       needcomm = 1;
        }
        }
-       hdr = lflg ? lhdr : 
-                       (vflg ? vhdr : 
-                               (uflg ? uhdr : shdr));
-       if (lflg+vflg+uflg+sflg == 0)
-               hdr += strlen("SSIZ ");
-       cmdstart = strlen(hdr);
-       printf("%s COMMAND\n", hdr);
-       (void) fflush(stdout);
-}
-
-cantread(what, fromwhat)
-       char *what, *fromwhat;
-{
-
-       fprintf(stderr, "ps: error reading %s from %s\n", what, fromwhat);
+       totwidth--;
 }
 
 }
 
-struct direct *dbuf;
-int    dialbase;
 
 
-getdev()
+/* XXX - redo */
+saveuser(ki)
+       KINFO *ki;
 {
 {
-       register DIR *df;
+       register struct usave *usp;
+       register struct user *up;
 
 
-       if (chdir("/dev") < 0) {
-               perror("/dev");
-               exit(1);
-       }
-       dialbase = -1;
-       if ((df = opendir(".")) == NULL) {
-               fprintf(stderr, "Can't open . in /dev\n");
+       if ((usp = (struct usave *)calloc(1, sizeof(struct usave))) == NULL) {
+               (void)fprintf(stderr, "ps: %s\n", strerror(errno));
                exit(1);
        }
                exit(1);
        }
-       while ((dbuf = readdir(df)) != NULL) 
-               maybetty();
-       closedir(df);
+       ki->ki_u = usp;
+       up = kvm_getu(ki->ki_p);
+       /*
+        * save arguments if needed
+        */
+       ki->ki_args = needcomm ? strdup(kvm_getargs(ki->ki_p, up)) : NULL;
+       if (up != NULL) {
+               /*
+                * save important fields
+                */
+               usp->u_procp = up->u_procp;
+               usp->u_start = up->u_start;
+               usp->u_ru = up->u_ru;
+               usp->u_cru = up->u_cru;
+               usp->u_acflag = up->u_acflag;
+       }
+}
+
+pscomp(k1, k2)
+       KINFO *k1, *k2;
+{
+       int i;
+#define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize)
+
+       if (sortby == SORTCPU)
+               return (getpcpu(k2) - getpcpu(k1));
+       if (sortby == SORTMEM)
+               return (VSIZE(k2) - VSIZE(k1));
+       i =  k1->ki_e->e_tdev - k2->ki_e->e_tdev;
+       if (i == 0)
+               i = k1->ki_p->p_pid - k2->ki_p->p_pid;
+       return (i);
 }
 
 /*
 }
 
 /*
- * Attempt to avoid stats by guessing minor device
- * numbers from tty names.  Console is known,
- * know that r(hp|up|mt) are unlikely as are different mem's,
- * floppy, null, tty, etc.
+ * ICK (all for getopt), would rather hide the ugliness
+ * here than taint the main code.
+ *
+ *  ps foo -> ps -foo
+ *  ps 34 -> ps -p34
+ *
+ * The old convention that 't' with no trailing tty arg means the users
+ * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
+ * feature is available with the option 'T', which takes no argument.
  */
  */
-maybetty()
-{
-       register char *cp = dbuf->d_name;
-       register struct ttys *dp;
-       int x;
-       struct stat stb;
-
-       switch (cp[0]) {
-
-       case 'c':
-               if (!strcmp(cp, "console")) {
-                       x = 0;
-                       goto donecand;
-               }
-               /* cu[la]? are possible!?! don't rule them out */
-               break;
-
-       case 'd':
-               if (!strcmp(cp, "drum"))
-                       return;
-               break;
-
-       case 'f':
-               if (!strcmp(cp, "floppy"))
-                       return;
-               break;
-
-       case 'k':
-               cp++;
-               if (*cp == 'U')
-                       cp++;
-               goto trymem;
-
-       case 'r':
-               cp++;
-#define is(a,b) cp[0] == 'a' && cp[1] == 'b'
-               if (is(h,p) || is(r,a) || is(u,p) || is(r,k) 
-                   || is(r,m) || is(m,t)) {
-                       cp += 2;
-                       if (isdigit(*cp) && cp[2] == 0)
-                               return;
-               }
-               break;
-
-       case 'm':
-trymem:
-               if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
-                       return;
-               if (cp[0] == 'm' && cp[1] == 't')
-                       return;
-               break;
-
-       case 'n':
-               if (!strcmp(cp, "null"))
-                       return;
-               break;
-
-       case 'v':
-               if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
-                   cp[3] == 0)
-                       return;
-               break;
-       }
-       cp = dbuf->d_name + dbuf->d_namlen - 1;
-       x = 0;
-       if (cp[-1] == 'd') {
-               if (dialbase == -1) {
-                       if (stat("ttyd0", &stb) == 0)
-                               dialbase = stb.st_rdev & 017;
-                       else
-                               dialbase = -2;
-               }
-               if (dialbase == -2)
-                       x = 0;
-               else
-                       x = 11;
-       }
-       if (cp > dbuf->d_name && isdigit(cp[-1]) && isdigit(*cp))
-               x += 10 * (cp[-1] - ' ') + cp[0] - '0';
-       else if (*cp >= 'a' && *cp <= 'f')
-               x += 10 + *cp - 'a';
-       else if (isdigit(*cp))
-               x += *cp - '0';
-       else
-               x = -1;
-donecand:
-       if (nttys >= MAXTTYS) {
-               fprintf(stderr, "ps: tty table overflow\n");
-               exit(1);
-       }
-       dp = &allttys[nttys++];
-       (void) strcpy(dp->name, dbuf->d_name);
-       if (Uflg) {
-               if (stat(dp->name, &stb) == 0 &&
-                  (stb.st_mode&S_IFMT)==S_IFCHR)
-                       dp->ttyd = x = stb.st_rdev;
-               else {
-                       nttys--;
-                       return;
-               }
-       } else
-               dp->ttyd = -1;
-       if (x == -1)
-               return;
-       x &= 017;
-       dp->cand = cand[x];
-       cand[x] = dp;
-}
-
 char *
 char *
-gettty()
+kludge_oldps_options(s)
+       char *s;
 {
 {
-       register char *p;
-       register struct ttys *dp;
-       struct stat stb;
-       int x;
+       size_t len;
+       char *newopts, *ns, *cp;
 
 
-       if (u.u_ttyp == 0)
-               return("?");
-       x = u.u_ttyd & 017;
-       for (dp = cand[x]; dp; dp = dp->cand) {
-               if (dp->ttyd == -1) {
-                       if (stat(dp->name, &stb) == 0 &&
-                          (stb.st_mode&S_IFMT)==S_IFCHR)
-                               dp->ttyd = stb.st_rdev;
-                       else
-                               dp->ttyd = -2;
-               }
-               if (dp->ttyd == u.u_ttyd)
-                       goto found;
-       }
-       /* ick */
-       for (dp = allttys; dp < &allttys[nttys]; dp++) {
-               if (dp->ttyd == -1) {
-                       if (stat(dp->name, &stb) == 0 &&
-                          (stb.st_mode&S_IFMT)==S_IFCHR)
-                               dp->ttyd = stb.st_rdev;
-                       else
-                               dp->ttyd = -2;
-               }
-               if (dp->ttyd == u.u_ttyd)
-                       goto found;
-       }
-       return ("?");
-found:
-       p = dp->name;
-       if (p[0]=='t' && p[1]=='t' && p[2]=='y')
-               p += 3;
-       return (p);
-}
-
-save()
-{
-       register struct savcom *sp;
-       register struct asav *ap;
-       register char *cp;
-       register struct text *xp;
-       char *ttyp, *cmdp;
-
-       if (mproc->p_stat != SZOMB && getu() == 0)
-               return;
-       ttyp = gettty();
-       if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2))
-               return;
-       sp = &savcom[npr];
-       cmdp = getcmd();
-       if (cmdp == 0)
-               return;
-       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);
-       e(a_uid, p_uid); e(a_pid, p_pid); e(a_pri, p_pri);
-       e(a_slptime, p_slptime); e(a_time, p_time);
-       ap->a_tty[0] = ttyp[0];
-       ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' ';
-       if (ap->a_stat == SZOMB) {
-               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;
-               ap->a_cpu = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec;
-               if (sumcpu)
-                       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;
-                       ap->a_txtrss = xp->x_rssize;
-                       ap->a_xccount = xp->x_ccount;
-               }
-       }
-#undef e
-       ap->a_maxrss = mproc->p_maxrss;
-       if (lflg) {
-               register struct lsav *lp;
-
-               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)
-                       e(l_wchan, p_wchan);
-#undef e
-               lp->l_addr = pcbpf;
-       } else if (vflg) {
-               register struct vsav *vp;
-
-               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);
-                       vp->v_majflt = u.u_ru.ru_majflt;
-                       if (mproc->p_textp)
-                               vp->v_txtswrss = xp->x_swrss;
-               }
-               vp->v_pctcpu = pcpu();
-#undef e
-       } else if (uflg)
-               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;
-                       sp->s_un.s_ssiz = (&user.upages[UPAGES][0] - cp);
-               }
-       }
-
-       npr++;
-}
-
-double
-pmem(ap)
-       register struct asav *ap;
-{
-       double fracmem;
-       int szptudot;
-
-       if ((ap->a_flag&SLOAD) == 0)
-               fracmem = 0.0;
-       else {
-               szptudot = UPAGES + clrnd(ctopt(ap->a_size+ap->a_tsiz));
-               fracmem = ((float)ap->a_rss+szptudot)/CLSIZE/ecmx;
-               if (ap->a_xccount)
-                       fracmem += ((float)ap->a_txtrss)/CLSIZE/
-                           ap->a_xccount/ecmx;
-       }
-       return (100.0 * fracmem);
-}
-
-double
-pcpu()
-{
-       time_t time;
-
-       time = mproc->p_time;
-       if (time == 0 || (mproc->p_flag&SLOAD) == 0)
-               return (0.0);
-       if (rawcpu)
-               return (100.0 * mproc->p_pctcpu);
-       return (100.0 * mproc->p_pctcpu / (1.0 - exp(time * log(ccpu))));
-}
-
-getu()
-{
-       struct pte *pteaddr, apte;
-       struct pte arguutl[UPAGES+CLSIZE];
-       register int i;
-       int ncl, size;
-
-       size = sflg ? ctob(UPAGES) : sizeof (struct user);
-       if ((mproc->p_flag & SLOAD) == 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);
-                       return (0);
-               }
-               pcbpf = 0;
-               argaddr = 0;
-               return (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",
-                   mproc->p_pid, kmemf);
-               return (0);
-       }
-       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",
-                   mproc->p_pid, memf);
-               return (0);
-       }
-       if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
-               argaddr = ctob(arguutl[0].pg_pfnum);
-       else
-               argaddr = 0;
-       pcbpf = arguutl[CLSIZE].pg_pfnum;
-       ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
-       while (--ncl >= 0) {
-               i = ncl * CLSIZE;
-               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);
-                       return(0);
-               }
-       }
-       return (1);
-}
-
-char *
-getcmd()
-{
-       char cmdbuf[CLSIZE*NBPG];
-       union {
-               char    argc[CLSIZE*NBPG];
-               int     argi[CLSIZE*NBPG/sizeof (int)];
-       } argspac;
-       register char *cp;
-       register int *ip;
-       char c;
-       int nbad;
-       struct dblock db;
-       char *file;
-
-       if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
-               return ("");
-       if (cflg) {
-               (void) strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm));
-               return (savestr(cmdbuf));
-       }
-       if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) {
-               if (swap < 0)
-                       goto retucomm;
-               vstodb(0, CLSIZE, &u.u_smap, &db, 1);
-               (void) lseek(swap, (long)dtob(db.db_base), 0);
-               if (read(swap, (char *)&argspac, sizeof(argspac))
-                   != sizeof(argspac))
-                       goto bad;
-               file = swapf;
-       } else {
-               lseek(mem, (long)argaddr, 0);
-               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 */
-       while (*--ip)
-               if (ip == argspac.argi)
-                       goto retucomm;
-       *(char *)ip = ' ';
-       ip++;
-       nbad = 0;
-       for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) {
-               c = *cp & 0177;
-               if (c == 0)
-                       *cp = ' ';
-               else if (c < ' ' || c > 0176) {
-                       if (++nbad >= 5*(eflg+1)) {
-                               *cp++ = ' ';
-                               break;
-                       }
-                       *cp = '?';
-               } else if (eflg == 0 && c == '=') {
-                       while (*--cp != ' ')
-                               if (cp <= (char *)ip)
-                                       break;
-                       break;
-               }
-       }
-       *cp = 0;
-       while (*--cp == ' ')
-               *cp = 0;
-       cp = (char *)ip;
-       (void) strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp);
-       if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
-               (void) strcat(cmdbuf, " (");
-               (void) strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
-               (void) strcat(cmdbuf, ")");
-       }
-       return (savestr(cmdbuf));
-
-bad:
-       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));
-       (void) strcat(cmdbuf, ")");
-       return (savestr(cmdbuf));
-}
-
-char   *lhdr =
-"      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;
-       register struct lsav *lp = sp->s_un.lp;
-
-       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_nice-NZERO, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss));
-       printf(lp->l_wchan ? " %6x" : "       ", (int)lp->l_wchan&0xffffff);
-       printf(" %4.4s ", state(ap));
-       ptty(ap->a_tty);
-       ptime(ap);
-}
-
-ptty(tp)
-       char *tp;
-{
-
-       printf("%-2.2s", tp);
-}
-
-ptime(ap)
-       struct asav *ap;
-{
-
-       printf("%3ld:%02ld", ap->a_cpu / 60, ap->a_cpu % 60);
-}
-
-char   *uhdr =
-"USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME";
-upr(sp)
-       struct savcom *sp;
-{
-       register struct asav *ap = sp->ap;
-       int vmsize, rmsize;
-
-       vmsize = pgtok((ap->a_size + ap->a_tsiz));
-       rmsize = pgtok(ap->a_rss);
-       if (ap->a_xccount)
-               rmsize += pgtok(ap->a_txtrss/ap->a_xccount);
-       printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
-           getname(ap->a_uid), ap->a_pid, sp->s_un.u_pctcpu, pmem(ap),
-           vmsize, rmsize);
-       putchar(' ');
-       ptty(ap->a_tty);
-       printf(" %4.4s", state(ap));
-       ptime(ap);
-}
-
-char *vhdr =
-" SIZE  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM"+5;
-vpr(sp)
-       struct savcom *sp;
-{
-       register struct vsav *vp = sp->s_un.vp;
-       register struct asav *ap = sp->ap;
-
-       printf("%5u ", ap->a_pid);
-       ptty(ap->a_tty);
-       printf(" %4.4s", state(ap));
-       ptime(ap);
-       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,
-          pgtok(ap->a_size), pgtok(ap->a_rss));
-       if (ap->a_maxrss == (RLIM_INFINITY/NBPG))
-               printf("   xx");
-       else
-               printf("%5d", pgtok(ap->a_maxrss));
-       printf("%5d%4d%5.1f%5.1f",
-          pgtok(ap->a_tsiz), pgtok(ap->a_txtrss), vp->v_pctcpu, pmem(ap));
-}
-
-char   *shdr =
-"SSIZ   PID TT STAT  TIME";
-spr(sp)
-       struct savcom *sp;
-{
-       register struct asav *ap = sp->ap;
-
-       if (sflg)
-               printf("%4d ", sp->s_un.s_ssiz);
-       printf("%5u", ap->a_pid);
-       putchar(' ');
-       ptty(ap->a_tty);
-       printf(" %4.4s", state(ap));
-       ptime(ap);
-}
-
-char *
-state(ap)
-       register struct asav *ap;
-{
-       char stat, load, nice, anom;
-       static char res[5];
-
-       switch (ap->a_stat) {
-
-       case SSTOP:
-               stat = 'T';
-               break;
-
-       case SSLEEP:
-               if (ap->a_pri >= PZERO)
-                       if (ap->a_slptime >= MAXSLP)
-                               stat = 'I';
-                       else
-                               stat = 'S';
-               else if (ap->a_flag & SPAGE)
-                       stat = 'P';
-               else
-                       stat = 'D';
-               break;
-
-       case SWAIT:
-       case SRUN:
-       case SIDL:
-               stat = 'R';
-               break;
-
-       case SZOMB:
-               stat = 'Z';
-               break;
-
-       default:
-               stat = '?';
-       }
-       load = ap->a_flag & SLOAD ? (ap->a_rss>ap->a_maxrss ? '>' : ' ') : 'W';
-       if (ap->a_nice < NZERO)
-               nice = '<';
-       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;
-       return (res);
-}
-
-/*
- * Given a base/size pair in virtual swap area,
- * return a physical base/size pair which is the
- * (largest) initial, physically contiguous block.
- */
-vstodb(vsbase, vssize, dmp, dbp, rev)
-       register int vsbase;
-       int vssize;
-       struct dmap *dmp;
-       register struct dblock *dbp;
-{
-       register int blk = dmmin;
-       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 (blk < dmmax)
-                       blk *= 2;
-               ip++;
+       len = strlen(s);
+       if ((newopts = ns = malloc(len + 2)) == NULL) {
+               (void)fprintf(stderr, "ps: %s\n", strerror(errno));
+               exit(1);
        }
        }
-       if (*ip <= 0 || *ip + blk > nswap)
-               panic("vstodb *ip");
-       dbp->db_size = min(vssize, blk - vsbase);
-       dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
-}
-
-/*ARGSUSED*/
-panic(cp)
-       char *cp;
-{
-
-#ifdef DEBUG
-       printf("%s\n", cp);
-#endif
-}
-
-min(a, b)
-{
-
-       return (a < b ? a : b);
-}
-
-pscomp(s1, s2)
-       struct savcom *s1, *s2;
-{
-       register int i;
-
-       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 (i == 0)
-               i = s1->ap->a_pid - s2->ap->a_pid;
-       return (i);
-}
-
-vsize(sp)
-       struct savcom *sp;
-{
-       register struct asav *ap = sp->ap;
-       register struct vsav *vp = sp->s_un.vp;
-       
-       if (ap->a_flag & SLOAD)
-               return (ap->a_rss +
-                   ap->a_txtrss / (ap->a_xccount ? ap->a_xccount : 1));
-       return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss));
-}
-
-#define        NMAX    8       /* sizeof loginname (should be sizeof (utmp.ut_name)) */
-#define NUID   2048    /* must not be a multiple of 5 */
-
-struct nametable {
-       char    nt_name[NMAX+1];
-       int     nt_uid;
-} nametable[NUID];
-
-struct nametable *
-findslot(uid)
-int    uid;
-{
-       register struct nametable       *n, *start;
-
        /*
        /*
-        * find the uid or an empty slot.
-        * return NULL if neither found.
+        * options begin with '-'
         */
         */
-
-       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);
-}
-
-char *
-getname(uid)
-{
-       register struct passwd          *pw;
-       static                          init = 0;
-       struct passwd                   *getpwent();
-       register struct nametable       *n;
-
+       if (*s != '-')
+               *ns++ = '-';    /* add option flag */
        /*
        /*
-        * find uid in hashed table; add it if not found.
-        * return pointer to name.
+        * gaze to end of argv[1]
         */
         */
+       cp = s + len - 1;
+       /*
+        * if last letter is a 't' flag with no argument (in the context
+        * of the oldps options -- option string NOT starting with a '-' --
+        * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
+        */
+       if (*cp == 't' && *s != '-')
+               *cp = 'T';
+       else {
+               /*
+                * otherwise check for trailing number, which *may* be a
+                * pid.
+                */
+               while (cp >= s && isdigit(*cp))
+                       --cp;
+       }
+       cp++;
+       bcopy(s, ns, (size_t)(cp - s)); /* copy up to trailing number */
+       ns += cp - s;
+       /*
+        * if there's a trailing number, and not a preceding 'p' (pid) or
+        * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
+        */
+       if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' &&
+           (cp - 1 == s || cp[-2] != 't')))
+               *ns++ = 'p';
+       (void) strcpy(ns, cp);          /* and append the number */
 
 
-       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);
-       }
-       /* NOTREACHED */
+       return (newopts);
 }
 
 }
 
-char *
-savestr(cp)
-       char *cp;
+#ifdef lint
+/* VARARGS1 */
+error(fmt) char *fmt; { (void) fputs(fmt, stderr); exit(1); /* NOTREACHED */ }
+#else
+error(fmt)
+       char *fmt;
 {
 {
-       register unsigned len;
-       register char *dp;
+       va_list ap;
 
 
-       len = strlen(cp);
-       dp = (char *)calloc(len+1, sizeof (char));
-       (void) strcpy(dp, cp);
-       return (dp);
+       va_start(ap, fmt);
+       (void) fprintf(stderr, "ps: ");
+       (void) vfprintf(stderr, fmt, ap);
+       (void) fprintf(stderr, "\n");
+       va_end(ap);
+       exit(1);
 }
 }
+#endif
 
 
-/*
- * This routine was stolen from adb to simulate memory management
- * on the VAX.
- */
-off_t
-vtophys(loc)
-long   loc;
+usage()
 {
 {
-       register        p;
-       off_t   newloc;
-
-       newloc = loc & ~0xc0000000;
-       p = btop(newloc);
-       if ((loc & 0xc0000000) == 0) {
-               fprintf(stderr, "Vtophys: translating non-kernel address\n");
-               return((off_t) -1);
-       }
-       if (p >= Syssize) {
-               fprintf(stderr, "Vtophys: page out of bound (%d>=%d)\n",
-                       p, Syssize);
-               return((off_t) -1);
-       }
-       if (Sysmap[p].pg_v == 0
-       && (Sysmap[p].pg_fod || Sysmap[p].pg_pfnum == 0)) {
-               fprintf(stderr, "Vtophys: page not valid\n");
-               return((off_t) -1);
-       }
-       loc = (long) (ptob(Sysmap[p].pg_pfnum) + (loc & PGOFSET));
-       return(loc);
+       (void) fprintf(stderr,
+           "usage:\tps [ -aChjlmrSsTuvwx ] [ -O|o fmt ] [ -p pid ] [ -t tty ] [ system ] [ core ] [ swap ]\n\t ps [ -L ]\n");
+       exit(1);
 }
 }