]>
git.proxmox.com Git - rustc.git/blob - src/rt/rust_builtin.c
1a2917a1dd67f9c8d0251fff51b1d8b0cbc425b4
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.
24 #include <sys/types.h>
34 #include <TargetConditionals.h>
35 #include <mach/mach_time.h>
37 #if !(TARGET_OS_IPHONE)
38 #include <crt_externs.h>
42 /* Foreign builtins. */
43 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
45 #include "valgrind/valgrind.h"
49 # define RUST_BUILTIN_API __declspec(dllexport)
51 # define RUST_BUILTIN_API
56 rust_list_dir_val(struct dirent
* entry_ptr
) {
57 return entry_ptr
->d_name
;
61 rust_dir_get_mode(struct dirent
* entry_ptr
) {
62 #if defined(_DIRENT_HAVE_D_TYPE) || defined(__APPLE__)
63 switch (entry_ptr
->d_type
) {
64 case DT_BLK
: return S_IFBLK
;
65 case DT_CHR
: return S_IFCHR
;
66 case DT_FIFO
: return S_IFIFO
;
67 case DT_LNK
: return S_IFLNK
;
68 case DT_REG
: return S_IFREG
;
69 case DT_SOCK
: return S_IFSOCK
;
70 case DT_DIR
: return S_IFDIR
;
77 rust_dir_get_ino(struct dirent
* entry_ptr
) {
78 return entry_ptr
->d_ino
;
82 rust_opendir(char *dirname
) {
83 return opendir(dirname
);
87 rust_readdir_r(DIR *dirp
, struct dirent
*entry
, struct dirent
**result
) {
88 return readdir_r(dirp
, entry
, result
);
92 rust_dirent_t_size() {
93 return sizeof(struct dirent
);
101 GetSystemInfo(&sysinfo
);
103 return (int) sysinfo
.dwNumberOfProcessors
;
105 #elif defined(__BSD__)
108 /* swiped from http://stackoverflow.com/questions/150355/
109 programmatically-find-the-number-of-cores-on-a-machine */
113 size_t len
= sizeof(numCPU
);
115 /* set the mib for hw.ncpu */
117 mib
[1] = HW_AVAILCPU
; // alternatively, try HW_NCPU;
119 /* get the number of CPUs from the system */
120 sysctl(mib
, 2, &numCPU
, &len
, NULL
, 0);
124 sysctl( mib
, 2, &numCPU
, &len
, NULL
, 0 );
132 #elif defined(__GNUC__)
135 return sysconf(_SC_NPROCESSORS_ONLN
);
141 rust_get_num_cpus() {
142 return get_num_cpus();
146 rust_running_on_valgrind() {
150 return RUNNING_ON_VALGRIND
;
154 #if defined(__DragonFly__)
156 // In DragonFly __error() is an inline function and as such
157 // no symbol exists for it.
158 int *__dfly_error(void) { return __error(); }
161 #if defined(__Bitrig__)
163 #include <sys/param.h>
164 #include <sys/sysctl.h>
167 int rust_get_path(void *p
, size_t* sz
)
178 if ((p
== NULL
) && (sz
== NULL
))
181 /* get the argv array */
183 mib
[1] = KERN_PROC_ARGS
;
185 mib
[3] = KERN_PROC_ENV
;
187 /* get the number of bytes needed to get the env */
189 if (sysctl(mib
, 4, NULL
, &maxlen
, NULL
, 0) == -1)
192 /* allocate the buffer */
193 if ((menv
= calloc(maxlen
, sizeof(char))) == NULL
)
196 /* get the env array */
197 if (sysctl(mib
, 4, menv
, &maxlen
, NULL
, 0) == -1)
203 mib
[3] = KERN_PROC_NENV
;
205 /* get the length of env array */
206 if (sysctl(mib
, 4, &nenv
, &len
, NULL
, 0) == -1)
212 /* find _ key and resolve the value */
213 for (i
= 0; i
< nenv
; i
++)
215 if ((eq
= strstr(menv
[i
], "=")) == NULL
)
222 if (strncmp(key
, "PATH", maxlen
) != 0)
227 /* return the length of the value + NUL */
228 *sz
= strnlen(val
, maxlen
) + 1;
234 /* copy *sz bytes to the output buffer */
245 int rust_get_path_array(void * p
, size_t * sz
)
252 if ((p
== NULL
) && (sz
== NULL
))
255 /* get the length of the PATH value */
256 if (rust_get_path(NULL
, &len
) == -1)
262 /* allocate the buffer */
263 if ((path
= calloc(len
, sizeof(char))) == NULL
)
266 /* get the PATH value */
267 if (rust_get_path(path
, &len
) == -1)
273 /* count the number of parts in the PATH */
275 for(str
= path
; *str
!= '\0'; str
++)
281 /* calculate the size of the buffer for the 2D array */
282 len
= (num
* sizeof(char*) + 1) + strlen(path
) + 1;
291 /* make sure we have enough buffer space */
298 /* zero out the buffer */
302 /* copy the data into the right place */
303 str
= p
+ ((num
+1) * sizeof(char*));
304 memcpy(str
, path
, strlen(path
));
306 /* parse the path into it's parts */
307 for (i
= 0; i
< num
&& (buf
[i
] = strsep(&str
, ":")) != NULL
; i
++) {;}
314 int rust_get_argv_zero(void* p
, size_t* sz
)
320 if ((p
== NULL
) && (sz
== NULL
))
323 /* get the argv array */
325 mib
[1] = KERN_PROC_ARGS
;
327 mib
[3] = KERN_PROC_ARGV
;
329 /* request KERN_PROC_ARGV size */
331 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
334 /* allocate buffer to receive the values */
335 if ((argv
= malloc(len
)) == NULL
)
338 /* get the argv array */
339 if (sysctl(mib
, 4, argv
, &len
, NULL
, 0) == -1)
345 /* get length of argv[0] */
346 len
= strnlen(argv
[0], len
) + 1;
362 memcpy(p
, argv
[0], len
);
367 const char * rust_current_exe()
369 static char *self
= NULL
;
374 char buf
[2*PATH_MAX
], exe
[2*PATH_MAX
];
379 if (rust_get_argv_zero(NULL
, &sz
) == -1)
381 if ((argv0
= calloc(sz
, sizeof(char))) == NULL
)
383 if (rust_get_argv_zero(argv0
, &sz
) == -1)
389 /* if argv0 is a relative or absolute path, resolve it with realpath */
390 if ((*argv0
== '.') || (*argv0
== '/') || (strstr(argv0
, "/") != NULL
))
392 self
= realpath(argv0
, NULL
);
397 /* get the path array */
398 if (rust_get_path_array(NULL
, &sz
) == -1)
403 if ((paths
= calloc(sz
, sizeof(char))) == NULL
)
408 if (rust_get_path_array(paths
, &sz
) == -1)
415 for(i
= 0; paths
[i
] != NULL
; i
++)
417 snprintf(buf
, 2*PATH_MAX
, "%s/%s", paths
[i
], argv0
);
418 if (realpath(buf
, exe
) == NULL
)
421 if (access(exe
, F_OK
| X_OK
) == -1)
435 #elif defined(__OpenBSD__)
437 #include <sys/param.h>
438 #include <sys/sysctl.h>
441 const char * rust_current_exe() {
442 static char *self
= NULL
;
451 mib
[1] = KERN_PROC_ARGS
;
453 mib
[3] = KERN_PROC_ARGV
;
455 /* request KERN_PROC_ARGV size */
457 if (sysctl(mib
, 4, NULL
, &argv_len
, NULL
, 0) == -1)
461 if ((argv
= malloc(argv_len
)) == NULL
)
464 /* request KERN_PROC_ARGV */
465 if (sysctl(mib
, 4, argv
, &argv_len
, NULL
, 0) == -1) {
470 /* get realpath if possible */
471 if ((argv
[0] != NULL
) && ((*argv
[0] == '.') || (*argv
[0] == '/')
472 || (strstr(argv
[0], "/") != NULL
)))
474 self
= realpath(argv
[0], NULL
);
491 // indent-tabs-mode: nil
493 // buffer-file-coding-system: utf-8-unix