aboutsummaryrefslogtreecommitdiff
path: root/graphics/grafx2/files/patch-setup.c
blob: 8389a2b590de50ae0bf3e906e42c9d43b27f8545 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
--- setup.c.orig	2020-02-01 17:05:20 UTC
+++ setup.c
@@ -43,6 +43,7 @@
   #import <sys/param.h>
 #elif defined(__FreeBSD__)
   #include <sys/param.h>
+  #include <unistd.h>
 #elif defined(__MINT__)
     #include <mint/osbind.h>
     #include <mint/sysbind.h>
@@ -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)
       {
@@ -157,6 +163,11 @@ char * Get_program_directory(const char * argv0)
   #else
   program_dir = Extract_path(NULL, argv0);
   #endif
+  if (program_dir == NULL)
+  {
+    GFX2_Log(GFX2_WARNING, "Failed to detect program directory, using current directory\n");
+    program_dir = strdup("." PATH_SEPARATOR);
+  }
   return program_dir;
 }