From 24b3efd08d9ffb71e3937010c5f677aab454d414 Mon Sep 17 00:00:00 2001
From: freebsd <[EMAIL REDACTED]>
Date: Tue, 12 Jul 2022 04:33:56 +0000
Subject: [PATCH] (OpenBSD) Exe Path: Use PWD instead of CWD and use CWD as
fallback
---
src/filesystem/unix/SDL_sysfilesystem.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c
index e25229d6d6a..67f908d2d7c 100644
--- a/src/filesystem/unix/SDL_sysfilesystem.c
+++ b/src/filesystem/unix/SDL_sysfilesystem.c
@@ -150,7 +150,7 @@ SDL_GetBasePath(void)
}
#endif
#if defined(__OPENBSD__)
- /* Please note that this will fail if the process was launched with a relative path and the cwd has changed, or argv is altered. So don't do that. Or add a new sysctl to OpenBSD. */
+ /* Please note that this will fail if the process was launched with a relative path and $PWD + the cwd have changed, or argv is altered. So don't do that. Or add a new sysctl to OpenBSD. */
char **cmdline;
size_t len;
const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
@@ -172,13 +172,28 @@ SDL_GetBasePath(void)
sysctl(mib, 4, cmdline, &len, NULL, 0);
exe = cmdline[0];
+ char *pwddst = NULL;
if (SDL_strchr(exe, '/') == NULL) { /* not a relative or absolute path, check $PATH for it */
exe = search_path_for_binary(cmdline[0]);
+ } else {
+ if (exe && *exe == '.') {
+ const char *pwd = SDL_getenv("PWD");
+ if (pwd && *pwd) {
+ SDL_asprintf(&pwddst, "%s/%s", pwd, exe);
+ }
+ }
}
if (exe) {
- if (realpath(exe, realpathbuf) != NULL) {
- retval = realpathbuf;
+ if (pwddst == NULL) {
+ if (realpath(exe, realpathbuf) != NULL) {
+ retval = realpathbuf;
+ }
+ } else {
+ if (realpath(pwddst, realpathbuf) != NULL) {
+ retval = realpathbuf;
+ }
+ SDL_free(pwddst);
}
if (exe != cmdline[0]) {