use getopt; fix usage statement; -a should imply -p; minor cleanups
[unix-history] / usr / src / usr.sbin / pstat / pstat.c
index e5cb16b..b784c59 100644 (file)
@@ -8,19 +8,16 @@
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)pstat.c    5.4 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)pstat.c    5.17 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * Print system stuff
  */
 
 
 /*
  * Print system stuff
  */
 
-#define mask(x) (x&0377)
-#define        clear(x) ((int)x&0x7fffffff)
-
 #include <sys/param.h>
 #include <sys/dir.h>
 #define        KERNEL
 #include <sys/param.h>
 #include <sys/dir.h>
 #define        KERNEL
@@ -37,6 +34,10 @@ static char sccsid[] = "@(#)pstat.c  5.4 (Berkeley) %G%";
 #include <sys/vm.h>
 #include <nlist.h>
 #include <machine/pte.h>
 #include <sys/vm.h>
 #include <nlist.h>
 #include <machine/pte.h>
+#include <stdio.h>
+
+#define mask(x)                (x&0377)
+#define        clear(x)        ((int)x &~ KERNBASE)
 
 char   *fcore  = "/dev/kmem";
 char   *fmem   = "/dev/mem";
 
 char   *fcore  = "/dev/kmem";
 char   *fmem   = "/dev/mem";
@@ -50,56 +51,72 @@ struct nlist nl[] = {
        { "_text" },
 #define        SPROC   2
        { "_proc" },
        { "_text" },
 #define        SPROC   2
        { "_proc" },
-#define        SDZ     3
-       { "_dz_tty" },
-#define        SNDZ    4
-       { "_dz_cnt" },
-#define        SKL     5
+#define        SCONS   3
        { "_cons" },
        { "_cons" },
-#define        SFIL    6
+#define        SFIL    4
        { "_file" },
        { "_file" },
-#define        USRPTMA 7
+#define        USRPTMA 5
        { "_Usrptmap" },
        { "_Usrptmap" },
-#define        USRPT   8
+#define        USRPT   6
        { "_usrpt" },
        { "_usrpt" },
-#define        SWAPMAP 9
+#define        SWAPMAP 7
        { "_swapmap" },
        { "_swapmap" },
-#define        SDH     10
-       { "_dh11" },
-#define        SNDH    11
-       { "_ndh11" },
-#define        SNPROC  12
+#define        SNPROC  8
        { "_nproc" },
        { "_nproc" },
-#define        SNTEXT  13
+#define        SNTEXT  9
        { "_ntext" },
        { "_ntext" },
-#define        SNFILE  14
+#define        SNFILE  10
        { "_nfile" },
        { "_nfile" },
-#define        SNINODE 15
+#define        SNINODE 11
        { "_ninode" },
        { "_ninode" },
-#define        SNSWAPMAP 16
+#define        SNSWAPMAP 12
        { "_nswapmap" },
        { "_nswapmap" },
-#define        SPTY    17
+#define        SPTY    13
        { "_pt_tty" },
        { "_pt_tty" },
-#define        SDMMIN  18
+#define        SDMMIN  14
        { "_dmmin" },
        { "_dmmin" },
-#define        SDMMAX  19
+#define        SDMMAX  15
        { "_dmmax" },
        { "_dmmax" },
-#define        SNSWDEV 20
+#define        SNSWDEV 16
        { "_nswdev" },
        { "_nswdev" },
-#define        SSWDEVT 21
+#define        SSWDEVT 17
        { "_swdevt" },
        { "_swdevt" },
-#define        SDMF    22
+#define        SYSMAP  18
+       { "_Sysmap" },
+#define        SNPTY   19
+       { "_npty" },
+#ifdef vax
+#define        SDZ     (SNPTY+1)
+       { "_dz_tty" },
+#define        SNDZ    (SNPTY+2)
+       { "_dz_cnt" },
+#define        SDMF    (SNPTY+3)
        { "_dmf_tty" },
        { "_dmf_tty" },
-#define        SNDMF   23
+#define        SNDMF   (SNPTY+4)
        { "_ndmf" },
        { "_ndmf" },
-#define        SNPTY   24
-       { "_npty" },
-#define        SDHU    25
+#define        SDH     (SNPTY+5)
+       { "_dh11" },
+#define        SNDH    (SNPTY+6)
+       { "_ndh11" },
+#define        SDHU    (SNPTY+7)
        { "_dhu_tty" },
        { "_dhu_tty" },
-#define        SNDHU   26
+#define        SNDHU   (SNPTY+8)
        { "_ndhu" },
        { "_ndhu" },
-#define        SYSMAP  27
-       { "_Sysmap" },
+#define        SDMZ    (SNPTY+9)
+       { "_dmz_tty" },
+#define        SNDMZ   (SNPTY+10)
+       { "_ndmz" },
+#define        SQD     (SNPTY+11)
+       { "_qd_tty" },
+#define        SNQD    (SNPTY+12)
+       { "_nNQD" },
+#endif
+#ifdef tahoe
+#define        SVX     (SNPTY+1)
+       { "_vx_tty" },
+#define        SNVX    (SNPTY+2)
+       { "_nvx" },
+#endif
        { "" }
 };
 
        { "" }
 };
 
