#include <dos.h>
#include <i86.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#include <stdio.h>
#include <io.h>
#include "3dproj.h"
#include "3ddefs.h"
#include "3dprep.h"
#include "3dvarse.h"
#include <fcntl.h>


#include <sys\stat.h>
#include <sys\types.h>
//#include "jjjtargt.h"


void apiline(int c,int x1,int y1, int x2, int y2)
{
  int dx,dy,x,y,p,const1,const2,grad;
  if(x1<0) x1=0;
  if(y1<0) y1=0;
  if(x2<0) x2=0;
  if(y2<0) y2=0;
  if(x1>319) x1=319;
  if(y1>199) y1=199;
  if(x2>319) x2=319;
  if(y2>199) y2=199;
  x1=x1*modex;
  y1=y1*modey;
  x2=x2*modex;
  y2=y2*modey;
  if (y2==y1)
  {
    if(modey==2)
    {
      if(x2>x1) for (x=x1; x<=x2; x++) dddSetPixel(c,x,y1+1);
      else for (x=x2; x<=x1; x++) dddSetPixel(c,x,y1+1);
    }
    if(x2>x1) for (x=x1; x<=x2; x++) dddSetPixel(c,x,y1);
    else for (x=x2; x<=x1; x++) dddSetPixel(c,x,y1);
    return;
  }
  else if (x2==x1)
  {
    if(y2>y1) for (y=y1; y<=y2; y++) dddSetPixel(c,x1,y);
    else for (y=y2; y<=y1; y++) dddSetPixel(c,x1,y);
    return;
  }
  dx=abs(x1-x2);
  dy=abs(y1-y2);
  if (dy<dx)
    {
    if (x1>x2)
    {
      x=x1; x1=x2; x2=x;
      y=y1; y1=y2; y2=y;
    }
    p=2*dy-dx;
    const1=2*dy;
    const2=2*(dy-dx);
    if (y2>y1) grad=1; else grad=-1;
    x=x1; y=y1;
    dddSetPixel(c,x,y);
    while (x<x2)
    {
      x=x+1;
      if (p<0) p=p+const1;
        else
      {
        y=y+grad;
        p=p+const2;
      }
      dddSetPixel(c,x,y);
    }
  }
  else
  {
    if (y1>y2)
    {
     x=x1; x1=x2; x2=x;
     y=y1; y1=y2; y2=y;
    }
    p=2*dx-dy;
    const1=2*dx;
    const2=2*(dx-dy);
    if (x1<x2) grad=1;
    else grad=-1;
    x=x1; y=y1;
    dddSetPixel(c,x,y);
    while (y<y2)
    {
      y=y+1;
      if (p<0) p=p+const1;
      else
      {
        x=x+grad;
        p=p+const2;
      }
      dddSetPixel(c,x,y);
    }
  }
}

void apirect(int c,int x1,int y1, int x2, int y2)
{
  int x,y;
  if(x1<0) x1=0;
  if(y1<0) y1=0;
  if(x2<0) x2=0;
  if(y2<0) y2=0;
  if(x1>319) x1=319;
  if(y1>199) y1=199;
  if(x2>319) x2=319;
  if(y2>199) y2=199;
  x1=x1*modex;
  y1=y1*modey;
  x2=x2*modex;
  y2=y2*modey;
  if(x2>x1) for (x=x1; x<=x2; x++)
  {
    dddSetPixel(c,x,y1);
    dddSetPixel(c,x,y2);
  }
  else for (x=x2; x<=x1; x++)
  {
    dddSetPixel(c,x,y1);
    dddSetPixel(c,x,y2);
  }
  if(y2>y1) for (y=y1; y<=y2; y++)
  {
    dddSetPixel(c,x1,y);
    dddSetPixel(c,x2,y);
  }
  else for (y=y2; y<=y1; y++)
  {
    dddSetPixel(c,x1,y);
    dddSetPixel(c,x2,y);
  }
}

void apibox(int c,int x1,int y1, int x2, int y2)
{
  int f,g;

  if(x1<0) x1=0;
  if(y1<0) y1=0;
  if(x2<0) x2=0;
  if(y2<0) y2=0;
  if(x1>319) x1=319;
  if(y1>199) y1=199;
  if(x2>319) x2=319;
  if(y2>199) y2=199;
  x1=x1*modex;
  y1=y1*modey;
  x2=x2*modex;
  y2=y2*modey;
  for (f=y1; f<=y2; f++) for (g=x1; g<=x2; g++) dddSetPixel(c,g,f);
}

