/* Copyright (C) 2014-2015 Ben Kurtovic Released under the terms of the MIT License. See LICENSE for details. */ #include #include #include #include #include #include "rom.h" /* Create and return a ROM object located at the given path. Return NULL if there was an error; errno will be set appropriately. */ ROM* rom_open(const char *path) { ROM *rom; FILE* fp; struct stat s; if (!(fp = fopen(path, "r"))) return NULL; if (fstat(fileno(fp), &s)) { fclose(fp); return NULL; } if (!(s.st_mode & S_IFREG)) { if (s.st_mode & S_IFDIR) errno = EISDIR; fclose(fp); return NULL; } if (!(rom = malloc(sizeof(ROM)))) { fclose(fp); return NULL; } rom->name = malloc(sizeof(char) * (strlen(path) + 1)); strcpy(rom->name, path); // TODO: load data from file into a buffer fclose(fp); return rom; } /* Free a ROM object previously created with rom_open(). */ void rom_close(ROM *rom) { free(rom); }