//read.c, as a part of "making your own toy boot floppy" article.
#include <sys/types.h> /* unistd.h needs this */
#include <unistd.h> /* contains read/write */
#include <fcntl.h>

int main()
{
        char boot_buf[512];
        int floppy_desc, file_desc;
	int i,j;
	unsigned char ch;
        
        file_desc = open("boot.sec", O_CREAT|O_WRONLY);
        floppy_desc = open("/dev/fd0", O_RDWR);
        lseek(floppy_desc, 0, SEEK_CUR);
        read(floppy_desc, boot_buf, 512);
	write(file_desc, boot_buf, 512);
        close(file_desc);
	close(floppy_desc);
	printf("boot sector in HEX:\n");
	j=0;
	for(i=0; i<512; i++)
	{
		ch=(unsigned char) boot_buf[i];
		if (j==8) printf(" -");
		if (j==16) 
		{
			printf("\n");
			j=0;
		}
		j++;
		if (ch<0x10) printf(" 0%x",ch);
		else printf(" %2x",ch);
	}
	printf("\n");
}
 

