Platform: detect real path of exe path

This commit is contained in:
李通洲
2024-01-28 09:56:36 +08:00
parent 9aad3e339a
commit 59c1e3d8c5
2 changed files with 21 additions and 7 deletions
+13 -7
View File
@@ -17,17 +17,17 @@
static void getExePath(FFPlatform* platform)
{
FFstrbuf* const exePath = &platform->exePath;
ffStrbufEnsureFree(exePath, PATH_MAX);
char exePath[PATH_MAX + 1];
#ifdef __linux__
ssize_t exePathLen = readlink("/proc/self/exe", exePath->chars, exePath->allocated - 1);
ssize_t exePathLen = readlink("/proc/self/exe", exePath, sizeof(exePath) - 1);
exePath[exePathLen] = '\0';
#elif defined(__APPLE__)
int exePathLen = proc_pidpath((int) getpid(), exePath->chars, exePath->allocated);
int exePathLen = proc_pidpath((int) getpid(), exePath, sizeof(exePath));
#elif defined(__FreeBSD__)
size_t exePathLen = exePath->allocated;
size_t exePathLen = sizeof(exePath);
if(sysctl(
(int[]){CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()}, 4,
exePath->chars, &exePathLen,
exePath, &exePathLen,
NULL, 0
) < 0)
exePathLen = 0;
@@ -35,7 +35,13 @@ static void getExePath(FFPlatform* platform)
exePathLen--; // remove terminating NUL
#endif
if (exePathLen > 0)
exePath->length = (uint32_t) exePathLen;
{
ffStrbufEnsureFree(&platform->exePath, PATH_MAX);
if (realpath(platform->exePath.chars, exePath))
ffStrbufRecalculateLength(&platform->exePath);
else
ffStrbufSetNS(&platform->exePath, (uint32_t) exePathLen, exePath);
}
}
static void platformPathAddEnv(FFlist* dirs, const char* env)
+8
View File
@@ -18,6 +18,14 @@ static void getExePath(FFPlatform* platform)
DWORD exePathWLen = GetModuleFileNameW(NULL, exePathW, MAX_PATH);
if (exePathWLen == 0 && exePathWLen >= MAX_PATH) return;
FF_AUTO_CLOSE_FD HANDLE hPath = CreateFileW(exePathW, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hPath != INVALID_HANDLE_VALUE)
{
DWORD len = GetFinalPathNameByHandleW(hPath, exePathW, MAX_PATH, FILE_NAME_OPENED);
if (len > 0 && len < MAX_PATH)
exePathWLen = len;
}
ffStrbufSetNWS(&platform->exePath, exePathWLen, exePathW);
if (ffStrbufStartsWithS(&platform->exePath, "\\\\?\\"))
ffStrbufSubstrAfter(&platform->exePath, 3);