Skip to content

Commit faf9559

Browse files
committed
get_exec_path: return bool + fixes
return true fix FreeBSD and generic variant - the logic was inversed to the definition, returning 0 on success and !0 otherwise
1 parent f806f01 commit faf9559

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/utils/fs.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ const char *get_temp_dir(void)
9696
/**
9797
* see also <https://stackoverflow.com/a/1024937>
9898
* @param path buffer with size MAX_PATH_SIZE where function stores path to executable
99-
* @return 1 - SUCCESS, 0 - ERROR
10099
*/
101-
static int
100+
static bool
102101
get_exec_path(char *path)
103102
{
104103
#ifdef _WIN32
@@ -120,43 +119,43 @@ get_exec_path(char *path)
120119
#elif defined __FreeBSD__
121120
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
122121
size_t cb = MAX_PATH_SIZE;
123-
return sysctl(mib, sizeof mib / sizeof mib[0], path, &cb, NULL, 0);
122+
return sysctl(mib, sizeof mib / sizeof mib[0], path, &cb, NULL, 0) == 0;
124123
#else
125124
if (uv_argv[0][0] == '/') { // with absolute path
126125
if (snprintf(path, MAX_PATH_SIZE, "%s", uv_argv[0]) ==
127126
MAX_PATH_SIZE) {
128-
return -1; // truncated
127+
return false; // truncated
129128
}
130-
return 0;
129+
return true;
131130
}
132131
if (strchr(uv_argv[0], '/') != NULL) { // or with relative path
133132
char cwd[MAX_PATH_SIZE];
134133
if (getcwd(cwd, sizeof cwd) != cwd) {
135-
return -1;
134+
return false;
136135
}
137136
if (snprintf(path, MAX_PATH_SIZE, "%s/%s", cwd, uv_argv[0]) ==
138137
MAX_PATH_SIZE) {
139-
return -1; // truncated
138+
return false; // truncated
140139
}
141-
return 0;
140+
return true;
142141
}
143142
// else launched from PATH
144143
char args[1024];
145144
snprintf(args, sizeof args, "command -v %s", uv_argv[0]);
146145
FILE *f = popen(args, "r");
147146
if (f == NULL) {
148-
return -1;
147+
return false;
149148
}
150149
if (fgets(path, MAX_PATH_SIZE, f) == NULL) {
151150
fclose(f);
152-
return -1;
151+
return false;
153152
}
154153
fclose(f);
155154
if (strlen(path) == 0 || path[strlen(path) - 1] != '\n') {
156-
return -1; // truncated (?)
155+
return false; // truncated (?)
157156
}
158157
path[strlen(path) - 1] = '\0';
159-
return 0;
158+
return true;
160159
}
161160
#endif
162161
}

0 commit comments

Comments
 (0)