From d82708b44806589ee46925da19f73745fd6ed518 Mon Sep 17 00:00:00 2001 From: Jonathan Bergeron Date: Fri, 4 Jan 2019 13:20:17 -0500 Subject: [PATCH] added windows implementation of sys_removedir and used snprintf in the unix one --- src/backends/unix/system.c | 2 +- src/backends/windows/system.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/backends/unix/system.c b/src/backends/unix/system.c index 25657466..3cc04332 100644 --- a/src/backends/unix/system.c +++ b/src/backends/unix/system.c @@ -504,7 +504,7 @@ Sys_RemoveDir(const char *path) { while ((file = readdir(directory)) != NULL) { - sprintf(filepath, "%s/%s", path, file->d_name); + snprintf(filepath, MAX_OSPATH, "%s/%s", path, file->d_name); Sys_Remove(filepath); } diff --git a/src/backends/windows/system.c b/src/backends/windows/system.c index fb99b5d9..c0ec1ffb 100644 --- a/src/backends/windows/system.c +++ b/src/backends/windows/system.c @@ -556,6 +556,37 @@ Sys_Rename(const char *from, const char *to) return _wrename(wfrom, wto); } +void +Sys_RemoveDir(const char *path) +{ + WCHAR wpath[MAX_OSPATH] = {0}; + WCHAR wpathwithwildcard[MAX_OSPATH] = {0}; + WCHAR wpathwithfilename[MAX_OSPATH] = {0}; + WIN32_FIND_DATAW fd; + + MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_OSPATH); + + wcscat_s(wpathwithwildcard, MAX_OSPATH, wpath); + wcscat_s(wpathwithwildcard, MAX_OSPATH, L"\\*.*"); + + HANDLE hFind = FindFirstFileW(wpathwithwildcard, &fd); + if (hFind != INVALID_HANDLE_VALUE) + { + do + { + wmemset(wpathwithfilename, 0, MAX_OSPATH); + wcscat_s(wpathwithfilename, MAX_OSPATH, wpath); + wcscat_s(wpathwithfilename, MAX_OSPATH, fd.cFileName); + + DeleteFileW(wpathwithfilename); + } + while (FindNextFileW(hFind, &fd)); + FindClose(hFind); + } + + RemoveDirectoryW(wpath); +} + /* ======================================================================= */ void *