void apiplot(int c,int x1,int y1)
{
  dddSetPixel(c,x1*modex,y1*modey);
}

void apiprojectile(char style, VtkObj object, char flags, short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
  if(rendermethod==NONE) style=DRW_SIMPLE;
  if(rendermethod==FLAT && style==DRAWFULL) style=DRAWMOST;
  dddSetShapeFlags(object,flags);
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWMOST:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|FLAT_SHADE);
    break;
    case DRAWLINE:
      dddSetRenderMethod(object,LINEON|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED);
    break;
  }
  dddInsertObject(object);
}

void apishape(char style, VtkObj object, char flags, short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
  if(rendermethod==FULL) flags=1; //Texture mapping on
  if(rendermethod==NONE) style=DRW_SIMPLE;
  if(rendermethod==FLAT && style==DRAWFULL) style=DRAWMOST;
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  dddSetShapeFlags(object,flags);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWMOST:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|FLAT_SHADE);
    break;
    case DRAWLINE:
      dddSetRenderMethod(object,LINEON|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED);
    break;
  }
  dddInsertObject(object);
}

void apiground(char style, VtkObj object, short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
  int flags;
  if(rendermethod==FULL) flags=ground; //Texture mapping on
  else flags=ground+10;
  if(rendermethod==NONE) style=DRW_SIMPLE;
  if(rendermethod==FLAT && style==DRAWFULL) style=DRAWMOST;
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  dddSetShapeFlags(object,flags);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWMOST:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|FLAT_SHADE);
    break;
    case DRAWLINE:
      dddSetRenderMethod(object,LINEON|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED);
    break;
  }
  dddInsertObject(object);
}

void apiguishape(char style, VtkObj object,char flags,short Xpos,short Ypos,short Zpos,short Xangle,short Yangle,short Zangle)
{
  dddSetShapeFlags(object,flags);
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
      break;
    case DRAWLINE:
      dddSetRenderMethod(object,RENDERED|LINEON);
      break;
    case DRAWROOM:
      dddSetRenderMethod(object,RENDERED|GOURAUD_SHADE);
      break;
  }
  dddInsertObject(object);
}

void apismallshape(char style, VtkObj object, char flags, short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
  if(smalldetails==BASIC) style=DRW_SIMPLE;
  if(smalldetails==FLAT && style==DRAWFULL) style=DRAWMOST;
  dddSetShapeFlags(object,flags);
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWLINE:
      dddSetRenderMethod(object,LINEON|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWMOST:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED|FLAT_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED|ZBUFFERED);
    break;
  }
  dddInsertObject(object);
}

void apiplanet( VtkObj object,short shape ,short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
int style,f=0;
  if(shape==97 || shape==94|| shape==95|| shape==96) // ground
  {
    if(planets==2) f=1;
    style=DRW_SIMPLE;
    if(shape==97)
    {
      if(planets==1) dddSetShapeFlags(object,ground+10);
      else if(planets==2) dddSetShapeFlags(object,ground);
    }
    else dddSetShapeFlags(object,f);
  }
  else if(planets==1 || (shape==81 ||shape==82 ||shape==83 || shape==60 || shape==101 || shape==102 || shape==103))
  {
    style=DRW_SIMPLE;
    dddSetShapeFlags(object,0);
  }
  else if(planets==2)
  {
    style=DRAWFULL;
    dddSetShapeFlags(object,1);
  }
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|GOURAUD_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED);
    break;
  }
  dddInsertObject(object);
}

void apicockpitshape(char style, VtkObj object, char flags, short Xpos, short Ypos, short Zpos, short Xangle, short Yangle, short Zangle)
{
  if(cockpitrendermethod==NONE) style=DRW_SIMPLE;
  if(cockpitrendermethod==FLAT && style==DRAWFULL) style=DRAWMOST;
  dddSetShapeFlags(object,flags);
  dddMoveTo(object,Xpos,Ypos,Zpos);
  dddRotateTo(object,Xangle,Yangle,Zangle);
  switch(style)
  {
    case DRAWFULL:
      dddSetRenderMethod(object,RENDERED|GOURAUD_SHADE);
    break;
    case DRAWLINE:
      dddSetRenderMethod(object,LINEON|ZBUFFERED|GOURAUD_SHADE);
    break;
    case DRAWMOST:
      dddSetRenderMethod(object,RENDERED|FLAT_SHADE);
    break;
    case DRW_SIMPLE:
      dddSetRenderMethod(object,RENDERED);
    break;
  }
  dddInsertObject(object);
}

