/*********************************
 * copy.c02 - binary file copier *
 *********************************
load "copy.prg",8
*/

#include <vic20.h02>
#include <stdlib.h02>
#include <stdio.h02>
#include <string.h02>
#include <file.h02>

char fp; //File Pointer
char srcnam[128];
char dstnam[128];
char buffer[255];
char i, n;

main:
    //Initialize File System
    fsinit();

    //Open File
    newlin();
    strdst(&buffer);
    strcpy(&filnam);
    strcat(",S,W");
    fp = fopen(8, &buffer);
    if (fp) {
        outstr("CHANNEL ");
        ctoa(fp, &buffer);
        outstr(&buffer);
        putstr(" OPENED TO ");
        putstr(&filnam);
    } 
    else {
        prterr();
        putstr(" OPENING FILE");
        putstr(&filnam);
        goto exit;
    }

    //fill buffer
    i = 0;
    do {
      buffer[i] = i;
      i++;
    } while (i);

    //Print File Contents
    newlin();
    putstr("WRITING FILE");
    fssrc(&buffer);
    for (n=1; n<255; n++) {
      fwrite(fp, n);
      putchr('.');
    }
    newlin();
    newlin();

    //Close File
    if (fclose(fp)) {
        prterr();
        putstr(" CLOSING FILE");
        putstr(&filnam);
    }
    else {
        outstr("FILE ");
        putstr(&filnam);
        putstr("CLOSED");
    }

goto exit;

void prterr() {
    outstr("ERROR ");
    ctoa(fstat(0), &buffer);
    outstr(&buffer);
}
