]> git.lizzy.rs Git - rust.git/blobdiff - src/rt/rust_builtin.c
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.git] / src / rt / rust_builtin.c
index 03cd3fd6b88ea45fcec630a09da75ff76fe4fc31..b756602ead4f43286408ad769742f79e4fc32934 100644 (file)
@@ -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,8 @@ 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 +200,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 * rust_current_exe() {
+    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++