/*
 * runstart.c
 *
 * crabbed and modified from Matt Dillon's fifo-handler
 *
 * Last modified: Fri Oct  8 19:32:19 1993 too
 *
 * HISTORY
 * $Log: runstart.c,v $
 * Revision 1.1  1993/10/24  12:50:39  too
 * Initial revision
 *
 * Revision 1.1  1993/10/24  12:50:39  too
 * Initial revision
 *
 *
 */

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>

#define _INL_SOCKET_H_ /* socket functions aren't needed here */
#define PROTO_SOCKET_H
#include "system_includes.h"

#include "runstart.h"

static struct DosList * dl; /* well, this is for testing only */

extern struct DosLibrary * DOSBase;

struct MsgPort * mkDevice(char * devname, int namelen)
{
  struct RootNode *	root;
  struct DosInfo *	info;
  struct MsgPort *	PktPort;
  
  if ((PktPort = CreateMsgPort()) == NULL)
    return NULL;
  
  dl = (struct DosList *)
    AllocMem(sizeof(struct DosList) + namelen + 2, MEMF_PUBLIC);
  if (dl == NULL) {
    DeleteMsgPort(PktPort);
    return NULL;
  }
  CopyMem(devname, (char *)(dl + 1) + 1, namelen);
  *(char *)(dl + 1) = namelen;
  
  dl->dol_Type = DLT_DEVICE;
  dl->dol_Task = PktPort;
  dl->dol_Name = MKBADDR((char *)(dl+1));
  
  Forbid();
  root  = (struct RootNode *)DOSBase->dl_Root;
  info  = (struct DosInfo  *)BADDR(root->rn_Info);
  dl->dol_Next = info->di_DevInfo;
  info->di_DevInfo = MKBADDR(dl);
  Permit();
  
  return PktPort;
}

void delDevice()
{
  struct DosInfo *	info;
  struct RootNode *	root;
  struct DosList *	dls;
  BPTR *		bpp;
  
  Forbid();
  root  = (struct RootNode *)DOSBase->dl_Root;
  info  = (struct DosInfo  *)BADDR(root->rn_Info);
  
  for (bpp = &info->di_DevInfo; (dls = BADDR(*bpp)); bpp = &dls->dol_Next)
    if (dls == dl) {
      *bpp = dls->dol_Next;
      DeleteMsgPort(dl->dol_Task);
      FreeMem(dl, sizeof(struct DosList) +
	      (int)*(char *)BADDR(dl->dol_Name) + 2);
    }
  Permit();
}