void apigameprint3(char Color, short Xpos, short Ypos, char *text)
{
  if(modey==2) Ypos++;
  if(Xpos>0 && Xpos<319 && Ypos>0 && Ypos<199)
    dddPrint(dddFont3x5,Color,Xpos*modex,Ypos*modey,text);
}
void apiniceprint3(char Color, char Color2, short Xpos, short Ypos, char *text)
{
  if(modey==2) Ypos++;
  if(Xpos>0 && Xpos<319 && Ypos>0 && Ypos<199)
  {
    dddPrint(dddFont3x5,Color2,(Xpos*modex)+1,Ypos*modey,text);
    dddPrint(dddFont3x5,Color2,(Xpos*modex)-1,Ypos*modey,text);
    dddPrint(dddFont3x5,Color,Xpos*modex,Ypos*modey,text);
  }
}
void apiniceprint4(char Color, char Color2, char Color3,char Color4,short Xpos, short Ypos, char *text)
{
  if(modey==2) Ypos++;
  if(Xpos>0 && Xpos<319 && Ypos>0 && Ypos<199)
  {
    dddPrint(dddFont3x5,Color4,(Xpos*modex)+3,Ypos*modey,text);
    dddPrint(dddFont3x5,Color3,(Xpos*modex)+2,Ypos*modey,text);
    dddPrint(dddFont3x5,Color2,(Xpos*modex)+1,Ypos*modey,text);
    dddPrint(dddFont3x5,Color,Xpos*modex,Ypos*modey,text);
  }
}
void apicoolprint3(char Color, char Bk, short Xpos, short Ypos, char *text)
{
  dddPrint(dddFont3x5,Bk,(Xpos*modex)+1,(Ypos*modey)+1,text);
  dddPrint(dddFont3x5,Color,Xpos*modex,Ypos*modey,text);
}
void apigameprint5(char Color, short Xpos, short Ypos, char *text)
{
  dddPrint(dddFont7x7,Color,Xpos*modex,Ypos*modey,text);
}

long apireadtimer(void)
{
  return(jjjtimer_elapsed(0));
}

void apisetviewpos(short x, short y, short z)
{
  dddMoveTo(camera,x,y,z);
}

void apisetviewangles(short xang, short yang, short zang)
{
  dddRotateTo(camera,xang,yang,zang);
}

void apiclearcliparea(void)
{
  dddCls();
}

void apiclearobjects(void)
{
  dddZBCls();
  dddClearObjects();
}

void apidrawobjects(void)
{
 dddDrawObjects();
}

void apiscreenswap(void)
{
 dddScreenCopy();
}

void apisetpal(void)
{
  FullPal=dddPaletteLoad("sec32.col");
  MyPal=dddPaletteLoad("sec32.col");
  dddSetPalette(MyPal);
  dddCreateTransparencyTable(MyPal);
}

void apiprint7(char Color, short Xpos, short Ypos, char *text)
{
  dddPrint(dddFont8x9,Color,Xpos*modex,Ypos*modey,text);
}

void apiinstall(void)
{
  jjjtimer_init(1);
  jjjtimer_start(0);
  dddInitDos(malloc,free,dddLoadFile);
  apisetvideo();
  installKeyboardHandler();
  MInit();
}

void apirestore(void)
{
  dddSetVideo(0x03);
  removeKeyboardHandler();
  printf("Please wait - freeing memory . . .\n");
  dddRestoreAll();
  jjjtimer_shutdown();
}

void apisetvideo(void)
{
  if (videomode==0) // 320x200x256
  {
    dddSelectMemoryScreen(&Default320x200);
    old_video_mode=dddSetVideo(VGA8BIT320X200);
    modex=1; modey=1;
  }
  if (videomode==1) // 320x400x256
  {
    dddSelectMemoryScreen(&Default320x400);
    old_video_mode=dddSetVideo(VGA8BIT320X400);
    modex=1; modey=2;
  }
  apisetpal();
}

void apigetmousepos(void)
{
        MCheck();
  apimouse_x=m.mx*3.3;
  apimouse_y=m.my*5;
  apilbutton=m.lmb;
  apirbutton=m.rmb;
}

void apireadjoystick(void)
{
  JCheck();
  apijoystick_x_position=j.xpos;
  apijoystick_y_position=j.ypos;
  if(j.sw1==1) apijoystick_right_button=0; else apijoystick_right_button=1;
  if(j.sw2==1) apijoystick_left_button=0; else apijoystick_left_button=1;
}

