From 0e538b99f67980cf36397061f624dfb87accf7ad Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sun, 2 Feb 2020 21:44:32 +0100 Subject: [PATCH] FreeBSD: fix Get_program_directory() when launched from the path --- src/setup.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/setup.c b/src/setup.c index 72af034a..f29efbe4 100644 --- a/src/setup.c +++ b/src/setup.c @@ -43,6 +43,7 @@ #import #elif defined(__FreeBSD__) #include + #include #elif defined(__MINT__) #include #include @@ -114,12 +115,17 @@ char * Get_program_directory(const char * argv0) getcwd(program_dir, MAX_PATH_CHARACTERS); strcat(program_dir, "/"); // Linux: argv[0] unreliable - #elif defined(__linux__) + #elif defined(__linux__) || defined(__FreeBSD__) + #if defined(__linux__) + #define SELF_PATH "/proc/self/exe" + #elif defined(__FreeBSD__) + #define SELF_PATH "/proc/curproc/file" + #endif if (argv0[0]!='/') { ssize_t path_len; char path[PATH_MAX]; - path_len = readlink("/proc/self/exe", path, sizeof(path)); + path_len = readlink(SELF_PATH, path, sizeof(path)); if (path_len >= 0) { path[path_len] = '\0'; // add null terminating char @@ -132,7 +138,7 @@ char * Get_program_directory(const char * argv0) size_t len; program_dir = NULL; - GFX2_Log(GFX2_WARNING, "readlink(%s) failed : %s\n", "/proc/self/exe", strerror(errno)); + GFX2_Log(GFX2_WARNING, "readlink(%s) failed : %s\n", SELF_PATH, strerror(errno)); current_dir = Get_current_directory(NULL, NULL, 0); if (current_dir != NULL) {