]>
git.proxmox.com Git - rustc.git/blob - src/rt/rust_builtin.c
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.
25 #include <sys/types.h>
29 #include <TargetConditionals.h>
30 #include <mach/mach_time.h>
32 #if !(TARGET_OS_IPHONE)
33 #include <crt_externs.h>
38 rust_list_dir_val(struct dirent
* entry_ptr
) {
39 return entry_ptr
->d_name
;
43 rust_dir_get_mode(struct dirent
* entry_ptr
) {
44 #if defined(_DIRENT_HAVE_D_TYPE) || defined(__APPLE__)
45 switch (entry_ptr
->d_type
) {
46 case DT_BLK
: return S_IFBLK
;
47 case DT_CHR
: return S_IFCHR
;
48 case DT_FIFO
: return S_IFIFO
;
49 case DT_LNK
: return S_IFLNK
;
50 case DT_REG
: return S_IFREG
;
51 case DT_SOCK
: return S_IFSOCK
;
52 case DT_DIR
: return S_IFDIR
;
59 rust_dir_get_ino(struct dirent
* entry_ptr
) {
60 return entry_ptr
->d_ino
;
64 rust_opendir(char *dirname
) {
65 return opendir(dirname
);
69 rust_readdir_r(DIR *dirp
, struct dirent
*entry
, struct dirent
**result
) {
70 return readdir_r(dirp
, entry
, result
);
74 rust_dirent_t_size() {
75 return sizeof(struct dirent
);
81 /* swiped from http://stackoverflow.com/questions/150355/
82 programmatically-find-the-number-of-cores-on-a-machine */
86 size_t len
= sizeof(numCPU
);
88 /* set the mib for hw.ncpu */
90 mib
[1] = HW_AVAILCPU
; // alternatively, try HW_NCPU;
92 /* get the number of CPUs from the system */
93 sysctl(mib
, 2, &numCPU
, &len
, NULL
, 0);
97 sysctl( mib
, 2, &numCPU
, &len
, NULL
, 0 );
105 #elif defined(__GNUC__)
108 return sysconf(_SC_NPROCESSORS_ONLN
);
113 rust_get_num_cpus() {
114 return get_num_cpus();
117 #if defined(__DragonFly__)
119 // In DragonFly __error() is an inline function and as such
120 // no symbol exists for it.
121 int *__dfly_error(void) { return __error(); }
124 #if defined(__Bitrig__)
126 #include <sys/param.h>
127 #include <sys/sysctl.h>
130 int rust_get_path(void *p
, size_t* sz
)
141 if ((p
== NULL
) && (sz
== NULL
))
144 /* get the argv array */
146 mib
[1] = KERN_PROC_ARGS
;
148 mib
[3] = KERN_PROC_ENV
;
150 /* get the number of bytes needed to get the env */
152 if (sysctl(mib
, 4, NULL
, &maxlen
, NULL
, 0) == -1)
155 /* allocate the buffer */
156 if ((menv
= calloc(maxlen
, sizeof(char))) == NULL
)
159 /* get the env array */
160 if (sysctl(mib
, 4, menv
, &maxlen
, NULL
, 0) == -1)
166 mib
[3] = KERN_PROC_NENV
;
168 /* get the length of env array */
169 if (sysctl(mib
, 4, &nenv
, &len
, NULL
, 0) == -1)
175 /* find _ key and resolve the value */
176 for (i
= 0; i
< nenv
; i
++)
178 if ((eq
= strstr(menv
[i
], "=")) == NULL
)
185 if (strncmp(key
, "PATH", maxlen
) != 0)
190 /* return the length of the value + NUL */
191 *sz
= strnlen(val
, maxlen
) + 1;
197 /* copy *sz bytes to the output buffer */
208 int rust_get_path_array(void * p
, size_t * sz
)
215 if ((p
== NULL
) && (sz
== NULL
))
218 /* get the length of the PATH value */
219 if (rust_get_path(NULL
, &len
) == -1)
225 /* allocate the buffer */
226 if ((path
= calloc(len
, sizeof(char))) == NULL
)
229 /* get the PATH value */
230 if (rust_get_path(path
, &len
) == -1)
236 /* count the number of parts in the PATH */
238 for(str
= path
; *str
!= '\0'; str
++)
244 /* calculate the size of the buffer for the 2D array */
245 len
= (num
* sizeof(char*) + 1) + strlen(path
) + 1;
254 /* make sure we have enough buffer space */
261 /* zero out the buffer */
265 /* copy the data into the right place */
266 str
= p
+ ((num
+1) * sizeof(char*));
267 memcpy(str
, path
, strlen(path
));
269 /* parse the path into it's parts */
270 for (i
= 0; i
< num
&& (buf
[i
] = strsep(&str
, ":")) != NULL
; i
++) {;}
277 int rust_get_argv_zero(void* p
, size_t* sz
)
283 if ((p
== NULL
) && (sz
== NULL
))
286 /* get the argv array */
288 mib
[1] = KERN_PROC_ARGS
;
290 mib
[3] = KERN_PROC_ARGV
;
292 /* request KERN_PROC_ARGV size */
294 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
297 /* allocate buffer to receive the values */
298 if ((argv
= malloc(len
)) == NULL
)
301 /* get the argv array */
302 if (sysctl(mib
, 4, argv
, &len
, NULL
, 0) == -1)
308 /* get length of argv[0] */
309 len
= strnlen(argv
[0], len
) + 1;
325 memcpy(p
, argv
[0], len
);
330 const char * rust_current_exe()
332 static char *self
= NULL
;
337 char buf
[2*PATH_MAX
], exe
[2*PATH_MAX
];
342 if (rust_get_argv_zero(NULL
, &sz
) == -1)
344 if ((argv0
= calloc(sz
, sizeof(char))) == NULL
)
346 if (rust_get_argv_zero(argv0
, &sz
) == -1)
352 /* if argv0 is a relative or absolute path, resolve it with realpath */
353 if ((*argv0
== '.') || (*argv0
== '/') || (strstr(argv0
, "/") != NULL
))
355 self
= realpath(argv0
, NULL
);
360 /* get the path array */
361 if (rust_get_path_array(NULL
, &sz
) == -1)
366 if ((paths
= calloc(sz
, sizeof(char))) == NULL
)
371 if (rust_get_path_array(paths
, &sz
) == -1)
378 for(i
= 0; paths
[i
] != NULL
; i
++)
380 snprintf(buf
, 2*PATH_MAX
, "%s/%s", paths
[i
], argv0
);
381 if (realpath(buf
, exe
) == NULL
)
384 if (access(exe
, F_OK
| X_OK
) == -1)
398 #elif defined(__OpenBSD__)
400 #include <sys/param.h>
401 #include <sys/sysctl.h>
404 const char * rust_current_exe() {
405 static char *self
= NULL
;
414 mib
[1] = KERN_PROC_ARGS
;
416 mib
[3] = KERN_PROC_ARGV
;
418 /* request KERN_PROC_ARGV size */
420 if (sysctl(mib
, 4, NULL
, &argv_len
, NULL
, 0) == -1)
424 if ((argv
= malloc(argv_len
)) == NULL
)
427 /* request KERN_PROC_ARGV */
428 if (sysctl(mib
, 4, argv
, &argv_len
, NULL
, 0) == -1) {
433 /* get realpath if possible */
434 if ((argv
[0] != NULL
) && ((*argv
[0] == '.') || (*argv
[0] == '/')
435 || (strstr(argv
[0], "/") != NULL
)))
437 self
= realpath(argv
[0], NULL
);
450 #endif // !defined(_WIN32)
456 // indent-tabs-mode: nil
458 // buffer-file-coding-system: utf-8-unix