FreeBSD: fix Get_program_directory() when launched from the path

This commit is contained in:
Thomas Bernard 2020-02-02 21:44:32 +01:00
parent ea7f66de74
commit 0e538b99f6
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -43,6 +43,7 @@
#import <sys/param.h> #import <sys/param.h>
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <sys/param.h> #include <sys/param.h>
#include <unistd.h>
#elif defined(__MINT__) #elif defined(__MINT__)
#include <mint/osbind.h> #include <mint/osbind.h>
#include <mint/sysbind.h> #include <mint/sysbind.h>
@ -114,12 +115,17 @@ char * Get_program_directory(const char * argv0)
getcwd(program_dir, MAX_PATH_CHARACTERS); getcwd(program_dir, MAX_PATH_CHARACTERS);
strcat(program_dir, "/"); strcat(program_dir, "/");
// Linux: argv[0] unreliable // 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]!='/') if (argv0[0]!='/')
{ {
ssize_t path_len; ssize_t path_len;
char path[PATH_MAX]; 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) if (path_len >= 0)
{ {
path[path_len] = '\0'; // add null terminating char path[path_len] = '\0'; // add null terminating char
@ -132,7 +138,7 @@ char * Get_program_directory(const char * argv0)
size_t len; size_t len;
program_dir = NULL; 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); current_dir = Get_current_directory(NULL, NULL, 0);
if (current_dir != NULL) if (current_dir != NULL)
{ {