/*
 *  Open a FileHandle to a process.
 *
 */

#include <stdio.h>
#include <fcntl.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include "bcpl.h"

extern void *AllocMem();
extern struct FileHandle *Open();
extern FILE *fdopen();
extern void *CreatePort();
extern void *GetMsg();

main()
{
  struct FileHandle *pin,*pout,*ret,*temp;
  int i;
  char buf[100],buf2[100];

  if((pin=AllocMem(sizeof(struct FileHandle),MEMF_PUBLIC|MEMF_CLEAR))==NULL) {
    return(NULL);
  }
  if((pout=Open("PIP:4096",2000L))==NULL) {
    FreeMem(pin,sizeof(struct FileHandle));
    return(NULL);
  }
  temp=(struct FileHandle*)BTOC(pout);
  (*pin)=(*temp);
  pin=(struct FileHandle*)CTOB(pin);

  printf("Created two handles: %08x and %08x\n",pin,pout);
  printf("Please type a line: ");
  fgets(buf,100,stdin);
  printf("Got %s\n",buf);
  i=Write(pin,buf,strlen(buf));
  printf("Wrote string (%d)\n",i);
  i=Read(pout,buf2,100);
  buf2[i]='\0';
  printf("Read (%d) %s\n",i,buf2);

/*
  printf("Please type a last line: ");
  fgets(buf,100,stdin);
  printf("Got %s\n",buf);
  i=Write(pin,buf,strlen(buf));
  printf("Wrote string (%d)\n",i);
  printf("Closed pin %d\n",Close(pin));

  printf("Reading pout:");
  i=Read(pout,buf2,100);
  buf2[i]='\0';
  printf("Read (%d) %s\n",i,buf2);
 */
  Close(pin);
  i=Read(pout,buf2,100);
  buf2[i]='\0';
  printf("Read (%d) %s\n",i,buf2);
  Close(pout);

}

