backends: add portable variant of getcwd

This commit is contained in:
John Törnblom 2024-11-23 13:12:36 +01:00
parent e87bcfb906
commit 24888cc3c1

View file

@ -145,6 +145,31 @@ static void SetExecutablePath(char* exePath)
#endif
}
qboolean Sys_GetCwd(char *buf, size_t size)
{
#ifdef _WIN32
WCHAR wpath[PATH_MAX];
DWORD len;
if (_wgetcwd(wpath, PATH_MAX) == NULL)
{
return false;
}
len = WideCharToMultiByte(CP_UTF8, 0, wpath, -1, buf, size, NULL, NULL);
if (len <= 0 || len == size)
{
return false;
}
#else
if (getcwd(buf, size) == NULL)
{
return false;
}
#endif
return Q_strlcat(buf, "/", size) == 1;
}
const char *Sys_GetBinaryDir(void)
{
static char exeDir[PATH_MAX] = {0};
@ -156,8 +181,10 @@ const char *Sys_GetBinaryDir(void)
SetExecutablePath(exeDir);
if (exeDir[0] == '\0') {
getcwd(exeDir, sizeof(exeDir));
strcat(exeDir, "/");
if (Sys_GetCwd(exeDir, sizeof(exeDir)) == false)
{
Q_strlcpy(exeDir, "./", sizeof(exeDir));
}
Com_Printf("Couldn't determine executable path. Using %s instead.\n", exeDir);
} else {
// cut off executable name