(OpenBSD) Exe Path: Use PWD instead of CWD and use CWD as fallback

This commit is contained in:
freebsd 2022-07-12 04:33:56 +00:00 committed by Sam Lantinga
parent 883409ea07
commit 24b3efd08d
1 changed files with 18 additions and 3 deletions

View File

@ -150,7 +150,7 @@ SDL_GetBasePath(void)
} }
#endif #endif
#if defined(__OPENBSD__) #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; char **cmdline;
size_t len; size_t len;
const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; const int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
@ -172,14 +172,29 @@ SDL_GetBasePath(void)
sysctl(mib, 4, cmdline, &len, NULL, 0); sysctl(mib, 4, cmdline, &len, NULL, 0);
exe = cmdline[0]; exe = cmdline[0];
char *pwddst = NULL;
if (SDL_strchr(exe, '/') == NULL) { /* not a relative or absolute path, check $PATH for it */ if (SDL_strchr(exe, '/') == NULL) { /* not a relative or absolute path, check $PATH for it */
exe = search_path_for_binary(cmdline[0]); 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 (exe) {
if (pwddst == NULL) {
if (realpath(exe, realpathbuf) != NULL) { if (realpath(exe, realpathbuf) != NULL) {
retval = realpathbuf; retval = realpathbuf;
} }
} else {
if (realpath(pwddst, realpathbuf) != NULL) {
retval = realpathbuf;
}
SDL_free(pwddst);
}
if (exe != cmdline[0]) { if (exe != cmdline[0]) {
SDL_free(exe); SDL_free(exe);