home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PTREE10.ZIP / P_PROC.C < prev    next >
C/C++ Source or Header  |  1990-03-22  |  4KB  |  181 lines

  1. /* p_proc.c
  2. **
  3. ** Non-Copyright (c) 1990, Christopher Laforet.
  4. ** Released to the public domain.
  5. **
  6. ** Revision History:
  7. **
  8. ** $Header$
  9. **
  10. */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "proctree.h"
  17.  
  18.  
  19.  
  20. extern USHORT APIENTRY DosQProcStatus(PVOID pBuf,USHORT cbBuf);
  21. UCHAR bBuf[0x8000];
  22.  
  23.  
  24.  
  25.  
  26. int ppid_compare(struct node **arg1,struct node **arg2)
  27.     {
  28.     return stricmp((*arg1)->name,(*arg2)->name);
  29.     }
  30.  
  31.  
  32.  
  33. void get_procedures(void)
  34.     {
  35.     USHORT type;
  36.     USHORT selector;
  37.     USHORT offset;
  38.     USHORT count;
  39.     USHORT kount;
  40.     USHORT temp;
  41.     UCHAR buffer[256];
  42.     UCHAR *cptr;
  43.     UCHAR *ptr;
  44.  
  45.     if (DosQProcStatus(bBuf,sizeof(bBuf)))
  46.         {
  47.         printf("Error: Unable to get process information\n");
  48.         exit(1);
  49.         }
  50.     ptr = bBuf;
  51.     selector = SELECTOROF(ptr);
  52.     while ((type = *(USHORT *)ptr) != 0xffff)
  53.         {
  54.         ptr += 2;
  55.         offset = *(USHORT *)ptr;
  56.         ptr += 2;
  57.  
  58.         if (!type)        /* relationship */
  59.             {
  60.             if (cur_nodes >= max_nodes)
  61.                 {
  62.                 if (!(nodes = realloc(nodes,(max_nodes += 50) * sizeof(struct node *))))
  63.                     {
  64.                     printf("Error: Out of memory!\n");
  65.                     exit(1);
  66.                     }
  67.                 }
  68.             if (!(nodes[cur_nodes] = calloc(1,sizeof(struct node))))
  69.                 {
  70.                 printf("Error: Out of memory!\n");
  71.                 exit(1);
  72.                 }
  73.             nodes[cur_nodes]->pid = *(USHORT *)ptr;
  74.             ptr += 2;
  75.             nodes[cur_nodes]->ppid = *(USHORT *)ptr;
  76.             ptr += 2;
  77.             ptr += 2;
  78.             nodes[cur_nodes]->signiture = *(USHORT *)ptr;
  79.             ++cur_nodes;
  80.             }
  81.         else if (type == 1)        /* thread information */
  82.             {
  83.             ptr += 2;
  84.             temp = *(USHORT *)ptr;
  85.             for (count = 0; count < cur_nodes; count++)
  86.                 {
  87.                 if (nodes[count]->pid == temp)
  88.                     {
  89.                     ++nodes[count]->threads;
  90.                     break;
  91.                     }
  92.                 }
  93.             }
  94.         else if (type == 2)        /* process name */
  95.             {
  96.             temp = *(USHORT *)ptr;
  97.             for (count = 0; count < cur_nodes; count++)
  98.                 {
  99.                 if (nodes[count]->signiture == temp)
  100.                     {
  101.                     ptr += 2;
  102.                     temp = *(USHORT *)ptr;
  103.                     ptr += 2;
  104.                     ptr += 2;
  105.                     ptr += 2;
  106.  
  107.                     for (kount = 0; kount < temp; kount++)
  108.                         ptr += 2;
  109.             
  110.                     cptr = buffer;
  111.                     while (*cptr++ = *ptr)
  112.                         ++ptr;
  113.  
  114.                     if (!(nodes[count]->name = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
  115.                         {
  116.                         printf("Error: Out of memory!\n");
  117.                         exit(1);
  118.                         }
  119.                     strcpy(nodes[count]->name,buffer);
  120.                     break;
  121.                     }
  122.                 }
  123.             }
  124.  
  125.         ptr = MAKEP(selector,offset);
  126.         }
  127.  
  128.     for (count = 0; count < cur_nodes; count++)        /* look for any nodes without names */
  129.         {
  130.         if (!nodes[count]->name)
  131.             {
  132.             sprintf(buffer,"Unknown",nodes[count]->pid);
  133.             if (!(nodes[count]->name = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
  134.                 {
  135.                 printf("Error: Out of memory!\n");
  136.                 exit(1);
  137.                 }
  138.             strcpy(nodes[count]->name,buffer);
  139.             }
  140.         }
  141.  
  142.     for (count = 0; count < cur_nodes; count++)        /* look for any nodes without living parents */
  143.         {
  144.         if (nodes[count]->ppid)        /* process 0 is special */
  145.             {
  146.             for (kount = 0; kount < cur_nodes; kount++)
  147.                 {
  148.                 if (nodes[count]->ppid == nodes[kount]->pid)
  149.                     break;
  150.                 }
  151.             if (kount >= cur_nodes)        /* parent died, invent a "fake" parental node */
  152.                 {
  153.                 if (cur_nodes >= max_nodes)
  154.                     {
  155.                     if (!(nodes = realloc(nodes,(max_nodes += 50) * sizeof(struct node *))))
  156.                         {
  157.                         printf("Error: Out of memory!\n");
  158.                         exit(1);
  159.                         }
  160.                     }
  161.                 if (!(nodes[cur_nodes] = calloc(1,sizeof(struct node))))
  162.                     {
  163.                     printf("Error: Out of memory!\n");
  164.                     exit(1);
  165.                     }
  166.                 nodes[cur_nodes]->pid = nodes[count]->ppid;
  167.                 nodes[cur_nodes]->ppid = 0;        /* since we do not KNOW the parent - the parent is considered the startup */
  168.                 sprintf(buffer,"Pid %04x (Killed)",nodes[cur_nodes]->pid);
  169.                 if (!(nodes[cur_nodes]->name = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
  170.                     {
  171.                     printf("Error: Out of memory!\n");
  172.                     exit(1);
  173.                     }
  174.                 strcpy(nodes[cur_nodes]->name,buffer);
  175.                 ++cur_nodes;
  176.                 }
  177.             }
  178.         }
  179.     qsort(nodes,cur_nodes,sizeof(struct node *),ppid_compare);
  180.     }
  181.