@@ -119,90 +136,81 @@ int       allflg;
 int    kflg;
 struct pte *Usrptma;
 struct pte *usrpt;
 int    kflg;
 struct pte *Usrptma;
 struct pte *usrpt;
+u_long getword();
+off_t  mkphys();
 
 main(argc, argv)
 
 main(argc, argv)
-char **argv;
+       int argc;
+       char **argv;
 {
 {
-       register char *argp;
-       int allflags;
-
-       argc--, argv++;
-       while (argc > 0 && **argv == '-') {
-               argp = *argv++;
-               argp++;
-               argc--;
-               while (*argp++)
-               switch (argp[-1]) {
+       extern char *optarg;
+       extern int optind;
+       int ch;
 
 
+       while ((ch = getopt(argc, argv, "Tafikptu:sx")) != EOF)
+               switch((char)ch) {
                case 'T':
                        totflg++;
                        break;
                case 'T':
                        totflg++;
                        break;
-
                case 'a':
                        allflg++;
                case 'a':
                        allflg++;
+                       /*FALLTHROUGH*/
+               case 'p':
+                       prcf++;
+                       break;
+               case 'f':
+                       filf++;
                        break;
                        break;
-
                case 'i':
                        inof++;
                        break;
                case 'i':
                        inof++;
                        break;
-
-               case 'k':
+               case 'k':                       /* undocumented */
                        kflg++;
                        fcore = fmem = "/vmcore";
                        break;
                        kflg++;
                        fcore = fmem = "/vmcore";
                        break;
-
-               case 'x':
-                       txtf++;
-                       break;
-
-               case 'p':
-                       prcf++;
-                       break;
-
                case 't':
                        ttyf++;
                        break;
                case 't':
                        ttyf++;
                        break;
-
                case 'u':
                case 'u':
-                       if (argc == 0)
-                               break;
-                       argc--;
                        usrf++;
                        usrf++;
-                       sscanf( *argv++, "%x", &ubase);
-                       break;
-
-               case 'f':
-                       filf++;
+                       sscanf(optarg, "%x", &ubase);
                        break;
                case 's':
                        swpf++;
                        break;
                        break;
                case 's':
                        swpf++;
                        break;
+               case 'x':
+                       txtf++;
+                       break;
+               case '?':
                default:
                default:
-                       usage();
+                       printf("usage: pstat -[Tafiptsx] [-u [ubase]] [system] [core]\n");
                        exit(1);
                }
                        exit(1);
                }
-       }
-       if (argc>1)
+       argc -= optind;
+       argv += optind;
+
+       if (argc>1) {
                fcore = fmem = argv[1];
                fcore = fmem = argv[1];
-       if ((fc = open(fcore, 0)) < 0) {
-               printf("Can't find %s\n", fcore);
+               kflg++;
+       }
+       if ((fc = open(fcore, O_RDONLY, 0)) < 0) {
+               perror(fcore);
                exit(1);
        }
                exit(1);
        }
-       if ((fm = open(fmem, 0)) < 0) {
-               printf("Can't find %s\n", fmem);
+       if ((fm = open(fmem, O_RDONLY, 0)) < 0) {
+               perror(fmem);
                exit(1);
        }
        if (argc>0)
                fnlist = argv[0];
        nlist(fnlist, nl);
                exit(1);
        }
        if (argc>0)
                fnlist = argv[0];
        nlist(fnlist, nl);
-       usrpt = (struct pte *)nl[USRPT].n_value;
-       Usrptma = (struct pte *)nl[USRPTMA].n_value;
        if (nl[0].n_type == 0) {
        if (nl[0].n_type == 0) {
-               printf("no namelist\n");
+               printf("pstat: no namelist.\n");
                exit(1);
        }
                exit(1);
        }
-       allflags = filf | totflg | inof | prcf | txtf | ttyf | usrf | swpf;
-       if (allflags == 0) {
+       usrpt = (struct pte *)nl[USRPT].n_value;
+       Usrptma = (struct pte *)nl[USRPTMA].n_value;
+       if (!(filf | totflg | inof | prcf | txtf | ttyf | usrf | swpf)) {
                printf("pstat: one or more of -[aixptfsu] is required\n");
                exit(1);
        }
                printf("pstat: one or more of -[aixptfsu] is required\n");
                exit(1);
        }
@@ -222,12 +230,6 @@ char **argv;
                doswap();
 }
 
                doswap();
 }
 
