home *** CD-ROM | disk | FTP | other *** search
- /* running.c
- **
- ** Copyright (c) 1989, Christopher Laforet
- ** All Rights Reserved
- **
- ** Started: 21 October 1989
- **
- ** Revision Information:
- **
- ** $Logfile: D:/os2/running/vcs/running.c_v $
- ** $Date: 25 Oct 1989 21:28:10 $
- ** $Revision: 1.2 $
- **
- */
-
- #define LINT_ARGS
-
- #define INCL_DOS
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <os2.h>
- #include "running.h"
-
-
-
- struct id **ids = NULL;
- USHORT max_ids = 0;
- USHORT cur_ids = 0;
- struct proc **procs = NULL;
- USHORT max_procs = 0;
- USHORT cur_procs = 0;
- UCHAR bBuf[0x8000];
-
-
-
- int name_compare(struct proc **arg1,struct proc **arg2)
- {
- if (((*arg1)->pid && (*arg2)->pid) || (!(*arg1)->pid && !(*arg2)->pid))
- {
- return(stricmp((*arg1)->process,(*arg2)->process));
- }
- else if ((*arg1)->pid)
- {
- return(-1);
- }
- else
- {
- return(1);
- }
- }
-
-
-
- int pid_compare(struct proc **arg1,struct proc **arg2)
- {
- if ((*arg1)->pid && !(*arg2)->pid)
- {
- return(-1);
- }
- else if (!(*arg1)->pid && (*arg2)->pid)
- {
- return(1);
- }
- return(0);
- }
-
-
-
- void parse_status(void)
- {
- USHORT type;
- USHORT selector;
- USHORT offset;
- USHORT count;
- USHORT kount;
- USHORT temp;
- UCHAR buffer[256];
- UCHAR *cptr;
- UCHAR *ptr;
-
- ptr = bBuf;
- selector = SELECTOROF(ptr);
- while ((type = *(USHORT *)ptr) != 0xffff)
- {
- ptr += 2;
- offset = *(USHORT *)ptr;
- ptr += 2;
-
- if (!type) /* relationship */
- {
- if (cur_ids >= max_ids)
- {
- if (!(ids = realloc(ids,(max_ids += 50) * sizeof(struct id *))))
- {
- printf("Error: Out of memory 1!\n");
- DosExit(EXIT_PROCESS,1);
- }
- }
- if (!(ids[cur_ids] = calloc(1,sizeof(struct id))))
- {
- printf("Error: Out of memory 2!\n");
- DosExit(EXIT_PROCESS,1);
- }
- ids[cur_ids]->pid = *(USHORT *)ptr;
- ptr += 2;
- ids[cur_ids]->ppid = *(USHORT *)ptr;
- ptr += 2;
- ptr += 2;
- ids[cur_ids]->signiture = *(USHORT *)ptr;
- ids[cur_ids]->threads = 0;
- ++cur_ids;
- }
- else if (type == 1)
- {
- ptr += 2;
- temp = *(USHORT *)ptr;
- for (count = 0; count < cur_ids; count++)
- {
- if (ids[count]->pid == temp)
- {
- ++ids[count]->threads;
- break;
- }
- }
- }
- else if (type == 2)
- {
- if (cur_procs >= max_procs)
- {
- if (!(procs = realloc(procs,(max_procs += 50) * sizeof(struct proc *))))
- {
- printf("Error: Out of memory 3!\n");
- DosExit(EXIT_PROCESS,1);
- }
- }
- if (!(procs[cur_procs] = calloc(1,sizeof(struct proc))))
- {
- printf("Error: Out of memory 4!\n");
- DosExit(EXIT_PROCESS,1);
- }
- procs[cur_procs]->signiture = *(USHORT *)ptr;
- ptr += 2;
- procs[cur_procs]->max_dependents = *(USHORT *)ptr;
- ptr += 2;
- ptr += 2;
- ptr += 2;
-
- if (procs[cur_procs]->max_dependents)
- {
- if (!(procs[cur_procs]->dependents = calloc(procs[cur_procs]->max_dependents,sizeof(USHORT))))
- {
- printf("Error: Out of memory 5!\n");
- DosExit(EXIT_PROCESS,1);
- }
-
- for (count = 0; count < procs[cur_procs]->max_dependents; count++)
- {
- procs[cur_procs]->dependents[count] = *(USHORT *)ptr;
- ptr += 2;
- }
- }
-
- cptr = buffer;
- while (*cptr++ = *ptr)
- {
- ++ptr;
- }
-
- if (!(procs[cur_procs]->process = calloc(strlen(buffer) + 1,sizeof(UCHAR))))
- {
- printf("Error: Out of memory 6!\n");
- DosExit(EXIT_PROCESS,1);
- }
- strcpy(procs[cur_procs]->process,buffer);
-
- ++cur_procs;
- }
-
- ptr = MAKEP(selector,offset);
- }
-
- for (count = 0; count < cur_procs; count++)
- {
- for (kount = 0; kount < cur_ids; kount++)
- {
- if (ids[kount]->signiture == procs[count]->signiture)
- {
- procs[count]->pid = ids[kount]->pid;
- procs[count]->ppid = ids[kount]->ppid;
- procs[count]->threads = ids[kount]->threads;
- break;
- }
- }
- }
- for (count = 0; count < cur_procs; count++)
- {
- for (kount = 0; kount < cur_ids; kount++)
- {
- if (ids[kount]->ppid == procs[count]->pid)
- {
- ++procs[count]->children;
- }
- }
- }
- }
-
-
-
- int main(int argc,char *argv[])
- {
- PIDINFO pid;
- USHORT count;
- USHORT kount;
- USHORT fold;
- BOOL fshow_pro = 1;
- BOOL fshow_res = 1;
- BOOL fsort = 1;
- BOOL flist_res;
-
- fprintf(stderr,"RUNNING (v %u.%02u of %s) : Shows Processes Running in OS/2\n",MAJOR_VERSION,MINOR_VERSION,__DATE__);
- fprintf(stderr,"Copyright (c) 1989, Christopher Laforet. Released to the Public Domain.\n\n");
- DosGetPID(&pid);
- if (argc > 1)
- {
- for (count = 1; count < argc; count++)
- {
- if (*argv[count] == '/' || *argv[count] == '-')
- {
- switch (argv[count][1])
- {
- case 'P':
- case 'p':
- fshow_pro = 1;
- fshow_res = 0;
- break;
- case 'R':
- case 'r':
- fshow_pro = 0;
- fshow_res = 1;
- break;
- case 'N':
- case 'n':
- fsort = 0;
- break;
- case '?':
- case 'H':
- case 'h':
- fprintf(stderr,"Usage is RUNNING [-p][-r][-n]\n");
- fprintf(stderr," where: -p means show processes only\n");
- fprintf(stderr," -r means show resources only\n");
- fprintf(stderr," -n means do not sort list by name\n\n");
- DosExit(EXIT_PROCESS,0);
- break;
- default:
- fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
- break;
- }
- }
- else
- {
- fprintf(stderr,"Invalid argument \"%s\"!\n\n",argv[count]);
- }
- }
- }
- if (!DosQProcStatus(bBuf,sizeof(bBuf)))
- {
- parse_status();
-
- if (fsort)
- {
- qsort(procs,cur_procs,sizeof(struct proc *),name_compare);
- }
- else
- {
- qsort(procs,cur_procs,sizeof(struct proc *),pid_compare);
- }
-
- printf("PID PPID Mod# Process Name Thds Chld ");
- if (fshow_res)
- {
- printf("Resource Module #s");
- }
- printf("\n");
- printf("---- ---- ---- -------------------- ---- ---- ");
- if (fshow_res)
- {
- printf("-------------------------");
- }
- printf("\n");
- for (count = 0; count < cur_procs; count++)
- {
- flist_res = 0;
- if (procs[count]->pid != pid.pid)
- {
- if (fshow_pro && procs[count]->pid)
- {
- 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);
- if (fshow_res)
- {
- flist_res = 1;
- }
- else
- {
- printf("\n");
- }
- }
- else if (fshow_res && !procs[count]->pid)
- {
- printf("*Resource* %04x %-20.20s ",procs[count]->signiture,procs[count]->process);
- flist_res = 1;
- }
- if (flist_res)
- {
- for (kount = 0, fold = 0; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
- {
- if (fold && !(fold % 5))
- {
- printf("\n%52.52s","");
- }
- printf("%04x ",procs[count]->dependents[kount]);
- }
- printf("\n");
- }
- }
- }
- }
- else
- {
- printf("Error: Unable to get trace information!\n");
- return(1);
- }
- return(0);
- }
-