From c55263b9569ac4491b973b98801aede9e9a82921 Mon Sep 17 00:00:00 2001 From: Themaister Date: Mon, 22 Aug 2011 18:13:30 +0200 Subject: [PATCH] Clean up some file handling stuff ... --- Makefile.win32 | 2 +- Makefile.win64 | 2 +- file.c | 44 ++++++++++++++++++++++---------------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Makefile.win32 b/Makefile.win32 index 4afd0a81a0..2b2c2f91b8 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -22,7 +22,7 @@ HAVE_PYTHON = 1 HAVE_FFMPEG = 0 libsnes ?= -lsnes -LIBS = -lm +LIBS = -lm -lshlwapi DEFINES = -I. -DHAVE_CONFIGFILE LDFLAGS = -L. -static-libgcc -s LDCXXFLAGS = -static-libstdc++ diff --git a/Makefile.win64 b/Makefile.win64 index 480977c653..d43ac62606 100644 --- a/Makefile.win64 +++ b/Makefile.win64 @@ -21,7 +21,7 @@ HAVE_PYTHON = 0 HAVE_FFMPEG = 1 libsnes ?= -lsnes -LIBS = -lm +LIBS = -lm -lshlwapi DEFINES = -I. -DHAVE_CONFIGFILE LDFLAGS = -L. -static-libgcc -s LDCXXFLAGS = -static-libstdc++ diff --git a/file.c b/file.c index 1b58857578..ac324e62a9 100644 --- a/file.c +++ b/file.c @@ -36,6 +36,7 @@ #include #include #include +#include #else #include #include @@ -703,8 +704,6 @@ bool init_rom_file(enum ssnes_game_type type) // Yep, this is C alright ;) char** dir_list_new(const char *dir, const char *ext) { - size_t path_len = strlen(dir); - size_t cur_ptr = 0; size_t cur_size = 32; char **dir_list = NULL; @@ -713,18 +712,18 @@ char** dir_list_new(const char *dir, const char *ext) WIN32_FIND_DATAW ffd; HANDLE hFind = INVALID_HANDLE_VALUE; + wchar_t wchar_buf[MAXPATHLEN]; + char utf8_buf[MAXPATHLEN]; - size_t final_off = MAX_PATH + path_len + 2; + if (strlcpy(utf8_buf, dir, sizeof(utf8_buf)) >= sizeof(utf8_buf)) + goto error; + if (strlcat(utf8_buf, "/*", sizeof(utf8_buf)) >= sizeof(utf8_buf)) + goto error; + if (strlcat(utf8_buf, ext, sizeof(utf8_buf)) >= sizeof(utf8_buf)) + goto error; - wchar_t wchar_buf[MAX_PATH + 1]; - char utf8_buf[MAX_PATH + 3]; - - strlcpy(utf8_buf, dir, sizeof(utf8_buf)); - strlcat(utf8_buf, "/*", sizeof(utf8_buf)); - utf8_buf[MAX_PATH + 2] = '\0'; - - int ret = MultiByteToWideChar(CP_UTF8, 0, utf8_buf, strlen(utf8_buf), wchar_buf, MAX_PATH); - wchar_buf[ret] = 0; + if (MultiByteToWideChar(CP_UTF8, 0, utf8_buf, -1, wchar_buf, MAXPATHLEN) == 0) + goto error; hFind = FindFirstFileW(wchar_buf, &ffd); if (hFind == INVALID_HANDLE_VALUE) @@ -732,7 +731,6 @@ char** dir_list_new(const char *dir, const char *ext) #else DIR *directory = NULL; const struct dirent *entry = NULL; - size_t final_off = sizeof(entry->d_name) + path_len + 2; directory = opendir(dir); if (!directory) @@ -753,8 +751,8 @@ char** dir_list_new(const char *dir, const char *ext) #ifdef _WIN32 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; - int ret = WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, wcslen(ffd.cFileName), utf8_buf, MAX_PATH, NULL, NULL); - utf8_buf[ret] = '\0'; + if (WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, -1, utf8_buf, MAX_PATH, NULL, NULL) == 0) + continue; if (ext && !strstr(utf8_buf, ext)) continue; #else @@ -762,16 +760,16 @@ char** dir_list_new(const char *dir, const char *ext) continue; #endif - dir_list[cur_ptr] = malloc(final_off); + dir_list[cur_ptr] = malloc(MAXPATHLEN); if (!dir_list[cur_ptr]) goto error; - strlcpy(dir_list[cur_ptr], dir, final_off); - strlcat(dir_list[cur_ptr], "/", final_off); + strlcpy(dir_list[cur_ptr], dir, MAXPATHLEN); + strlcat(dir_list[cur_ptr], "/", MAXPATHLEN); #ifdef _WIN32 - strlcat(dir_list[cur_ptr], utf8_buf, final_off); + strlcat(dir_list[cur_ptr], utf8_buf, MAXPATHLEN); #else - strlcat(dir_list[cur_ptr], entry->d_name, final_off); + strlcat(dir_list[cur_ptr], entry->d_name, MAXPATHLEN); #endif cur_ptr++; @@ -788,7 +786,6 @@ char** dir_list_new(const char *dir, const char *ext) } #ifdef _WIN32 while (FindNextFileW(hFind, &ffd) != 0); - #endif #ifdef _WIN32 @@ -825,7 +822,10 @@ void dir_list_free(char **dir_list) bool path_is_directory(const char *path) { #ifdef _WIN32 - return false; + wchar_t buf[MAXPATHLEN]; + if (MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, MAXPATHLEN) == 0) + return false; + return PathIsDirectoryW(buf) == FILE_ATTRIBUTE_DIRECTORY; #else struct stat buf; if (stat(path, &buf) < 0)