home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
RUNNING.ZIP
/
RUNNING.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-02-12
|
9KB
|
390 lines
/* 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: 12 Feb 1990 04:04:12 $
** $Revision: 1.3 $
**
*/
#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;
++cur_ids;
}
else if (type == 1)
{
ptr += 2;
temp = *(USHORT *)ptr;
for (count = 0; count < cur_ids; count++)
{
if (ids[count]->pid == temp)
{
ptr += 6; /* now for the thread state */
switch (*(USHORT *)ptr)
{
case 0:
++ids[count]->start_threads;
break;
case 2:
++ids[count]->ready_threads;
break;
case 3:
++ids[count]->blocked_threads;
break;
case 4:
++ids[count]->frozen_threads;
break;
case 5:
++ids[count]->critsec_threads;
break;
case 6:
++ids[count]->background_threads;
break;
case 7:
++ids[count]->other_threads;
break;
}
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]->start_threads = ids[kount]->start_threads;
procs[count]->ready_threads = ids[kount]->ready_threads;
procs[count]->blocked_threads = ids[kount]->blocked_threads;
procs[count]->frozen_threads = ids[kount]->frozen_threads;
procs[count]->critsec_threads = ids[kount]->critsec_threads;
procs[count]->background_threads = ids[kount]->background_threads;
procs[count]->other_threads = ids[kount]->other_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;
USHORT *usptr;
BOOL fshow_pro = 1;
BOOL fshow_res = 0;
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 'R':
case 'r':
fshow_res = 1;
break;
case 'N':
case 'n':
fsort = 0;
break;
case '?':
case 'H':
case 'h':
fprintf(stderr,"Usage is RUNNING [-r][-n]\n");
fprintf(stderr," where: -r means show resource (DLL) listing\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(" ┌──────── Threads ────────┐\n");
printf("PID PPID Mod# Process Name Chld Str Rdy Blk Fzn Crt Bkd Oth\n");
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 ",procs[count]->pid,procs[count]->ppid,procs[count]->signiture,procs[count]->process,procs[count]->children);
for (kount = 0; kount < 7; kount++)
{
switch (kount)
{
case 0:
usptr = &procs[count]->start_threads;
break;
case 1:
usptr = &procs[count]->ready_threads;
break;
case 2:
usptr = &procs[count]->blocked_threads;
break;
case 3:
usptr = &procs[count]->frozen_threads;
break;
case 4:
usptr = &procs[count]->critsec_threads;
break;
case 5:
usptr = &procs[count]->background_threads;
break;
case 6:
usptr = &procs[count]->other_threads;
break;
}
if (*usptr)
printf(" %3d",*usptr);
else
printf(" ");
}
printf("\n");
if (fshow_res)
{
printf("**** Depends on:");
for (kount = 0, fold = 17; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
{
if (fold > 41)
{
printf("\n%17.17s","");
fold = 17;
}
printf(" %04x",procs[count]->dependents[kount]);
fold += 5;
}
printf("\n");
}
}
}
}
if (fshow_res)
{
printf("\nResources (DLLs) In Use:\n");
printf("Handle Resource Name Dependencies\n");
printf("------ -------------------- -----------------------------------------------\n");
for (count = 0; count < cur_procs; count++)
{
if (fshow_res && !procs[count]->pid)
{
printf("%04x %-20.20s ",procs[count]->signiture,procs[count]->process);
for (kount = 0, fold = 30; fshow_res && kount < procs[count]->max_dependents; kount++, fold++)
{
if (fold > 75)
{
printf("\n%30.30s","");
fold = 30;
}
printf(" %04x",procs[count]->dependents[kount]);
fold += 5;
}
if (!procs[count]->max_dependents)
printf(" <None>");
printf("\n");
}
}
}
}
else
{
fprintf(stderr,"Error: Unable to get trace information!\n");
return(1);
}
return(0);
}