$OpenBSD: patch-Core_SystemToolbox_cpp,v 1.2 2017/08/09 17:02:35 jasper Exp $

Implement GetPathToExecutableInternal()

Index: Core/SystemToolbox.cpp
--- Core/SystemToolbox.cpp.orig
+++ Core/SystemToolbox.cpp
@@ -55,12 +55,16 @@
 #endif
 
 
-#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || \
+	defined(__OpenBSD__)
 #  include <limits.h>      /* PATH_MAX */
 #  include <signal.h>
 #  include <unistd.h>
 #endif
 
+#if defined(__OpenBSD__)
+#  include <sys/sysctl.h>
+#endif
 
 // Inclusions for UUID
 // http://stackoverflow.com/a/1626302
@@ -157,7 +161,8 @@ namespace Orthanc
   {
 #if defined(_WIN32)
     ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
-#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__native_client__)
+#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || \
+  defined(__FreeBSD__) || defined(__native_client__) || defined(__OpenBSD__)
     usleep(microSeconds);
 #else
 #error Support your platform here
@@ -370,6 +375,26 @@ namespace Orthanc
     return std::string(pathbuf);
   }
 
+#elif defined(__OpenBSD__)
+  static std::string GetPathToExecutableInternal()
+  {
+    size_t len;
+    char **args;
+    int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
+
+    if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) {
+      throw OrthancException(ErrorCode_PathToExecutable);
+    }
+
+    args = (char **)malloc(len);
+
+    if (sysctl(mib, 4, args, &len, NULL, 0) == -1) {
+      free(args);
+      throw OrthancException(ErrorCode_PathToExecutable);
+    }
+
+    return std::string(args[0]);
+  }
 #else
 #error Support your platform here
 #endif
