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.
11 /* Foreign builtins. */
13 #include "valgrind/valgrind.h"
21 #if !defined(__WIN32__)
23 #include <sys/types.h>
36 #include <TargetConditionals.h>
37 #include <mach/mach_time.h>
39 #if !(TARGET_OS_IPHONE)
40 #include <crt_externs.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;
104 #if defined(__WIN32__)
105 rust_list_dir_wfd_size() {
106 return sizeof(WIN32_FIND_DATAW);
109 rust_list_dir_wfd_size() {
115 #if defined(__WIN32__)
116 rust_list_dir_wfd_fp_buf(WIN32_FIND_DATAW* wfd) {
121 return wfd->cFileName;
125 rust_list_dir_wfd_fp_buf(void* wfd) {
144 void rust_tm_to_tm(rust_tm* in_tm, struct tm* out_tm) {
145 memset(out_tm, 0, sizeof(struct tm));
146 out_tm->tm_sec = in_tm->tm_sec;
147 out_tm->tm_min = in_tm->tm_min;
148 out_tm->tm_hour = in_tm->tm_hour;
149 out_tm->tm_mday = in_tm->tm_mday;
150 out_tm->tm_mon = in_tm->tm_mon;
151 out_tm->tm_year = in_tm->tm_year;
152 out_tm->tm_wday = in_tm->tm_wday;
153 out_tm->tm_yday = in_tm->tm_yday;
154 out_tm->tm_isdst = in_tm->tm_isdst;
157 void tm_to_rust_tm(struct tm* in_tm,
161 out_tm->tm_sec = in_tm->tm_sec;
162 out_tm->tm_min = in_tm->tm_min;
163 out_tm->tm_hour = in_tm->tm_hour;
164 out_tm->tm_mday = in_tm->tm_mday;
165 out_tm->tm_mon = in_tm->tm_mon;
166 out_tm->tm_year = in_tm->tm_year;
167 out_tm->tm_wday = in_tm->tm_wday;
168 out_tm->tm_yday = in_tm->tm_yday;
169 out_tm->tm_isdst = in_tm->tm_isdst;
170 out_tm->tm_gmtoff = gmtoff;
171 out_tm->tm_nsec = nsec;
174 #if defined(__WIN32__)
175 #define TZSET() _tzset()
176 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
177 #define GMTIME(clock, result) gmtime_s((result), (clock))
178 #define LOCALTIME(clock, result) localtime_s((result), (clock))
179 #define TIMEGM(result) _mkgmtime64(result)
181 struct tm* GMTIME(const time_t *clock, struct tm *result) {
182 struct tm* t = gmtime(clock);
183 if (t == NULL || result == NULL) { return NULL; }
187 struct tm* LOCALTIME(const time_t *clock, struct tm *result) {
188 struct tm* t = localtime(clock);
189 if (t == NULL || result == NULL) { return NULL; }
193 #define TIMEGM(result) mktime((result)) - _timezone
196 #define TZSET() tzset()
197 #define GMTIME(clock, result) gmtime_r((clock), (result))
198 #define LOCALTIME(clock, result) localtime_r((clock), (result))
199 #define TIMEGM(result) timegm(result)
208 rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
213 tm_to_rust_tm(&tm, timeptr, 0, nsec);
217 rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
222 #if defined(__WIN32__)
223 int32_t gmtoff = -timezone;
225 int32_t gmtoff = tm.tm_gmtoff;
228 tm_to_rust_tm(&tm, timeptr, gmtoff, nsec);
232 rust_timegm(rust_tm* timeptr) {
234 rust_tm_to_tm(timeptr, &t);
239 rust_mktime(rust_tm* timeptr) {
241 rust_tm_to_tm(timeptr, &t);
248 rust_opendir(char *dirname) {
249 return opendir(dirname);
253 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
254 return readdir_r(dirp, entry, result);
258 rust_dirent_t_size() {
259 return sizeof(struct dirent);
273 rust_dirent_t_size() {
279 rust_running_on_valgrind() {
280 return RUNNING_ON_VALGRIND;
283 #if defined(__WIN32__)
287 GetSystemInfo(&sysinfo);
289 return (int) sysinfo.dwNumberOfProcessors;
291 #elif defined(__BSD__)
294 /* swiped from http://stackoverflow.com/questions/150355/
295 programmatically-find-the-number-of-cores-on-a-machine */
299 size_t len = sizeof(numCPU);
301 /* set the mib for hw.ncpu */
303 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
305 /* get the number of CPUs from the system */
306 sysctl(mib, 2, &numCPU, &len, NULL, 0);
310 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
318 #elif defined(__GNUC__)
321 return sysconf(_SC_NPROCESSORS_ONLN);
326 rust_get_num_cpus() {
327 return get_num_cpus();
331 rust_valgrind_stack_register(void *start, void *end) {
332 return VALGRIND_STACK_REGISTER(start, end);
336 rust_valgrind_stack_deregister(unsigned int id) {
337 VALGRIND_STACK_DEREGISTER(id);
340 #if defined(__WIN32__)
343 rust_unset_sigprocmask() {
344 // empty stub for windows to keep linker happy
350 rust_unset_sigprocmask() {
351 // this can't be safely converted to rust code because the
352 // representation of sigset_t is platform-dependent
355 sigprocmask(SIG_SETMASK, &sset, NULL);
360 #if defined(__DragonFly__)
362 // In DragonFly __error() is an inline function and as such
363 // no symbol exists for it.
364 int *__dfly_error(void) { return __error(); }
371 // indent-tabs-mode: nil
373 // buffer-file-coding-system: utf-8-unix