Platform (Unix): add exePath

This commit is contained in:
李通洲
2023-08-24 14:20:48 +08:00
parent 5e9166847d
commit b2ead8735b
+28 -20
View File
@@ -14,6 +14,27 @@
#include <sys/sysctl.h>
#endif
static void getExePath(FFPlatform* platform)
{
FFstrbuf* const exePath = &platform->exePath;
ffStrbufEnsureFree(exePath, PATH_MAX);
#ifdef __linux__
ssize_t exePathLen = readlink("/proc/self/exe", exePath->chars, exePath->allocated);
#elif defined(__APPLE__)
int exePathLen = proc_pidpath((int) getpid(), exePath->chars, exePath->allocated);
#elif defined(__FreeBSD__)
size_t exePathLen = exePath->allocated;
if(sysctl(
(int[]){CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()}, 4,
exePath->chars, &exePathLen,
NULL, 0
))
exePathLen = 0;
#endif
if (exePathLen > 0)
exePath->length = (uint32_t) exePathLen;
}
static void platformPathAddEnv(FFlist* dirs, const char* env)
{
const char* envValue = getenv(env);
@@ -90,27 +111,13 @@ static void getDataDirs(FFPlatform* platform)
ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/");
// Add ${currentExePath}/../share
FF_STRBUF_AUTO_DESTROY exePath = ffStrbufCreateA(PATH_MAX);
#ifdef __linux__
ssize_t exePathLen = readlink("/proc/self/exe", exePath.chars, exePath.allocated);
#elif defined(__APPLE__)
int exePathLen = proc_pidpath((int) getpid(), exePath.chars, exePath.allocated);
#elif defined(__FreeBSD__)
size_t exePathLen = exePath.allocated;
if(sysctl(
(int[]){CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()}, 4,
exePath.chars, &exePathLen,
NULL, 0
))
exePathLen = 0;
#endif
if (exePathLen > 0)
if (platform->exePath.length > 0)
{
exePath.length = (uint32_t) exePathLen;
ffStrbufSubstrBeforeLastC(&exePath, '/');
ffStrbufSubstrBeforeLastC(&exePath, '/');
ffStrbufAppendS(&exePath, "/share");
ffPlatformPathAddAbsolute(&platform->dataDirs, exePath.chars);
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateCopy(&platform->exePath);
ffStrbufSubstrBeforeLastC(&path, '/');
ffStrbufSubstrBeforeLastC(&path, '/');
ffStrbufAppendS(&path, "/share");
ffPlatformPathAddAbsolute(&platform->dataDirs, path.chars);
}
#ifdef __APPLE__
@@ -183,6 +190,7 @@ void ffPlatformInitImpl(FFPlatform* platform)
if(uname(&uts) != 0)
memset(&uts, 0, sizeof(uts));
getExePath(platform);
getHomeDir(platform, pwd);
getCacheDir(platform);
getConfigDirs(platform);