SDL: fs: Use getexecpath() on OpenBSD for SDL_GetBasePath().

From bb100eb5f3293d4a5324a187fc4fb53390671d29 Mon Sep 17 00:00:00 2001
From: Brad Smith <[EMAIL REDACTED]>
Date: Sun, 27 Sep 2026 01:38:17 -0400
Subject: [PATCH] fs: Use getexecpath() on OpenBSD for SDL_GetBasePath().

---
 CMakeLists.txt                          |  1 +
 configure                               |  6 ++++++
 configure.ac                            |  2 +-
 include/SDL_config.h.cmake              |  1 +
 include/SDL_config.h.in                 |  1 +
 src/filesystem/unix/SDL_sysfilesystem.c | 13 ++++++++++++-
 6 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5a2bdc5353932..663199c88be4d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1109,6 +1109,7 @@ if(SDL_LIBC)
       check_symbol_exists("${_FN}" "${STDC_HEADER_NAMES}" ${_HAVEVAR})
     endforeach()
 
+    check_symbol_exists(getexecpath "unistd.h" HAVE_GETEXECPATH)
     check_symbol_exists(sigaction "signal.h" HAVE_SIGACTION)
     check_symbol_exists(sigtimedwait "signal.h" HAVE_SIGTIMEDWAIT)
     check_symbol_exists(setjmp "setjmp.h" HAVE_SETJMP)
diff --git a/configure b/configure
index 62f76d98fd666..eb0a5a875b87d 100755
--- a/configure
+++ b/configure
@@ -19814,6 +19814,12 @@ if test "x$ac_cv_func__Exit" = xyes
 then :
   printf "%s\n" "#define HAVE__EXIT 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "getexecpath" "ac_cv_func_getexecpath"
+if test "x$ac_cv_func_getexecpath" = xyes
+then :
+  printf "%s\n" "#define HAVE_GETEXECPATH 1" >>confdefs.h
+
 fi
 
 
diff --git a/configure.ac b/configure.ac
index 2fb84384fdad3..cf9d591c64f58 100644
--- a/configure.ac
+++ b/configure.ac
@@ -359,7 +359,7 @@ dnl Checks for library functions.
         AC_DEFINE(HAVE_MPROTECT, 1, [ ])
         ],[]),
     )
-    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv bsearch qsort abs bcopy memset memcmp memcpy memmove wcslen wcslcpy wcslcat _wcsdup wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr index rindex strchr strrchr strstr strtok_r itoa _ltoa _ultoa strtod strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp strcasestr vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction sigtimedwait setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll memfd_create posix_fallocate _Exit)
+    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv bsearch qsort abs bcopy memset memcmp memcpy memmove wcslen wcslcpy wcslcat _wcsdup wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr index rindex strchr strrchr strstr strtok_r itoa _ltoa _ultoa strtod strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp strcasestr vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction sigtimedwait setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll memfd_create posix_fallocate _Exit getexecpath)
 
     AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
     AC_CHECK_FUNCS(acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf trunc truncf fmod fmodf log logf log10 log10f lround lroundf pow powf round roundf scalbn scalbnf sin sinf sqrt sqrtf tan tanf)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index dbee3c7bce10d..f79d14e89102d 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -166,6 +166,7 @@
 #cmakedefine HAVE_FLOORF 1
 #cmakedefine HAVE_FMOD 1
 #cmakedefine HAVE_FMODF 1
+#cmakedefine HAVE_GETEXECPATH 1
 #cmakedefine HAVE_LOG 1
 #cmakedefine HAVE_LOGF 1
 #cmakedefine HAVE_LOG10 1
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index d28c696745a8d..f87f7e98f506a 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -214,6 +214,7 @@
 #undef HAVE_MEMFD_CREATE
 #undef HAVE_POSIX_FALLOCATE
 #undef HAVE__EXIT
+#undef HAVE_GETEXECPATH
 
 #else
 
diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c
index 63ba61ca8d670..a755a696022e5 100644
--- a/src/filesystem/unix/SDL_sysfilesystem.c
+++ b/src/filesystem/unix/SDL_sysfilesystem.c
@@ -77,7 +77,7 @@ static char *readSymLink(const char *path)
 #endif
 
 
-#if defined(__OPENBSD__)
+#if defined(__OPENBSD__) && !defined(HAVE_GETEXECPATH)
 static char *search_path_for_binary(const char *bin)
 {
     char *envr = SDL_getenv("PATH");
@@ -145,6 +145,16 @@ char *SDL_GetBasePath(void)
     }
 #endif
 #if defined(__OPENBSD__)
+#ifdef HAVE_GETEXECPATH
+    char fullpath[PATH_MAX];
+    if (getexecpath(fullpath, sizeof(fullpath)) == 0) {
+        retval = SDL_strdup(fullpath);
+        if (!retval) {
+            SDL_OutOfMemory();
+            return NULL;
+        }
+    }
+#else
     /* 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;
@@ -202,6 +212,7 @@ char *SDL_GetBasePath(void)
 
         SDL_free(cmdline);
     }
+#endif
 #endif
 
     /* is a Linux-style /proc filesystem available? */