Skip to content

Instantly share code, notes, and snippets.

@jeandrek
Created April 9, 2018 04:46
Show Gist options
  • Save jeandrek/40a52e342e5c695c1d5fe4807ce5c802 to your computer and use it in GitHub Desktop.
Save jeandrek/40a52e342e5c695c1d5fe4807ce5c802 to your computer and use it in GitHub Desktop.
Extract MS-DOS 8.0 disk image
#include <stdio.h>
#include <stdlib.h>
#define SECTOR 512
#define BOOTMAGIC 0xaa55
char disk[SECTOR*2880];
int
main(int argc, const char *argv[])
{
char name[48];
FILE *f;
int c;
snprintf(name, 48, "%s/System32/diskcopy.dll", getenv("SystemRoot"));
if (argc > 1)
f = fopen(argv[1], "rb");
else
f = fopen(name, "rb");
if (f == NULL) {
perror("Error: cannot open DLL");
return 1;
}
while ((c = getc(f)) != EOF) {
if (c == (BOOTMAGIC & 0xff)) {
c = getc(f);
if (c == BOOTMAGIC >> 8) {
puts("Found boot sector!");
goto found;
}
}
}
fputs("Error: cannot find boot sector\n", stderr);
return 1;
found:
fseek(f, -SECTOR, SEEK_CUR);
fread(disk, SECTOR, 2880, f);
fclose(f);
f = fopen("msdos.img", "wb");
fwrite(disk, SECTOR, 2880, f);
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment