- maybetty(dp);
- }
- fclose(df);
-}
-
-/*
- * 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.
- */
-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 (0);
- break;
-
- case 'f':
- if (!strcmp(cp, "floppy"))
- return (0);
- break;
-
- case 'k':
- cp++;
- if (*cp == 'U')
- cp++;
- goto trymem;
-
- case 'r':
- cp++;
- if (*cp == 'r' || *cp == 'u' || *cp == 'h')
- cp++;
-#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)) {
- cp += 2;
- if (isdigit(*cp) && cp[2] == 0)
- return (0);
- }
- break;
-
- case 'm':
-trymem:
- if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
- return (0);
- break;
-
- case 'n':
- if (!strcmp(cp, "null"))
- return (0);
- break;
-
- case 'v':
- if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
- cp[3] == 0)
- return (0);
- break;
- }
-mightbe:
- cp = dbuf.d_name;
- while (cp < &dbuf.d_name[DIRSIZ] && *cp)
- cp++;
- --cp;
- 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:
- dp = (struct ttys *)alloc(sizeof (struct ttys));
- strncpy(dp->name, dbuf.d_name, DIRSIZ);
- dp->next = allttys;
- dp->ttyd = -1;
- allttys = dp;
- if (x == -1)
- return;
- x &= 017;
- dp->cand = cand[x];
- cand[x] = dp;
-}
-
-char *
-gettty()
-{
- register char *p;
- register struct ttys *dp;
- struct stat stb;
- int x;
-
- 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; dp = dp->next) {
- if (dp->ttyd == -1) {
- if (stat(dp->name, &stb) == 0)
- 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 && strcmpn(tptr, ttyp, 2))
- return;
- sp = &savcom[npr];
- cmdp = getcmd();
- if (cmdp == 0)
- return;
- sp->ap = ap = (struct asav *)alloc(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) {
- register struct xproc *xp = (struct xproc *)mproc;
-
- ap->a_cpu = xp->xp_vm.vm_utime + xp->xp_vm.vm_stime;
- } 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;
- if (sumcpu)
- ap->a_cpu += u.u_cvm.vm_utime + u.u_cvm.vm_stime;
- if (mproc->p_textp && text) {
- xp = &text[mproc->p_textp -
- (struct text *)nl[X_TEXT].n_value];
- ap->a_tsiz = xp->x_size;
- ap->a_txtrss = xp->x_rssize;
- ap->a_xccount = xp->x_ccount;
- }
- }
-#undef e
- ap->a_cpu /= HZ;
- if (lflg) {
- register struct lsav *lp;
-
- sp->sun.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)
- e(l_wchan, p_wchan);
-#undef e
- lp->l_addr = pcbpf;
- } else if (vflg) {
- register struct vsav *vp;
-
- sp->sun.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);
- vp->v_majflt = u.u_vm.vm_majflt;
- if (mproc->p_textp)
- vp->v_txtswrss = xp->x_swrss;