Skip to content

Commit f806f01

Browse files
committed
fs: get_exec_path: static + consolidate
1 parent d1667f8 commit f806f01

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

src/utils/fs.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@
5050
#include "utils/fs.h"
5151
#include "utils/string.h"
5252

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+
5367
/**
5468
* Returns temporary path ending with path delimiter ('/' or '\' in Windows)
5569
*/
@@ -79,48 +93,35 @@ const char *get_temp_dir(void)
7993
return temp_dir;
8094
}
8195

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+
{
83104
#ifdef _WIN32
84-
int get_exec_path(char* path) {
85105
return GetModuleFileNameA(NULL, path, MAX_PATH_SIZE) != 0;
86-
}
87106
#elif defined __linux__
88-
int get_exec_path(char* path) {
89107
return realpath("/proc/self/exe", path) != NULL;
90-
}
91108
#elif defined __DragonFly__
92-
int get_exec_path(char* path) {
93109
return realpath("/proc/curproc/file", path) != NULL;
94-
}
95110
#elif defined __NetBSD__
96-
int get_exec_path(char* path) {
97111
return realpath("/proc/curproc/exe", path) != NULL;
98-
}
99112
#elif defined __APPLE__
100-
#include <mach-o/dyld.h> //_NSGetExecutablePath
101-
#include <unistd.h>
102-
103-
int get_exec_path(char* path) {
104113
char raw_path_name[MAX_PATH_SIZE];
105114
uint32_t raw_path_size = (uint32_t)(sizeof(raw_path_name));
106115

107116
if (_NSGetExecutablePath(raw_path_name, &raw_path_size) != 0) {
108117
return false;
109118
}
110119
return realpath(raw_path_name, path) != NULL;
111-
}
112120
#elif defined __FreeBSD__
113-
#include <sys/sysctl.h>
114-
#include <sys/types.h>
115-
int get_exec_path(char* path) {
116121
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
117122
size_t cb = MAX_PATH_SIZE;
118123
return sysctl(mib, sizeof mib / sizeof mib[0], path, &cb, NULL, 0);
119-
}
120124
#else
121-
#include <unistd.h> // for getcwd
122-
#include "host.h" // for uv_argv
123-
int get_exec_path(char* path) {
124125
if (uv_argv[0][0] == '/') { // with absolute path
125126
if (snprintf(path, MAX_PATH_SIZE, "%s", uv_argv[0]) ==
126127
MAX_PATH_SIZE) {
@@ -157,7 +158,8 @@ int get_exec_path(char* path) {
157158
path[strlen(path) - 1] = '\0';
158159
return 0;
159160
}
160-
#endif
161+
#endif
162+
}
161163

162164
/**
163165
* @returns installation root without trailing '/', eg. installation prefix on

src/utils/fs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ extern "C" {
5959
#include <stdio.h>
6060
#endif
6161

62-
/**
63-
* @param path buffer with size MAX_PATH_SIZE where function stores path to executable
64-
* @return 1 - SUCCESS, 0 - ERROR
65-
*/
66-
int get_exec_path(char* path);
6762
const char *get_temp_dir(void);
6863
FILE *get_temp_file(const char **filename);
6964
const char *get_install_root(void);

0 commit comments

Comments
 (0)