]> 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 5dc07f859aed02817cd3457a6a62be973de6e9dd..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.
 //
 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
 #include "valgrind/valgrind.h"
 
-#ifdef __ANDROID__
-time_t
-timegm(struct tm *tm)
-{
-    time_t ret;
-    char *tz;
-
-    tz = getenv("TZ");
-    if (tz)
-        tz = strdup(tz);
-    setenv("TZ", "", 1);
-    tzset();
-    ret = mktime(tm);
-    if (tz) {
-        setenv("TZ", tz, 1);
-        free(tz);
-    } else
-        unsetenv("TZ");
-    tzset();
-    return ret;
-}
-#endif
-
 #ifdef __APPLE__
 #if (TARGET_OS_IPHONE)
 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
 
@@ -100,121 +78,6 @@ rust_list_dir_val(struct dirent* entry_ptr) {
 }
 #endif
 
-typedef struct {
-    int32_t tm_sec;
-    int32_t tm_min;
-    int32_t tm_hour;
-    int32_t tm_mday;
-    int32_t tm_mon;
-    int32_t tm_year;
-    int32_t tm_wday;
-    int32_t tm_yday;
-    int32_t tm_isdst;
-    int32_t tm_gmtoff;
-    int32_t tm_nsec;
-} rust_tm;
-
-void rust_tm_to_tm(rust_tm* in_tm, struct tm* out_tm) {
-    memset(out_tm, 0, sizeof(struct tm));
-    out_tm->tm_sec = in_tm->tm_sec;
-    out_tm->tm_min = in_tm->tm_min;
-    out_tm->tm_hour = in_tm->tm_hour;
-    out_tm->tm_mday = in_tm->tm_mday;
-    out_tm->tm_mon = in_tm->tm_mon;
-    out_tm->tm_year = in_tm->tm_year;
-    out_tm->tm_wday = in_tm->tm_wday;
-    out_tm->tm_yday = in_tm->tm_yday;
-    out_tm->tm_isdst = in_tm->tm_isdst;
-}
-
-void tm_to_rust_tm(struct tm* in_tm,
-                   rust_tm* out_tm,
-                   int32_t gmtoff,
-                   int32_t nsec) {
-    out_tm->tm_sec = in_tm->tm_sec;
-    out_tm->tm_min = in_tm->tm_min;
-    out_tm->tm_hour = in_tm->tm_hour;
-    out_tm->tm_mday = in_tm->tm_mday;
-    out_tm->tm_mon = in_tm->tm_mon;
-    out_tm->tm_year = in_tm->tm_year;
-    out_tm->tm_wday = in_tm->tm_wday;
-    out_tm->tm_yday = in_tm->tm_yday;
-    out_tm->tm_isdst = in_tm->tm_isdst;
-    out_tm->tm_gmtoff = gmtoff;
-    out_tm->tm_nsec = nsec;
-}
-
-#if defined(__WIN32__)
-#define TZSET() _tzset()
-#if defined(_MSC_VER) && (_MSC_VER >= 1400)
-#define GMTIME(clock, result) gmtime_s((result), (clock))
-#define LOCALTIME(clock, result) localtime_s((result), (clock))
-#define TIMEGM(result) _mkgmtime64(result)
-#else
-struct tm* GMTIME(const time_t *clock, struct tm *result) {
-    struct tm* t = gmtime(clock);
-    if (t == NULL || result == NULL) { return NULL; }
-    *result = *t;
-    return result;
-}
-struct tm* LOCALTIME(const time_t *clock, struct tm *result) {
-    struct tm* t = localtime(clock);
-    if (t == NULL || result == NULL) { return NULL; }
-    *result = *t;
-    return result;
-}
-#define TIMEGM(result) mktime((result)) - _timezone
-#endif
-#else
-#define TZSET() tzset()
-#define GMTIME(clock, result) gmtime_r((clock), (result))
-#define LOCALTIME(clock, result) localtime_r((clock), (result))
-#define TIMEGM(result) timegm(result)
-#endif
-
-void
-rust_tzset() {
-    TZSET();
-}
-
-void
-rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
-    struct tm tm;
-    time_t s = sec;
-    GMTIME(&s, &tm);
-
-    tm_to_rust_tm(&tm, timeptr, 0, nsec);
-}
-
-void
-rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
-    struct tm tm;
-    time_t s = sec;
-    LOCALTIME(&s, &tm);
-
-#if defined(__WIN32__)
-    int32_t gmtoff = -timezone;
-#else
-    int32_t gmtoff = tm.tm_gmtoff;
-#endif
-
-    tm_to_rust_tm(&tm, timeptr, gmtoff, nsec);
-}
-
-int64_t
-rust_timegm(rust_tm* timeptr) {
-    struct tm t;
-    rust_tm_to_tm(timeptr, &t);
-    return TIMEGM(&t);
-}
-
-int64_t
-rust_mktime(rust_tm* timeptr) {
-    struct tm t;
-    rust_tm_to_tm(timeptr, &t);
-    return mktime(&t);
-}
-
 #ifndef _WIN32
 
 DIR*
@@ -337,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++