diff --git a/src/include/86box/plat.h b/src/include/86box/plat.h index 552b074f7..b76728047 100644 --- a/src/include/86box/plat.h +++ b/src/include/86box/plat.h @@ -150,6 +150,7 @@ extern void plat_get_temp_dir(char *outbuf, uint8_t len); extern void plat_get_vmm_dir(char *outbuf, size_t len); extern void plat_init_rom_paths(void); extern int plat_dir_check(char *path); +extern int plat_file_check(const char *path); extern int plat_dir_create(char *path); extern void *plat_mmap(size_t size, uint8_t executable); extern void plat_munmap(void *ptr, size_t size); diff --git a/src/mem/rom.c b/src/mem/rom.c index f7b2b2b0d..c7db01d72 100644 --- a/src/mem/rom.c +++ b/src/mem/rom.c @@ -28,6 +28,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" @@ -129,6 +130,34 @@ rom_get_full_path(char *dest, const char *fn) } } +bool +rom_fcheck(const char *fn) +{ + char temp[1024]; + bool exists = false; + + if (fn == NULL) + return false; + + if (strstr(fn, "roms/") == fn) { + /* Relative path */ + for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { + path_append_filename(temp, rom_path->path, fn + 5); + + exists = plat_file_check(temp); + + if (exists) { + return exists; + } + } + + return exists; + } else { + /* Absolute path */ + return plat_file_check(fn); + } +} + FILE * rom_fopen(const char *fn, char *mode) { @@ -186,15 +215,7 @@ rom_getfile(char *fn, char *s, int size) int rom_present(const char *fn) { - FILE *fp; - - fp = rom_fopen(fn, "rb"); - if (fp != NULL) { - (void) fclose(fp); - return 1; - } - - return 0; + return rom_fcheck(fn); } uint8_t diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index a0c2aa502..f36856e5f 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -64,6 +64,8 @@ # include #endif +#include + #ifdef Q_OS_OPENBSD # include #endif @@ -250,6 +252,22 @@ plat_dir_check(char *path) return fi.isDir() ? 1 : 0; } +int +plat_file_check(const char *path) +{ +#ifdef _WIN32 + auto data = QString::fromUtf8(path).toStdWString(); + auto res = GetFileAttributesW(data.c_str()); + return (res != INVALID_FILE_ATTRIBUTES && !(res & FILE_ATTRIBUTE_DIRECTORY)); +#else + struct stat dummy; + if (stat(path, &dummy) < 0) { + return 0; + } + return !S_ISDIR(dummy.st_mode); +#endif +} + int plat_getcwd(char *bufp, int max) { diff --git a/src/unix/unix.c b/src/unix/unix.c index 70cf5b87c..20727928c 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -412,6 +412,16 @@ plat_dir_check(char *path) return S_ISDIR(dummy.st_mode); } +int +plat_file_check(const char *path) +{ + struct stat dummy; + if (stat(path, &dummy) < 0) { + return 0; + } + return !S_ISDIR(dummy.st_mode); +} + int plat_dir_create(char *path) {