int apirandom(int input)
{
//  if(norand==0)
  return(rand() % input);
//  else return(input/2);
}

MInit()
{
  union REGS mo;
  mo.w.ax = 0x0000;
  int386(0x33, &mo, &mo);
  return(mo.h.ah);
}

MCheck()
{
  union REGS mo;
  mo.w.ax = 0x0003;                  /* Get current x/y position and */
  int386(0x33, &mo, &mo);             /* button status */
  m.px = m.mx;                       /* Store previous positions */
  m.py = m.my;
  m.lmb = mo.h.bl & 1;               /* LMB currently down */
  m.rmb = mo.h.bl & 2;               /* RMB currently down */
  m.mx  = mo.w.cx;                   /* Current x/y position of mouse */
  m.my  = mo.w.dx;
  m.mx = m.mx / 2;
  mo.w.ax = 0x0006;                  /* Get left button release status */
  mo.w.bx = 0;
  int386(0x33, &mo, &mo);
  m.lmr = mo.h.bl;
  mo.w.ax = 0x0006;                  /* Get right button release status */
  mo.w.bx = 1;
  int386(0x33, &mo, &mo);
  m.rmr = mo.h.bl;
  return(1);
}

JCheck()
{
  union REGS regs;
  regs.w.dx = 0;
  regs.h.ah = 0x84;
  int386(0x15, &regs, &regs);
  if (regs.h.al & 16) j.sw1 = 0;
  else j.sw1 = 1;
  if (regs.h.al & 32) j.sw2 = 0;
  else j.sw2 = 1;
  regs.w.dx = 1;
  regs.h.ah = 0x84;
  int386(0x15, &regs, &regs);
  j.xpos = regs.w.ax;
  j.ypos = regs.w.bx;
  return(0);
}

void __interrupt __far keyboardHandler()
{
  int keyCode=inp(0x60);
  outp(0x20,0x20);
  if(keyCode==0xE0) { keyAdd=128; keyCode=0; }
  else
  {
    if(keyCode>127)
    {
      keyCode&=127;
      keyCode+=keyAdd;
      key[keyCode]=0;
    }
    else
    {
      keyCode+=keyAdd;
      if(!key[keyCode]) key[keyCode]=1;
    }
    keyAdd=0;
    keyCode=0;
  }
}

void installKeyboardHandler(void)
{
  int i;
  if(keyboardHandlerInstalled)return;
  for(i=0;i<256;i++) key[i]=0;
  biosKeyboardHandler=_dos_getvect(0x09);
  _dos_setvect(0x09,keyboardHandler);
  keyboardHandlerInstalled=1;
  keyAdd=0;
}

void removeKeyboardHandler(void)
{
  if(keyboardHandlerInstalled) _dos_setvect(0x09,biosKeyboardHandler);
  keyboardHandlerInstalled=0;
}

int jjjtimer_init(unsigned int num_timers)
{
  if(jjjtimer_not_initailized==0) return(0);
  if(num_timers>0)
  {
    jjj_start_times=(unsigned long *) calloc ( num_timers, sizeof ( unsigned long ) );
    if(jjj_start_times==NULL) return(-1);
    jjj_num_timers = num_timers;
  }
  else return(-1);
  outp ( 0x43, 0x34 );
  outp ( 0x40, 0x00 );
  outp ( 0x40, 0x00 );
  jjjtimer_not_initailized = 0;
  return(0);
}

void jjjtimer_shutdown(void)
{
  if(jjjtimer_not_initailized!=0) return;
  if (jjj_start_times!=NULL)
  {
    free ( jjj_start_times );
    jjj_start_times = NULL;
  }
  outp ( 0x43, 0x36 ); //was outportb
  outp ( 0x40, 0x00 ); //was outportb
  outp ( 0x40, 0x00 ); //was outportb
  jjjtimer_not_initailized = 1;
}

unsigned long read_timer(void)
{
  register unsigned char LSB;        /* least significant byte           */
  register unsigned char MSB;        /* most significant byte            */
  outp ( 0x43, 0x00 ); //was outportb
  LSB = inp ( 0x40 );  //was inportb          /* get low order byte  */
  MSB = inp ( 0x40 );  //was inportb          /* get high order byte */
  return((unsigned long)(TM_LARGE_TIMER << 16)|(unsigned int)(0xFFFF-((((unsigned int)MSB)<<8)|LSB)));
}

