diff --git a/src/backends/unix/system.c b/src/backends/unix/system.c index c4bd90f4..dfc56f36 100644 --- a/src/backends/unix/system.c +++ b/src/backends/unix/system.c @@ -519,6 +519,20 @@ Sys_RemoveDir(const char *path) } } +void +Sys_Realpath(const char *in, char *out, size_t size) +{ + char *converted = realpath(in, NULL); + + if (converted == NULL) + { + Com_Error(ERR_FATAL, "Couldn't get realpath for %s\n", in); + } + + Q_strlcpy(out, converted, size); + free(converted); +} + /* ================================================================ */ void * diff --git a/src/backends/windows/system.c b/src/backends/windows/system.c index 861b99e1..2d955725 100755 --- a/src/backends/windows/system.c +++ b/src/backends/windows/system.c @@ -586,6 +586,23 @@ Sys_RemoveDir(const char *path) RemoveDirectoryW(wpath); } +void +Sys_Realpath(const char *in, char *out, size_t size) +{ + WCHAR win[MAX_OSPATH] = {0}; + WCHAR wconverted[MAX_OSPATH] = {0}; + + MultiByteToWideChar(CP_UTF8, 0, in, -1, win, sizeof(win)); + _wfullpath(wconverted, win, size); + + if (wconverted == NULL) + { + Com_Error(ERR_FATAL, "Couldn't get realpath for %s\n", in); + } + + WideCharToMultiByte(CP_UTF8, 0, wconverted, -1, out, size, NULL, NULL); +} + /* ======================================================================= */ void * diff --git a/src/common/filesystem.c b/src/common/filesystem.c index 2ab603f5..63ca7233 100644 --- a/src/common/filesystem.c +++ b/src/common/filesystem.c @@ -1805,9 +1805,12 @@ const char* FS_GetFilenameForHandle(fileHandle_t f) // -------- void FS_AddDirToRawPath (const char *rawdir, qboolean create) { - // Convert backslashes to forward slashes. - char *dir = strdup(rawdir); + char dir[MAX_OSPATH] = {0}; + // Get the realpath. + Sys_Realpath(rawdir, dir, sizeof(dir)); + + // Convert backslashes to forward slashes. for (int i = 0; i < strlen(dir); i++) { if (dir[i] == '\\') @@ -1835,9 +1838,6 @@ void FS_AddDirToRawPath (const char *rawdir, qboolean create) { search->create = create; search->next = fs_rawPath; fs_rawPath = search; - - // Cleanup - free(dir); } diff --git a/src/common/header/common.h b/src/common/header/common.h index 51835694..74a05c59 100644 --- a/src/common/header/common.h +++ b/src/common/header/common.h @@ -821,6 +821,7 @@ void *Sys_GetGameAPI(void *parms); void Sys_UnloadGame(void); void Sys_GetWorkDir(char *buffer, size_t len); qboolean Sys_SetWorkDir(char *path); +void Sys_Realpath(const char *in, char *out, size_t size); // Windows only (system.c) #ifdef _WIN32