cleanup window handling, use subwindows exclusively
[unix-history] / usr / src / usr.bin / systat / pigs.c
index a496efd..1ce24cc 100644 (file)
@@ -1,8 +1,39 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)pigs.c     1.2 (Lucasfilm) %G%";
+static char sccsid[] = "@(#)pigs.c     1.7 (Berkeley) %G%";
 #endif
 
 #include "systat.h"
 #endif
 
 #include "systat.h"
+#include <sys/param.h>
+#include <sys/dir.h>
+#include <sys/time.h>
+#include <sys/proc.h>
+#include <sys/file.h>
+#include <nlist.h>
+#include <pwd.h>
+
+WINDOW *
+openpigs()
+{
+
+       return (subwin(stdscr, LINES-5-1, 0, 5, 0));
+}
+
+closepigs(w)
+       WINDOW *w;
+{
+
+       if (w == NULL)
+               return;
+       wclear(w);
+       wrefresh(w);
+       delwin(w);
+}
+
+int    maxind;
+int     factor;
+float   total;
+struct  passwd *getpwuid();
+char    pidname[30];
 
 showpigs()
 {
 
 showpigs()
 {
@@ -14,6 +45,8 @@ showpigs()
         register struct users *knptr;
         char *getpname(), *pnamp;
 
         register struct users *knptr;
         char *getpname(), *pnamp;
 
+       if (pt == NULL)
+               return;
         /* Accumulate the percent of cpu per user. */
         ptptr = pt;
         numprocs = 0;
         /* Accumulate the percent of cpu per user. */
         ptptr = pt;
         numprocs = 0;
@@ -39,8 +72,10 @@ showpigs()
                 total = 1.0;
         factor = 50.0/total;
 
                 total = 1.0;
         factor = 50.0/total;
 
-        /* Find the top ten by executing a "bubble pass" ten times. */
-        y = numprocs + 1 < 15 ? numprocs + 1 : 15;
+        /* Find the top few by executing a "bubble pass" ten times. */
+       y = numprocs + 1;
+       if (y > wnd->_maxy-1)
+               y = wnd->_maxy-1;
         for (i = 0; i < y; i++) {
                 ptptr = &pt[i];
                 max = -10000.0;
         for (i = 0; i < y; i++) {
                 ptptr = &pt[i];
                 max = -10000.0;
@@ -58,11 +93,11 @@ showpigs()
                         pt[maxind] = temppt;
                 }
         }
                         pt[maxind] = temppt;
                 }
         }
-
-        /* Display the top fifteen. */
-        y = 4;
+        y = 1;
         ptptr = pt;
         ptptr = pt;
-        i = numprocs+1 < 15 ? numprocs+1 : 15;
+       i = numprocs + 1;
+       if (i > wnd->_maxy-1)
+               i = wnd->_maxy-1;
         for (; i > 0 && ptptr->pt_pctcpu > 0.01; i--) {
                 /* Find the user's name. */
                 knptr = known;
         for (; i > 0 && ptptr->pt_pctcpu > 0.01; i--) {
                 /* Find the user's name. */
                 knptr = known;
@@ -89,64 +124,50 @@ showpigs()
                 wmove(wnd, y, 0);
                 wclrtoeol(wnd);
                 mvwaddstr(wnd, y, 0, namp);
                 wmove(wnd, y, 0);
                 wclrtoeol(wnd);
                 mvwaddstr(wnd, y, 0, namp);
-                sprintf(pidname, "%8.8s", pnamp);
-                mvwaddstr(wnd, y, 6, pidname);
-                wmove(wnd, y++, 15);
+                sprintf(pidname, "%10.10s", pnamp);
+                mvwaddstr(wnd, y, 9, pidname);
+                wmove(wnd, y++, 20);
                 for (j = ptptr->pt_pctcpu*factor + 0.5; j > 0; j--)
                         waddch(wnd, 'X');
                 ptptr++;
         }
                 for (j = ptptr->pt_pctcpu*factor + 0.5; j > 0; j--)
                         waddch(wnd, 'X');
                 ptptr++;
         }
