home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / running / running.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-01  |  7.0 KB  |  336 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:   25 Oct 1989 21:28:10  $
  12. ** $Revision:   1.2  $
  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.             ids[cur_ids]->threads = 0;
  113.             ++cur_ids;
  114.             }
  115.         else if (type == 1)
  116.             {
  117.             ptr += 2;
  118.             temp = *(USHORT *)ptr;
  119.             for (count = 0; count < cur_ids; count++)
  120.                 {
  121.                 if (ids[count]->pid == temp)
  122.                     {
  123.                     ++ids[count]->threads;
  124.                     break;
  125.                     }
  126.                 }
  127.             }
  128.         else if (type == 2)
  129.             {
  130.             if (cur_procs >= max_procs)
  131.                 {
  132.                 if (!(procs = realloc(procs,(max_procs += 50) * sizeof(struct proc *))))
  133.                     {
  134.                     printf("Error: Out of memory 3!\n");
  135.                     DosExit(EXIT_PROCESS,1);
  136.                     }
  137.                 }
  138.             if (!(procs[cur_procs] = calloc(1,sizeof(struct proc))))
  139.                 {
  140.                 printf("Error: Out of memory 4!\n");
  141.                 DosExit(EXIT_PROCESS,1);
  142.                 }
  143.             procs[cur_procs]->signiture = *(USHORT *)ptr;
  144.             ptr += 2;
  145.             procs[cur_procs]->max_dependents = *(USHORT *)ptr;
  146.             ptr += 2;
  147.             ptr += 2;
  148.             ptr += 2;
  149.  
  150.             if (procs[cur_procs]->max_dependents)
  151.                 {
  152.                 if (!(procs[cur_procs]->dependents = calloc(procs[cur_procs]->max_dependents,sizeof(USHORT))))
  153.                     {
  154.                     printf("Error: Out of memory 5!\n");
  155.                     DosExit(EXIT_PROCESS,1);
  156.                     }
  157.  
  158.                 for (count = 0; count < procs[cur_procs]->max_dependents; count++)
  159.                     {
  160.                     procs[cur_procs]->dependents[count] = *(USHORT *)ptr;
  161.                     ptr += 2;
  162.                     }
  163.                 }
  164.             
  165.             cptr = buffer;
  166.             while (*cptr++ = *ptr)
  167.                 {
  168.                 ++ptr;
  169.                 }
  170.  
  171.             if (!(procs[cur_procs]->process = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
  172.                 {
  173.                 printf("Error: Out of memory 6!\n");
  174.                 DosExit(EXIT_PROCESS,1);
  175.                 }
  176.             strcpy(procs[cur_procs]->process,buffer);
  177.  
  178.             ++cur_procs;
  179.             }
  180.  
  181.         ptr = MAKEP(selector,offset);
  182.         }
  183.  
  184.     for (count = 0; count < cur_procs; count++)
  185.         {
  186.         for (kount = 0; kount < cur_ids; kount++)
  187.             {
  188.             if (ids[kount]->signiture == procs[count]->signiture)
  189.                 {
  190.                 procs[count]->pid = ids[kount]->pid;
  191.                 procs[count]->ppid = ids[kount]->ppid;
  192.                 procs[count]->threads = ids[kount]->threads;
  193.                 break;
  194.                 }
  195.             }
  196.         }
  197.     for (count = 0; count < cur_procs; count++)
  198.         {
  199.         for (kount = 0; kount < cur_ids; kount++)
  200.             {
  201.             if (ids[kount]->ppid == procs[count]->pid)
  202.                 {
  203.                 ++procs[count]->children;
  204.                 }
  205.             }
  206.         }
  207.     }
  208.  
  209.  
  210.  
  211. int main(int argc,char *argv[])
  212.     {
  213.     PIDINFO pid;
  214.     USHORT count;
  215.     USHORT kount;
  216.     USHORT fold;
  217.     BOOL fshow_pro = 1;
  218.     BOOL fshow_res = 1;
  219.     BOOL fsort = 1;
  220.     BOOL flist_res;
  221.  
  222.     fprintf(stderr,"RUNNING (v %u.%02u of %s) : Shows Processes Running in OS/2\n",MAJOR_VERSION,MINOR_VERSION,__DATE__);
  223.     fprintf(stderr,"Copyright (c) 1989, Christopher Laforet.  Released to the Public Domain.\n\n");
  224.     DosGetPID(&pid);
  225.     if (argc > 1)
  226.         {
  227.         for (count = 1; count < argc; count++)
  228.             {
  229.             if (*argv[count] == '/' || *argv[count] == '-')
  230.                 {
  231.                 switch (argv[count][1])
  232.                     {
  233.                     case 'P':
  234.                     case 'p':
  235.                         fshow_pro = 1;
  236.                         fshow_res = 0;
  237.                         break;
  238.                     case 'R':
  239.                     case 'r':
  240.                         fshow_pro = 0;
  241.                         fshow_res = 1;
  242.                         break;
  243.                     case 'N':
  244.                     case 'n':
  245.                         fsort = 0;
  246.                         break;
  247.                     case '?':
  248.                     case 'H':
  249.                     case 'h':
  250.                         fprintf(stderr,"Usage is RUNNING [-p][-r][-n]\n");
  251.                         fprintf(stderr,"      where:  -p means show processes only\n");
  252.                         fprintf(stderr,"              -r means show resources only\n");
  253.                         fprintf(stderr,"              -n means do not sort list by name\n\n");
  254.                         DosExit(EXIT_PROCESS,0);
  255.                         break;
  256.                     default:
  257.                         fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
  258.                         break;
  259.                     }
  260.                 }
  261.             else
  262.                 {
  263.                 fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
  264.                 }
  265.             }
  266.         }
  267.     if (!DosQProcStatus(bBuf,sizeof(bBuf)))
  268.         {
  269.         parse_status();
  270.  
  271.         if (fsort)
  272.             {
  273.             qsort(procs,cur_procs,sizeof(struct proc *),name_compare);
  274.             }
  275.         else
  276.             {
  277.             qsort(procs,cur_procs,sizeof(struct proc *),pid_compare);
  278.             }
  279.  
  280.         printf("PID   PPID  Mod#  Process Name          Thds  Chld  ");
  281.         if (fshow_res)
  282.             {
  283.             printf("Resource Module #s");
  284.             }
  285.         printf("\n");
  286.         printf("----  ----  ----  --------------------  ----  ----  ");
  287.         if (fshow_res)
  288.             {
  289.             printf("-------------------------");
  290.             }
  291.         printf("\n");
  292.         for (count = 0; count < cur_procs; count++)
  293.             {
  294.             flist_res = 0;
  295.             if (procs[count]->pid != pid.pid)
  296.                 {
  297.                 if (fshow_pro && procs[count]->pid)
  298.                     {
  299.                     printf("%04x  %04x  %04x  %-20.20s  %3u   %3u   ",procs[count]->pid,procs[count]->ppid,procs[count]->signiture,procs[count]->process,procs[count]->threads,procs[count]->children);
  300.                     if (fshow_res)
  301.                         {
  302.                         flist_res = 1;
  303.                         }
  304.                     else
  305.                         {
  306.                         printf("\n");
  307.                         }
  308.                     }
  309.                 else if (fshow_res && !procs[count]->pid) 
  310.                     {
  311.                     printf("*Resource*  %04x  %-20.20s              ",procs[count]->signiture,procs[count]->process);
  312.                     flist_res = 1;
  313.                     }
  314.                 if (flist_res)
  315.                     {
  316.                     for (kount = 0, fold = 0; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
  317.                         {
  318.                         if (fold && !(fold % 5))
  319.                             {
  320.                             printf("\n%52.52s","");
  321.                             }
  322.                         printf("%04x ",procs[count]->dependents[kount]);
  323.                         }
  324.                     printf("\n");
  325.                     }
  326.                 }
  327.             }
  328.         }
  329.     else
  330.         {
  331.         printf("Error: Unable to get trace information!\n");
  332.         return(1);
  333.         }
  334.     return(0);
  335.     }
  336.