From 308f7f93ee6baa5a299ee748b3fa94759af1efd6 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 10 Aug 2026 15:35:39 +0200 Subject: [PATCH] put directory changing and getting stuff into functions from https://gitlab.com/kart-krew-dev/ring-racers/-/commit/7ef40f5d743bf6bf251e2edc89cda23e8fb36ee5 changed a bit so windows also can print the "warning" on failure --- src/d_main.c | 2 +- src/dedicated/i_system.c | 23 ++++++++++++++++++++--- src/i_system.h | 15 +++++++++++++++ src/sdl/i_system.cpp | 28 +++++++++++++++++++++++++--- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 42f91fc61..14900aa29 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1356,7 +1356,7 @@ static void IdentifyVersion(void) #endif char tempsrb2path[256] = "."; - if (getcwd(tempsrb2path, 256) == NULL) + if (I_GetCwd(tempsrb2path, 256) == NULL) strcpy(tempsrb2path, "."); // get the current directory (possible problem on NT with "." as current dir) diff --git a/src/dedicated/i_system.c b/src/dedicated/i_system.c index e9d191c97..8e99e18ba 100644 --- a/src/dedicated/i_system.c +++ b/src/dedicated/i_system.c @@ -1799,6 +1799,24 @@ INT32 I_mkdir(const char *dirname, INT32 unixright) #endif } +INT32 I_ChDir(const char *path) +{ +#ifdef _WIN32 + return (SetCurrentDirectoryA(path) ? 0 : -1); +#else + return chdir(path); +#endif +} + +char *I_GetCwd(char *buf, size_t size) +{ +#ifdef _WIN32 + return (GetCurrentDirectoryA((DWORD)size, buf) ? buf : NULL); +#else + return getcwd(buf, size); +#endif +} + char *I_GetEnv(const char *name) { return getenv(name); @@ -2003,12 +2021,11 @@ const char *I_LocateWad(void) // change to the directory where we found srb2.srb #if defined (_WIN32) waddir = _fullpath(NULL, waddir, MAX_PATH); - SetCurrentDirectoryA(waddir); #else waddir = realpath(waddir, NULL); - if (waddir == NULL || chdir(waddir) == -1) - I_OutputMsg("Couldn't change working directory\n"); #endif + if (waddir == NULL || I_ChDir(waddir) == -1) + I_OutputMsg("Couldn't change working directory\n"); } return waddir; diff --git a/src/i_system.h b/src/i_system.h index 812db9008..82e3204a3 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -241,6 +241,21 @@ char *I_GetUserName(void); */ INT32 I_mkdir(const char *dirname, INT32 unixright); +/** \brief Change current working directory + + \param path directory path + \return 0 on success, -1 on error +*/ +INT32 I_ChDir(const char *path); + +/** \brief Get current working directory + + \param buf buffer to store path + \param size size of buffer + \return buf on success, NULL on error +*/ +char *I_GetCwd(char *buf, size_t size); + /** \brief Find main WAD \return path to main WAD */ diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 08f28ceb4..d5438b42e 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -63,10 +63,13 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T); #include #include #include + #ifdef __GNUC__ #include #endif + #if defined (__unix__) || defined (UNIXCOMMON) +#include #include #endif @@ -2266,6 +2269,7 @@ void I_ShutdownSystem(void) for (c = MAX_QUIT_FUNCS-1; c >= 0; c--) if (quit_funcs[c]) (*quit_funcs[c])(); + #ifdef LOGMESSAGES if (logstream) { @@ -2375,6 +2379,24 @@ INT32 I_mkdir(const char *dirname, INT32 unixright) #endif } +INT32 I_ChDir(const char *path) +{ +#ifdef _WIN32 + return (SetCurrentDirectoryA(path) ? 0 : -1); +#else + return chdir(path); +#endif +} + +char *I_GetCwd(char *buf, size_t size) +{ +#ifdef _WIN32 + return (GetCurrentDirectoryA((DWORD)size, buf) ? buf : NULL); +#else + return getcwd(buf, size); +#endif +} + char *I_GetEnv(const char *name) { #ifdef NEED_SDL_GETENV @@ -2465,6 +2487,7 @@ const char *I_ClipboardPaste(void) *i = '?'; // Nonprintable chars become question marks ++i; } + return (const char *)&clipboard_modified; } @@ -2618,12 +2641,11 @@ extern "C" const char *I_LocateWad(void) // change to the directory where we found srb2.srb #if defined (_WIN32) waddir = _fullpath(NULL, waddir, MAX_PATH); - SetCurrentDirectoryA(waddir); #else waddir = realpath(waddir, NULL); - if (waddir == NULL || chdir(waddir) == -1) - I_OutputMsg("Couldn't change working directory\n"); #endif + if (waddir == NULL || I_ChDir(waddir) == -1) + I_OutputMsg("Couldn't change working directory\n"); } return waddir;