
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <intuition/intuition.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>


#define AllocPacket() (struct StandardPacket *)\
                       AllocMem(sizeof(struct StandardPacket),\
	               MEMF_CLEAR|MEMF_PUBLIC)


LONG MyDoPkt(struct MsgPort *port,
	     LONG type,
             LONG arg1,
             LONG arg2,
             LONG arg3,
             LONG arg4,
             LONG arg5)
{
 struct StandardPacket *packet;
 struct MsgPort *reply;
 LONG res;

 if(!port || !(reply = CreatePort(NULL,0)))
	return NULL;

 if(!(packet = AllocPacket())) {
	DeletePort(reply);
	return NULL;
 }

 packet->sp_Pkt.dp_Arg1 = arg1;
 packet->sp_Pkt.dp_Arg2 = arg2;
 packet->sp_Pkt.dp_Arg3 = arg3;
 packet->sp_Pkt.dp_Arg4 = arg4;
 packet->sp_Pkt.dp_Arg5 = arg5;
 packet->sp_Pkt.dp_Type = type;
 packet->sp_Pkt.dp_Link = &packet->sp_Msg;
 packet->sp_Pkt.dp_Port = reply;
 packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);

 PutMsg(port,(struct Message *)packet);
 WaitPort(reply);
 GetMsg(reply);

 res = packet->sp_Pkt.dp_Res1;
 FreeMem(packet,sizeof(struct StandardPacket));
 DeletePort(reply);
 return res;
}


struct MsgPort *MyGetConsoleTask()
{
 struct Process *myproc = (struct Process *)
                          FindTask(NULL);

 if(myproc->pr_Task.tc_Node.ln_Type == NT_PROCESS)
	return myproc->pr_ConsoleTask;
 else
	return NULL;
}


#define AllocID() (struct InfoData *)\
                  AllocMem(sizeof(struct InfoData),\
                  MEMF_CLEAR|MEMF_PUBLIC)

struct Window *GetWin(BPTR fh)
{
 struct FileHandle *handle = BADDR(fh);
 struct InfoData *info;
 LONG result;
 struct Window *win;

 if(handle->fh_Type < 0)
	return NULL;
 if(!(info = AllocID()))
	return NULL;

 result = MyDoPkt(handle->fh_Type,
                  ACTION_DISK_INFO,
                  MKBADDR(info),
                  0,0,0,0);
 win = (struct Window *)info->id_VolumeNode;
 FreeMem(info,sizeof(struct InfoData));

 return result ? win : NULL;
}


