#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <exec/types.h>
#include <libraries/dosextens.h>
#include <dialog/resman.h>
#include <devices/tpt.h>

#include <proto/dos.h>
#include <proto/dlg.h>

extern struct Library  *DLGBase;
extern char           **SA;


BOOL EditFile(char *filename,struct USER_DATA *stuser,struct Ram_File *rstruct,char *ext)

{ULONG size;
 long  counter;

 char *tempbuf;
 char *buf;

 char  tempfile    [256];
 char  tempfilename[256];
 BPTR  fh;

 strcpy(tempfilename, filename);
 for(counter=0; tempfilename[counter]; counter++)
     if (tempfilename[counter] == ' ')  tempfilename[counter] = 0;

 ASPrintf(NULL, tempfile, "%s~", tempfilename);
 Copy(tempfilename, tempfile);

 if (CallEditor(NULL,NULL,tempfilename,0,stuser,rstruct,ext)!=-1)
    {FileSize(tempfilename, &size);

     if (!(buf = malloc(size)))
        {AFPrintf(stuser, Output(), SA[3422]);
         DeleteFile(tempfilename);
         Rename(tempfile, tempfilename);
         return(FALSE);
        }

     GetFirstStruct(tempfilename, buf, size);
     for(tempbuf=buf, counter=0L;counter<size;counter++)
        {if(*tempbuf==13)  *tempbuf=10;
         tempbuf++;
        }

     DeleteFile(tempfilename);
     if (!(fh = Open(tempfilename, MODE_NEWFILE)))
        {AFPrintf(stuser, Output(), SA[3422]);
         Rename(tempfile, tempfilename);
         free(buf);
         return(FALSE);
        }
     Write(fh,buf,size);
     Close(fh);

     free(buf);
    }

 DeleteFile(tempfile);
 return(TRUE);
}
