/*************************************************************************

	ParTask.c: Parent task finder

Question:
	How can a program know from what CLI it has been launched?

It is easy if the program is run by typing the filename, i.e.

	CLI> program

Using FindTask(NULL) will solve the problem. However, it the program is
run by 'RUN' command, i.e.

	CLI> RUN program

FindTask() won't help because it is now on a different task.
How you do it then?


Answer:
	The theory behind this is that every program that run with or without
'Run' command will shared the SAME console task with the CLI it originated.

Pratical purpose:
	Be creative, and you might have a purpose of this example. I did have
a purpose so I tried to do this program the first place. You will see...

Author:
	Andry Rachmat
	10715 Roosevelt NE #7
	Seattle, WA 98125

Dated:
	May 20, 1987

Status:
	Not Copyrighted yet.

****************************************************************************/


#include <exec/types.h>
#include <exec/tasks.h>
#include <libraries/dosextens.h>

#define ROOTNODE	((struct RootNode *)DOSBase->dl_Root)
#define PROC(task)	((struct Process *)task)

extern struct DosLibrary *DOSBase;
extern struct Process *FindTask();

main()
{
register ULONG *tt;
register int i;
register UBYTE *myport;
register struct Task *mytask;
struct Process *myprocess;
long myconsole;
int found=FALSE;

tt=(ULONG *)(BADDR(ROOTNODE->rn_TaskArray));

myprocess=FindTask(NULL);
myconsole=(long)myprocess->pr_ConsoleTask;

/* The following fragment is mostly based on 'ps' by Dewi Williams,
	Thanks Dewi! */

Forbid();

for(i=1;i<=(int)tt[0];i++){
	if(tt[i]==0L) continue;		/* non existing task */
	myport=(UBYTE *)tt[i];
	mytask=(struct Task *)(myport-(long)sizeof(struct Task));
	if(PROC(mytask)->pr_TaskNum==0L || PROC(mytask)->pr_CLI==NULL) continue;

	if(myconsole==(long)PROC(mytask)->pr_ConsoleTask){

		/* we are not interested in the background Task */

		if(strcmp(mytask->tc_Node.ln_Name,"Initial CLI")==0 ||
			strcmp(mytask->tc_Node.ln_Name,"New CLI")==0){
			   found=TRUE;
			   printf("My mommy is task %ld: %s\n", 
				PROC(mytask)->pr_TaskNum, mytask->tc_Node.ln_Name);
			   break;
			} /* end if strcmp */
		} /* end if myconsole */
	}
Permit();

/* Something strange happening, what you did so it failed to find the parent
	please report to me, I will appreciate it. Thanks */

if(!found) printf("Uh, where is my MOM ??\n");

}
