home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RUNNING.ZIP / RUNNING.C < prev    next >
C/C++ Source or Header  |  1990-02-12  |  9KB  |  390 lines

  1. /* running.c
  2. **
  3. ** Copyright (c) 1989, Christopher Laforet
  4. ** All Rights Reserved
  5. **
  6. ** Started: 21 October 1989
  7. **
  8. ** Revision Information:
  9. **
  10. ** $Logfile:   D:/os2/running/vcs/running.c_v  $
  11. ** $Date:   12 Feb 1990 04:04:12  $
  12. ** $Revision:   1.3  $
  13. **
  14. */
  15.  
  16. #define LINT_ARGS
  17.  
  18. #define INCL_DOS
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <os2.h>
  24. #include "running.h"
  25.  
  26.  
  27.  
  28. struct id **ids = NULL;
  29. USHORT max_ids = 0;
  30. USHORT cur_ids = 0;
  31. struct proc **procs = NULL;
  32. USHORT max_procs = 0;
  33. USHORT cur_procs = 0;
  34. UCHAR bBuf[0x8000];
  35.  
  36.  
  37.  
  38. int name_compare(struct proc **arg1,struct proc **arg2)
  39.     {
  40.     if (((*arg1)->pid && (*arg2)->pid) || (!(*arg1)->pid && !(*arg2)->pid))
  41.         {
  42.         return(stricmp((*arg1)->process,(*arg2)->process));
  43.         }
  44.     else if ((*arg1)->pid)
  45.         {
  46.         return(-1);
  47.         }
  48.     else
  49.         {
  50.         return(1);
  51.         }
  52.     }
  53.  
  54.  
  55.  
  56. int pid_compare(struct proc **arg1,struct proc **arg2)
  57.     {
  58.     if ((*arg1)->pid && !(*arg2)->pid)
  59.         {
  60.         return(-1);
  61.         }
  62.     else if (!(*arg1)->pid && (*arg2)->pid)
  63.         {
  64.         return(1);
  65.         }
  66.     return(0);
  67.     }
  68.  
  69.  
  70.  
  71. void parse_status(void)
  72.     {
  73.     USHORT type;
  74.     USHORT selector;
  75.     USHORT offset;
  76.     USHORT count;
  77.     USHORT kount;
  78.     USHORT temp;
  79.     UCHAR buffer[256];
  80.     UCHAR *cptr;
  81.     UCHAR *ptr;
  82.  
  83.     ptr = bBuf;
  84.     selector = SELECTOROF(ptr);
  85.     while ((type = *(USHORT *)ptr) != 0xffff)
  86.         {
  87.         ptr += 2;
  88.         offset = *(USHORT *)ptr;
  89.         ptr += 2;
  90.  
  91.         if (!type)        /* relationship */
  92.             {
  93.             if (cur_ids >= max_ids)
  94.                 {
  95.                 if (!(ids = realloc(ids,(max_ids += 50) * sizeof(struct id *))))
  96.                     {
  97.                     printf("Error: Out of memory 1!\n");
  98.                     DosExit(EXIT_PROCESS,1);
  99.                     }
  100.                 }
  101.             if (!(ids[cur_ids] = calloc(1,sizeof(struct id))))
  102.                 {
  103.                 printf("Error: Out of memory 2!\n");
  104.                 DosExit(EXIT_PROCESS,1);
  105.                 }
  106.             ids[cur_ids]->pid = *(USHORT *)ptr;
  107.             ptr += 2;
  108.             ids[cur_ids]->ppid = *(USHORT *)ptr;
  109.             ptr += 2;
  110.             ptr += 2;
  111.             ids[cur_ids]->signiture = *(USHORT *)ptr;
  112.             ++cur_ids;
  113.             }
  114.         else if (type == 1)
  115.             {
  116.             ptr += 2;
  117.             temp = *(USHORT *)ptr;
  118.             for (count = 0; count < cur_ids; count++)
  119.                 {
  120.                 if (ids[count]->pid == temp)
  121.                     {
  122.                     ptr += 6;        /* now for the thread state */
  123.                     switch (*(USHORT *)ptr)
  124.                         {
  125.                         case 0:
  126.                             ++ids[count]->start_threads;
  127.                             break;
  128.                         case 2:
  129.                             ++ids[count]->ready_threads;
  130.                             break;
  131.                         case 3:
  132.                             ++ids[count]->blocked_threads;
  133.                             break;
  134.                         case 4:
  135.                             ++ids[count]->frozen_threads;
  136.                             break;
  137.                         case 5:
  138.                             ++ids[count]->critsec_threads;
  139.                             break;
  140.                         case 6:
  141.                             ++ids[count]->background_threads;
  142.                             break;
  143.                         case 7:
  144.                             ++ids[count]->other_threads;
  145.                             break;
  146.                         }
  147.                     break;
  148.                     }
  149.                 }
  150.             }
  151.         else if (type == 2)
  152.             {
  153.             if (cur_procs >= max_procs)
  154.                 {
  155.                 if (!(procs = realloc(procs,(max_procs += 50) * sizeof(struct proc *))))
  156.                     {
  157.                     printf("Error: Out of memory 3!\n");
  158.                     DosExit(EXIT_PROCESS,1);
  159.                     }
  160.                 }
  161.             if (!(procs[cur_procs] = calloc(1,sizeof(struct proc))))
  162.                 {
  163.                 printf("Error: Out of memory 4!\n");
  164.                 DosExit(EXIT_PROCESS,1);
  165.                 }
  166.             procs[cur_procs]->signiture = *(USHORT *)ptr;
  167.             ptr += 2;
  168.             procs[cur_procs]->max_dependents = *(USHORT *)ptr;
  169.             ptr += 2;
  170.             ptr += 2;
  171.             ptr += 2;
  172.  
  173.             if (procs[cur_procs]->max_dependents)
  174.                 {
  175.                 if (!(procs[cur_procs]->dependents = calloc(procs[cur_procs]->max_dependents,sizeof(USHORT))))
  176.                     {
  177.                     printf("Error: Out of memory 5!\n");
  178.                     DosExit(EXIT_PROCESS,1);
  179.                     }
  180.  
  181.                 for (count = 0; count < procs[cur_procs]->max_dependents; count++)
  182.                     {
  183.                     procs[cur_procs]->dependents[count] = *(USHORT *)ptr;
  184.                     ptr += 2;
  185.                     }
  186.                 }
  187.             
  188.             cptr = buffer;
  189.             while (*cptr++ = *ptr)
  190.                 ++ptr;
  191.  
  192.             if (!(procs[cur_procs]->process = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
  193.                 {
  194.                 printf("Error: Out of memory 6!\n");
  195.                 DosExit(EXIT_PROCESS,1);
  196.                 }
  197.             strcpy(procs[cur_procs]->process,buffer);
  198.  
  199.             ++cur_procs;
  200.             }
  201.  
  202.         ptr = MAKEP(selector,offset);
  203.         }
  204.  
  205.     for (count = 0; count < cur_procs; count++)
  206.         {
  207.         for (kount = 0; kount < cur_ids; kount++)
  208.             {
  209.             if (ids[kount]->signiture == procs[count]->signiture)
  210.                 {
  211.                 procs[count]->pid = ids[kount]->pid;
  212.                 procs[count]->ppid = ids[kount]->ppid;
  213.                 procs[count]->start_threads = ids[kount]->start_threads;
  214.                 procs[count]->ready_threads = ids[kount]->ready_threads;
  215.                 procs[count]->blocked_threads = ids[kount]->blocked_threads;
  216.                 procs[count]->frozen_threads = ids[kount]->frozen_threads;
  217.                 procs[count]->critsec_threads = ids[kount]->critsec_threads;
  218.                 procs[count]->background_threads = ids[kount]->background_threads;
  219.                 procs[count]->other_threads = ids[kount]->other_threads;
  220.                 break;
  221.                 }
  222.             }
  223.         }
  224.     for (count = 0; count < cur_procs; count++)
  225.         {
  226.         for (kount = 0; kount < cur_ids; kount++)
  227.             {
  228.             if (ids[kount]->ppid == procs[count]->pid)
  229.                 {
  230.                 ++procs[count]->children;
  231.                 }
  232.             }
  233.         }
  234.     }
  235.  
  236.  
  237.  
  238. int main(int argc,char *argv[])
  239.     {
  240.     PIDINFO pid;
  241.     USHORT count;
  242.     USHORT kount;
  243.     USHORT fold;
  244.     USHORT *usptr;
  245.     BOOL fshow_pro = 1;
  246.     BOOL fshow_res = 0;
  247.     BOOL fsort = 1;
  248.     BOOL flist_res;
  249.  
  250.     fprintf(stderr,"RUNNING (v %u.%02u of %s) : Shows Processes Running in OS/2\n",MAJOR_VERSION,MINOR_VERSION,__DATE__);
  251.     fprintf(stderr,"Copyright (c) 1989, Christopher Laforet.  Released to the Public Domain.\n\n");
  252.     DosGetPID(&pid);
  253.     if (argc > 1)
  254.         {
  255.         for (count = 1; count < argc; count++)
  256.             {
  257.             if (*argv[count] == '/' || *argv[count] == '-')
  258.                 {
  259.                 switch (argv[count][1])
  260.                     {
  261.                     case 'R':
  262.                     case 'r':
  263.                         fshow_res = 1;
  264.                         break;
  265.                     case 'N':
  266.                     case 'n':
  267.                         fsort = 0;
  268.                         break;
  269.                     case '?':
  270.                     case 'H':
  271.                     case 'h':
  272.                         fprintf(stderr,"Usage is RUNNING [-r][-n]\n");
  273.                         fprintf(stderr,"      where:  -r means show resource (DLL) listing\n");
  274.                         fprintf(stderr,"              -n means do not sort list by name\n\n");
  275.                         DosExit(EXIT_PROCESS,0);
  276.                         break;
  277.                     default:
  278.                         fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
  279.                         break;
  280.                     }
  281.                 }
  282.             else
  283.                 fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
  284.             }
  285.         }
  286.     if (!DosQProcStatus(bBuf,sizeof(bBuf)))
  287.         {
  288.         parse_status();
  289.  
  290.         if (fsort)
  291.             qsort(procs,cur_procs,sizeof(struct proc *),name_compare);
  292.         else
  293.             qsort(procs,cur_procs,sizeof(struct proc *),pid_compare);
  294.  
  295.         printf("                                              ┌──────── Threads ────────┐\n");
  296.         printf("PID   PPID  Mod#  Process Name          Chld  Str Rdy Blk Fzn Crt Bkd Oth\n");
  297.         printf("----  ----  ----  --------------------  ----  --- --- --- --- --- --- ---\n");
  298.         for (count = 0; count < cur_procs; count++)
  299.             {
  300.             flist_res = 0;
  301.             if (procs[count]->pid != pid.pid)
  302.                 {
  303.                 if (fshow_pro && procs[count]->pid)
  304.                     {
  305.                     printf("%04x  %04x  %04x  %-20.20s  %3u ",procs[count]->pid,procs[count]->ppid,procs[count]->signiture,procs[count]->process,procs[count]->children);
  306.                     for (kount = 0; kount < 7; kount++)
  307.                         {
  308.                         switch (kount)
  309.                             {
  310.                             case 0:
  311.                                 usptr = &procs[count]->start_threads;
  312.                                 break;
  313.                             case 1:
  314.                                 usptr = &procs[count]->ready_threads;
  315.                                 break;
  316.                             case 2:
  317.                                 usptr = &procs[count]->blocked_threads;
  318.                                 break;
  319.                             case 3:
  320.                                 usptr = &procs[count]->frozen_threads;
  321.                                 break;
  322.                             case 4:
  323.                                 usptr = &procs[count]->critsec_threads;
  324.                                 break;
  325.                             case 5:
  326.                                 usptr = &procs[count]->background_threads;
  327.                                 break;
  328.                             case 6:
  329.                                 usptr = &procs[count]->other_threads;
  330.                                 break;
  331.                             }
  332.                         if (*usptr)
  333.                             printf(" %3d",*usptr);
  334.                         else
  335.                             printf("    ");
  336.                         }
  337.                     printf("\n");
  338.                     if (fshow_res)
  339.                         {
  340.                         printf("****  Depends on:");
  341.                         for (kount = 0, fold = 17; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
  342.                             {
  343.                             if (fold > 41)
  344.                                 {
  345.                                 printf("\n%17.17s","");
  346.                                 fold = 17;
  347.                                 }
  348.                             printf(" %04x",procs[count]->dependents[kount]);
  349.                             fold += 5;
  350.                             }
  351.                         printf("\n");
  352.                         }
  353.                     }
  354.                 }
  355.             }
  356.         if (fshow_res)
  357.             {
  358.             printf("\nResources (DLLs) In Use:\n");
  359.             printf("Handle  Resource Name         Dependencies\n");
  360.             printf("------  --------------------  -----------------------------------------------\n");
  361.             for (count = 0; count < cur_procs; count++)
  362.                 {
  363.                 if (fshow_res && !procs[count]->pid) 
  364.                     {
  365.                     printf("%04x    %-20.20s  ",procs[count]->signiture,procs[count]->process);
  366.                     for (kount = 0, fold = 30; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
  367.                         {
  368.                         if (fold > 75)
  369.                             {
  370.                             printf("\n%30.30s","");
  371.                             fold = 30;
  372.                             }
  373.                         printf(" %04x",procs[count]->dependents[kount]);
  374.                         fold += 5;
  375.                         }
  376.                     if (!procs[count]->max_dependents)
  377.                         printf(" <None>");
  378.                     printf("\n");
  379.                     }
  380.                 }
  381.             }
  382.         }
  383.     else
  384.         {
  385.         fprintf(stderr,"Error: Unable to get trace information!\n");
  386.         return(1);
  387.         }
  388.     return(0);
  389.     }
  390.