-
-        while (y < 19) {
-                wmove(wnd, y++, 0);
-                wclrtoeol(wnd);
-        }
-}
-
-openpigs()
-{
-
-        kmemf = "/dev/kmem";
-        kmem = open(kmemf, 0);
-        if (kmem < 0) {
-                perror(kmemf);
-                exit(1);
-        }
-        memf = "/dev/mem";
-        mem = open(memf, 0);
-        if (mem < 0) {
-                perror(memf);
-                exit(1);
-        }
-        swapf = "/dev/drum";
-        swap = open(swapf, 0);
-        if (swap < 0) {
-                perror(swapf);
-                exit(1);
-        }
+       wmove(wnd, y, 0); wclrtobot(wnd);
 }
 
 }
 
-/* these don't belong here */
+static struct nlist nlst[] = {
 #define X_PROC          0
 #define X_PROC          0
+        { "_proc" },
 #define X_NPROC         1
 #define X_NPROC         1
-#define X_USRPTMAP      4
-#define X_USRPT         5
-
-struct proc *kprocp;
+        { "_nproc" },
+#define X_USRPTMAP      2
+        { "_Usrptmap" },
+#define X_USRPT         3
+        { "_usrpt" },
+        { "" }
+};
 
 initpigs()
 {
 
 
 initpigs()
 {
 
+       if (nlst[X_PROC].n_type == 0) {
+               nlist("/vmunix", nlst);
+               if (nlst[X_PROC].n_type == 0) {
+                       error("namelist on /vmunix failed");
+                       return;
+               }
+       }
         if (procp == NULL) {
                 procp = getw(nlst[X_PROC].n_value);
                 nproc = getw(nlst[X_NPROC].n_value);
         if (procp == NULL) {
                 procp = getw(nlst[X_PROC].n_value);
                 nproc = getw(nlst[X_NPROC].n_value);
-                kprocp = (struct proc *)malloc(sizeof (*kprocp) * nproc);
         }
         }
+       if (kprocp == NULL)
+                kprocp = (struct proc *)calloc(nproc, sizeof (struct proc));
         if (usrpt != NULL)
                return;
        usrpt = (struct pte *)nlst[X_USRPT].n_value;
        Usrptma = (struct pte *)nlst[X_USRPTMAP].n_value;
         if (usrpt != NULL)
                return;
        usrpt = (struct pte *)nlst[X_USRPT].n_value;
        Usrptma = (struct pte *)nlst[X_USRPTMAP].n_value;
-       pt = (struct p_times *)malloc(nproc * sizeof (struct p_times));
+       if (pt == NULL)
+               pt = (struct p_times *)calloc(nproc, sizeof (struct p_times));
 }
 
 fetchpigs()
 }
 
 fetchpigs()
@@ -156,6 +177,18 @@ fetchpigs()
         register float time;
         register struct proc *pp;
 
         register float time;
         register struct proc *pp;
 
+       if (nlst[X_PROC].n_type == 0)
+               return;
+       if (kprocp == NULL) {
+               kprocp = (struct proc *)calloc(nproc, sizeof (struct proc));
+               if (kprocp == NULL)
+                       return;
+       }
+       if (pt == NULL) {
+               pt = (struct p_times *)calloc(nproc, sizeof (struct p_times));
+               if (pt == NULL)
+                       return;
+       }
         prt = pt;
         lseek(kmem, procp, L_SET);
         read(kmem, kprocp, sizeof (struct proc) * nproc);
         prt = pt;
         lseek(kmem, procp, L_SET);
         read(kmem, kprocp, sizeof (struct proc) * nproc);
@@ -176,8 +209,7 @@ fetchpigs()
 labelpigs()
 {
 
 labelpigs()
 {
 
-        move(5, 0);
-        clrtoeol();
-        mvaddstr(5, 20,
+       wmove(wnd, 0, 0); wclrtoeol(wnd);
+        mvwaddstr(wnd, 0, 20,
                 "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
 }
                 "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
 }