-usage()
-{
-
-       printf("usage: pstat -[aixptfs] [-u [ubase]] [system] [core]\n");
-}
-
 doinode()
 {
        register struct inode *ip;
 doinode()
 {
        register struct inode *ip;
@@ -236,10 +238,19 @@ doinode()
        int ninode;
 
        nin = 0;
        int ninode;
 
        nin = 0;
-       ninode = getw(nl[SNINODE].n_value);
+       ninode = getword(nl[SNINODE].n_value);
        xinode = (struct inode *)calloc(ninode, sizeof (struct inode));
        xinode = (struct inode *)calloc(ninode, sizeof (struct inode));
-       ainode = (struct inode *)getw(nl[SINODE].n_value);
-       lseek(fc, mkphys((int)ainode), 0);
+       ainode = (struct inode *)getword(nl[SINODE].n_value);
+       if (ninode < 0 || ninode > 10000) {
+               fprintf(stderr, "number of inodes is preposterous (%d)\n",
+                       ninode);
+               return;
+       }
+       if (xinode == NULL) {
+               fprintf(stderr, "can't allocate memory for inode table\n");
+               return;
+       }
+       lseek(fc, mkphys((off_t)ainode), 0);
        read(fc, xinode, ninode * sizeof(struct inode));
        for (ip = xinode; ip < &xinode[ninode]; ip++)
                if (ip->i_count)
        read(fc, xinode, ninode * sizeof(struct inode));
        for (ip = xinode; ip < &xinode[ninode]; ip++)
                if (ip->i_count)
@@ -281,17 +292,16 @@ printf("   LOC      FLAGS    CNT DEVICE  RDC WRC  INO  MODE  NLK UID   SIZE/DEV\
        free(xinode);
 }
 
        free(xinode);
 }
 
-getw(loc)
+u_long
+getword(loc)
        off_t loc;
 {
        off_t loc;
 {
-       int word;
+       u_long word;
 
        if (kflg)
 
        if (kflg)
-               loc &= 0x7fffffff;
+               loc = clear(loc);
        lseek(fc, loc, 0);
        read(fc, &word, sizeof (word));
        lseek(fc, loc, 0);
        read(fc, &word, sizeof (word));
-       if (kflg)
-               word &= 0x7fffffff;
        return (word);
 }
 
        return (word);
 }
 
@@ -308,23 +318,36 @@ dotext()
        register struct text *xp;
        int ntext;
        struct text *xtext, *atext;
        register struct text *xp;
        int ntext;
        struct text *xtext, *atext;
-       int ntx;
+       int ntx, ntxca;
 
 
-       ntx = 0;
-       ntext = getw(nl[SNTEXT].n_value);
+       ntx = ntxca = 0;
+       ntext = getword(nl[SNTEXT].n_value);
        xtext = (struct text *)calloc(ntext, sizeof (struct text));
        xtext = (struct text *)calloc(ntext, sizeof (struct text));
-       atext = (struct text *)getw(nl[STEXT].n_value);
-       lseek(fc, mkphys((int)atext), 0);
+       atext = (struct text *)getword(nl[STEXT].n_value);
+       if (ntext < 0 || ntext > 10000) {
+               fprintf(stderr, "number of texts is preposterous (%d)\n",
+                       ntext);
+               return;
+       }
+       if (xtext == NULL) {
+               fprintf(stderr, "can't allocate memory for text table\n");
+               return;
+       }
+       lseek(fc, mkphys((off_t)atext), 0);
        read(fc, xtext, ntext * sizeof (struct text));
        read(fc, xtext, ntext * sizeof (struct text));
-       for (xp = xtext; xp < &xtext[ntext]; xp++)
-               if (xp->x_iptr!=NULL)
+       for (xp = xtext; xp < &xtext[ntext]; xp++) {
+               if (xp->x_iptr != NULL)
+                       ntxca++;
+               if (xp->x_count != 0)
                        ntx++;
                        ntx++;
+       }
        if (totflg) {
        if (totflg) {
-               printf("%3d/%3d texts\n", ntx, ntext);
+               printf("%3d/%3d texts active, %3d used\n", ntx, ntext, ntxca);
                return;
        }
                return;
        }
-       printf("%d/%d active texts\n", ntx, ntext);
-       printf("   LOC   FLAGS DADDR      CADDR  RSS SIZE      IPTR  CNT CCNT\n");
+       printf("%d/%d active texts, %d used\n", ntx, ntext, ntxca);
+       printf("\
+   LOC   FLAGS DADDR     CADDR  RSS SIZE     IPTR   CNT CCNT      FORW     BACK\n");
        for (xp = xtext; xp < &xtext[ntext]; xp++) {
                if (xp->x_iptr == NULL)
                        continue;
        for (xp = xtext; xp < &xtext[ntext]; xp++) {
                if (xp->x_iptr == NULL)
                        continue;
@@ -337,12 +360,14 @@ dotext()
                putf(xp->x_flag&XLOCK, 'K');
                putf(xp->x_flag&XWANT, 'w');
                printf("%5x", xp->x_daddr[0]);
                putf(xp->x_flag&XLOCK, 'K');
                putf(xp->x_flag&XWANT, 'w');
                printf("%5x", xp->x_daddr[0]);
-               printf("%11x", xp->x_caddr);
+               printf("%10x", xp->x_caddr);
                printf("%5d", xp->x_rssize);
                printf("%5d", xp->x_size);
                printf("%10.1x", xp->x_iptr);
                printf("%5d", xp->x_count&0377);
                printf("%5d", xp->x_ccount);
                printf("%5d", xp->x_rssize);
                printf("%5d", xp->x_size);
                printf("%10.1x", xp->x_iptr);
                printf("%5d", xp->x_count&0377);
                printf("%5d", xp->x_ccount);
+               printf("%10x", xp->x_forw);
+               printf("%9x", xp->x_back);
                printf("\n");
        }
        free(xtext);
                printf("\n");
        }
        free(xtext);
@@ -356,10 +381,19 @@ doproc()
        register loc, np;
        struct pte apte;
 
        register loc, np;
        struct pte apte;
 
-       nproc = getw(nl[SNPROC].n_value);
+       nproc = getword(nl[SNPROC].n_value);
        xproc = (struct proc *)calloc(nproc, sizeof (struct proc));
        xproc = (struct proc *)calloc(nproc, sizeof (struct proc));
-       aproc = (struct proc *)getw(nl[SPROC].n_value);
-       lseek(fc, mkphys((int)aproc), 0);
+       aproc = (struct proc *)getword(nl[SPROC].n_value);
+       if (nproc < 0 || nproc > 10000) {
+               fprintf(stderr, "number of procs is preposterous (%d)\n",
+                       nproc);
+               return;
+       }
+       if (xproc == NULL) {
+               fprintf(stderr, "can't allocate memory for proc table\n");
+               return;
+       }
+       lseek(fc, mkphys((off_t)aproc), 0);
        read(fc, xproc, nproc * sizeof (struct proc));
        np = 0;
        for (pp=xproc; pp < &xproc[nproc]; pp++)
        read(fc, xproc, nproc * sizeof (struct proc));
        np = 0;
        for (pp=xproc; pp < &xproc[nproc]; pp++)
@@ -404,6 +438,7 @@ doproc()
                printf(" %7x", clear(pp->p_textp));
                printf("\n");
        }
                printf(" %7x", clear(pp->p_textp));
                printf("\n");
        }
