]>
git.proxmox.com Git - rustc.git/blob - src/rt/rust_builtin.c
362439c146912dae5f05ebcd51de749e8d90e22d
1 // Copyright 2012-2015 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__)
23 #include <sys/types.h>
33 #include <TargetConditionals.h>
34 #include <mach/mach_time.h>
36 #if !(TARGET_OS_IPHONE)
37 #include <crt_externs.h>
41 /* Foreign builtins. */
42 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
43 #include "valgrind/valgrind.h"
47 rust_list_dir_val(struct dirent
* entry_ptr
) {
48 return entry_ptr
->d_name
;
52 rust_dir_get_mode(struct dirent
* entry_ptr
) {
53 #if defined(_DIRENT_HAVE_D_TYPE)
54 switch (entry_ptr
->d_type
) {
55 case DT_BLK
: return S_IFBLK
;
56 case DT_CHR
: return S_IFCHR
;
57 case DT_FIFO
: return S_IFIFO
;
58 case DT_LNK
: return S_IFLNK
;
59 case DT_REG
: return S_IFREG
;
60 case DT_SOCK
: return S_IFSOCK
;
67 rust_dir_get_ino(struct dirent
* entry_ptr
) {
68 return entry_ptr
->d_ino
;
72 rust_opendir(char *dirname
) {
73 return opendir(dirname
);
77 rust_readdir_r(DIR *dirp
, struct dirent
*entry
, struct dirent
**result
) {
78 return readdir_r(dirp
, entry
, result
);
82 rust_dirent_t_size() {
83 return sizeof(struct dirent
);
88 rust_running_on_valgrind() {
89 return RUNNING_ON_VALGRIND
;
92 #if defined(__WIN32__)
96 GetSystemInfo(&sysinfo
);
98 return (int) sysinfo
.dwNumberOfProcessors
;
100 #elif defined(__BSD__)
103 /* swiped from http://stackoverflow.com/questions/150355/
104 programmatically-find-the-number-of-cores-on-a-machine */
108 size_t len
= sizeof(numCPU
);
110 /* set the mib for hw.ncpu */
112 mib
[1] = HW_AVAILCPU
; // alternatively, try HW_NCPU;
114 /* get the number of CPUs from the system */
115 sysctl(mib
, 2, &numCPU
, &len
, NULL
, 0);
119 sysctl( mib
, 2, &numCPU
, &len
, NULL
, 0 );
127 #elif defined(__GNUC__)
130 return sysconf(_SC_NPROCESSORS_ONLN
);
135 rust_get_num_cpus() {
136 return get_num_cpus();
140 rust_valgrind_stack_register(void *start
, void *end
) {
141 return VALGRIND_STACK_REGISTER(start
, end
);
145 rust_valgrind_stack_deregister(unsigned int id
) {
146 VALGRIND_STACK_DEREGISTER(id
);
149 #if defined(__DragonFly__)
151 // In DragonFly __error() is an inline function and as such
152 // no symbol exists for it.
153 int *__dfly_error(void) { return __error(); }
156 #if defined(__Bitrig__)
158 #include <sys/param.h>
159 #include <sys/sysctl.h>
162 int rust_get_path(void *p
, size_t* sz
)
173 if ((p
== NULL
) && (sz
== NULL
))
176 /* get the argv array */
178 mib
[1] = KERN_PROC_ARGS
;
180 mib
[3] = KERN_PROC_ENV
;
182 /* get the number of bytes needed to get the env */
184 if (sysctl(mib
, 4, NULL
, &maxlen
, NULL
, 0) == -1)
187 /* allocate the buffer */
188 if ((menv
= calloc(maxlen
, sizeof(char))) == NULL
)
191 /* get the env array */
192 if (sysctl(mib
, 4, menv
, &maxlen
, NULL
, 0) == -1)
198 mib
[3] = KERN_PROC_NENV
;
200 /* get the length of env array */
201 if (sysctl(mib
, 4, &nenv
, &len
, NULL
, 0) == -1)
207 /* find _ key and resolve the value */
208 for (i
= 0; i
< nenv
; i
++)
210 if ((eq
= strstr(menv
[i
], "=")) == NULL
)
217 if (strncmp(key
, "PATH", maxlen
) != 0)
222 /* return the length of the value + NUL */
223 *sz
= strnlen(val
, maxlen
) + 1;
229 /* copy *sz bytes to the output buffer */
240 int rust_get_path_array(void * p
, size_t * sz
)
247 if ((p
== NULL
) && (sz
== NULL
))
250 /* get the length of the PATH value */
251 if (rust_get_path(NULL
, &len
) == -1)
257 /* allocate the buffer */
258 if ((path
= calloc(len
, sizeof(char))) == NULL
)
261 /* get the PATH value */
262 if (rust_get_path(path
, &len
) == -1)
268 /* count the number of parts in the PATH */
270 for(str
= path
; *str
!= '\0'; str
++)
276 /* calculate the size of the buffer for the 2D array */
277 len
= (num
* sizeof(char*) + 1) + strlen(path
) + 1;
286 /* make sure we have enough buffer space */
293 /* zero out the buffer */
297 /* copy the data into the right place */
298 str
= p
+ ((num
+1) * sizeof(char*));
299 memcpy(str
, path
, strlen(path
));
301 /* parse the path into it's parts */
302 for (i
= 0; i
< num
&& (buf
[i
] = strsep(&str
, ":")) != NULL
; i
++) {;}
309 int rust_get_argv_zero(void* p
, size_t* sz
)
315 if ((p
== NULL
) && (sz
== NULL
))
318 /* get the argv array */
320 mib
[1] = KERN_PROC_ARGS
;
322 mib
[3] = KERN_PROC_ARGV
;
324 /* request KERN_PROC_ARGV size */
326 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
329 /* allocate buffer to receive the values */
330 if ((argv
= malloc(len
)) == NULL
)
333 /* get the argv array */
334 if (sysctl(mib
, 4, argv
, &len
, NULL
, 0) == -1)
340 /* get length of argv[0] */
341 len
= strnlen(argv
[0], len
) + 1;
357 memcpy(p
, argv
[0], len
);
362 const char * rust_current_exe()
364 static char *self
= NULL
;
369 char buf
[2*PATH_MAX
], exe
[2*PATH_MAX
];
374 if (rust_get_argv_zero(NULL
, &sz
) == -1)
376 if ((argv0
= calloc(sz
, sizeof(char))) == NULL
)
378 if (rust_get_argv_zero(argv0
, &sz
) == -1)
384 /* if argv0 is a relative or absolute path, resolve it with realpath */
385 if ((*argv0
== '.') || (*argv0
== '/') || (strstr(argv0
, "/") != NULL
))
387 self
= realpath(argv0
, NULL
);
392 /* get the path array */
393 if (rust_get_path_array(NULL
, &sz
) == -1)
398 if ((paths
= calloc(sz
, sizeof(char))) == NULL
)
403 if (rust_get_path_array(paths
, &sz
) == -1)
410 for(i
= 0; paths
[i
] != NULL
; i
++)
412 snprintf(buf
, 2*PATH_MAX
, "%s/%s", paths
[i
], argv0
);
413 if (realpath(buf
, exe
) == NULL
)
416 if (access(exe
, F_OK
| X_OK
) == -1)
430 #elif defined(__OpenBSD__)
432 #include <sys/param.h>
433 #include <sys/sysctl.h>
436 const char * rust_current_exe() {
437 static char *self
= NULL
;
446 mib
[1] = KERN_PROC_ARGS
;
448 mib
[3] = KERN_PROC_ARGV
;
450 /* request KERN_PROC_ARGV size */
452 if (sysctl(mib
, 4, NULL
, &argv_len
, NULL
, 0) == -1)
456 if ((argv
= malloc(argv_len
)) == NULL
)
459 /* request KERN_PROC_ARGV */
460 if (sysctl(mib
, 4, argv
, &argv_len
, NULL
, 0) == -1) {
465 /* get realpath if possible */
466 if ((argv
[0] != NULL
) && ((*argv
[0] == '.') || (*argv
[0] == '/')
467 || (strstr(argv
[0], "/") != NULL
)))
469 self
= realpath(argv
[0], NULL
);
486 // indent-tabs-mode: nil
488 // buffer-file-coding-system: utf-8-unix