#include <gsptiga.h>

#define fopen(a,b)      \
     host_command( 0, 2, __STRING(a), __STRING(b) )
#define fread(a,b,c,d)  \
     host_command( 1, 4, __INBYTE_PTR(b*c,a), __WORD(b), __WORD(c), __DWORD(d) )
#define fwrite(a,b,c,d) \
     host_command( 2, 4, __BYTE_PTR(b*c,a), __WORD(b), __WORD(c), __DWORD(d) )
#define fclose(a)       \
     host_command( 3, 1, __DWORD(a) )

copy(arg1,arg2)
char *arg1,*arg2;
{
    long   infile,outfile;
    int    tmp,handle;
    char *buffer;

    printf("Copying file %s to %s\n",arg1,arg2);

    if( !(outfile = fopen(arg2,"wb")) )
        { printf("Can't open output file: %s\n",arg2); return(1); }

    if( !(infile = fopen(arg1,"rb")))
        { printf("Can't open source file: %s\n",arg1); return(1); }

    if( !(handle = gsph_alloc( 2000 )) )
        { printf("Can't alloc file buffer\n"); return(1); }
    buffer = (char *)gsph_deref( handle );

    while((tmp = fread(buffer,1,2000,infile)) == 2000)
        fwrite(buffer,2000,1,outfile);
    fwrite(buffer,tmp,1,outfile);

    fclose(infile);
    fclose(outfile);

    printf("[ Copy Complete ]\n");
}