+       free(xproc);
 }
 
 static char mesg[] =
 }
 
 static char mesg[] =
@@ -421,11 +456,14 @@ dotty()
        }
        printf("1 cons\n");
        if (kflg)
        }
        printf("1 cons\n");
        if (kflg)
-               nl[SKL].n_value = clear(nl[SKL].n_value);
-       lseek(fc, (long)nl[SKL].n_value, 0);
+               nl[SCONS].n_value = clear(nl[SCONS].n_value);
+       lseek(fc, (long)nl[SCONS].n_value, 0);
        read(fc, tty, sizeof(*tty));
        printf(mesg);
        ttyprt(&tty[0], 0);
        read(fc, tty, sizeof(*tty));
        printf(mesg);
        ttyprt(&tty[0], 0);
+#ifdef vax
+       if (nl[SNQD].n_type != 0) 
+               doqdss();
        if (nl[SNDZ].n_type != 0)
                dottytype("dz", SDZ, SNDZ);
        if (nl[SNDH].n_type != 0)
        if (nl[SNDZ].n_type != 0)
                dottytype("dz", SDZ, SNDZ);
        if (nl[SNDH].n_type != 0)
@@ -434,10 +472,42 @@ dotty()
                dottytype("dmf", SDMF, SNDMF);
        if (nl[SNDHU].n_type != 0)
                dottytype("dhu", SDHU, SNDHU);
                dottytype("dmf", SDMF, SNDMF);
        if (nl[SNDHU].n_type != 0)
                dottytype("dhu", SDHU, SNDHU);
