about summary refs log tree commit diff
path: root/src/rt
diff options
context:
space:
mode:
authorSébastien Marie <semarie@users.noreply.github.com>2015-01-29 08:19:28 +0100
committerSébastien Marie <semarie@users.noreply.github.com>2015-02-01 14:41:38 +0100
commitfcb30a0b67b1bd4acbc3422ff74fac5d031ae1ae (patch)
tree055fbf1fe9f0b9bd89481f29105fef90370d7789 /src/rt
parentf1f9cb705df95171fce4e575374c959509e58dea (diff)
downloadrust-fcb30a0b67b1bd4acbc3422ff74fac5d031ae1ae.tar.gz
rust-fcb30a0b67b1bd4acbc3422ff74fac5d031ae1ae.zip
openbsd support
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/rust_builtin.c54
1 files changed, 52 insertions, 2 deletions
diff --git a/src/rt/rust_builtin.c b/src/rt/rust_builtin.c
index 03cd3fd6b88..372ff0f2147 100644
--- a/src/rt/rust_builtin.c
+++ b/src/rt/rust_builtin.c
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -47,7 +47,7 @@ extern char **environ;
 #endif
 #endif
 
-#if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__) || defined(__DragonFly__)
+#if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__) || defined(__DragonFly__) || defined(__OpenBSD__)
 extern char **environ;
 #endif
 
@@ -199,6 +199,56 @@ rust_unset_sigprocmask() {
 int *__dfly_error(void) { return __error(); }
 #endif
 
+#if defined(__OpenBSD__)
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <limits.h>
+
+const char * __load_self() {
+    static char *self = NULL;
+
+    if (self == NULL) {
+        int mib[4];
+        char **argv = NULL;
+        size_t argv_len;
+
+        /* initialize mib */
+        mib[0] = CTL_KERN;
+        mib[1] = KERN_PROC_ARGS;
+        mib[2] = getpid();
+        mib[3] = KERN_PROC_ARGV;
+
+        /* request KERN_PROC_ARGV size */
+        argv_len = 0;
+        if (sysctl(mib, 4, NULL, &argv_len, NULL, 0) == -1)
+            return (NULL);
+
+        /* allocate size */
+        if ((argv = malloc(argv_len)) == NULL)
+            return (NULL);
+
+        /* request KERN_PROC_ARGV */
+        if (sysctl(mib, 4, argv, &argv_len, NULL, 0) == -1) {
+            free(argv);
+            return (NULL);
+        }
+
+        /* get realpath if possible */
+        if ((argv[0] != NULL) && ((*argv[0] == '.') || (*argv[0] == '/')
+				|| (strstr(argv[0], "/") != NULL)))
+
+            self = realpath(argv[0], NULL);
+        else
+            self = NULL;
+
+        /* cleanup */
+        free(argv);
+    }
+
+    return (self);
+}
+#endif
+
 //
 // Local Variables:
 // mode: C++