1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
17 #if !defined(__WIN32__)
19 #include <sys/types.h>
32 #include <TargetConditionals.h>
33 #include <mach/mach_time.h>
35 #if !(TARGET_OS_IPHONE)
36 #include <crt_externs.h>
40 /* Foreign builtins. */
41 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
42 #include "valgrind/valgrind.h"
68 #if (TARGET_OS_IPHONE)
69 extern char **environ;
73 #if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__) || defined(__DragonFly__)
74 extern char **environ;
77 #if defined(__WIN32__)
85 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
86 char **environ = *_NSGetEnviron();
93 #if defined(__WIN32__)
94 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
95 return entry_ptr->cFileName;
98 rust_list_dir_val(struct dirent* entry_ptr) {
99 return entry_ptr->d_name;
117 void rust_tm_to_tm(rust_tm* in_tm, struct tm* out_tm) {
118 memset(out_tm, 0, sizeof(struct tm));
119 out_tm->tm_sec = in_tm->tm_sec;
120 out_tm->tm_min = in_tm->tm_min;
121 out_tm->tm_hour = in_tm->tm_hour;
122 out_tm->tm_mday = in_tm->tm_mday;
123 out_tm->tm_mon = in_tm->tm_mon;
124 out_tm->tm_year = in_tm->tm_year;
125 out_tm->tm_wday = in_tm->tm_wday;
126 out_tm->tm_yday = in_tm->tm_yday;
127 out_tm->tm_isdst = in_tm->tm_isdst;
130 void tm_to_rust_tm(struct tm* in_tm,
134 out_tm->tm_sec = in_tm->tm_sec;
135 out_tm->tm_min = in_tm->tm_min;
136 out_tm->tm_hour = in_tm->tm_hour;
137 out_tm->tm_mday = in_tm->tm_mday;
138 out_tm->tm_mon = in_tm->tm_mon;
139 out_tm->tm_year = in_tm->tm_year;
140 out_tm->tm_wday = in_tm->tm_wday;
141 out_tm->tm_yday = in_tm->tm_yday;
142 out_tm->tm_isdst = in_tm->tm_isdst;
143 out_tm->tm_gmtoff = gmtoff;
144 out_tm->tm_nsec = nsec;
147 #if defined(__WIN32__)
148 #define TZSET() _tzset()
149 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
150 #define GMTIME(clock, result) gmtime_s((result), (clock))
151 #define LOCALTIME(clock, result) localtime_s((result), (clock))
152 #define TIMEGM(result) _mkgmtime64(result)
154 struct tm* GMTIME(const time_t *clock, struct tm *result) {
155 struct tm* t = gmtime(clock);
156 if (t == NULL || result == NULL) { return NULL; }
160 struct tm* LOCALTIME(const time_t *clock, struct tm *result) {
161 struct tm* t = localtime(clock);
162 if (t == NULL || result == NULL) { return NULL; }
166 #define TIMEGM(result) mktime((result)) - _timezone
169 #define TZSET() tzset()
170 #define GMTIME(clock, result) gmtime_r((clock), (result))
171 #define LOCALTIME(clock, result) localtime_r((clock), (result))
172 #define TIMEGM(result) timegm(result)
181 rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
186 tm_to_rust_tm(&tm, timeptr, 0, nsec);
190 rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
195 #if defined(__WIN32__)
196 int32_t gmtoff = -timezone;
198 int32_t gmtoff = tm.tm_gmtoff;
201 tm_to_rust_tm(&tm, timeptr, gmtoff, nsec);
205 rust_timegm(rust_tm* timeptr) {
207 rust_tm_to_tm(timeptr, &t);
212 rust_mktime(rust_tm* timeptr) {
214 rust_tm_to_tm(timeptr, &t);
221 rust_opendir(char *dirname) {
222 return opendir(dirname);
226 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
227 return readdir_r(dirp, entry, result);
231 rust_dirent_t_size() {
232 return sizeof(struct dirent);
246 rust_dirent_t_size() {
252 rust_running_on_valgrind() {
253 return RUNNING_ON_VALGRIND;
256 #if defined(__WIN32__)
260 GetSystemInfo(&sysinfo);
262 return (int) sysinfo.dwNumberOfProcessors;
264 #elif defined(__BSD__)
267 /* swiped from http://stackoverflow.com/questions/150355/
268 programmatically-find-the-number-of-cores-on-a-machine */
272 size_t len = sizeof(numCPU);
274 /* set the mib for hw.ncpu */
276 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
278 /* get the number of CPUs from the system */
279 sysctl(mib, 2, &numCPU, &len, NULL, 0);
283 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
291 #elif defined(__GNUC__)
294 return sysconf(_SC_NPROCESSORS_ONLN);
299 rust_get_num_cpus() {
300 return get_num_cpus();
304 rust_valgrind_stack_register(void *start, void *end) {
305 return VALGRIND_STACK_REGISTER(start, end);
309 rust_valgrind_stack_deregister(unsigned int id) {
310 VALGRIND_STACK_DEREGISTER(id);
313 #if defined(__WIN32__)
316 rust_unset_sigprocmask() {
317 // empty stub for windows to keep linker happy
323 rust_unset_sigprocmask() {
324 // this can't be safely converted to rust code because the
325 // representation of sigset_t is platform-dependent
328 sigprocmask(SIG_SETMASK, &sset, NULL);
333 #if defined(__DragonFly__)
335 // In DragonFly __error() is an inline function and as such
336 // no symbol exists for it.
337 int *__dfly_error(void) { return __error(); }
344 // indent-tabs-mode: nil
346 // buffer-file-coding-system: utf-8-unix