Skip to content

Instantly share code, notes, and snippets.

@hippietrail
Last active December 25, 2024 09:34
Show Gist options
  • Save hippietrail/2fbf6e82eeb75d0154fb076f78a63308 to your computer and use it in GitHub Desktop.
Save hippietrail/2fbf6e82eeb75d0154fb076f78a63308 to your computer and use it in GitHub Desktop.
Detect whether an Apple ][ .dsk .do or .po disk image file is in DOS 3 or ProDOS format and in DOS 3 or ProDOS sector order
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define TRACKS 35
#define SECTORS_PER_TRACK 16
#define SECTOR_SIZE 256
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <path_to_disk_image>\n", argv[0]);
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "rb");
if (file) {
struct stat st;
if (fstat(fileno(file), &st) == 0) {
if (st.st_size == (TRACKS * SECTORS_PER_TRACK * SECTOR_SIZE)) {
unsigned char t0s4[SECTOR_SIZE];
unsigned char t0s11[SECTOR_SIZE];
unsigned char t17s0[SECTOR_SIZE + 3];
fseek(file, (0 * SECTORS_PER_TRACK + 4) * SECTOR_SIZE, SEEK_SET);
fread(t0s4, sizeof(unsigned char), SECTOR_SIZE, file);
fseek(file, (0 * SECTORS_PER_TRACK + 11) * SECTOR_SIZE, SEEK_SET);
fread(t0s11, sizeof(unsigned char), SECTOR_SIZE, file);
fseek(file, (17 * SECTORS_PER_TRACK) * SECTOR_SIZE, SEEK_SET);
fread(t17s0, sizeof(unsigned char), SECTOR_SIZE + 3, file);
unsigned char *pro[2] = {t0s4, t0s11};
for (int i = 0; i < 2; i++) {
if ((pro[i][4] >> 4) == 0xf &&
(pro[i][4] & 0x0f) > 0 && (pro[i][4] & 0x0f) <= 15 &&
pro[i][5] >= 'A' && pro[i][5] <= 'Z') {
printf("Looks like ProDOS in %s order: '%s'\n", i == 0 ? "ProDOS" : "DOS 3", argv[1]);
}
}
unsigned char *dos = t17s0;
if (dos[0x27] == 122 && dos[0x30] < TRACKS && (dos[0x31] == 1 || dos[0x31] == 0xff) &&
dos[0x34] == 35 && dos[0x35] == 16 && dos[0x36] == 0 && dos[0x37] == 1) {
if (dos[SECTOR_SIZE + 1] == 0 && dos[SECTOR_SIZE + 2] == 0) {
printf("Looks like DOS 3.3 in DOS 3 order: '%s'\n", argv[1]);
} else if (dos[SECTOR_SIZE + 1] == 17 && dos[SECTOR_SIZE + 2] == 13) {
printf("Looks like DOS 3.3 in ProDOS order: '%s'\n", argv[1]);
}
}
}
}
fclose(file);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment