1const _SC_ARG_MAX: c_int = 0;
9const _SC_CHILD_MAX: c_int = 1;
10const _SC_CLK_TCK: c_int = 2;
11const _SC_NGROUPS_MAX: c_int = 3;
12const _SC_OPEN_MAX: c_int = 4;
13const _SC_STREAM_MAX: c_int = 5;
14const _SC_TZNAME_MAX: c_int = 6;
15const _SC_PAGE_SIZE: c_int = 30;
16const _SC_NPROCESSORS_CONF: c_int = 83;
17const _SC_NPROCESSORS_ONLN: c_int = 84;
18const _SC_PHYS_PAGES: c_int = 85;
19const _SC_AVPHYS_PAGES: c_int = 86;
20const _SC_IOV_MAX: c_int = 60;
21const _SC_SEM_NSEMS_MAX: c_int = 32;
22const _SC_SEM_VALUE_MAX: c_int = 33;
23const _SC_MQ_OPEN_MAX: c_int = 27;
24const _SC_MQ_PRIO_MAX: c_int = 28;
25const _SC_DELAYTIMER_MAX: c_int = 26;
26const _SC_THREAD_DESTRUCTOR_ITERATIONS: c_int = 73;
27const _SC_THREAD_KEYS_MAX: c_int = 74;
28const _SC_THREAD_STACK_MIN: c_int = 75;
29const _SC_LOGIN_NAME_MAX: c_int = 71;
30const _SC_TTY_NAME_MAX: c_int = 72;
31const _SC_SYMLOOP_MAX: c_int = 173;
32const _SC_HOST_NAME_MAX: c_int = 180;
33const _SC_NZERO: c_int = 109;
34const _SC_VERSION: c_int = 29;
35const _SC_2_VERSION: c_int = 46;
36const _SC_XOPEN_VERSION: c_int = 89;
37
38#[no_mangle]
39pub unsafe extern "C" fn mmap(
40 addr: *mut c_void,
41 len: usize,
42 prot: c_int,
43 flags: c_int,
44 fd: c_int,
45 off: i64,
46) -> *mut c_void {
47 let r = crate::syscall(
48 SYS_MMAP,
49 addr as c_long,
50 len as c_long,
51 prot as c_long,
52 flags as c_long,
53 fd as c_long,
54 off as c_long,
55 );
56 if r == -1 {
57 return -1isize as *mut c_void;
58 }
59 r as *mut c_void
60}
61
62#[no_mangle]
63pub unsafe extern "C" fn munmap(addr: *mut c_void, len: usize) -> c_int {
64 crate::syscall(SYS_MUNMAP, addr as c_long, len as c_long, 0, 0, 0, 0) as c_int
65}
66
67#[no_mangle]
68pub unsafe extern "C" fn pread(fd: c_int, buf: *mut c_void, count: usize, offset: i64) -> isize {
69 crate::syscall(
70 SYS_PREAD64,
71 fd as c_long,
72 buf as c_long,
73 count as c_long,
74 offset as c_long,
75 0,
76 0,
77 ) as isize
78}
79
80unsafe fn rlimit_sc(resource: c_int) -> c_long {
81 let mut lim: crate::Rlimit = core::mem::zeroed();
82 if crate::getrlimit(resource, &mut lim) != 0 {
83 return -1;
84 }
85 if lim.rlim_cur == !0u64 {
86 return -1;
87 }
88 if lim.rlim_cur > c_long::MAX as u64 {
89 return c_long::MAX;
90 }
91 lim.rlim_cur as c_long
92}
93
94unsafe fn online_cpus() -> c_long {
95 let mut set = [0u8; 128];
96 let r = crate::sched_getaffinity(0, set.len(), set.as_mut_ptr());
97 if r != 0 {
98 return 1;
99 }
100 let mut count = 0u32;
101 for b in set.iter() {
102 count += b.count_ones();
103 }
104 if count == 0 { 1 } else { count as c_long }
105}
106
107unsafe fn memory_pages(free: bool) -> c_long {
108 let mut info = [0u64; 16];
110 if crate::sysinfo(info.as_mut_ptr() as *mut core::ffi::c_void) != 0 {
111 return -1;
112 }
113 let total = if free {
114 info[5].saturating_add(info[7])
115 } else {
116 info[4]
117 };
118 let unit = (info[13] & 0xffff_ffff) as u64;
119 let bytes = total.saturating_mul(if unit == 0 { 1 } else { unit });
120 (bytes / 4096) as c_long
121}
122
123#[no_mangle]
124pub unsafe extern "C" fn sysconf(name: c_int) -> c_long {
125 const POSIX_VERSION: c_long = 200809;
126 match name {
127 _SC_ARG_MAX => 131072,
128 _SC_CHILD_MAX => rlimit_sc(crate::RLIMIT_NPROC),
129 _SC_CLK_TCK => 100,
130 _SC_NGROUPS_MAX => 32,
131 _SC_OPEN_MAX => rlimit_sc(crate::RLIMIT_NOFILE),
132 _SC_STREAM_MAX => -1,
133 _SC_TZNAME_MAX => 6,
134 7 | 8 => 1, 9 | 11 | 12 => POSIX_VERSION, 10 | 13 | 14 => -1, 15..=22 => POSIX_VERSION, 23 | 24 => -1, 25 => 0, _SC_DELAYTIMER_MAX => 0x7fffffff,
141 _SC_MQ_OPEN_MAX => -1,
142 _SC_MQ_PRIO_MAX => 32768,
143 _SC_VERSION => POSIX_VERSION,
144 _SC_PAGE_SIZE => 4096,
145 31 => 30, _SC_SEM_NSEMS_MAX => 256,
147 _SC_SEM_VALUE_MAX => 0x7fffffff,
148 34 | 35 => -1, 36 => 99, 37 => 2048, 38 => 99, 39 => 1000, 40 => 2, 42 | 43 => -1, 44 => 255, _SC_2_VERSION => POSIX_VERSION,
154 47 => POSIX_VERSION, 48..=52 => -1, _SC_IOV_MAX => 1024,
157 67 | 68 => POSIX_VERSION, 69 | 70 => -1, _SC_LOGIN_NAME_MAX => 256,
160 _SC_TTY_NAME_MAX => 32,
161 _SC_THREAD_DESTRUCTOR_ITERATIONS => 4,
162 _SC_THREAD_KEYS_MAX => 128,
163 _SC_THREAD_STACK_MIN => 2048,
164 76 => -1, 77 | 78 | 79 => POSIX_VERSION, 80 | 81 => -1, 82 => POSIX_VERSION, _SC_NPROCESSORS_CONF | _SC_NPROCESSORS_ONLN => online_cpus(),
169 _SC_PHYS_PAGES => memory_pages(false),
170 _SC_AVPHYS_PAGES => memory_pages(true),
171 87 | 88 => -1, _SC_XOPEN_VERSION => 700,
173 90 => 700, 91 => 1, 92 => -1, 93 | 94 => 1, 95 => -1, 97 => -1, 98..=100 => -1, _SC_NZERO => 20,
181 125 | 126 | 128 => -1, 127 => 1, 129..=131 => -1, 132 | 133 => POSIX_VERSION, 137 | 138 | 139 => POSIX_VERSION,
186 149 => POSIX_VERSION, 153 | 154 | 155 | 157 | 159 => POSIX_VERSION,
188 160 | 161 => -1,
189 164 => POSIX_VERSION, 165 => -1, 168..=172 => -1, _SC_SYMLOOP_MAX => 40,
193 174 => 0, 175 => -1,
195 176 | 177 | 179 => -1, 178 => 1, _SC_HOST_NAME_MAX => 255,
198 181..=184 => -1, 235 | 236 => POSIX_VERSION, 237 | 238 | 240 => -1, 239 => 1, 241..=245 => -1, _ => {
204 crate::__errno_location().write(EINVAL);
205 -1
206 }
207 }
208}
209
210#[no_mangle]
211pub unsafe extern "C" fn execle(path: *const c_char, arg: *const c_char, mut args: ...) -> c_int {
212 let mut argv: [*const c_char; 128] = [core::ptr::null(); 128];
213 argv[0] = arg;
214 let mut n = 1usize;
215 loop {
216 let a: *const c_char = args.next_arg();
217 if a.is_null() {
218 break;
219 }
220 if n >= argv.len() - 1 {
221 crate::__errno_location().write(E2BIG);
222 return -1;
223 }
224 argv[n] = a;
225 n += 1;
226 }
227 argv[n] = core::ptr::null();
228 let envp: *const *const c_char = args.next_arg();
229 crate::execve(path, argv.as_ptr(), envp)
230}
231
232#[no_mangle]
233pub unsafe extern "C" fn flockfile(f: *mut crate::FILE) {
234 if (*f).lock < 0 { return; }
235 let tid = crate::sys_gettid() as i32;
236 loop {
237 let v = crate::a_load(&raw const (*f).lock);
238 if v == tid {
239 (*f).lockcount += 1;
240 return;
241 }
242 if v == 0 && crate::a_cas(&raw mut (*f).lock, 0, tid) == 0 {
243 (*f).lockcount = 1;
244 return;
245 }
246 if v != 0 {
247 crate::futex_wait(&raw mut (*f).lock, v);
248 }
249 }
250}
251
252#[no_mangle]
253pub unsafe extern "C" fn funlockfile(f: *mut crate::FILE) {
254 if (*f).lock < 0 { return; }
255 if (*f).lockcount > 1 {
256 (*f).lockcount -= 1;
257 return;
258 }
259 (*f).lockcount = 0;
260 crate::a_store(&raw mut (*f).lock, 0);
261 crate::futex_wake(&raw mut (*f).lock, 1);
262}
263
264#[no_mangle]
265pub unsafe extern "C" fn __libc_current_sigrtmin() -> c_int {
266 34
267}
268
269#[no_mangle]
270pub unsafe extern "C" fn strdup(s: *const c_char) -> *mut c_char {
271 if s.is_null() {
272 return core::ptr::null_mut();
273 }
274 let len = crate::strlen(s);
275 let p = crate::malloc(len + 1) as *mut c_char;
276 if p.is_null() {
277 return core::ptr::null_mut();
278 }
279 crate::memcpy(p as *mut c_void, s as *const c_void, len + 1);
280 p
281}
282
283#[cfg(feature = "coverage")]
286#[no_mangle]
287pub unsafe extern "C" fn __strdup(s: *const c_char) -> *mut c_char {
288 strdup(s)
289}
290
291static mut MKTEMP_COUNTER: c_uint = 0;
292
293unsafe fn mktemp_internal(
294 template: *mut c_char,
295 mkdir_fn: Option<unsafe extern "C" fn(*const c_char, c_uint) -> c_int>,
296) -> *mut c_char {
297 if template.is_null() {
298 crate::__errno_location().write(EINVAL);
299 return core::ptr::null_mut();
300 }
301 let len = crate::strlen(template);
302 if len < 6 {
303 crate::__errno_location().write(EINVAL);
304 return core::ptr::null_mut();
305 }
306 let mut xcount = 0usize;
307 while xcount < 6 {
308 let ch = *template.add(len - 1 - xcount) as u8;
309 if ch != b'X' {
310 break;
311 }
312 xcount += 1;
313 }
314 if xcount != 6 {
315 crate::__errno_location().write(EINVAL);
316 return core::ptr::null_mut();
317 }
318 let _pid = crate::getpid();
319 let mut c = MKTEMP_COUNTER;
320 for _ in 0..1000 {
321 let mut n = c;
322 for i in 0..6 {
323 let digit = (n % 36) as u8;
324 let ch = if digit < 10 { b'0' + digit } else { b'a' + digit - 10 };
325 *template.add(len - 6 + i) = ch as c_char;
326 n /= 36;
327 }
328 c = c.wrapping_add(1);
329 if mkdir_fn.is_some() {
330 if crate::mkdir(template, 0o700) == 0 {
331 MKTEMP_COUNTER = c;
332 return template;
333 }
334 let e = crate::__errno_location().read();
335 if e != EEXIST && e != EINTR {
336 break;
337 }
338 } else {
339 MKTEMP_COUNTER = c;
340 return template;
341 }
342 }
343 MKTEMP_COUNTER = c;
344 template
345}
346
347#[no_mangle]
348pub unsafe extern "C" fn mkdtemp(template: *mut c_char) -> *mut c_char {
349 mktemp_internal(template, Some(crate::mkdir))
350}
351
352#[no_mangle]
353pub unsafe extern "C" fn mktemp(template: *mut c_char) -> *mut c_char {
354 mktemp_internal(template, None)
355}