/*
 * $Id: fromflash.c,v 1.2 2006/03/01 01:02:13 jlivesey Exp $
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define PAGE_SIZE 2048
#define SPARE_SIZE 64

#define bit(c, n) ((c & (1 << n)) >> n)

int
main (int argc, char **argv)
{
    char *infilename = "", *outfilename = "";
    FILE *infile, *outfile;
    unsigned char data[PAGE_SIZE];
    unsigned int spare[SPARE_SIZE/sizeof(unsigned int)];
    unsigned int status;
    int page = 0, eof = 0;

    if (argc > 2) {
        infilename = argv[1];
        printf ("%s\n", infilename);
        if ((infile = fopen (infilename, "r")) == NULL) {
            perror ("open");
            exit (1);
        }
        outfilename = argv[2];
        printf ("%s\n", outfilename);
        if ((outfile = fopen (outfilename, "w")) == NULL) {
            perror ("open");
            exit (1);
        }
    } else {
        fprintf (stderr, "usage: %s file1 file2\n", argv[0]);
        exit (1);
    }

    /*
     * Repeatedly read a page and its spare, and then just
     * write out the spare.
     */
    while (1) {
        status = fread (&data[0], sizeof(char), sizeof (data), infile);
        if (status != sizeof(data)) {
            if (status == 0) {
                break;
            }
            if (feof(infile)) {
                eof = 1;
            }
        }
        status = fread(&spare[0], sizeof(char), sizeof (spare), infile);
        if (status != sizeof(spare)) {
            if (feof(infile)) {
                printf ("Found EOF\n");
            }
        }
        fwrite (&data[0], sizeof (data), 1, outfile);
        page++;
    }
    
    exit (0);
}
