Skip to main content

c/
compat_symbols.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// Remaining symbols - batch 1
6// ============================================================
7
8#[no_mangle]
9pub unsafe extern "C" fn __lgammal_r(x: f64, signgamp: *mut i32) -> f64 {
10    *signgamp = 1;
11    lgamma(x)
12}
13
14#[no_mangle]
15pub unsafe extern "C" fn __libc_current_sigrtmax() -> i32 {
16    64
17}
18
19#[no_mangle]
20pub unsafe extern "C" fn _longjmp(env: *mut core::ffi::c_void, val: i32) {
21    crate::longjmp(env as *mut u64, val)
22}
23
24#[no_mangle]
25pub unsafe extern "C" fn __newlocale(_mask: i32, _name: *const core::ffi::c_char, base: *mut core::ffi::c_void) -> *mut core::ffi::c_void {
26    base
27}
28
29#[no_mangle]
30pub unsafe extern "C" fn __nl_langinfo(_item: i32) -> *mut core::ffi::c_char {
31    b"\0".as_ptr() as *mut core::ffi::c_char
32}
33
34#[no_mangle]
35pub unsafe extern "C" fn __nl_langinfo_l(_item: i32, _loc: *mut core::ffi::c_void) -> *mut core::ffi::c_char {
36    b"\0".as_ptr() as *mut core::ffi::c_char
37}
38
39#[no_mangle]
40pub unsafe extern "C" fn ns_get16(cp: *const u8) -> u16 {
41    ((*cp.add(0) as u16) << 8) | *cp.add(1) as u16
42}
43
44#[no_mangle]
45pub unsafe extern "C" fn ns_get32(cp: *const u8) -> u32 {
46    ((*cp.add(0) as u32) << 24) | ((*cp.add(1) as u32) << 16) | ((*cp.add(2) as u32) << 8) | *cp.add(3) as u32
47}
48
49#[no_mangle]
50pub unsafe extern "C" fn ns_put16(s: u16, dst: *mut u8) {
51    *dst.add(0) = (s >> 8) as u8;
52    *dst.add(1) = (s & 0xff) as u8;
53}
54
55#[no_mangle]
56pub unsafe extern "C" fn ns_put32(l: u32, dst: *mut u8) {
57    *dst.add(0) = (l >> 24) as u8;
58    *dst.add(1) = ((l >> 16) & 0xff) as u8;
59    *dst.add(2) = ((l >> 8) & 0xff) as u8;
60    *dst.add(3) = (l & 0xff) as u8;
61}
62
63#[no_mangle]
64pub unsafe extern "C" fn posix_close(fd: i32) -> i32 {
65    crate::close(fd)
66}
67
68#[no_mangle]
69pub unsafe extern "C" fn posix_openpt(flags: i32) -> i32 {
70    crate::open(b"/dev/ptmx\0".as_ptr() as *const core::ffi::c_char, flags, 0)
71}
72
73#[no_mangle]
74pub unsafe extern "C" fn putw(w: i32, f: *mut core::ffi::c_void) {
75    putc((w & 0xff) as i32, f as *mut FILE);
76    putc(((w >> 8) & 0xff) as i32, f as *mut FILE);
77    putc(((w >> 16) & 0xff) as i32, f as *mut FILE);
78    putc(((w >> 24) & 0xff) as i32, f as *mut FILE);
79}
80
81
82
83#[no_mangle]
84pub unsafe extern "C" fn rand_r(seedp: *mut u32) -> i32 {
85    let seed = (*seedp).wrapping_mul(1103515245).wrapping_add(12345);
86    *seedp = seed;
87    ((seed >> 16) & 0x7fff) as i32
88}
89
90#[no_mangle]
91pub unsafe extern "C" fn reallocarray(ptr: *mut core::ffi::c_void, nmemb: usize, size: usize) -> *mut core::ffi::c_void {
92    let total = match nmemb.checked_mul(size) {
93        Some(v) => v,
94        None => { ERRNO!() = ENOMEM; return core::ptr::null_mut(); }
95    };
96    crate::realloc(ptr, total)
97}
98
99#[no_mangle]
100pub unsafe extern "C" fn __res_state() -> *mut core::ffi::c_void {
101    static mut STATE: [u8; 512] = [0; 512];
102    STATE.as_mut_ptr() as *mut core::ffi::c_void
103}
104
105#[no_mangle]
106pub unsafe extern "C" fn sbrk(increment: isize) -> *mut u8 {
107    // musl does not support growing the break directly (malloc uses mmap);
108    // sbrk(nonzero) always fails with ENOMEM.
109    if increment != 0 {
110        ERRNO!() = ENOMEM;
111        return -1isize as usize as *mut u8;
112    }
113    <Arch as Syscalls>::syscall1(SYS_BRK, 0) as *mut u8
114}
115
116#[no_mangle]
117pub unsafe extern "C" fn scalb(x: f64, exp: f64) -> f64 {
118    if x.is_nan() || exp.is_nan() { return x * exp; }
119    if !exp.is_finite() {
120        let r = if exp > 0.0 { x * exp } else { x / (-exp) };
121        if r.is_nan() && !x.is_nan() {
122            feraiseexcept(FE_INVALID);
123        }
124        return r;
125    }
126    if libm::rint(exp) != exp {
127        return __math_invalid(exp);
128    }
129    if exp > 65000.0 { return libm::scalbn(x, 65000); }
130    if -exp > 65000.0 { return libm::scalbn(x, -65000); }
131    libm::scalbn(x, exp as i32)
132}
133
134#[no_mangle]
135pub unsafe extern "C" fn scalbf(x: f32, exp: f32) -> f32 {
136    if x.is_nan() || exp.is_nan() { return x * exp; }
137    if !exp.is_finite() {
138        let r = if exp > 0.0 { x * exp } else { x / (-exp) };
139        if r.is_nan() && !x.is_nan() {
140            feraiseexcept(FE_INVALID);
141        }
142        return r;
143    }
144    if libm::rintf(exp) != exp {
145        return __math_invalidf(exp);
146    }
147    if exp > 65000.0 { return libm::scalbnf(x, 65000); }
148    if -exp > 65000.0 { return libm::scalbnf(x, -65000); }
149    libm::scalbnf(x, exp as i32)
150}
151
152#[no_mangle]
153pub unsafe extern "C" fn __sched_cpucount(setsize: usize, setp: *const u8) -> i32 {
154    let mut count = 0;
155    let mut i = 0;
156    while i < setsize * 8 {
157        if (*setp.add(i / 8) >> (i % 8)) & 1 != 0 { count += 1; }
158        i += 1;
159    }
160    count
161}
162
163
164
165#[no_mangle]
166pub unsafe extern "C" fn select(nfds: i32, readfds: *mut u8, writefds: *mut u8, exceptfds: *mut u8, timeout: *mut core::ffi::c_void) -> i32 {
167    syscall_ret(<Arch as Syscalls>::syscall6(SYS_SELECT, nfds as i64, readfds as i64, writefds as i64, exceptfds as i64, timeout as i64, 0)) as i32
168}
169
170
171
172#[no_mangle]
173pub unsafe extern "C" fn sendfile(out_fd: i32, in_fd: i32, offset: *mut i64, count: usize) -> isize {
174    syscall_ret(<Arch as Syscalls>::syscall4(SYS_SENDFILE, out_fd as i64, in_fd as i64, offset as i64, count as i64)) as isize
175}
176
177
178
179
180
181#[no_mangle]
182#[cfg(not(any(target_arch = "powerpc64", target_arch = "loongarch64")))]
183pub unsafe extern "C" fn __setjmp(env: *mut core::ffi::c_void) -> i32 {
184    crate::setjmp(env as *mut u64)
185}
186
187#[no_mangle]
188#[cfg(not(any(target_arch = "powerpc64", target_arch = "loongarch64")))]
189pub unsafe extern "C" fn _setjmp(env: *mut core::ffi::c_void) -> i32 {
190    crate::setjmp(env as *mut u64)
191}
192
193#[no_mangle]
194pub unsafe extern "C" fn setlinebuf(stream: *mut core::ffi::c_void) {
195    setvbuf(stream as *mut FILE, core::ptr::null_mut(), 1, 0);
196}
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212#[no_mangle]
213pub unsafe extern "C" fn significand(x: f64) -> f64 {
214    let bits = x.to_bits();
215    let exp = ((bits >> 52) & 0x7ff) as i32;
216    if exp == 0 || exp == 0x7ff { return x; }
217    f64::from_bits((bits & 0x000fffffffffffff) | (1023u64 << 52))
218}
219
220#[no_mangle]
221pub unsafe extern "C" fn significandf(x: f32) -> f32 {
222    let bits = x.to_bits();
223    let exp = ((bits >> 23) & 0xff) as i32;
224    if exp == 0 || exp == 0xff { return x; }
225    f32::from_bits((bits & 0x007fffff) | (127u32 << 23))
226}
227
228
229
230
231
232#[no_mangle]
233pub unsafe extern "C" fn sigset(sig: i32, handler: usize) -> usize {
234    // BSD/glibc-compatible sigset() is an alias for signal().
235    signal(sig, handler)
236}
237
238#[no_mangle]
239pub unsafe extern "C" fn sincos(x: f64, sin: *mut f64, cos: *mut f64) {
240    *sin = libm::sin(x);
241    *cos = libm::cos(x);
242}
243
244#[no_mangle]
245pub unsafe extern "C" fn sincosf(x: f32, sin: *mut f32, cos: *mut f32) {
246    *sin = libm::sinf(x);
247    *cos = libm::cosf(x);
248}
249
250
251
252
253
254
255
256
257
258unsafe fn nd_ascii_lower(c: c_char) -> c_int {
259    let b = c as u8;
260    if b >= b'A' && b <= b'Z' { (b + 32) as c_int } else { b as c_int }
261}
262
263unsafe fn nd_strncasecmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
264    let mut i = 0usize;
265    while i < n {
266        let c1 = nd_ascii_lower(*s1.add(i));
267        let c2 = nd_ascii_lower(*s2.add(i));
268        if c1 != c2 || c1 == 0 { return c1 - c2; }
269        i += 1;
270    }
271    0
272}
273
274#[no_mangle]
275pub unsafe extern "C" fn strcasecmp(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char) -> i32 {
276    nd_strncasecmp(s1, s2, usize::MAX)
277}
278
279#[no_mangle]
280pub unsafe extern "C" fn __strcasecmp_l(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char, _loc: *mut core::ffi::c_void) -> i32 {
281    nd_strncasecmp(s1, s2, usize::MAX)
282}
283
284#[no_mangle]
285pub unsafe extern "C" fn strcasestr(haystack: *const core::ffi::c_char, needle: *const core::ffi::c_char) -> *mut core::ffi::c_char {
286    let nlen = crate::strlen(needle);
287    if nlen == 0 { return haystack as *mut core::ffi::c_char; }
288    let mut i = 0;
289    loop {
290        let h = haystack.add(i);
291        if *h == 0 { return core::ptr::null_mut(); }
292        if crate::strncasecmp(h, needle, nlen) == 0 { return h as *mut core::ffi::c_char; }
293        i += 1;
294    }
295}
296
297#[no_mangle]
298pub unsafe extern "C" fn __strcoll_l(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char, _loc: *mut core::ffi::c_void) -> i32 {
299    crate::strcmp(s1 as *const u8, s2 as *const u8)
300}
301
302#[no_mangle]
303pub unsafe extern "C" fn strncasecmp(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char, n: usize) -> i32 {
304    nd_strncasecmp(s1, s2, n)
305}
306
307#[no_mangle]
308pub unsafe extern "C" fn __strncasecmp_l(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char, n: usize, _loc: *mut core::ffi::c_void) -> i32 {
309    nd_strncasecmp(s1, s2, n)
310}
311
312#[no_mangle]
313pub unsafe extern "C" fn strsep(stringp: *mut *mut core::ffi::c_char, delim: *const core::ffi::c_char) -> *mut core::ffi::c_char {
314    let s = *stringp;
315    if s.is_null() { return core::ptr::null_mut(); }
316    let mut p = s;
317    while *p != 0 {
318        let mut d = delim;
319        while *d != 0 {
320            if *p == *d { *p = 0; *stringp = p.add(1); return s; }
321            d = d.add(1);
322        }
323        p = p.add(1);
324    }
325    *stringp = core::ptr::null_mut();
326    s
327}
328
329#[no_mangle]
330pub unsafe extern "C" fn strtoimax(nptr: *const core::ffi::c_char, endptr: *mut *mut core::ffi::c_char, base: i32) -> i64 {
331    crate::strtol(nptr, endptr, base)
332}
333
334#[no_mangle]
335pub unsafe extern "C" fn strtok_r(s: *mut core::ffi::c_char, delim: *const core::ffi::c_char, saveptr: *mut *mut core::ffi::c_char) -> *mut core::ffi::c_char {
336    let mut s = if s.is_null() { *saveptr } else { s };
337    if s.is_null() { return core::ptr::null_mut(); }
338    // Skip leading delimiters
339    while *s != 0 {
340        let mut d = delim;
341        let mut found = false;
342        while *d != 0 {
343            if *s == *d { found = true; break; }
344            d = d.add(1);
345        }
346        if !found { break; }
347        s = s.add(1);
348    }
349    if *s == 0 { *saveptr = s; return core::ptr::null_mut(); }
350    let start = s;
351    // Find end of token
352    while *s != 0 {
353        let mut d = delim;
354        while *d != 0 {
355            if *s == *d { *s = 0; *saveptr = s.add(1); return start; }
356            d = d.add(1);
357        }
358        s = s.add(1);
359    }
360    *saveptr = s;
361    start
362}
363
364#[no_mangle]
365pub unsafe extern "C" fn strtoumax(nptr: *const core::ffi::c_char, endptr: *mut *mut core::ffi::c_char, base: i32) -> u64 {
366    crate::strtoul(nptr, endptr, base) as u64
367}
368
369#[no_mangle]
370pub unsafe extern "C" fn __strxfrm_l(dest: *mut core::ffi::c_char, src: *const core::ffi::c_char, n: usize, _loc: *mut core::ffi::c_void) -> usize {
371    crate::strlcpy(dest as *mut u8, src as *const u8, n)
372}
373
374#[no_mangle]
375pub unsafe extern "C" fn swab(src: *const u8, dest: *mut u8, nbytes: isize) {
376    let mut i = 0;
377    while i + 1 < nbytes {
378        let a = *src.add(i as usize);
379        let b = *src.add(i as usize + 1);
380        *dest.add(i as usize) = b;
381        *dest.add(i as usize + 1) = a;
382        i += 2;
383    }
384}
385
386
387
388
389
390
391
392
393
394
395
396#[no_mangle]
397pub unsafe extern "C" fn __tolower_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
398    if c >= 'A' as i32 && c <= 'Z' as i32 { c + 32 } else { c }
399}
400
401#[no_mangle]
402pub unsafe extern "C" fn __toupper_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
403    if c >= 'a' as i32 && c <= 'z' as i32 { c - 32 } else { c }
404}
405
406#[no_mangle]
407pub unsafe extern "C" fn ulimit(cmd: c_int, mut args: ...) -> c_long {
408    let mut rl: Rlimit = core::mem::zeroed();
409    if getrlimit(RLIMIT_FSIZE, &mut rl) != 0 { return -1; }
410    if cmd == 2 {
411        let val = args.next_arg::<c_long>();
412        rl.rlim_cur = 512u64.wrapping_mul(val as u64);
413        if setrlimit(RLIMIT_FSIZE, &rl) != 0 { return -1; }
414    }
415    (rl.rlim_cur / 512) as c_long
416}
417
418#[repr(C)]
419struct UtsName {
420    sysname: [u8; 65],
421    nodename: [u8; 65],
422    release: [u8; 65],
423    version: [u8; 65],
424    machine: [u8; 65],
425    domainname: [u8; 65],
426}
427
428#[no_mangle]
429pub unsafe extern "C" fn uname(buf: *mut c_void) -> c_int {
430    // Let the kernel validate the user pointer: an invalid but non-null
431    // address must produce EFAULT instead of a fault in a local copy.
432    let r = <Arch as Syscalls>::syscall1(SYS_UNAME, buf as i64);
433    syscall_ret(r) as c_int
434}
435
436#[no_mangle]
437pub unsafe extern "C" fn unshare(flags: i32) -> i32 {
438    let r = <Arch as Syscalls>::syscall1(SYS_UNSHARE, flags as i64);
439    syscall_ret(r) as c_int
440}
441
442#[no_mangle]
443pub unsafe extern "C" fn updwtmpx(wtmpx_file: *const core::ffi::c_char, utx: *const core::ffi::c_void) {
444    updwtmp(wtmpx_file, utx)
445}
446
447#[no_mangle]
448pub unsafe extern "C" fn __uselocale(loc: *mut core::ffi::c_void) -> *mut core::ffi::c_void {
449    loc
450}
451
452#[repr(C)]
453pub struct Utimbuf { actime: i64, modtime: i64 }
454
455#[no_mangle]
456pub unsafe extern "C" fn utime(filename: *const c_char, times: *const Utimbuf) -> c_int {
457    if times.is_null() {
458        return utimensat(AT_FDCWD, filename, core::ptr::null(), 0);
459    }
460    let ts = [
461        timespec { tv_sec: (*times).actime, tv_nsec: 0 },
462        timespec { tv_sec: (*times).modtime, tv_nsec: 0 },
463    ];
464    utimensat(AT_FDCWD, filename, ts.as_ptr(), 0)
465}
466
467#[no_mangle]
468pub unsafe extern "C" fn vhangup() -> c_int {
469    let r = <Arch as Syscalls>::syscall0(SYS_VHANGUP);
470    syscall_ret(r) as c_int
471}
472
473#[no_mangle]
474pub unsafe extern "C" fn vmsplice(fd: i32, iov: *const c_void, nr_segs: usize, flags: u32) -> isize {
475    let r = <Arch as Syscalls>::syscall4(SYS_VMSPLICE, fd as i64, iov as i64, nr_segs as i64, flags as i64);
476    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
477}
478
479
480
481#[no_mangle]
482pub unsafe extern "C" fn wait4(pid: i32, wstatus: *mut i32, options: i32, rusage: *mut core::ffi::c_void) -> i32 {
483    syscall_ret(<Arch as Syscalls>::syscall4(SYS_WAIT4, pid as i64, wstatus as i64, options as i64, rusage as i64)) as i32
484}
485
486#[no_mangle]
487pub unsafe extern "C" fn waitid(idtype: i32, id: u32, infop: *mut core::ffi::c_void, options: i32) -> i32 {
488    // The kernel takes a fifth rusage pointer; it must be NULL, not whatever
489    // happens to be in r10.
490    syscall_ret(<Arch as Syscalls>::syscall5(
491        SYS_WAITID,
492        idtype as i64,
493        id as i64,
494        infop as i64,
495        options as i64,
496        0,
497    )) as i32
498}
499
500#[no_mangle]
501pub unsafe extern "C" fn __wcscoll_l(s1: *const u32, s2: *const u32, _loc: *mut core::ffi::c_void) -> i32 {
502    crate::wcscmp(s1 as *const i32, s2 as *const i32)
503}
504
505#[no_mangle]
506pub unsafe extern "C" fn __wcsxfrm_l(dest: *mut u32, src: *const u32, n: usize, _loc: *mut core::ffi::c_void) -> usize {
507    wcsxfrm(dest as *mut wchar_t, src as *const wchar_t, n)
508}
509
510#[inline]
511fn wctable_width(table: &[u8], wc: u32) -> bool {
512    let page = table[(wc >> 8) as usize] as usize;
513    (table[page * 32 + ((wc & 255) >> 3) as usize] >> (wc & 7)) & 1 != 0
514}
515
516#[no_mangle]
517pub unsafe extern "C" fn wcwidth(c: u32) -> i32 {
518    // Port of musl's wcwidth, using the nonspacing/wide tables.
519    let wc = c;
520    if wc < 0xff {
521        return if ((wc + 1) & 0x7f) >= 0x21 {
522            1
523        } else if wc != 0 {
524            -1
525        } else {
526            0
527        };
528    }
529    if (wc & 0xfffe_ffff) < 0xfffe {
530        if wctable_width(&WCTYPE_NONSPACING_TABLE, wc) {
531            return 0;
532        }
533        if wctable_width(&WCTYPE_WIDE_TABLE, wc) {
534            return 2;
535        }
536        return 1;
537    }
538    if (wc & 0xfffe) == 0xfffe {
539        return -1;
540    }
541    if wc.wrapping_sub(0x20000) < 0x20000 {
542        return 2;
543    }
544    if wc == 0xe0001 || wc.wrapping_sub(0xe0020) < 0x5f || wc.wrapping_sub(0xe0100) < 0xef {
545        return 0;
546    }
547    1
548}
549
550
551
552
553