From 14c17c90cd10523290f17a3f98801d47cb308ca5 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Tue, 4 May 2021 17:23:38 -0300 Subject: [PATCH] Better method of getting exe paths in Linux and FreeBSD --- src/d_protocol.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index ee9e91021..f9330aabe 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -43,24 +43,16 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch static char *GetExePath(void) { static char buf[PATH_MAX]; -#if defined (__unix__) || defined (UNIXCOMMON) - static char *p = NULL; - FILE *fp = NULL; - - if (!(fp = fopen("/proc/self/maps", "r"))) - return NULL; - - while (fgets(buf, PATH_MAX, fp)) - fclose(fp); - - *(p = strchr(buf, '\n')) = '\0'; - while (*p != ' ') - p--; - return p+1; +#if defined (__linux__) + if (readlink("/proc/self/exe", buf, PATH_MAX) < 0) + I_Error("PROTOCOL: Error reading /proc/"); +#elif defined (__FreeBSD__) + if (readlink("/proc/curproc/file", buf, PATH_MAX) < 0) + I_Error("PROTOCOL: Error reading /proc/"); #elif defined (__WIN32) GetModuleFileName(NULL, buf, PATH_MAX); +#endif return buf; - #endif } static void RegisterProtocols(const char *path)