/*****************************************
 * FSTEST - Test/Demo Module filesys.h02 *
 *****************************************/
 
//Specify System Header using -H option
#include <screen.h02>
#include <stddef.h02>
#include <stdlib.h02>
#include <intlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <fileio.h02>
#include <filesys.h02>

const char dir = "TEMPDIR";
const char oldnam = "OLDFILE.TMP";
const char newnam = "NEWFILE.TMP";

char d, e, err, f, i, n, r;
char m[128];
char aa,xx,yy;
main:
  newfil();   
  renfil();
  delfil();
  prtdrv();  
  chgdrv();
goto exit;

void chkerr(aa) {
  puts(": ");
  if (err) {
    e = ferror(0, &m);
    printf(e, "ERROR %d - ");
    putln(&m);
  }
  else if (aa) putln("FAILED");
  else putln("SUCCESS");
}

void prtdrv() {
  d, err = getdrv(); 
  printf(d,"GETDRV()='%c'"); 
  chkerr(0);
}

void newfil() {
  puts("CREATING FILE "); puts(oldnam);
  f, err = fopen(#MWRITE, oldnam); chkerr(0);
  puts("WRITING TO FILE");
  n, err = fputln(f, "FILE CONTENTS"); chkerr(0);
  puts("CLOSING FILE");
  err = fclose(f); chkerr(0);
}

void renfil() {
  puts("RENAMING "); puts(oldnam); puts(" TO "); puts(newnam);
  fsname(oldnam);
  err = rename(0, newnam); chkerr(r);
}

void delfil() {
  puts("REMOVING "); puts(newnam);
  err = remove(0, newnam); chkerr(r);
}

void chgdrv() {
  for (i=0; i<#DRIVES; i++) {
    d = drive[i];
    printf(d, "CHDRV('%c') ");
    err = chdrv(d); chkerr(r);
  }
}

error:
  printf(err, "ERROR %d%n");
  goto exit;
