#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <io.h>

#include <windows.h>

int             buffer_size = 200 /* K */ ,
    debug_output = FALSE,
    tape_density = 0,
    UnitNumber = 4,
    SCSI_lun = 0,
    tape_file = 0,
    partition = 0;

main(int argc, char **argv)
{
	
	int             finished = FALSE;
	int tape_file;
	char *tape_filename = "tape.op",
		*tape_dev = "\\\\.\\tape1";
	HANDLE td;
    char           *Buffer;
    if(argc > 1)
        tape_dev = argv[1];

    printf("Using device %s\n",tape_dev);

    buffer_size *= 1024;

    Buffer = (char*)malloc (buffer_size);

	td = CreateFile(tape_dev, GENERIC_READ, 0,0, OPEN_EXISTING, 
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);
    if (td >= 0)
    {
        int r, bytes_in_buffer, bytes_read, finished=0;
        int bn=0;
		if (!(tape_file = open (tape_filename,
                                O_BINARY | O_RDWR  | O_CREAT | O_TRUNC,0666)))
		{
            free (Buffer);
            puts ("Couldn't open tape file for output");
            exit(-1);
		}
		fprintf (stderr, "Read entire tape partition\n");

		while (!finished)
        {
            r = ReadFile( td, Buffer, buffer_size, &bytes_read, 0) ;
			  
			if (bytes_read)
			{
			    /*
			     * Output contents of buffer
			     */
			    write (tape_file, Buffer, bytes_read);
				fprintf (stderr,"Block read %d\n",bn);
			    bn++;
			}
			if (!r)
			{
			    finished = TRUE;
			}
        }
        fprintf (stderr, "%-4d blocks read\n", bn);
        close (tape_file);
        CloseHandle(td);
	}
}
