mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Platform (Unix): add exePath
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user