From 28c01599d571f52b7eb5b78cb2bf0ad5d3428efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 24 Aug 2023 10:49:59 +0800 Subject: [PATCH] Platform: add `$EXE_PATH/../share` to `dataDir` Support loading presets when fastfetch is installed in a special path. eg. `/opt/homebrew/bin/` --- .../terminalshell/terminalshell_linux.c | 2 +- src/util/platform/FFPlatform_unix.c | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index fb2f7d755..58e29f1f8 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -48,7 +48,7 @@ static void getProcessInformation(pid_t pid, FFstrbuf* processName, FFstrbuf* ex if(length > 0) exe->length = (uint32_t)length; - #else + #elif defined(__FreeBSD__) size_t size = exe->allocated; if(!sysctl( diff --git a/src/util/platform/FFPlatform_unix.c b/src/util/platform/FFPlatform_unix.c index a6e5d1b13..2fa403ca6 100644 --- a/src/util/platform/FFPlatform_unix.c +++ b/src/util/platform/FFPlatform_unix.c @@ -5,8 +5,15 @@ #include #include +#include #include +#ifdef __APPLE__ + #include +#elif defined(__FreeBSD__) + #include +#endif + static void platformPathAddEnv(FFlist* dirs, const char* env) { const char* envValue = getenv(env); @@ -82,7 +89,31 @@ static void getDataDirs(FFPlatform* platform) platformPathAddEnv(&platform->dataDirs, "XDG_DATA_HOME"); ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/"); - #if defined(__APPLE__) + // 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) + { + exePath.length = (uint32_t) exePathLen; + ffStrbufSubstrBeforeLastC(&exePath, '/'); + ffStrbufSubstrBeforeLastC(&exePath, '/'); + ffStrbufAppendS(&exePath, "/share"); + ffPlatformPathAddAbsolute(&platform->dataDirs, exePath.chars); + } + + #ifdef __APPLE__ ffPlatformPathAddHome(&platform->dataDirs, platform, "Library/Application Support/"); #endif