+       if (nl[SNDMZ].n_type != 0)
+               dottytype("dmz", SDMZ, SNDMZ);
+#endif
+#ifdef tahoe
+       if (nl[SNVX].n_type != 0)
+               dottytype("vx", SVX, SNVX);
+#endif
        if (nl[SNPTY].n_type != 0)
                dottytype("pty", SPTY, SNPTY);
 }
 
        if (nl[SNPTY].n_type != 0)
                dottytype("pty", SPTY, SNPTY);
 }
 
+/* 
+ * Special case the qdss because there are 4 tty structs per qdss 
+ * and only the first of each is used as a tty.  
+ */
+#ifdef vax
+doqdss()
+{
+       int nqd;
+       register struct tty *tp;
+
+       if (kflg) {
+               nl[SNQD].n_value = clear(nl[SNQD].n_value);
+               nl[SQD].n_value = clear(nl[SQD].n_value);
+       }
+       lseek(fc, (long)nl[SNQD].n_value, 0);
+       read(fc, &nqd, sizeof(nqd));
+       printf("%d qd\n", nqd);
+       lseek(fc, (long)nl[SQD].n_value, 0);
+       read(fc, tty, nqd * sizeof(struct tty) * 4);
+       printf(mesg);
+       for (tp = tty; tp < &tty[nqd * 4]; tp += 4)
+               ttyprt(tp, tp - tty);
+}
+#endif
+
 dottytype(name, type, number)
 char *name;
 {
 dottytype(name, type, number)
 char *name;
 {
@@ -445,7 +515,7 @@ char *name;
        register struct tty *tp;
        extern char *realloc();
 
        register struct tty *tp;
        extern char *realloc();
 
-       if (tty == (struct tty *)0)
+       if (tty == (struct tty *)0) 
                return;
        if (kflg) {
                nl[number].n_value = clear(nl[number].n_value);
                return;
        if (kflg) {
                nl[number].n_value = clear(nl[number].n_value);
@@ -477,14 +547,14 @@ struct tty *atp;
        tp = atp;
        switch (tp->t_line) {
 
        tp = atp;
        switch (tp->t_line) {
 
-/*
+#ifdef notdef
        case NETLDISC:
                if (tp->t_rec)
                        printf("%4d%4d", 0, tp->t_inbuf);
                else
                        printf("%4d%4d", tp->t_inbuf, 0);
                break;
        case NETLDISC:
                if (tp->t_rec)
                        printf("%4d%4d", 0, tp->t_inbuf);
                else
                        printf("%4d%4d", tp->t_inbuf, 0);
                break;
-*/
+#endif
 
        default:
                printf("%4d%4d", tp->t_rawq.c_cc, tp->t_canq.c_cc);
 
        default:
                printf("%4d%4d", tp->t_rawq.c_cc, tp->t_canq.c_cc);
@@ -513,15 +583,15 @@ struct tty *atp;
                break;
 
        case NETLDISC:
                break;
 
        case NETLDISC:
-               printf(" net\n");
+               printf(" berknet\n");
                break;
 
        case TABLDISC:
                printf(" tab\n");
                break;
 
                break;
 
        case TABLDISC:
                printf(" tab\n");
                break;
 
-       case NTABLDISC:
-               printf(" ntab\n");
+       case SLIPDISC:
+               printf(" slip\n");
                break;
 
        default:
                break;
 
        default:
@@ -612,27 +682,27 @@ dousr()
                printf("\n");
        printf("code\t%.1x\n", U.u_code);
        printf("ar0\t%.1x\n", U.u_ar0);
                printf("\n");
        printf("code\t%.1x\n", U.u_code);
        printf("ar0\t%.1x\n", U.u_ar0);
-       printf("prof\t%X %X %X %X\n", U.u_prof.pr_base, U.u_prof.pr_size,
+       printf("prof\t%x %x %x %x\n", U.u_prof.pr_base, U.u_prof.pr_size,
            U.u_prof.pr_off, U.u_prof.pr_scale);
        printf("\neosys\t%d\n", U.u_eosys);
        printf("ttyp\t%.1x\n", U.u_ttyp);
        printf("ttyd\t%d,%d\n", major(U.u_ttyd), minor(U.u_ttyd));
        printf("comm %.14s\n", U.u_comm);
            U.u_prof.pr_off, U.u_prof.pr_scale);
        printf("\neosys\t%d\n", U.u_eosys);
        printf("ttyp\t%.1x\n", U.u_ttyp);
        printf("ttyd\t%d,%d\n", major(U.u_ttyd), minor(U.u_ttyd));
        printf("comm %.14s\n", U.u_comm);
-       printf("start\t%D\n", U.u_start);
-       printf("acflag\t%D\n", U.u_acflag);
-       printf("cmask\t%D\n", U.u_cmask);
+       printf("start\t%ld\n", U.u_start.tv_sec);
+       printf("acflag\t%ld\n", U.u_acflag);
+       printf("cmask\t%ld\n", U.u_cmask);
        printf("sizes\t%.1x %.1x %.1x\n", U.u_tsize, U.u_dsize, U.u_ssize);
        printf("ru\t");
        ip = (int *)&U.u_ru;
        for (i = 0; i < sizeof(U.u_ru)/sizeof(int); i++)
        printf("sizes\t%.1x %.1x %.1x\n", U.u_tsize, U.u_dsize, U.u_ssize);
        printf("ru\t");
        ip = (int *)&U.u_ru;
        for (i = 0; i < sizeof(U.u_ru)/sizeof(int); i++)
-               printf("%D ", ip[i]);
+               printf("%ld ", ip[i]);
        printf("\n");
        ip = (int *)&U.u_cru;
        printf("cru\t");
        for (i = 0; i < sizeof(U.u_cru)/sizeof(int); i++)
        printf("\n");
        ip = (int *)&U.u_cru;
        printf("cru\t");
        for (i = 0; i < sizeof(U.u_cru)/sizeof(int); i++)
-               printf("%D ", ip[i]);
+               printf("%ld ", ip[i]);
        printf("\n");
        printf("\n");
-/*
+#ifdef notdef
        i =  U.u_stack - &U;
        while (U[++i] == 0);
        i &= ~07;
        i =  U.u_stack - &U;
        while (U[++i] == 0);
        i &= ~07;
@@ -642,7 +712,7 @@ dousr()
                        printf("%9x", U[i++]);
                printf("\n");
        }
                        printf("%9x", U[i++]);
                printf("\n");
        }
-*/
+#endif
 }
 
 oatoi(s)
 }
 
 oatoi(s)
@@ -666,10 +736,19 @@ dofile()
        static char *dtypes[] = { "???", "inode", "socket" };
 
        nf = 0;
        static char *dtypes[] = { "???", "inode", "socket" };
 
        nf = 0;
-       nfile = getw(nl[SNFILE].n_value);
+       nfile = getword(nl[SNFILE].n_value);
        xfile = (struct file *)calloc(nfile, sizeof (struct file));
        xfile = (struct file *)calloc(nfile, sizeof (struct file));
-       afile = (struct file *)getw(nl[SFIL].n_value);
-       lseek(fc, (mkphys((int)afile)), 0);
+       afile = (struct file *)getword(nl[SFIL].n_value);
+       if (nfile < 0 || nfile > 10000) {
+               fprintf(stderr, "number of files is preposterous (%d)\n",
+                       nfile);
+               return;
+       }
+       if (xfile == NULL) {
+               fprintf(stderr, "can't allocate memory for file table\n");
+               return;
+       }
+       lseek(fc, mkphys((off_t)afile), 0);
        read(fc, xfile, nfile * sizeof (struct file));
        for (fp=xfile; fp < &xfile[nfile]; fp++)
                if (fp->f_count)
        read(fc, xfile, nfile * sizeof (struct file));
        for (fp=xfile; fp < &xfile[nfile]; fp++)
                if (fp->f_count)
@@ -687,7 +766,7 @@ dofile()
                if (fp->f_type <= DTYPE_SOCKET)
                        printf("%-8.8s", dtypes[fp->f_type]);
                else
                if (fp->f_type <= DTYPE_SOCKET)
                        printf("%-8.8s", dtypes[fp->f_type]);
                else
-                       printf("8d", fp->f_type);
+                       printf("%8d", fp->f_type);
                putf(fp->f_flag&FREAD, 'R');
                putf(fp->f_flag&FWRITE, 'W');
                putf(fp->f_flag&FAPPEND, 'A');
                putf(fp->f_flag&FREAD, 'R');
                putf(fp->f_flag&FWRITE, 'W');
                putf(fp->f_flag&FAPPEND, 'A');
@@ -702,6 +781,7 @@ dofile()
                else
                        printf("  %ld\n", fp->f_offset);
        }
                else
                        printf("  %ld\n", fp->f_offset);
        }
+       free(xfile);
 }
 
 int dmmin, dmmax, nswdev;
 }
 
 int dmmin, dmmax, nswdev;
