home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s2 / ps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  4.5 KB  |  293 lines

  1. #
  2.  
  3. /*
  4.  *    ps - process status
  5.  *    examine and print certain things about processes
  6.  */
  7.  
  8. #include "/usr/sys/param.h"
  9. #include "/usr/sys/proc.h"
  10. #include "/usr/sys/tty.h"
  11. #include "/usr/sys/user.h"
  12.  
  13. struct {
  14.     char name[8];
  15.     int  type;
  16.     char  *value;
  17. } nl[3];
  18.  
  19. struct proc proc[NPROC];
  20. struct tty tty;
  21. struct user u;
  22. int    lflg;
  23. int    kflg;
  24. int    xflg;
  25. int    tflg;
  26. int    aflg;
  27. int    mem;
  28. int    swap;
  29.  
  30. int    stbuf[257];
  31. int    ndev;
  32. char    devc[65];
  33. int    devl[65];
  34. int    devt[65];
  35. char    *coref;
  36. struct ibuf {
  37.     char    idevmin, idevmaj;
  38.     int    inum;
  39.     int    iflags;
  40.     char    inl;
  41.     char    iuid;
  42.     char    igid;
  43.     char    isize0;
  44.     int    isize;
  45.     int    iaddr[8];
  46.     char    *ictime[2];
  47.     char    *imtime[2];
  48.     int    fill;
  49. };
  50. int    obuf[259];
  51.  
  52.  
  53. main(argc, argv)
  54. char **argv;
  55. {
  56.     struct proc *p;
  57.     int n, b;
  58.     int i, c, mtty;
  59.     char *ap;
  60.     int uid, puid;
  61.  
  62.     obuf[0] = 1;
  63.     if (argc>1) {
  64.         ap = argv[1];
  65.         while (*ap) switch (*ap++) {
  66.         case 'a':
  67.             aflg++;
  68.             break;
  69.  
  70.         case 't':
  71.             tflg++;
  72.             break;
  73.  
  74.         case 'x':
  75.             xflg++;
  76.             break;
  77.  
  78.         case 'l':
  79.             lflg++;
  80.             break;
  81.  
  82.         case 'k':
  83.             kflg++;
  84.             break;
  85.  
  86.         }
  87.     }
  88.  
  89.     if(chdir("/dev") < 0) {
  90.         printf("cannot change to /dev\n");
  91.         done();
  92.     }
  93.     setup(&nl[0], "_proc");
  94.     setup(&nl[1], "_swapdev");
  95.     nlist(argc>2? argv[2]:"/unix", nl);
  96.     if (nl[0].type==0) {
  97.         printf("No namelist\n");
  98.         return;
  99.     }
  100.     coref = "/dev/mem";
  101.     if(kflg)
  102.         coref = "/usr/sys/core";
  103.     if ((mem = open(coref, 0)) < 0) {
  104.         printf("No mem\n");
  105.         done();
  106.     }
  107.     seek(mem, nl[1].value, 0);
  108.     read(mem, &nl[1].value, 2);
  109.     seek(mem, nl[0].value, 0);
  110.     read(mem, proc, sizeof proc);
  111.     getdev();
  112.     mtty = ttyn(0);
  113.     uid = getuid() & 0377;
  114.     if(lflg)
  115.     printf("TTY F S UID   PID PRI ADDR  SZ  WCHAN COMMAND\n"); else
  116.         printf("TTY  PID COMMAND\n");
  117.     for (i=0; i<NPROC; i++) {
  118.         if (proc[i].p_stat==0)
  119.             continue;
  120.         if (proc[i].p_ttyp==0) {
  121.             if (xflg==0)
  122.                 continue;
  123.             c = '?';
  124.         } else {
  125.             for(c=0; c<ndev; c++)
  126.             if(devt[c] == proc[i].p_ttyp) {
  127.                 c = devc[c];
  128.                 goto out;
  129.             }
  130.             seek(mem, proc[i].p_ttyp, 0);
  131.             read(mem, &tty, sizeof tty);
  132.             for(c=0; c<ndev; c++)
  133.             if(devl[c] == tty.t_dev) {
  134.                 devt[c] = proc[i].p_ttyp;
  135.                 c = devc[c];
  136.                 goto out;
  137.             }
  138.             c = '?';
  139.         out:;
  140.         }
  141.         puid = proc[i].p_uid & 0377;
  142.         if (uid != puid && aflg==0)
  143.             continue;
  144.         if (lflg || c!=mtty)
  145.             printf("%c:", c);
  146.         else
  147.             printf("  ");
  148.         if (lflg) {
  149.             printf("%3o %c%4d", proc[i].p_flag,
  150.                 "0SWRIZT"[proc[i].p_stat], puid);
  151.         }
  152.         printf("%6l", proc[i].p_pid);
  153.         if (lflg) {
  154.             printf("%4d%5o%4d", proc[i].p_pri, proc[i].p_addr,
  155.                 (proc[i].p_size+7)>>3);
  156.             if (proc[i].p_wchan)
  157.                 printf("%7o", proc[i].p_wchan); else
  158.                 printf("       ");
  159.         }
  160.         if (proc[i].p_stat==5)
  161.             printf(" <defunct>");
  162.         else
  163.             prcom(i);
  164.         printf("\n");
  165.     }
  166.     done();
  167. }
  168.  
  169. getdev()
  170. {
  171.     register struct { int dir_ino; char dir_n[14]; } *p;
  172.     register i, c;
  173.     int f;
  174.     char dbuf[512];
  175.     int sbuf[20];
  176.  
  177.     f = open("/dev");
  178.     if(f < 0) {
  179.         printf("cannot open /dev\n");
  180.         done();
  181.     }
  182.     swap = -1;
  183.     c = 0;
  184.  
  185. loop:
  186.     i = read(f, dbuf, 512);
  187.     if(i <= 0) {
  188.         close(f);
  189.         if(swap < 0) {
  190.             printf("no swap device\n");
  191.             done();
  192.         }
  193.         ndev = c;
  194.         return;
  195.     }
  196.     while(i < 512)
  197.         dbuf[i++] = 0;
  198.     for(p = dbuf; p < dbuf+512; p++) {
  199.         if(p->dir_ino == 0)
  200.             continue;
  201.         if(p->dir_n[0] == 't' &&
  202.            p->dir_n[1] == 't' &&
  203.            p->dir_n[2] == 'y' &&
  204.            p->dir_n[4] == 0 &&
  205.            p->dir_n[3] != 0) {
  206.             if(stat(p->dir_n, sbuf) < 0)
  207.                 continue;
  208.             devc[c] = p->dir_n[3];
  209.             devl[c] = sbuf->iaddr[0];
  210.             c++;
  211.             continue;
  212.         }
  213.         if(swap >= 0)
  214.             continue;
  215.         if(stat(p->dir_n, sbuf) < 0)
  216.             continue;
  217.         if((sbuf->iflags & 060000) != 060000)
  218.             continue;
  219.         if(sbuf->iaddr[0] == nl[1].value)
  220.             swap = open(p->dir_n, 0);
  221.     }
  222.     goto loop;
  223. }
  224.  
  225. setup(p, s)
  226. char *p, *s;
  227. {
  228.     while (*p++ = *s++);
  229. }
  230.  
  231. prcom(i)
  232. {
  233.     int baddr, laddr, mf;
  234.     register int *ip;
  235.     register char *cp, *cp1;
  236.     int c, nbad;
  237.  
  238.     baddr = 0;
  239.     laddr = 0;
  240.     if (proc[i].p_flag&SLOAD) {
  241.         laddr = proc[i].p_addr;
  242.         mf = mem;
  243.     } else {
  244.         baddr = proc[i].p_addr;
  245.         mf = swap;
  246.     }
  247.     laddr =+ proc[i].p_size - 8;
  248.     baddr =+ laddr>>3;
  249.     laddr = (laddr&07)<<6;
  250.     seek(mf, baddr, 3);
  251.     seek(mf, laddr, 1);
  252.     if (read(mf, stbuf, 512) != 512)
  253.         return(0);
  254.     for (ip = &stbuf[256]; ip > &stbuf[0];) {
  255.         if (*--ip == -1) {
  256.             cp = ip+1;
  257.             if (*cp==0)
  258.                 cp++;
  259.             nbad = 0;
  260.             for (cp1 = cp; cp1 < &stbuf[256]; cp1++) {
  261.                 c = *cp1;
  262.                 if (c==0)
  263.                     *cp1 = ' ';
  264.                 else if (c < ' ' || c > 0176) {
  265.                     if (++nbad >= 5) {
  266.                         *cp1++ = ' ';
  267.                         break;
  268.                     }
  269.                     *cp1 = '?';
  270.                 }
  271.             }
  272.             while (*--cp1==' ')
  273.                 *cp1 = 0;
  274.             printf(lflg?" %.16s":" %.64s", cp);
  275.             return(1);
  276.         }
  277.     }
  278.     return(0);
  279. }
  280.  
  281. done()
  282. {
  283.  
  284.     fflush(obuf);
  285.     exit();
  286. }
  287.  
  288. putchar(c)
  289. {
  290.  
  291.     putc(c, obuf);
  292. }
  293.