From c2c6ee8a6bb098332bce72be6985c1da00435ed0 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 4 Mar 2019 12:40:28 +1100 Subject: [PATCH] finish openbsd support --- src/Platform.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Platform.c b/src/Platform.c index f014d803b..b43fb615d 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -90,6 +90,7 @@ const ReturnCode ReturnCode_SocketWouldBlock = EWOULDBLOCK; #include #elif defined CC_BUILD_OPENBSD #define CC_BUILD_UNIX +#include #elif defined CC_BUILD_SOLARIS #define CC_BUILD_UNIX #include @@ -1945,6 +1946,37 @@ ReturnCode Platform_GetExePath(String* path) { return 0; } #endif +#ifdef CC_BUILD_OPENBSD +ReturnCode Platform_GetExePath(String* path) { + char tmp[600]; + int mib[4]; + size_t size; + char* argv[100]; + char* str; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC_ARGS; + mib[2] = getpid(); + mib[3] = KERN_PROC_ARGV; + + /* NOTE: OpenBSD doesn't seem to let us get executable's location, so fallback to argv[0] */ + /* See OpenBSD sysctl manpage for why argv array is so large */ + /*... The buffer pointed to by oldp is filled with an array of char pointers followed by the strings themselves... */ + size = 100 * sizeof(char*); + if (sysctl(mib, 4, argv, &size, NULL, 0) == -1) return errno; + + str = argv[0]; + if (str[0] != '/') { + /* relative path */ + if (!realpath(str, tmp)) return errno; + str = tmp; + } + + size = String_CalcLen(str, 600); + Convert_DecodeUtf8(path, str, size); + return 0; +} +#endif #ifdef CC_BUILD_SOLARIS ReturnCode Platform_GetExePath(String* path) { char str[600];