@@ -721,30 +801,53 @@ doswap()
        register struct mapent *me;
        register struct text *xp;
        int i, j;
        register struct mapent *me;
        register struct text *xp;
        int i, j;
+       long rmalloc();
 
 
-       nproc = getw(nl[SNPROC].n_value);
+       nproc = getword(nl[SNPROC].n_value);
+       ntext = getword(nl[SNTEXT].n_value);
+       if (nproc < 0 || nproc > 10000 || ntext < 0 || ntext > 10000) {
+               fprintf(stderr, "number of procs/texts is preposterous (%d, %d)\n",
+                       nproc, ntext);
+               return;
+       }
        proc = (struct proc *)calloc(nproc, sizeof (struct proc));
        proc = (struct proc *)calloc(nproc, sizeof (struct proc));
-       ntext = getw(nl[SNTEXT].n_value);
+       if (proc == NULL) {
+               fprintf(stderr, "can't allocate memory for proc table\n");
+               exit(1);
+       }
        xtext = (struct text *)calloc(ntext, sizeof (struct text));
        xtext = (struct text *)calloc(ntext, sizeof (struct text));
-       nswapmap = getw(nl[SNSWAPMAP].n_value);
+       if (xtext == NULL) {
+               fprintf(stderr, "can't allocate memory for text table\n");
+               exit(1);
+       }
+       nswapmap = getword(nl[SNSWAPMAP].n_value);
        swapmap = (struct map *)calloc(nswapmap, sizeof (struct map));
        swapmap = (struct map *)calloc(nswapmap, sizeof (struct map));
