#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* #include "\nxdos\src\toolbox.h" */

int main(int argc, char *argv[]) {
#define editsize 65535/2
    int  err    = 0;
    char buffer[editsize];
    int  ptr    = 0;
    int  blocks = 0;
    int  i      = 0;
    char c;
    FILE *ef;

    ef = fopen(argv[1], "r+"); 
    if (ef==NULL) {
         printf("Error: Cant edit file.\n"); 
         err = 2;
       } else {
          printf("Loading %s...\n",argv[1]);
          while (!feof(ef)) {
               buffer[ptr] = fgetc(ef);
               ptr++;
               if (ptr=editsize) break;
          }          
          blocks = ptr;
          ptr = 0;

         while (c!=27) {
        /* _clrscr(); */
         for (i=0; i<=24; i++) {
              c = buffer[ptr+i]; 
              printf("%c",c); 
            }
          c = getc(stdin);
          buffer[ptr]=c;
          ptr++;               
         } 
        printf("Saving %s...\n",argv[1]);
        fseek(ef, 0, SEEK_SET); 
        for (i=0; i<=blocks; i++) {
             fputc(ef,buffer[i]);
            }
        fclose(ef);
       }

 return err;
}
