|
50 | 50 | #include "utils/fs.h" |
51 | 51 | #include "utils/string.h" |
52 | 52 |
|
| 53 | +// for get_exec_path |
| 54 | +#ifdef __APPLE__ |
| 55 | +#include <mach-o/dyld.h> //_NSGetExecutablePath |
| 56 | +#include <unistd.h> |
| 57 | +#elif defined __FreeBSD__ |
| 58 | +#include <sys/sysctl.h> |
| 59 | +#include <sys/types.h> |
| 60 | +#elif !defined(_WIN32) && !defined(__linux__) && !defined(__DragonFly__) && \ |
| 61 | + !defined(__NetBSD__) |
| 62 | +#include <unistd.h> // for getcwd |
| 63 | +#include "host.h" // for uv_argv |
| 64 | +#endif |
| 65 | + |
| 66 | + |
53 | 67 | /** |
54 | 68 | * Returns temporary path ending with path delimiter ('/' or '\' in Windows) |
55 | 69 | */ |
@@ -79,48 +93,35 @@ const char *get_temp_dir(void) |
79 | 93 | return temp_dir; |
80 | 94 | } |
81 | 95 |
|
82 | | -// see also <https://stackoverflow.com/a/1024937> |
| 96 | +/** |
| 97 | + * see also <https://stackoverflow.com/a/1024937> |
| 98 | + * @param path buffer with size MAX_PATH_SIZE where function stores path to executable |
| 99 | + * @return 1 - SUCCESS, 0 - ERROR |
| 100 | + */ |
| 101 | +static int |
| 102 | +get_exec_path(char *path) |
| 103 | +{ |
83 | 104 | #ifdef _WIN32 |
84 | | -int get_exec_path(char* path) { |
85 | 105 | return GetModuleFileNameA(NULL, path, MAX_PATH_SIZE) != 0; |
86 | | -} |
87 | 106 | #elif defined __linux__ |
88 | | -int get_exec_path(char* path) { |
89 | 107 | return realpath("/proc/self/exe", path) != NULL; |
90 | | -} |
91 | 108 | #elif defined __DragonFly__ |
92 | | -int get_exec_path(char* path) { |
93 | 109 | return realpath("/proc/curproc/file", path) != NULL; |
94 | | -} |
95 | 110 | #elif defined __NetBSD__ |
96 | | -int get_exec_path(char* path) { |
97 | 111 | return realpath("/proc/curproc/exe", path) != NULL; |
98 | | -} |
99 | 112 | #elif defined __APPLE__ |
100 | | -#include <mach-o/dyld.h> //_NSGetExecutablePath |
101 | | -#include <unistd.h> |
102 | | - |
103 | | -int get_exec_path(char* path) { |
104 | 113 | char raw_path_name[MAX_PATH_SIZE]; |
105 | 114 | uint32_t raw_path_size = (uint32_t)(sizeof(raw_path_name)); |
106 | 115 |
|
107 | 116 | if (_NSGetExecutablePath(raw_path_name, &raw_path_size) != 0) { |
108 | 117 | return false; |
109 | 118 | } |
110 | 119 | return realpath(raw_path_name, path) != NULL; |
111 | | -} |
112 | 120 | #elif defined __FreeBSD__ |
113 | | -#include <sys/sysctl.h> |
114 | | -#include <sys/types.h> |
115 | | -int get_exec_path(char* path) { |
116 | 121 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; |
117 | 122 | size_t cb = MAX_PATH_SIZE; |
118 | 123 | return sysctl(mib, sizeof mib / sizeof mib[0], path, &cb, NULL, 0); |
119 | | -} |
120 | 124 | #else |
121 | | -#include <unistd.h> // for getcwd |
122 | | -#include "host.h" // for uv_argv |
123 | | -int get_exec_path(char* path) { |
124 | 125 | if (uv_argv[0][0] == '/') { // with absolute path |
125 | 126 | if (snprintf(path, MAX_PATH_SIZE, "%s", uv_argv[0]) == |
126 | 127 | MAX_PATH_SIZE) { |
@@ -157,7 +158,8 @@ int get_exec_path(char* path) { |
157 | 158 | path[strlen(path) - 1] = '\0'; |
158 | 159 | return 0; |
159 | 160 | } |
160 | | -#endif |
| 161 | +#endif |
| 162 | +} |
161 | 163 |
|
162 | 164 | /** |
163 | 165 | * @returns installation root without trailing '/', eg. installation prefix on |
|
0 commit comments