-       nswdev = getw(nl[SNSWDEV].n_value);
+       if (swapmap == NULL) {
+               fprintf(stderr, "can't allocate memory for swapmap\n");
+               exit(1);
+       }
+       nswdev = getword(nl[SNSWDEV].n_value);
        swdevt = (struct swdevt *)calloc(nswdev, sizeof (struct swdevt));
        swdevt = (struct swdevt *)calloc(nswdev, sizeof (struct swdevt));
-       lseek(fc, nl[SSWDEVT].n_value, L_SET);
+       if (swdevt == NULL) {
+               fprintf(stderr, "can't allocate memory for swdevt table\n");
+               exit(1);
+       }
+       lseek(fc, mkphys((off_t)nl[SSWDEVT].n_value), L_SET);
        read(fc, swdevt, nswdev * sizeof (struct swdevt));
        read(fc, swdevt, nswdev * sizeof (struct swdevt));
-       lseek(fc, getw(nl[SPROC].n_value), 0);
+       lseek(fc, mkphys((off_t)getword(nl[SPROC].n_value)), 0);
        read(fc, proc, nproc * sizeof (struct proc));
        read(fc, proc, nproc * sizeof (struct proc));
-       lseek(fc, getw(nl[STEXT].n_value), 0);
+       lseek(fc, mkphys((off_t)getword(nl[STEXT].n_value)), 0);
        read(fc, xtext, ntext * sizeof (struct text));
        read(fc, xtext, ntext * sizeof (struct text));
