Skip to content

Commit

Permalink
add support for OpenBSD
Browse files Browse the repository at this point in the history
since OpenBSD has no API to get the path that was used to create the
current process, the get_exe_dir() implementation makes use of argv[0]
and the PATH environment variable.
  • Loading branch information
Thomas Lindner committed Oct 1, 2023
1 parent d2631ab commit 2329997
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pool-prj-mgr/pool-prj-mgr-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

int main(int argc, char *argv[])
{
#ifdef __OpenBSD__
extern char *argv0;
argv0 = argv[0];
#endif

#ifdef G_OS_WIN32
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
#endif
Expand Down
46 changes: 46 additions & 0 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "str_util.hpp"
#include "placement.hpp"

#ifdef __OpenBSD__
char *argv0;
#endif

namespace horizon {

std::ifstream make_ifstream(const std::string &filename_utf8, std::ios_base::openmode mode)
Expand Down Expand Up @@ -118,6 +122,48 @@ std::string get_exe_dir()
return "";
}
}
#elif defined(__OpenBSD__)
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
std::string get_exe_dir()
{
char *path, *p, *p2, buf[PATH_MAX];
struct stat sb;
if (strchr(argv0, '/')) {
if (realpath(argv0, buf)) {
return Glib::path_get_dirname(buf);
}
}
else {
if (!(path = getenv("PATH"))) {
path = strdup("/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin");
}
else {
path = strdup(path);
}
for (p = path; (p2 = strsep(&p, ":")) != NULL;) {
if (*p2 == '\0') {
getcwd(buf, sizeof(buf));
}
else {
strlcpy(buf, p2, sizeof(buf));
}
strlcat(buf, "/", sizeof(buf));
strlcat(buf, argv0, sizeof(buf));
if (!stat(buf, &sb) && (sb.st_mode & S_IXUSR)) {
free(path);
return Glib::path_get_dirname(buf);
}
}
free(path);
}
throw std::runtime_error("can't find executable");
return "";
}
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
std::string get_exe_dir()
Expand Down

0 comments on commit 2329997

Please sign in to comment.