|
5 | 5 |
|
6 | 6 | #include <unistd.h> |
7 | 7 | #include <pwd.h> |
| 8 | +#include <limits.h> |
8 | 9 | #include <sys/utsname.h> |
9 | 10 |
|
| 11 | +#ifdef __APPLE__ |
| 12 | + #include <libproc.h> |
| 13 | +#elif defined(__FreeBSD__) |
| 14 | + #include <sys/sysctl.h> |
| 15 | +#endif |
| 16 | + |
10 | 17 | static void platformPathAddEnv(FFlist* dirs, const char* env) |
11 | 18 | { |
12 | 19 | const char* envValue = getenv(env); |
@@ -82,7 +89,31 @@ static void getDataDirs(FFPlatform* platform) |
82 | 89 | platformPathAddEnv(&platform->dataDirs, "XDG_DATA_HOME"); |
83 | 90 | ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/"); |
84 | 91 |
|
85 | | - #if defined(__APPLE__) |
| 92 | + // Add ${currentExePath}/../share |
| 93 | + FF_STRBUF_AUTO_DESTROY exePath = ffStrbufCreateA(PATH_MAX); |
| 94 | + #ifdef __linux__ |
| 95 | + ssize_t exePathLen = readlink("/proc/self/exe", exePath.chars, exePath.allocated); |
| 96 | + #elif defined(__APPLE__) |
| 97 | + int exePathLen = proc_pidpath((int) getpid(), exePath.chars, exePath.allocated); |
| 98 | + #elif defined(__FreeBSD__) |
| 99 | + size_t exePathLen = exePath.allocated; |
| 100 | + if(sysctl( |
| 101 | + (int[]){CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()}, 4, |
| 102 | + exePath.chars, &exePathLen, |
| 103 | + NULL, 0 |
| 104 | + )) |
| 105 | + exePathLen = 0; |
| 106 | + #endif |
| 107 | + if (exePathLen > 0) |
| 108 | + { |
| 109 | + exePath.length = (uint32_t) exePathLen; |
| 110 | + ffStrbufSubstrBeforeLastC(&exePath, '/'); |
| 111 | + ffStrbufSubstrBeforeLastC(&exePath, '/'); |
| 112 | + ffStrbufAppendS(&exePath, "/share"); |
| 113 | + ffPlatformPathAddAbsolute(&platform->dataDirs, exePath.chars); |
| 114 | + } |
| 115 | + |
| 116 | + #ifdef __APPLE__ |
86 | 117 | ffPlatformPathAddHome(&platform->dataDirs, platform, "Library/Application Support/"); |
87 | 118 | #endif |
88 | 119 |
|
|
0 commit comments