int jjjtimer_start(unsigned int tid)
{
  if(jjjtimer_not_initailized!=0) return(0);
  if(tid<jjj_num_timers) jjj_start_times[tid]=read_timer();
  else return(0);
  return(1);
}

unsigned long jjjtimer_elapsed(unsigned int tid)
{
  if(jjjtimer_not_initailized!=0) return(0);
  if(tid<jjj_num_timers) /* Timer is valid, so return number */
    return( read_timer() - jjj_start_times[tid] );
  else
    return(0);
}

int jjjtargt_open(char filename[256])
{
  int i,bytes;
  char s1[80];
  tempofile[0]='\0';
  handle=open(filename,O_BINARY|O_RDONLY);
  if(handle==-1) return(-1);
  bytes=read(handle,&mainh,sizeof(struct jjjtar_mainheader));
  if(bytes!=sizeof(struct jjjtar_mainheader)) return(-1);    //Failed to read header
  if(strcmp(mainh.identifier,"JWJMJSJTBB\0")!=0) return(-1); //Initial chars not right
  filep=(struct jjjtar_filepointer*)malloc(mainh.files*sizeof(struct jjjtar_filepointer));
  if(filep==NULL) return(-1);
  for(i=0;i<mainh.files;i++)
  {
	 bytes=read(handle,&filep[i],sizeof(struct jjjtar_filepointer));
	 if(bytes!=sizeof(struct jjjtar_filepointer))
   {
     sprintf(s1,"GUN: File number %d read %d bytes, should have read %d bytes",i,bytes,sizeof(filep[i]));
     crashout(s1);
   }
  }
  return(0);
}

char * jjjtargt_tempextract(char filename[])
{
  char *bigbuff;
  int i,handle2,bytes;

  if(handle==-1) return(NULL);
  if(tempofile[0]!='\0')
  {
  	remove(tempofile);
	  tempofile[0]='\0';
  }
  strupr(filename);
  for(i=0;i<mainh.files;i++) if(strcmp(filep[i].name,filename)==0)
  {
	  // Allocate memory, set position in .sec file and read in the data
	  bigbuff=(char *)malloc(filep[i].size*sizeof(char));
    if(bigbuff==NULL) crashout("MEMORY: You don't have enough");

	  lseek(handle,filep[i].offset,SEEK_SET);
	  bytes=read(handle,bigbuff,filep[i].size*sizeof(char));
 	  if(bytes!=filep[i].size*sizeof(char)) return(NULL);
    // Make temporary filename, and keep extension
    strcpy(tempofile,filep[i].name);

 	  handle2=creat(tempofile,S_IWRITE);
    if(handle2==-1) return(NULL);
 	  close(handle2);
    handle2=open(tempofile,O_BINARY|O_WRONLY);
	  if(handle2==-1) return(NULL);
	  bytes=write(handle2,bigbuff,filep[i].size*sizeof(char));
	  if(bytes!=filep[i].size*sizeof(char)) return(NULL);
	  close(handle2);
    if(bigbuff!=NULL) free(bigbuff);
	  return(tempofile);
  }
  return(NULL);
}

char * jjjtargt_permextract(char filename[])
{
  char *bigbuff;
  int i,handle2,bytes;
  if(handle==-1) return(NULL);
  strupr(filename);
  for(i=0;i<mainh.files;i++) if(strcmp(filep[i].name,filename)==0)
  {
	  bigbuff=(char *)malloc(filep[i].size*sizeof(char));
    if(bigbuff==NULL) crashout("MEMORY: You don't have enough");

	  lseek(handle,filep[i].offset,SEEK_SET);
	  bytes=read(handle,bigbuff,filep[i].size*sizeof(char));
 	  if(bytes!=filep[i].size*sizeof(char)) return(NULL);

 	  handle2=creat(filep[i].name,S_IWRITE);
    if(handle2==-1) return(NULL);
 	  close(handle2);
    handle2=open(filep[i].name,O_BINARY|O_WRONLY);
	  if(handle2==-1) return(NULL);
	  bytes=write(handle2,bigbuff,filep[i].size*sizeof(char));
	  if(bytes!=filep[i].size*sizeof(char)) return(NULL);
	  close(handle2);
    if(bigbuff!=NULL) free(bigbuff);
	  return(filep[i].name);
  }
  return(NULL);
}

void jjjtargt_close(void)
{
  if(handle==-1) return;
  if(tempofile[0]!='\0')
  {
	  remove(tempofile);
	  tempofile[0]='\0';
  }
  if(filep!=NULL) free(filep);
  close(handle);
}