-       lseek(fc, getw(nl[SWAPMAP].n_value), 0);
+       lseek(fc, mkphys((off_t)getword(nl[SWAPMAP].n_value)), 0);
        read(fc, swapmap, nswapmap * sizeof (struct map));
        swapmap->m_name = "swap";
        swapmap->m_limit = (struct mapent *)&swapmap[nswapmap];
        read(fc, swapmap, nswapmap * sizeof (struct map));
        swapmap->m_name = "swap";
        swapmap->m_limit = (struct mapent *)&swapmap[nswapmap];
-       dmmin = getw(nl[SDMMIN].n_value);
-       dmmax = getw(nl[SDMMAX].n_value);
+       dmmin = getword(nl[SDMMIN].n_value);
+       dmmax = getword(nl[SDMMAX].n_value);
        nswap = 0;
        for (sw = swdevt; sw < &swdevt[nswdev]; sw++)
        nswap = 0;
        for (sw = swdevt; sw < &swdevt[nswdev]; sw++)
-               nswap += sw->sw_nblks,
+               if (sw->sw_freed)
+                       nswap += sw->sw_nblks;
        free = 0;
        for (me = (struct mapent *)(swapmap+1);
            me < (struct mapent *)&swapmap[nswapmap]; me++)
        free = 0;
        for (me = (struct mapent *)(swapmap+1);
            me < (struct mapent *)&swapmap[nswapmap]; me++)
@@ -1011,17 +1114,20 @@ badrmfree:
  * We return the phys addr by simulating kernel vm (/dev/kmem)
  * when we are reading a crash dump.
  */
  * We return the phys addr by simulating kernel vm (/dev/kmem)
  * when we are reading a crash dump.
  */
+off_t
 mkphys(addr)
 mkphys(addr)
+       off_t addr;
 {
 {
-       register o;
+       register off_t o;
 
        if (!kflg)
                return(addr);
 
        if (!kflg)
                return(addr);
+       addr = clear(addr);
        o = addr & PGOFSET;
        addr >>= PGSHIFT;
        addr &= PG_PFNUM;
        addr *=  NBPW;
        o = addr & PGOFSET;
        addr >>= PGSHIFT;
        addr &= PG_PFNUM;
        addr *=  NBPW;
-       addr = getw(nl[SYSMAP].n_value + addr);
+       addr = getword(nl[SYSMAP].n_value + addr);
        addr = ((addr & PG_PFNUM) << PGSHIFT) | o;
        return(addr);
 }
        addr = ((addr & PG_PFNUM) << PGSHIFT) | o;
        return(addr);
 }