filesystem: Added SDL_GetExeName().

core/unix had a more-limited copy of filesystem/unix's implementation, called
SDL_GetExeName(). Replace that with a real implementation in filesystem, and
allow each platform to implement it as appropriate.

Implemented for Unix and Windows; most implementations are currently FIXME
stubs at the moment.

Reference Issue #15692.

(cherry picked from commit 7d29ce8e31)
This commit is contained in:
Ryan C. Gordon
2026-05-26 15:23:31 -04:00
parent 597694e4a1
commit 2621d7aab2
23 changed files with 185 additions and 100 deletions

View File

@@ -54,6 +54,7 @@ typedef struct FcitxClient
static FcitxClient fcitx_client;
// !!! FIXME: should this just be dumped for src/core/unix's SDL_GetAppID()?
static const char *GetAppName(void)
{
const char *exe_name = SDL_GetExeName();

View File

@@ -24,39 +24,6 @@
#include "SDL_appid.h"
#include <unistd.h>
const char *SDL_GetExeName(void)
{
static const char *proc_name = NULL;
// TODO: Use a fallback if BSD has no mounted procfs (OpenBSD has no procfs at all)
if (!proc_name) {
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) || defined (SDL_PLATFORM_NETBSD) || defined(SDL_PLATFORM_HURD)
static char linkfile[1024];
int linksize;
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_HURD)
const char *proc_path = "/proc/self/exe";
#elif defined(SDL_PLATFORM_FREEBSD)
const char *proc_path = "/proc/curproc/file";
#elif defined(SDL_PLATFORM_NETBSD)
const char *proc_path = "/proc/curproc/exe";
#endif
linksize = readlink(proc_path, linkfile, sizeof(linkfile) - 1);
if (linksize > 0) {
linkfile[linksize] = '\0';
proc_name = SDL_strrchr(linkfile, '/');
if (proc_name) {
++proc_name;
} else {
proc_name = linkfile;
}
}
#endif
}
return proc_name;
}
const char *SDL_GetAppID(void)
{
const char *id_str = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
@@ -73,3 +40,4 @@ const char *SDL_GetAppID(void)
return id_str;
}

View File

@@ -24,7 +24,6 @@ freely, subject to the following restrictions:
#ifndef SDL_appid_h_
#define SDL_appid_h_
extern const char *SDL_GetExeName(void);
extern const char *SDL_GetAppID(void);
#endif // SDL_appid_h_

View File

@@ -730,4 +730,42 @@ const char *WIN_CheckDefaultArgcArgv(int *pargc, char ***pargv, void **pallocate
return NULL; // no error string.
}
char *WIN_GetModulePath(HMODULE handle)
{
DWORD buflen = 128;
WCHAR *path = NULL;
DWORD len = 0;
while (true) {
void *ptr = SDL_realloc(path, buflen * sizeof(WCHAR));
if (!ptr) {
SDL_free(path);
return NULL;
}
path = (WCHAR *)ptr;
len = GetModuleFileNameW(handle, path, buflen);
// if it truncated, then len >= buflen - 1
// if there was enough room (or failure), len < buflen - 1
if (len < (buflen - 1)) {
break;
}
// buffer too small? Try again.
buflen *= 2;
}
char *retval = NULL;
if (len == 0) {
WIN_SetError("Couldn't locate module");
} else {
retval = WIN_StringToUTF8W(path);
}
SDL_free(path);
return retval;
}
#endif // defined(SDL_PLATFORM_WINDOWS)

View File

@@ -213,7 +213,10 @@ extern SDL_AudioFormat SDL_WaveFormatExToSDLFormat(WAVEFORMATEX *waveformat);
extern int WIN_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWCH lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCCH lpDefaultChar, LPBOOL lpUsedDefaultChar);
// parse out command lines from OS if argv is NULL, otherwise pass through unchanged. `*pallocated` must be HeapFree'd by caller if not NULL on successful return. Returns NULL on success, error string on problems.
const char *WIN_CheckDefaultArgcArgv(int *pargc, char ***pargv, void **pallocated);
extern const char *WIN_CheckDefaultArgcArgv(int *pargc, char ***pargv, void **pallocated);
// Does all the win32 tapdancing to make GetModuleFileName work. Returns a SDL_malloc'd UTF-8 string, or NULL on failure.
extern char *WIN_GetModulePath(HMODULE handle);
// Ends C function definitions when using C++
#ifdef __cplusplus