Skip to main content

c/
compat_syscalls.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// timer/clock functions
6// ============================================================
7
8#[no_mangle]
9pub unsafe extern "C" fn timer_create(clockid: i32, sevp: *mut core::ffi::c_void, timerid: *mut i32) -> i32 {
10    syscall_ret(<Arch as Syscalls>::syscall3(SYS_TIMER_CREATE, clockid as i64, sevp as i64, timerid as i64)) as i32
11}
12
13#[no_mangle]
14pub unsafe extern "C" fn timer_delete(timerid: i32) -> i32 {
15    syscall_ret(<Arch as Syscalls>::syscall1(SYS_TIMER_DELETE, timerid as i64)) as i32
16}
17
18#[no_mangle]
19pub unsafe extern "C" fn timer_settime(timerid: i32, flags: i32, new_value: *const core::ffi::c_void, old_value: *mut core::ffi::c_void) -> i32 {
20    syscall_ret(<Arch as Syscalls>::syscall4(SYS_TIMER_SETTIME, timerid as i64, flags as i64, new_value as i64, old_value as i64)) as i32
21}
22
23#[no_mangle]
24pub unsafe extern "C" fn timer_gettime(timerid: i32, curr_value: *mut core::ffi::c_void) -> i32 {
25    syscall_ret(<Arch as Syscalls>::syscall2(SYS_TIMER_GETTIME, timerid as i64, curr_value as i64)) as i32
26}
27
28#[no_mangle]
29pub unsafe extern "C" fn timer_getoverrun(timerid: i32) -> i32 {
30    syscall_ret(<Arch as Syscalls>::syscall1(SYS_TIMER_GETOVERRUN, timerid as i64)) as i32
31}
32
33#[no_mangle]
34pub unsafe extern "C" fn clock_adjtime(_clockid: i32, buf: *mut core::ffi::c_void) -> i32 {
35    syscall_ret(<Arch as Syscalls>::syscall2(SYS_ADJTIMEX, buf as i64, 0)) as i32
36}
37
38#[no_mangle]
39pub unsafe extern "C" fn clock_getcpuclockid(pid: i32, clockid: *mut i32) -> i32 {
40    if clockid.is_null() { return EINVAL; }
41    let id: i32 = ((-(pid as i64) - 1) * 8 + 2) as i32;
42    let mut ts: timespec = core::mem::zeroed();
43    let r = <Arch as Syscalls>::syscall2(SYS_CLOCK_GETRES, id as i64, &mut ts as *mut timespec as i64);
44    if r < 0 {
45        let e = (-r) as c_int;
46        return if e == EINVAL { 3 } else { e };
47    }
48    *clockid = id;
49    0
50}
51
52// ============================================================
53// String functions
54// ============================================================
55
56#[no_mangle]
57pub unsafe extern "C" fn strcoll(s1: *const core::ffi::c_char, s2: *const core::ffi::c_char) -> i32 {
58    // musl's C locale compares byte strings directly (strcmp semantics).
59    let mut i = 0;
60    loop {
61        let c1 = *s1.add(i) as u8;
62        let c2 = *s2.add(i) as u8;
63        if c1 == 0 || c2 == 0 { return (c1 as i32) - (c2 as i32); }
64        if c1 != c2 { return (c1 as i32) - (c2 as i32); }
65        i += 1;
66    }
67}
68
69#[no_mangle]
70pub unsafe extern "C" fn strxfrm(dest: *mut core::ffi::c_char, src: *const core::ffi::c_char, n: usize) -> usize {
71    // musl/C locale: the transformed string is the source itself.  POSIX
72    // requires returning the full length even when it does not fit, and only
73    // copying when the destination has room for the terminator.
74    let len = crate::strlen(src);
75    if n > len && !dest.is_null() {
76        core::ptr::copy_nonoverlapping(src, dest, len + 1);
77    }
78    len
79}
80
81// ============================================================
82// Directory functions
83// ============================================================
84
85#[no_mangle]
86pub unsafe extern "C" fn scandir(
87    dirp: *const core::ffi::c_char,
88    namelist: *mut *mut *mut core::ffi::c_void,
89    filter: Option<unsafe extern "C" fn(*const core::ffi::c_void) -> i32>,
90    compar: Option<unsafe extern "C" fn(*const core::ffi::c_void, *const core::ffi::c_void) -> i32>,
91) -> c_int {
92    if dirp.is_null() || namelist.is_null() {
93        ERRNO!() = EINVAL;
94        return -1;
95    }
96    *namelist = core::ptr::null_mut();
97    let dir = opendir(dirp);
98    if dir.is_null() { return -1; }
99
100    let mut cap = 16usize;
101    let mut count = 0usize;
102    let mut list = crate::malloc(cap * core::mem::size_of::<*mut c_void>()) as *mut *mut c_void;
103    if list.is_null() { closedir(dir); ERRNO!() = ENOMEM; return -1; }
104
105    loop {
106        let ent = readdir(dir);
107        if ent.is_null() { break; }
108        if let Some(filter) = filter {
109            if filter(ent as *const c_void) == 0 { continue; }
110        }
111        if count == cap {
112            let new_cap = match cap.checked_mul(2) {
113                Some(n) => n,
114                None => {
115                    for i in 0..count { crate::free(*list.add(i)); }
116                    crate::free(list as *mut c_void);
117                    closedir(dir);
118                    ERRNO!() = ENOMEM;
119                    return -1;
120                }
121            };
122            let p = crate::realloc(
123                list as *mut c_void,
124                new_cap * core::mem::size_of::<*mut c_void>(),
125            ) as *mut *mut c_void;
126            if p.is_null() {
127                for i in 0..count { crate::free(*list.add(i)); }
128                crate::free(list as *mut c_void);
129                closedir(dir);
130                ERRNO!() = ENOMEM;
131                return -1;
132            }
133            list = p;
134            cap = new_cap;
135        }
136        // scandir returns an array of `struct dirent *`, not an array of name
137        // strings.  Copy the whole kernel dirent record so callers can read
138        // d_ino/d_off/d_reclen/d_type as well as d_name.
139        let reclen = (* (ent as *const linux_dirent64)).d_reclen as usize;
140        let name_off = core::mem::offset_of!(linux_dirent64, d_name);
141        if reclen <= name_off { continue; }
142        let copy = crate::malloc(reclen);
143        if copy.is_null() {
144            for i in 0..count { crate::free(*list.add(i)); }
145            crate::free(list as *mut c_void);
146            closedir(dir);
147            ERRNO!() = ENOMEM;
148            return -1;
149        }
150        core::ptr::copy_nonoverlapping(ent as *const u8, copy as *mut u8, reclen);
151        *list.add(count) = copy;
152        count += 1;
153    }
154    closedir(dir);
155
156    if let Some(compar) = compar {
157        if count > 1 {
158            qsort(
159                list as *mut c_void,
160                count,
161                core::mem::size_of::<*mut c_void>(),
162                compar,
163            );
164        }
165    }
166    *namelist = list;
167    count as c_int
168}
169
170#[no_mangle]
171pub unsafe extern "C" fn alphasort(a: *const *const core::ffi::c_void, b: *const *const core::ffi::c_void) -> i32 {
172    let name_off = core::mem::offset_of!(crate::DirentCopy, d_name);
173    let s1 = (*a as *const u8).add(name_off) as *const core::ffi::c_char;
174    let s2 = (*b as *const u8).add(name_off) as *const core::ffi::c_char;
175    crate::strcmp(s1 as *const u8, s2 as *const u8)
176}
177
178#[no_mangle]
179pub unsafe extern "C" fn versionsort(a: *const *const core::ffi::c_void, b: *const *const core::ffi::c_void) -> i32 {
180    let name_off = core::mem::offset_of!(crate::DirentCopy, d_name);
181    let s1 = (*a as *const u8).add(name_off) as *const core::ffi::c_char;
182    let s2 = (*b as *const u8).add(name_off) as *const core::ffi::c_char;
183    crate::strverscmp(s1, s2)
184}
185
186// ============================================================
187
188
189// ============================================================
190// err functions
191// ============================================================
192
193unsafe fn vwarn_impl(fmt: *const c_char, ap: VaList, with_errno: bool) {
194    let mut buf = [0 as c_char; 1024];
195    let n = vsnprintf(buf.as_mut_ptr(), buf.len(), fmt, ap);
196    if n > 0 {
197        let l = core::cmp::min(n as usize, buf.len() - 1);
198        crate::write(2, buf.as_ptr() as *const c_void, l);
199    }
200    if with_errno {
201        let e = ERRNO!();
202        crate::write(2, b": ".as_ptr() as *const c_void, 2);
203        let msg = strerror(e);
204        crate::write(2, msg as *const c_void, crate::strlen(msg));
205    }
206    crate::write(2, b"\n".as_ptr() as *const c_void, 1);
207}
208
209#[no_mangle]
210pub unsafe extern "C" fn verr(eval: c_int, fmt: *const c_char, ap: VaList) {
211    vwarn_impl(fmt, ap, true);
212    crate::exit(eval);
213}
214
215#[no_mangle]
216pub unsafe extern "C" fn verrx(eval: c_int, fmt: *const c_char, ap: VaList) {
217    vwarn_impl(fmt, ap, false);
218    crate::exit(eval);
219}
220
221#[no_mangle]
222pub unsafe extern "C" fn vwarn(fmt: *const c_char, ap: VaList) {
223    vwarn_impl(fmt, ap, true);
224}
225
226#[no_mangle]
227pub unsafe extern "C" fn vwarnx(fmt: *const c_char, ap: VaList) {
228    vwarn_impl(fmt, ap, false);
229}
230
231#[no_mangle]
232pub unsafe extern "C" fn err(eval: c_int, fmt: *const c_char, args: ...) {
233    verr(eval, fmt, args);
234}
235
236#[no_mangle]
237pub unsafe extern "C" fn errx(eval: c_int, fmt: *const c_char, args: ...) {
238    verrx(eval, fmt, args);
239}
240
241#[no_mangle]
242pub unsafe extern "C" fn warn(fmt: *const c_char, args: ...) {
243    vwarn(fmt, args);
244}
245
246#[no_mangle]
247pub unsafe extern "C" fn warnx(fmt: *const c_char, args: ...) {
248    vwarnx(fmt, args);
249}
250
251// ============================================================
252// ecvt functions
253// ============================================================
254
255#[no_mangle]
256pub unsafe extern "C" fn ecvt(value: f64, ndigit: i32, decpt: *mut i32, sign: *mut i32) -> *mut core::ffi::c_char {
257    static mut BUF: [core::ffi::c_char; 80] = [0; 80];
258    if value < 0.0 { *sign = 1; } else { *sign = 0; }
259    let v = if value < 0.0 { -value } else { value };
260    if v == 0.0 { *decpt = 0; BUF[0] = 0; return BUF.as_mut_ptr(); }
261    let mut d = 0i32;
262    let mut tmp = v;
263    while tmp >= 10.0 { tmp /= 10.0; d += 1; }
264    while tmp < 1.0 { tmp *= 10.0; d -= 1; }
265    *decpt = d + 1;
266    let mut i = 0;
267    let mut val = tmp;
268    while i < ndigit as usize && i < 79 {
269        let digit = val as i32;
270        BUF[i] = (b'0' + digit as u8) as c_char;
271        val = (val - digit as f64) * 10.0;
272        i += 1;
273    }
274    BUF[i] = 0;
275    BUF.as_mut_ptr()
276}
277
278#[no_mangle]
279pub unsafe extern "C" fn fcvt(value: f64, ndigit: i32, decpt: *mut i32, sign: *mut i32) -> *mut core::ffi::c_char {
280    static mut BUF: [core::ffi::c_char; 80] = [0; 80];
281    if value < 0.0 { *sign = 1; } else { *sign = 0; }
282    let v = if value < 0.0 { -value } else { value };
283    if v == 0.0 { *decpt = 0; BUF[0] = 0; return BUF.as_mut_ptr(); }
284    let mut d = 0i32;
285    let mut tmp = v;
286    while tmp >= 10.0 { tmp /= 10.0; d += 1; }
287    while tmp < 1.0 { tmp *= 10.0; d -= 1; }
288    *decpt = d + 1;
289    let mut val = tmp;
290    let mut i = 0;
291    while i < (*decpt) as usize && i < 79 {
292        let digit = val as i32;
293        BUF[i] = (b'0' + digit as u8) as c_char;
294        val = (val - digit as f64) * 10.0;
295        i += 1;
296    }
297    if ndigit > 0 && i < 79 {
298        BUF[i] = '.' as c_char;
299        i += 1;
300        let mut frac = val;
301        let mut j = 0;
302        while j < ndigit as usize && i < 79 {
303            let digit = frac as i32;
304            BUF[i] = (b'0' + digit as u8) as c_char;
305            frac = (frac - digit as f64) * 10.0;
306            i += 1;
307            j += 1;
308        }
309    }
310    BUF[i] = 0;
311    BUF.as_mut_ptr()
312}
313
314#[no_mangle]
315pub unsafe extern "C" fn gcvt(value: f64, ndigit: i32, buf: *mut core::ffi::c_char) -> *mut core::ffi::c_char {
316    if buf.is_null() {
317        return buf;
318    }
319    // musl implements gcvt as sprintf("%.*g"); cap the write at a size that
320    // always covers %g's output for the requested precision.
321    let cap = if ndigit > 0 {
322        (ndigit as usize).saturating_mul(2).saturating_add(32).min(4096)
323    } else {
324        32
325    };
326    crate::snprintf(buf, cap, b"%.*g\0".as_ptr() as *const core::ffi::c_char, ndigit, value);
327    buf
328}
329
330// ============================================================
331// usershell functions
332// ============================================================
333
334static mut SHELLS: [*const core::ffi::c_char; 3] = [
335    b"/bin/sh\0".as_ptr() as *const core::ffi::c_char,
336    b"/bin/bash\0".as_ptr() as *const core::ffi::c_char,
337    core::ptr::null(),
338];
339static mut SHELL_IDX: usize = 0;
340
341#[no_mangle]
342pub unsafe extern "C" fn getusershell() -> *mut core::ffi::c_char {
343    let s = SHELLS[SHELL_IDX];
344    if s.is_null() {
345        SHELL_IDX = 0;
346        return core::ptr::null_mut();
347    }
348    SHELL_IDX += 1;
349    s as *mut core::ffi::c_char
350}
351
352#[no_mangle]
353pub unsafe extern "C" fn setusershell() {
354    SHELL_IDX = 0;
355}
356
357#[no_mangle]
358pub unsafe extern "C" fn endusershell() {
359    SHELL_IDX = 0;
360}
361
362// ============================================================
363// ether functions
364// ============================================================
365
366#[no_mangle]
367pub unsafe extern "C" fn ether_aton(s: *const core::ffi::c_char) -> *mut u8 {
368    static mut EA: [u8; 6] = [0; 6];
369    if ether_aton_r(s, EA.as_mut_ptr()).is_null() { core::ptr::null_mut() } else { EA.as_mut_ptr() }
370}
371
372#[no_mangle]
373pub unsafe extern "C" fn ether_aton_r(s: *const core::ffi::c_char, ea: *mut u8) -> *mut u8 {
374    if ea.is_null() { return core::ptr::null_mut(); }
375    let mut i = 0;
376    let mut j = 0;
377    let mut val: u32 = 0;
378    let mut digits = 0;
379    while *s.add(i) != 0 && j < 6 {
380        let c = *s.add(i);
381        if c == ':' as c_char {
382            if digits == 0 { return core::ptr::null_mut(); }
383            *ea.add(j) = val as u8;
384            j += 1;
385            val = 0;
386            digits = 0;
387        } else if (c >= '0' as c_char && c <= '9' as c_char) || (c >= 'a' as c_char && c <= 'f' as c_char) || (c >= 'A' as c_char && c <= 'F' as c_char) {
388            let d = if c >= '0' as c_char && c <= '9' as c_char { (c - '0' as c_char) as u32 }
389                   else if c >= 'a' as c_char { (c - 'a' as c_char + 10) as u32 }
390                   else { (c - 'A' as c_char + 10) as u32 };
391            val = val * 16 + d;
392            if val > 255 { return core::ptr::null_mut(); }
393            digits += 1;
394            if digits > 2 { return core::ptr::null_mut(); }
395        } else {
396            return core::ptr::null_mut();
397        }
398        i += 1;
399    }
400    if digits == 0 || j != 5 { return core::ptr::null_mut(); }
401    *ea.add(j) = val as u8;
402    ea
403}
404
405#[no_mangle]
406pub unsafe extern "C" fn ether_ntoa(ea: *const u8) -> *mut core::ffi::c_char {
407    static mut BUF: [core::ffi::c_char; 18] = [0; 18];
408    ether_ntoa_r(ea, BUF.as_mut_ptr());
409    BUF.as_mut_ptr()
410}
411
412#[no_mangle]
413pub unsafe extern "C" fn ether_ntoa_r(ea: *const u8, buf: *mut core::ffi::c_char) -> *mut core::ffi::c_char {
414    let a = *ea.add(0); let b = *ea.add(1); let c = *ea.add(2);
415    let d = *ea.add(3); let e = *ea.add(4); let f = *ea.add(5);
416    let hex = b"0123456789abcdef\0".as_ptr() as *const u8;
417    let mut i = 0;
418    *buf.add(i) = *hex.add((a >> 4) as usize) as c_char; i += 1;
419    *buf.add(i) = *hex.add((a & 0xf) as usize) as c_char; i += 1;
420    *buf.add(i) = ':' as c_char; i += 1;
421    *buf.add(i) = *hex.add((b >> 4) as usize) as c_char; i += 1;
422    *buf.add(i) = *hex.add((b & 0xf) as usize) as c_char; i += 1;
423    *buf.add(i) = ':' as c_char; i += 1;
424    *buf.add(i) = *hex.add((c >> 4) as usize) as c_char; i += 1;
425    *buf.add(i) = *hex.add((c & 0xf) as usize) as c_char; i += 1;
426    *buf.add(i) = ':' as c_char; i += 1;
427    *buf.add(i) = *hex.add((d >> 4) as usize) as c_char; i += 1;
428    *buf.add(i) = *hex.add((d & 0xf) as usize) as c_char; i += 1;
429    *buf.add(i) = ':' as c_char; i += 1;
430    *buf.add(i) = *hex.add((e >> 4) as usize) as c_char; i += 1;
431    *buf.add(i) = *hex.add((e & 0xf) as usize) as c_char; i += 1;
432    *buf.add(i) = ':' as c_char; i += 1;
433    *buf.add(i) = *hex.add((f >> 4) as usize) as c_char; i += 1;
434    *buf.add(i) = *hex.add((f & 0xf) as usize) as c_char; i += 1;
435    *buf.add(i) = 0;
436    buf
437}
438
439// ============================================================
440// utmpx functions
441// ============================================================
442
443#[no_mangle]
444pub unsafe extern "C" fn getutxent() -> *mut core::ffi::c_void {
445    getutent()
446}
447
448#[no_mangle]
449pub unsafe extern "C" fn getutxid(ut: *const core::ffi::c_void) -> *mut core::ffi::c_void {
450    getutid(ut)
451}
452
453#[no_mangle]
454pub unsafe extern "C" fn getutxline(ut: *const core::ffi::c_void) -> *mut core::ffi::c_void {
455    getutline(ut)
456}
457
458#[no_mangle]
459pub unsafe extern "C" fn pututxline(ut: *const core::ffi::c_void) -> *mut core::ffi::c_void {
460    pututline(ut)
461}
462
463#[no_mangle]
464pub unsafe extern "C" fn setutxent() {
465    setutent()
466}
467
468#[no_mangle]
469pub unsafe extern "C" fn endutxent() {
470    endutent()
471}
472
473// ============================================================
474// exec functions
475// ============================================================
476
477#[no_mangle]
478pub unsafe extern "C" fn execv(path: *const core::ffi::c_char, argv: *const *const core::ffi::c_char) -> i32 {
479    crate::execve(path, argv, __environ as *const *const core::ffi::c_char)
480}
481
482#[no_mangle]
483pub unsafe extern "C" fn execlp(file: *const core::ffi::c_char, _args: ...) -> i32 {
484    let mut cap = 16usize;
485    let mut argv = malloc(cap * core::mem::size_of::<*const core::ffi::c_char>()) as *mut *const core::ffi::c_char;
486    if argv.is_null() { ERRNO!() = ENOMEM; return -1; }
487    let mut i = 0usize;
488    let mut args = _args;
489    loop {
490        let arg = args.next_arg::<*const core::ffi::c_char>();
491        if arg.is_null() { break; }
492        if i + 1 >= cap {
493            cap = match cap.checked_mul(2) { Some(v) => v, None => { free(argv as *mut c_void); ERRNO!() = ENOMEM; return -1; } };
494            let grown = realloc(argv as *mut c_void, cap * core::mem::size_of::<*const core::ffi::c_char>()) as *mut *const core::ffi::c_char;
495            if grown.is_null() { free(argv as *mut c_void); ERRNO!() = ENOMEM; return -1; }
496            argv = grown;
497        }
498        *argv.add(i) = arg;
499        i += 1;
500    }
501    *argv.add(i) = core::ptr::null();
502    let r = execvp(file, argv);
503    free(argv as *mut c_void);
504    r
505}
506
507#[no_mangle]
508pub unsafe extern "C" fn execvp(file: *const core::ffi::c_char, argv: *const *const core::ffi::c_char) -> i32 {
509    execvpe(file, argv, __environ as *const *const core::ffi::c_char)
510}
511
512unsafe fn write_decimal(dst: *mut c_char, mut value: usize) -> usize {
513    let mut tmp = [0u8; 20];
514    let mut n = 0usize;
515    if value == 0 {
516        tmp[0] = b'0';
517        n = 1;
518    } else {
519        while value > 0 {
520            tmp[n] = b'0' + (value % 10) as u8;
521            value /= 10;
522            n += 1;
523        }
524    }
525    for i in 0..n {
526        *dst.add(i) = tmp[n - 1 - i] as c_char;
527    }
528    n
529}
530
531#[no_mangle]
532pub unsafe extern "C" fn fexecve(fd: i32, argv: *const *const core::ffi::c_char, envp: *const *const core::ffi::c_char) -> i32 {
533    // Read /proc/self/fd/N to get the path
534    let mut path: [core::ffi::c_char; 64] = [0; 64];
535    let mut i = 0;
536    let src = b"/proc/self/fd/\0".as_ptr() as *const core::ffi::c_char;
537    while i < 63 && *src.add(i) != 0 { path[i] = *src.add(i); i += 1; }
538    if fd < 0 { ERRNO!() = EBADF; return -1; }
539    i += write_decimal(path.as_mut_ptr().add(i), fd as usize);
540    path[i] = 0;
541    crate::execve(path.as_ptr(), argv, envp)
542}
543
544// ============================================================
545// conf functions
546// ============================================================
547
548#[no_mangle]
549pub unsafe extern "C" fn confstr(name: i32, buf: *mut core::ffi::c_char, len: usize) -> usize {
550    // _CS_PATH is the only value with content; the POSIX_V6/V7 option
551    // strings are empty on Linux. Truncation follows snprintf semantics.
552    let value: &[u8] = if name == 0 {
553        b"/bin:/usr/bin"
554    } else if (1..=37).contains(&name) {
555        b""
556    } else {
557        ERRNO!() = EINVAL;
558        return 0;
559    };
560    let need = value.len() + 1;
561    if !buf.is_null() && len > 0 {
562        let n = core::cmp::min(value.len(), len - 1);
563        core::ptr::copy_nonoverlapping(value.as_ptr(), buf as *mut u8, n);
564        *buf.add(n) = 0;
565    }
566    need
567}
568
569#[no_mangle]
570pub unsafe extern "C" fn pathconf(_path: *const core::ffi::c_char, name: i32) -> i64 {
571    fpathconf(-1, name)
572}
573
574#[no_mangle]
575pub unsafe extern "C" fn fpathconf(_fd: i32, name: i32) -> i64 {
576    // Same policy as musl's conf/fpathconf.c: the values are filesystem
577    // independent defaults, so the path/fd argument is not consulted.
578    match name {
579        0 => 8,       // _PC_LINK_MAX (_POSIX_LINK_MAX)
580        1 => 255,     // _PC_MAX_CANON
581        2 => 255,     // _PC_MAX_INPUT
582        3 => 255,     // _PC_NAME_MAX
583        4 => 4096,    // _PC_PATH_MAX
584        5 => 4096,    // _PC_PIPE_BUF
585        6 => 1,       // _PC_CHOWN_RESTRICTED
586        7 => 1,       // _PC_NO_TRUNC
587        8 => 0,       // _PC_VDISABLE
588        9 => 1,       // _PC_SYNC_IO
589        10 => -1,     // _PC_ASYNC_IO
590        11 => -1,     // _PC_PRIO_IO
591        12 => -1,     // _PC_SOCK_MAXBUF
592        13 => 64,     // _PC_FILESIZEBITS
593        14..=17 => 4096, // _PC_REC_*_XFER_SIZE/_ALIGN
594        18 => 4096,   // _PC_ALLOC_SIZE_MIN
595        19 => -1,     // _PC_SYMLINK_MAX
596        20 => 1,      // _PC_2_SYMLINKS
597        _ => {
598            ERRNO!() = EINVAL;
599            -1
600        }
601    }
602}
603
604// ============================================================
605// memory functions
606// ============================================================
607
608#[no_mangle]
609pub unsafe extern "C" fn explicit_bzero(s: *mut u8, n: usize) {
610    core::ptr::write_bytes(s, 0, n);
611    core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
612}
613
614#[no_mangle]
615pub unsafe extern "C" fn memccpy(dest: *mut u8, src: *const u8, c: i32, n: usize) -> *mut u8 {
616    let mut i = 0;
617    while i < n {
618        let byte = *src.add(i);
619        *dest.add(i) = byte;
620        if byte == c as u8 {
621            return dest.add(i + 1);
622        }
623        i += 1;
624    }
625    core::ptr::null_mut()
626}
627
628// ============================================================
629// *at functions
630// ============================================================
631
632#[no_mangle]
633pub unsafe extern "C" fn faccessat(dirfd: i32, pathname: *const core::ffi::c_char, mode: i32, flags: i32) -> i32 {
634    syscall_ret(<Arch as Syscalls>::syscall4(SYS_FACCESSAT, dirfd as i64, pathname as i64, mode as i64, flags as i64)) as i32
635}
636
637#[no_mangle]
638pub unsafe extern "C" fn fchmodat(dirfd: i32, pathname: *const core::ffi::c_char, mode: u32, flags: i32) -> i32 {
639    if flags == 0 {
640        return syscall_ret(<Arch as Syscalls>::syscall4(SYS_FCHMODAT, dirfd as i64, pathname as i64, mode as i64, 0)) as i32;
641    }
642    let r = <Arch as Syscalls>::syscall4(SYS_FCHMODAT2, dirfd as i64, pathname as i64, mode as i64, flags as i64);
643    if r != -(ENOSYS as i64) {
644        return syscall_ret(r) as i32;
645    }
646    if flags != AT_SYMLINK_NOFOLLOW {
647        // Only AT_SYMLINK_NOFOLLOW is defined for fchmodat.
648        ERRNO!() = EINVAL;
649        return -1;
650    }
651    // Kernel without fchmodat2 (or an emulator that does not translate it):
652    // chmod the /proc/self/fd handle instead, as musl does.
653    let mut st: Stat = core::mem::zeroed();
654    if fstatat(dirfd, pathname, &mut st, AT_SYMLINK_NOFOLLOW) != 0 {
655        return -1;
656    }
657    if st.st_mode & S_IFMT == S_IFLNK {
658        ERRNO!() = EOPNOTSUPP;
659        return -1;
660    }
661    let fd2 = openat(
662        dirfd,
663        pathname,
664        O_RDONLY | O_PATH | O_NOFOLLOW | O_NOCTTY | O_CLOEXEC,
665        0,
666    );
667    if fd2 < 0 {
668        if ERRNO!() == ELOOP {
669            ERRNO!() = EOPNOTSUPP;
670        }
671        return -1;
672    }
673    const PREFIX: &[u8] = b"/proc/self/fd/";
674    let mut proc = [0u8; 32];
675    proc[..PREFIX.len()].copy_from_slice(PREFIX);
676    let n = PREFIX.len()
677        + write_decimal(
678            proc.as_mut_ptr().add(PREFIX.len()) as *mut core::ffi::c_char,
679            fd2 as usize,
680        );
681    proc[n] = 0;
682    let mut st2: Stat = core::mem::zeroed();
683    let mut res: i32 = -1;
684    if stat(proc.as_ptr() as *const core::ffi::c_char, &mut st2) == 0 {
685        if st2.st_mode & S_IFMT == S_IFLNK {
686            ERRNO!() = EOPNOTSUPP;
687        } else {
688            res = syscall_ret(<Arch as Syscalls>::syscall4(
689                SYS_FCHMODAT,
690                AT_FDCWD as i64,
691                proc.as_ptr() as i64,
692                mode as i64,
693                0,
694            )) as i32;
695        }
696    }
697    close(fd2);
698    res
699}
700
701#[no_mangle]
702pub unsafe extern "C" fn fchownat(dirfd: i32, pathname: *const core::ffi::c_char, uid: u32, gid: u32, flags: i32) -> i32 {
703    syscall_ret(<Arch as Syscalls>::syscall5(SYS_FCHOWNAT, dirfd as i64, pathname as i64, uid as i64, gid as i64, flags as i64)) as i32
704}
705
706#[no_mangle]
707pub unsafe extern "C" fn linkat(olddirfd: i32, oldpath: *const core::ffi::c_char, newdirfd: i32, newpath: *const core::ffi::c_char, flags: i32) -> i32 {
708    syscall_ret(<Arch as Syscalls>::syscall5(SYS_LINKAT, olddirfd as i64, oldpath as i64, newdirfd as i64, newpath as i64, flags as i64)) as i32
709}
710
711#[no_mangle]
712pub unsafe extern "C" fn mkdirat(dirfd: i32, pathname: *const core::ffi::c_char, mode: u32) -> i32 {
713    syscall_ret(<Arch as Syscalls>::syscall3(SYS_MKDIRAT, dirfd as i64, pathname as i64, mode as i64)) as i32
714}
715
716#[no_mangle]
717pub unsafe extern "C" fn mkfifoat(dirfd: i32, pathname: *const core::ffi::c_char, mode: u32) -> i32 {
718    mknodat(dirfd, pathname, mode | 0o010000, 0)
719}
720
721#[no_mangle]
722pub unsafe extern "C" fn mknodat(dirfd: i32, pathname: *const core::ffi::c_char, mode: u32, dev: u64) -> i32 {
723    syscall_ret(<Arch as Syscalls>::syscall4(SYS_MKNODAT, dirfd as i64, pathname as i64, mode as i64, dev as i64)) as i32
724}
725
726#[no_mangle]
727pub unsafe extern "C" fn readlinkat(dirfd: i32, pathname: *const core::ffi::c_char, buf: *mut core::ffi::c_char, bufsiz: usize) -> isize {
728    syscall_ret(<Arch as Syscalls>::syscall4(SYS_READLINKAT, dirfd as i64, pathname as i64, buf as i64, bufsiz as i64)) as isize
729}
730
731#[no_mangle]
732pub unsafe extern "C" fn renameat(olddirfd: i32, oldpath: *const core::ffi::c_char, newdirfd: i32, newpath: *const core::ffi::c_char) -> i32 {
733    syscall_ret(<Arch as Syscalls>::syscall5(SYS_RENAMEAT2, olddirfd as i64, oldpath as i64, newdirfd as i64, newpath as i64, 0)) as i32
734}
735
736#[no_mangle]
737pub unsafe extern "C" fn renameat2(olddirfd: i32, oldpath: *const core::ffi::c_char, newdirfd: i32, newpath: *const core::ffi::c_char, flags: u32) -> i32 {
738    syscall_ret(<Arch as Syscalls>::syscall5(SYS_RENAMEAT2, olddirfd as i64, oldpath as i64, newdirfd as i64, newpath as i64, flags as i64)) as i32
739}
740
741#[no_mangle]
742pub unsafe extern "C" fn symlinkat(target: *const core::ffi::c_char, newdirfd: i32, linkpath: *const core::ffi::c_char) -> i32 {
743    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SYMLINKAT, target as i64, newdirfd as i64, linkpath as i64)) as i32
744}
745
746#[no_mangle]
747pub unsafe extern "C" fn unlinkat(dirfd: i32, pathname: *const core::ffi::c_char, flags: i32) -> i32 {
748    syscall_ret(<Arch as Syscalls>::syscall3(SYS_UNLINKAT, dirfd as i64, pathname as i64, flags as i64)) as i32
749}
750
751#[no_mangle]
752pub unsafe extern "C" fn utimes(filename: *const core::ffi::c_char, times: *const core::ffi::c_void) -> i32 {
753    utimensat(AT_FDCWD, filename, times as *const timespec, 0)
754}
755
756#[no_mangle]
757pub unsafe extern "C" fn futimes(fd: i32, times: *const core::ffi::c_void) -> i32 {
758    utimensat(fd, core::ptr::null(), times as *const timespec, 0)
759}
760
761// ============================================================
762// inotify functions
763// ============================================================
764
765#[no_mangle]
766pub unsafe extern "C" fn inotify_init() -> i32 {
767    syscall_ret(<Arch as Syscalls>::syscall1(SYS_INOTIFY_INIT, 0)) as i32
768}
769
770#[no_mangle]
771pub unsafe extern "C" fn inotify_init1(flags: i32) -> i32 {
772    syscall_ret(<Arch as Syscalls>::syscall1(SYS_INOTIFY_INIT1, flags as i64)) as i32
773}
774
775#[no_mangle]
776pub unsafe extern "C" fn inotify_add_watch(fd: i32, pathname: *const core::ffi::c_char, mask: u32) -> i32 {
777    syscall_ret(<Arch as Syscalls>::syscall3(SYS_INOTIFY_ADD_WATCH, fd as i64, pathname as i64, mask as i64)) as i32
778}
779
780#[no_mangle]
781pub unsafe extern "C" fn inotify_rm_watch(fd: i32, wd: i32) -> i32 {
782    syscall_ret(<Arch as Syscalls>::syscall2(SYS_INOTIFY_RM_WATCH, fd as i64, wd as i64)) as i32
783}
784
785#[no_mangle]
786pub unsafe extern "C" fn fanotify_init(flags: i32, event_f_flags: i32) -> i32 {
787    syscall_ret(<Arch as Syscalls>::syscall2(SYS_FANOTIFY_INIT, flags as i64, event_f_flags as i64)) as i32
788}
789
790#[no_mangle]
791pub unsafe extern "C" fn fanotify_mark(fd: i32, flags: i32, mask: u64, dirfd: i32, pathname: *const core::ffi::c_char) -> i32 {
792    syscall_ret(<Arch as Syscalls>::syscall5(SYS_FANOTIFY_MARK, fd as i64, flags as i64, mask as i64, dirfd as i64, pathname as i64)) as i32
793}
794
795// ============================================================
796// ffs functions
797// ============================================================
798
799#[no_mangle]
800pub unsafe extern "C" fn ffs(x: i32) -> i32 {
801    if x == 0 { return 0; }
802    x.trailing_zeros() as i32 + 1
803}
804
805#[no_mangle]
806pub unsafe extern "C" fn ffsl(x: i64) -> i32 {
807    if x == 0 { return 0; }
808    x.trailing_zeros() as i32 + 1
809}
810
811#[no_mangle]
812pub unsafe extern "C" fn ffsll(x: i64) -> i32 {
813    if x == 0 { return 0; }
814    x.trailing_zeros() as i32 + 1
815}
816
817// ============================================================
818// More system functions
819// ============================================================
820
821#[no_mangle]
822pub unsafe extern "C" fn fchdir(fd: i32) -> i32 {
823    syscall_ret(<Arch as Syscalls>::syscall1(SYS_FCHDIR, fd as i64)) as i32
824}
825
826#[no_mangle]
827pub unsafe extern "C" fn fchown(fd: i32, uid: u32, gid: u32) -> i32 {
828    syscall_ret(<Arch as Syscalls>::syscall3(SYS_FCHOWN, fd as i64, uid as i64, gid as i64)) as i32
829}
830
831#[no_mangle]
832pub unsafe extern "C" fn fdopendir(fd: i32) -> *mut core::ffi::c_void {
833    if fd < 0 { ERRNO!() = EBADF; return core::ptr::null_mut(); }
834    dir_stream_new(fd)
835}
836
837#[no_mangle]
838pub unsafe extern "C" fn getdtablesize() -> i32 {
839    let mut rlim: Rlimit = core::mem::zeroed();
840    if getrlimit(RLIMIT_NOFILE, &mut rlim) == 0 {
841        rlim.rlim_cur as i32
842    } else {
843        1024
844    }
845}
846
847#[no_mangle]
848pub unsafe extern "C" fn getentropy(buf: *mut u8, buflen: usize) -> i32 {
849    if buflen > 256 {
850        ERRNO!() = EIO;
851        return -1;
852    }
853    let mut pos = buf;
854    let mut left = buflen;
855    while left > 0 {
856        let r = getrandom(pos, left, 0);
857        if r < 0 {
858            if ERRNO!() == EINTR {
859                continue;
860            }
861            return -1;
862        }
863        pos = pos.add(r as usize);
864        left -= r as usize;
865    }
866    0
867}
868
869#[no_mangle]
870pub unsafe extern "C" fn getrandom(buf: *mut u8, buflen: usize, flags: u32) -> isize {
871    if let Some(r) = vgetrandom(buf, buflen, flags) {
872        return if r < 0 {
873            ERRNO!() = (-r) as c_int;
874            -1
875        } else {
876            r
877        };
878    }
879    let r = <Arch as Syscalls>::syscall3(SYS_GETRANDOM, buf as i64, buflen as i64, flags as i64);
880    if r < 0 {
881        ERRNO!() = (-r) as c_int;
882        -1
883    } else {
884        r as isize
885    }
886}
887
888#[no_mangle]
889pub unsafe extern "C" fn killpg(pgrp: i32, sig: i32) -> i32 {
890    syscall_ret(<Arch as Syscalls>::syscall2(SYS_KILL, -pgrp as i64, sig as i64)) as i32
891}
892
893#[no_mangle]
894pub unsafe extern "C" fn mkfifo(pathname: *const core::ffi::c_char, mode: u32) -> i32 {
895    mknod(pathname, 0o010000 | mode, 0)
896}
897
898// ============================================================
899// Memory allocation functions
900// ============================================================
901
902#[no_mangle]
903pub unsafe extern "C" fn aligned_alloc(alignment: usize, size: usize) -> *mut core::ffi::c_void {
904    alloc_aligned_impl(alignment, size) as *mut core::ffi::c_void
905}
906
907#[no_mangle]
908pub unsafe extern "C" fn memalign(alignment: usize, size: usize) -> *mut core::ffi::c_void {
909    alloc_aligned_impl(alignment, size) as *mut core::ffi::c_void
910}
911
912#[no_mangle]
913pub unsafe extern "C" fn posix_memalign(memptr: *mut *mut core::ffi::c_void, alignment: usize, size: usize) -> i32 {
914    if memptr.is_null() { return EINVAL; }
915    if alignment < core::mem::size_of::<*mut core::ffi::c_void>() || !alignment.is_power_of_two() {
916        return EINVAL;
917    }
918    let p = alloc_aligned_impl(alignment, size);
919    if p.is_null() { return ENOMEM; }
920    *memptr = p as *mut core::ffi::c_void;
921    0
922}
923
924#[no_mangle]
925pub unsafe extern "C" fn valloc(size: usize) -> *mut core::ffi::c_void {
926    alloc_aligned_impl(PAGE, size) as *mut core::ffi::c_void
927}
928
929// ============================================================
930// glob functions
931// ============================================================
932
933// ============================================================
934// wordexp functions
935// ============================================================
936
937// ============================================================
938// gettext functions
939// ============================================================
940
941#[no_mangle]
942pub unsafe extern "C" fn gettext(msgid: *const core::ffi::c_char) -> *const core::ffi::c_char {
943    msgid
944}
945
946#[no_mangle]
947pub unsafe extern "C" fn dgettext(_domainname: *const core::ffi::c_char, msgid: *const core::ffi::c_char) -> *const core::ffi::c_char {
948    msgid
949}
950
951#[no_mangle]
952pub unsafe extern "C" fn dcgettext(_domainname: *const core::ffi::c_char, msgid: *const core::ffi::c_char, _category: i32) -> *const core::ffi::c_char {
953    msgid
954}
955
956#[no_mangle]
957pub unsafe extern "C" fn ngettext(msgid1: *const core::ffi::c_char, msgid2: *const core::ffi::c_char, n: u64) -> *const core::ffi::c_char {
958    if n == 1 { msgid1 } else { msgid2 }
959}
960
961#[no_mangle]
962pub unsafe extern "C" fn dngettext(_domainname: *const core::ffi::c_char, msgid1: *const core::ffi::c_char, msgid2: *const core::ffi::c_char, n: u64) -> *const core::ffi::c_char {
963    if n == 1 { msgid1 } else { msgid2 }
964}
965
966#[no_mangle]
967pub unsafe extern "C" fn dcngettext(_domainname: *const core::ffi::c_char, msgid1: *const core::ffi::c_char, msgid2: *const core::ffi::c_char, n: u64, _category: i32) -> *const core::ffi::c_char {
968    if n == 1 { msgid1 } else { msgid2 }
969}
970
971#[no_mangle]
972pub unsafe extern "C" fn textdomain(domainname: *const core::ffi::c_char) -> *mut core::ffi::c_char {
973    static mut DOMAIN: [core::ffi::c_char; 256] = [0; 256];
974    if domainname.is_null() { return DOMAIN.as_mut_ptr(); }
975    let mut i = 0;
976    while i < 255 && *domainname.add(i) != 0 {
977        DOMAIN[i] = *domainname.add(i);
978        i += 1;
979    }
980    DOMAIN[i] = 0;
981    DOMAIN.as_mut_ptr()
982}
983
984#[no_mangle]
985pub unsafe extern "C" fn bindtextdomain(_domainname: *const core::ffi::c_char, dirname: *const core::ffi::c_char) -> *mut core::ffi::c_char {
986    dirname as *mut core::ffi::c_char
987}
988
989#[no_mangle]
990pub unsafe extern "C" fn bind_textdomain_codeset(_domainname: *const core::ffi::c_char, codeset: *const core::ffi::c_char) -> *mut core::ffi::c_char {
991    codeset as *mut core::ffi::c_char
992}
993
994// ============================================================
995// Wide string functions
996// ============================================================
997
998#[no_mangle]
999pub unsafe extern "C" fn wmemcpy(dest: *mut u32, src: *const u32, n: usize) -> *mut u32 {
1000    let mut i = 0;
1001    while i < n {
1002        *dest.add(i) = *src.add(i);
1003        i += 1;
1004    }
1005    dest
1006}
1007
1008#[no_mangle]
1009pub unsafe extern "C" fn wmemmove(dest: *mut u32, src: *const u32, n: usize) -> *mut u32 {
1010    if dest as usize <= src as usize {
1011        let mut i = 0;
1012        while i < n {
1013            *dest.add(i) = *src.add(i);
1014            i += 1;
1015        }
1016    } else {
1017        let mut i = n;
1018        while i > 0 {
1019            i -= 1;
1020            *dest.add(i) = *src.add(i);
1021        }
1022    }
1023    dest
1024}
1025
1026#[no_mangle]
1027pub unsafe extern "C" fn wmemset(dest: *mut u32, c: u32, n: usize) -> *mut u32 {
1028    let mut i = 0;
1029    while i < n {
1030        *dest.add(i) = c;
1031        i += 1;
1032    }
1033    dest
1034}
1035
1036#[no_mangle]
1037pub unsafe extern "C" fn wmemcmp(s1: *const u32, s2: *const u32, n: usize) -> i32 {
1038    let mut i = 0;
1039    while i < n {
1040        let c1 = *s1.add(i);
1041        let c2 = *s2.add(i);
1042        if c1 != c2 { return (c1 as i32) - (c2 as i32); }
1043        i += 1;
1044    }
1045    0
1046}
1047
1048#[no_mangle]
1049pub unsafe extern "C" fn wmemchr(s: *const u32, c: u32, n: usize) -> *mut u32 {
1050    let mut i = 0;
1051    while i < n {
1052        if *s.add(i) == c { return s.add(i) as *mut u32; }
1053        i += 1;
1054    }
1055    core::ptr::null_mut()
1056}
1057
1058#[no_mangle]
1059pub unsafe extern "C" fn wcpcpy(dest: *mut u32, src: *const u32) -> *mut u32 {
1060    let mut i = 0;
1061    loop {
1062        let c = *src.add(i);
1063        *dest.add(i) = c;
1064        if c == 0 { return dest.add(i); }
1065        i += 1;
1066    }
1067}
1068
1069#[no_mangle]
1070pub unsafe extern "C" fn wcpncpy(dest: *mut u32, src: *const u32, n: usize) -> *mut u32 {
1071    let mut i = 0;
1072    while i < n {
1073        let c = *src.add(i);
1074        *dest.add(i) = c;
1075        if c == 0 { return dest.add(i); }
1076        i += 1;
1077    }
1078    dest.add(n)
1079}
1080
1081#[no_mangle]
1082pub unsafe extern "C" fn wprintf(fmt: *const wchar_t, args: ...) -> c_int {
1083    vfwprintf(stdout, fmt, args)
1084}
1085
1086#[no_mangle]
1087pub unsafe extern "C" fn vwprintf(fmt: *const wchar_t, ap: core::ffi::VaList) -> c_int {
1088    vfwprintf(stdout, fmt, ap)
1089}
1090
1091// ============================================================
1092// C32 functions
1093// ============================================================
1094
1095#[no_mangle]
1096pub unsafe extern "C" fn c16rtomb(s: *mut u8, c16: u16, ps: *mut core::ffi::c_void) -> usize {
1097    // UTF-16 to UTF-8, with surrogate pairs tracked in the mbstate.
1098    let state = ps as *mut u32;
1099    let saved: u32 = if state.is_null() { 0 } else { *state };
1100    if s.is_null() {
1101        if !state.is_null() { *state = 0; }
1102        return 1;
1103    }
1104    let cp: u32;
1105    if saved != 0 {
1106        if (c16 as u32) < UNICODE_LOW_SURROGATE_START || (c16 as u32) > UNICODE_SURROGATE_END {
1107            if !state.is_null() { *state = 0; }
1108            ERRNO!() = EILSEQ;
1109            return !0usize;
1110        }
1111        cp = UNICODE_SUPPLEMENTARY_BASE
1112            + (((saved - UNICODE_SURROGATE_START) << 10) | ((c16 as u32) - UNICODE_LOW_SURROGATE_START));
1113        if !state.is_null() { *state = 0; }
1114    } else if (c16 as u32) >= UNICODE_SURROGATE_START && (c16 as u32) <= UNICODE_HIGH_SURROGATE_END {
1115        if !state.is_null() { *state = c16 as u32; }
1116        return 0;
1117    } else if c16 >= 0xdc00 && c16 <= 0xdfff {
1118        ERRNO!() = EILSEQ;
1119        return !0usize;
1120    } else {
1121        cp = c16 as u32;
1122    }
1123    if cp < 0x80 {
1124        *s = cp as u8;
1125        1
1126    } else if cp < 0x800 {
1127        *s = (0xc0 | (cp >> 6)) as u8;
1128        *s.add(1) = (0x80 | (cp & 0x3f)) as u8;
1129        2
1130    } else if cp < 0x10000 {
1131        *s = (0xe0 | (cp >> 12)) as u8;
1132        *s.add(1) = (0x80 | ((cp >> 6) & 0x3f)) as u8;
1133        *s.add(2) = (0x80 | (cp & 0x3f)) as u8;
1134        3
1135    } else {
1136        *s = (0xf0 | (cp >> 18)) as u8;
1137        *s.add(1) = (0x80 | ((cp >> 12) & 0x3f)) as u8;
1138        *s.add(2) = (0x80 | ((cp >> 6) & 0x3f)) as u8;
1139        *s.add(3) = (0x80 | (cp & 0x3f)) as u8;
1140        4
1141    }
1142}
1143
1144#[no_mangle]
1145pub unsafe extern "C" fn c32rtomb(s: *mut u8, c32: u32, _ps: *mut core::ffi::c_void) -> usize {
1146    if s.is_null() { return 1; }
1147    if c32 > 0x10ffff || (c32 >= 0xd800 && c32 <= 0xdfff) {
1148        ERRNO!() = EILSEQ;
1149        return !0usize;
1150    }
1151    if c32 < 0x80 {
1152        *s = c32 as u8;
1153        1
1154    } else if c32 < 0x800 {
1155        *s = (0xc0 | (c32 >> 6)) as u8;
1156        *s.add(1) = (0x80 | (c32 & 0x3f)) as u8;
1157        2
1158    } else if c32 < 0x10000 {
1159        *s = (0xe0 | (c32 >> 12)) as u8;
1160        *s.add(1) = (0x80 | ((c32 >> 6) & 0x3f)) as u8;
1161        *s.add(2) = (0x80 | (c32 & 0x3f)) as u8;
1162        3
1163    } else {
1164        *s = (0xf0 | (c32 >> 18)) as u8;
1165        *s.add(1) = (0x80 | ((c32 >> 12) & 0x3f)) as u8;
1166        *s.add(2) = (0x80 | ((c32 >> 6) & 0x3f)) as u8;
1167        *s.add(3) = (0x80 | (c32 & 0x3f)) as u8;
1168        4
1169    }
1170}
1171
1172#[no_mangle]
1173pub unsafe extern "C" fn mbrtoc16(pc16: *mut u16, s: *const u8, n: usize, _ps: *mut core::ffi::c_void) -> usize {
1174    if s.is_null() { return 0; }
1175    if n == 0 { return 1; }
1176    let c = *s;
1177    if c == 0 { return 0; }
1178    if c < 0x80 {
1179        if !pc16.is_null() { *pc16 = c as u16; }
1180        return 1;
1181    }
1182    if c < 0xC2 { return 1; } // Invalid
1183    if c < 0xE0 {
1184        if n < 2 { return 1; }
1185        let c2 = *s.add(1);
1186        if (c2 & 0xC0) != 0x80 { return 1; }
1187        if !pc16.is_null() { *pc16 = ((((c & 0x1F) as u32) << 6) | ((c2 & 0x3F) as u32)) as u16; }
1188        return 2;
1189    }
1190    if c < 0xF0 {
1191        if n < 3 { return 1; }
1192        let c2 = *s.add(1);
1193        let c3 = *s.add(2);
1194        if (c2 & 0xC0) != 0x80 || (c3 & 0xC0) != 0x80 { return 1; }
1195        if !pc16.is_null() { *pc16 = ((((c & 0x0F) as u32) << 12) | (((c2 & 0x3F) as u32) << 6) | ((c3 & 0x3F) as u32)) as u16; }
1196        return 3;
1197    }
1198    1
1199}
1200
1201#[no_mangle]
1202pub unsafe extern "C" fn mbrtoc32(pc32: *mut u32, s: *const u8, n: usize, _ps: *mut core::ffi::c_void) -> usize {
1203    if s.is_null() { return 0; }
1204    if n == 0 { return 1; }
1205    let c = *s;
1206    if c == 0 { return 0; }
1207    if c < 0x80 {
1208        if !pc32.is_null() { *pc32 = c as u32; }
1209        return 1;
1210    }
1211    if c < 0xC2 { return 1; } // Invalid
1212    if c < 0xE0 {
1213        if n < 2 { return 1; }
1214        let c2 = *s.add(1);
1215        if (c2 & 0xC0) != 0x80 { return 1; }
1216        if !pc32.is_null() { *pc32 = (((c & 0x1F) as u32) << 6) | ((c2 & 0x3F) as u32); }
1217        return 2;
1218    }
1219    if c < 0xF0 {
1220        if n < 3 { return 1; }
1221        let c2 = *s.add(1);
1222        let c3 = *s.add(2);
1223        if (c2 & 0xC0) != 0x80 || (c3 & 0xC0) != 0x80 { return 1; }
1224        if !pc32.is_null() { *pc32 = (((c & 0x0F) as u32) << 12) | (((c2 & 0x3F) as u32) << 6) | ((c3 & 0x3F) as u32); }
1225        return 3;
1226    }
1227    if c < 0xF5 {
1228        if n < 4 { return 1; }
1229        let c2 = *s.add(1);
1230        let c3 = *s.add(2);
1231        let c4 = *s.add(3);
1232        if (c2 & 0xC0) != 0x80 || (c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80 { return 1; }
1233        if !pc32.is_null() { *pc32 = (((c & 0x07) as u32) << 18) | (((c2 & 0x3F) as u32) << 12) | (((c3 & 0x3F) as u32) << 6) | ((c4 & 0x3F) as u32); }
1234        return 4;
1235    }
1236    1
1237}
1238
1239// ============================================================
1240// Shadow functions
1241// ============================================================
1242
1243
1244
1245
1246// ============================================================
1247// chown functions
1248// ============================================================
1249
1250#[no_mangle]
1251pub unsafe extern "C" fn chown(path: *const core::ffi::c_char, uid: u32, gid: u32) -> i32 {
1252    syscall_ret(<Arch as Syscalls>::syscall5(
1253        SYS_FCHOWNAT,
1254        AT_FDCWD as i64,
1255        path as i64,
1256        uid as i64,
1257        gid as i64,
1258        0,
1259    )) as i32
1260}
1261
1262#[no_mangle]
1263pub unsafe extern "C" fn lchown(path: *const core::ffi::c_char, uid: u32, gid: u32) -> i32 {
1264    syscall_ret(<Arch as Syscalls>::syscall5(
1265        SYS_FCHOWNAT,
1266        AT_FDCWD as i64,
1267        path as i64,
1268        uid as i64,
1269        gid as i64,
1270        AT_SYMLINK_NOFOLLOW as i64,
1271    )) as i32
1272}
1273
1274// ============================================================
1275// chroot functions
1276// ============================================================
1277
1278#[no_mangle]
1279pub unsafe extern "C" fn chroot(path: *const core::ffi::c_char) -> i32 {
1280    syscall_ret(<Arch as Syscalls>::syscall1(SYS_CHROOT, path as i64)) as i32
1281}
1282
1283#[no_mangle]
1284pub unsafe extern "C" fn get_current_dir_name() -> *mut core::ffi::c_char {
1285    static mut BUF: [core::ffi::c_char; PATH_MAX_V] = [0; PATH_MAX_V];
1286    let r = getcwd(BUF.as_mut_ptr(), PATH_MAX_V);
1287    if r.is_null() { core::ptr::null_mut() } else { BUF.as_mut_ptr() }
1288}
1289
1290// ============================================================
1291// I/O functions
1292// ============================================================
1293
1294#[no_mangle]
1295pub unsafe extern "C" fn creat(pathname: *const core::ffi::c_char, mode: u32) -> i32 {
1296    // O_WRONLY | O_CREAT | O_TRUNC. The previous flags included O_EXCL.
1297    open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode as i32)
1298}
1299
1300#[no_mangle]
1301pub unsafe extern "C" fn openat(dirfd: i32, pathname: *const core::ffi::c_char, flags: i32, mode: u32) -> i32 {
1302    syscall_ret(<Arch as Syscalls>::syscall4(SYS_OPENAT, dirfd as i64, pathname as i64, flags as i64, mode as i64)) as i32
1303}
1304
1305#[no_mangle]
1306pub unsafe extern "C" fn pwrite(fd: i32, buf: *const u8, count: usize, offset: i64) -> isize {
1307    syscall_ret(<Arch as Syscalls>::syscall4(SYS_PWRITE64, fd as i64, buf as i64, count as i64, offset)) as isize
1308}
1309
1310#[no_mangle]
1311pub unsafe extern "C" fn readv(fd: i32, iov: *const core::ffi::c_void, iovcnt: i32) -> isize {
1312    syscall_ret(<Arch as Syscalls>::syscall3(SYS_READV, fd as i64, iov as i64, iovcnt as i64)) as isize
1313}
1314
1315#[no_mangle]
1316pub unsafe extern "C" fn writev(fd: i32, iov: *const core::ffi::c_void, iovcnt: i32) -> isize {
1317    syscall_ret(<Arch as Syscalls>::syscall3(SYS_WRITEV, fd as i64, iov as i64, iovcnt as i64)) as isize
1318}
1319
1320// ============================================================
1321// Lock functions
1322// ============================================================
1323
1324#[no_mangle]
1325pub unsafe extern "C" fn flock(fd: i32, operation: i32) -> i32 {
1326    syscall_ret(<Arch as Syscalls>::syscall2(SYS_FLOCK, fd as i64, operation as i64)) as i32
1327}
1328
1329#[repr(C)]
1330struct FlockArg {
1331    l_type: i16,
1332    l_whence: i16,
1333    l_start: i64,
1334    l_len: i64,
1335    l_pid: i32,
1336}
1337
1338#[no_mangle]
1339pub unsafe extern "C" fn lockf(fd: i32, cmd: i32, len: i64) -> i32 {
1340    // POSIX lockf commands map onto fcntl record locks relative to the
1341    // current file offset.
1342    const F_RDLCK: i16 = 0;
1343    const F_WRLCK: i16 = 1;
1344    const F_UNLCK: i16 = 2;
1345    const F_SETLK: i64 = 6;
1346    const F_SETLKW: i64 = 7;
1347    const SEEK_CUR: i16 = 1;
1348    let (l_type, wait) = match cmd {
1349        0 => (F_UNLCK, false), // F_ULOCK
1350        1 => (F_WRLCK, true),  // F_LOCK
1351        2 => (F_WRLCK, false), // F_TLOCK
1352        3 => (F_RDLCK, false), // F_TEST
1353        _ => {
1354            ERRNO!() = EINVAL;
1355            return -1;
1356        }
1357    };
1358    let fl = FlockArg { l_type, l_whence: SEEK_CUR, l_start: 0, l_len: len, l_pid: 0 };
1359    let r = <Arch as Syscalls>::syscall3(
1360        SYS_FCNTL,
1361        fd as i64,
1362        if wait { F_SETLKW } else { F_SETLK },
1363        &fl as *const FlockArg as i64,
1364    );
1365    syscall_ret(r) as c_int
1366}
1367
1368// ============================================================
1369// Hostname/login functions
1370// ============================================================
1371
1372#[no_mangle]
1373pub unsafe extern "C" fn getdomainname(name: *mut core::ffi::c_char, len: usize) -> i32 {
1374    if name.is_null() || len == 0 {
1375        ERRNO!() = EINVAL;
1376        return -1;
1377    }
1378    let mut uts: UtsName = core::mem::zeroed();
1379    let r = <Arch as Syscalls>::syscall1(SYS_UNAME, &mut uts as *mut UtsName as i64);
1380    if r < 0 {
1381        ERRNO!() = (-r) as c_int;
1382        return -1;
1383    }
1384    let mut n = 0usize;
1385    while n < uts.domainname.len() && uts.domainname[n] != 0 { n += 1; }
1386    if n >= len {
1387        ERRNO!() = EINVAL;
1388        return -1;
1389    }
1390    core::ptr::copy_nonoverlapping(uts.domainname.as_ptr(), name as *mut u8, n);
1391    *name.add(n) = 0;
1392    0
1393}
1394
1395#[no_mangle]
1396pub unsafe extern "C" fn setdomainname(name: *const core::ffi::c_char, len: usize) -> i32 {
1397    syscall_ret(<Arch as Syscalls>::syscall2(SYS_SETDOMAINNAME, name as i64, len as i64)) as i32
1398}
1399
1400#[no_mangle]
1401pub unsafe extern "C" fn sethostname(name: *const core::ffi::c_char, len: usize) -> i32 {
1402    syscall_ret(<Arch as Syscalls>::syscall2(SYS_SETHOSTNAME, name as i64, len as i64)) as i32
1403}
1404
1405#[no_mangle]
1406pub unsafe extern "C" fn getlogin_r(name: *mut core::ffi::c_char, bufsize: usize) -> i32 {
1407    let user = getlogin();
1408    if user.is_null() { return ENXIO; }
1409    let len = crate::strlen(user);
1410    if len >= bufsize { return 34; } // ERANGE
1411    core::ptr::copy_nonoverlapping(user, name, len + 1);
1412    0
1413}
1414
1415#[no_mangle]
1416pub unsafe extern "C" fn cuserid(s: *mut core::ffi::c_char) -> *mut core::ffi::c_char {
1417    let user = crate::getenv(b"USER\0".as_ptr() as *const core::ffi::c_char);
1418    if user.is_null() { return core::ptr::null_mut(); }
1419    if s.is_null() {
1420        static mut BUF: [core::ffi::c_char; 32] = [0; 32];
1421        let mut i = 0;
1422        while i < 31 && *user.add(i) != 0 { BUF[i] = *user.add(i); i += 1; }
1423        BUF[i] = 0;
1424        BUF.as_mut_ptr()
1425    } else {
1426        let mut i = 0;
1427        while i < 31 && *user.add(i) != 0 { *s.add(i) = *user.add(i); i += 1; }
1428        *s.add(i) = 0;
1429        s
1430    }
1431}
1432
1433include!("passwd_group.rs");
1434// ============================================================
1435// User/group functions
1436// ============================================================
1437
1438#[no_mangle]
1439pub unsafe extern "C" fn setgroups(ngroups: usize, gidset: *const u32) -> i32 {
1440    syscall_ret(<Arch as Syscalls>::syscall2(SYS_SETGROUPS, ngroups as i64, gidset as i64)) as i32
1441}
1442
1443#[no_mangle]
1444pub unsafe extern "C" fn setresuid(ruid: u32, euid: u32, suid: u32) -> i32 {
1445    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SETRESUID, ruid as i64, euid as i64, suid as i64)) as i32
1446}
1447
1448#[no_mangle]
1449pub unsafe extern "C" fn setresgid(rgid: u32, egid: u32, sgid: u32) -> i32 {
1450    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SETRESGID, rgid as i64, egid as i64, sgid as i64)) as i32
1451}
1452
1453#[no_mangle]
1454pub unsafe extern "C" fn getresuid(ruid: *mut u32, euid: *mut u32, suid: *mut u32) -> i32 {
1455    syscall_ret(<Arch as Syscalls>::syscall3(SYS_GETRESUID, ruid as i64, euid as i64, suid as i64)) as i32
1456}
1457
1458#[no_mangle]
1459pub unsafe extern "C" fn getresgid(rgid: *mut u32, egid: *mut u32, sgid: *mut u32) -> i32 {
1460    syscall_ret(<Arch as Syscalls>::syscall3(SYS_GETRESGID, rgid as i64, egid as i64, sgid as i64)) as i32
1461}
1462
1463// ============================================================
1464// Socket functions
1465// ============================================================
1466
1467// musl's public struct msghdr uses 32-bit msg_iovlen/msg_controllen followed
1468// by padding on 64-bit little-endian. The kernel reads 64-bit fields, so the
1469// padding must be zero; callers (nginx, for example) do not initialize it.
1470#[repr(C)]
1471struct KernelMsgHdr {
1472    msg_name: *mut c_void,
1473    msg_namelen: u32,
1474    __pad0: u32,
1475    msg_iov: *mut c_void,
1476    msg_iovlen: u64,
1477    msg_control: *mut c_void,
1478    msg_controllen: u64,
1479    msg_flags: u32,
1480    __pad1: u32,
1481}
1482
1483#[repr(C)]
1484struct UserMsgHdr {
1485    msg_name: *mut c_void,
1486    msg_namelen: u32,
1487    msg_iov: *mut c_void,
1488    msg_iovlen: i32,
1489    __pad1: i32,
1490    msg_control: *mut c_void,
1491    msg_controllen: u32,
1492    __pad2: i32,
1493    msg_flags: i32,
1494}
1495
1496unsafe fn msg_to_kernel(msg: *const UserMsgHdr, out: &mut KernelMsgHdr) {
1497    *out = KernelMsgHdr {
1498        msg_name: (*msg).msg_name,
1499        msg_namelen: (*msg).msg_namelen,
1500        __pad0: 0,
1501        msg_iov: (*msg).msg_iov,
1502        msg_iovlen: (*msg).msg_iovlen as u32 as u64,
1503        msg_control: (*msg).msg_control,
1504        msg_controllen: (*msg).msg_controllen as u64,
1505        msg_flags: (*msg).msg_flags as u32,
1506        __pad1: 0,
1507    };
1508}
1509
1510#[no_mangle]
1511pub unsafe extern "C" fn sendmsg(fd: i32, msg: *const core::ffi::c_void, flags: i32) -> isize {
1512    if msg.is_null() {
1513        ERRNO!() = EINVAL;
1514        return -1;
1515    }
1516    let mut k: KernelMsgHdr = core::mem::zeroed();
1517    msg_to_kernel(msg as *const UserMsgHdr, &mut k);
1518    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SENDMSG, fd as i64, &k as *const KernelMsgHdr as i64, flags as i64)) as isize
1519}
1520
1521#[no_mangle]
1522pub unsafe extern "C" fn recvmsg(fd: i32, msg: *mut core::ffi::c_void, flags: i32) -> isize {
1523    if msg.is_null() {
1524        ERRNO!() = EINVAL;
1525        return -1;
1526    }
1527    let user = msg as *mut UserMsgHdr;
1528    let mut k: KernelMsgHdr = core::mem::zeroed();
1529    msg_to_kernel(user, &mut k);
1530    let r = syscall_ret(<Arch as Syscalls>::syscall3(SYS_RECVMSG, fd as i64, &mut k as *mut KernelMsgHdr as i64, flags as i64));
1531    (*user).msg_flags = k.msg_flags as i32;
1532    (*user).msg_controllen = k.msg_controllen as u32;
1533    r as isize
1534}
1535
1536#[no_mangle]
1537pub unsafe extern "C" fn sendmmsg(fd: i32, msgvec: *mut core::ffi::c_void, vlen: u32, flags: i32) -> i32 {
1538    syscall_ret(<Arch as Syscalls>::syscall4(SYS_SENDMMSG, fd as i64, msgvec as i64, vlen as i64, flags as i64)) as i32
1539}
1540
1541#[no_mangle]
1542pub unsafe extern "C" fn recvmmsg(fd: i32, msgvec: *mut core::ffi::c_void, vlen: u32, flags: i32, timeout: *mut core::ffi::c_void) -> i32 {
1543    syscall_ret(<Arch as Syscalls>::syscall5(SYS_RECVMMSG, fd as i64, msgvec as i64, vlen as i64, flags as i64, timeout as i64)) as i32
1544}
1545
1546#[no_mangle]
1547pub unsafe extern "C" fn getpeername(fd: i32, addr: *mut core::ffi::c_void, addrlen: *mut u32) -> i32 {
1548    syscall_ret(<Arch as Syscalls>::syscall3(SYS_GETPEERNAME, fd as i64, addr as i64, addrlen as i64)) as i32
1549}
1550
1551#[no_mangle]
1552pub unsafe extern "C" fn getsockopt(fd: i32, level: i32, optname: i32, optval: *mut core::ffi::c_void, optlen: *mut u32) -> i32 {
1553    syscall_ret(<Arch as Syscalls>::syscall5(SYS_GETSOCKOPT, fd as i64, level as i64, optname as i64, optval as i64, optlen as i64)) as i32
1554}
1555
1556// ============================================================
1557// stdio unlocked functions
1558// ============================================================
1559
1560#[no_mangle]
1561pub unsafe extern "C" fn ftrylockfile(file: *mut FILE) -> i32 {
1562    if file.is_null() { return EINVAL; }
1563    if (*file).lock < 0 { return 0; }
1564    let tid = sys_gettid() as c_int;
1565    let v = a_load(&raw const (*file).lock);
1566    if v == tid {
1567        (*file).lockcount += 1;
1568        return 0;
1569    }
1570    if v == 0 && a_cas(&raw mut (*file).lock, 0, tid) == 0 {
1571        (*file).lockcount = 1;
1572        return 0;
1573    }
1574    EBUSY
1575}
1576
1577#[no_mangle]
1578pub unsafe extern "C" fn getc_unlocked(file: *mut core::ffi::c_char) -> i32 {
1579    getc(file as *mut FILE)
1580}
1581
1582#[no_mangle]
1583pub unsafe extern "C" fn getchar_unlocked() -> i32 {
1584    getchar()
1585}
1586
1587#[no_mangle]
1588pub unsafe extern "C" fn putc_unlocked(c: i32, file: *mut core::ffi::c_char) -> i32 {
1589    putc(c, file as *mut FILE)
1590}
1591
1592#[no_mangle]
1593pub unsafe extern "C" fn putchar_unlocked(c: i32) -> i32 {
1594    putchar(c)
1595}
1596
1597// ============================================================
1598// getopt functions
1599// ============================================================
1600
1601#[no_mangle] pub static mut optarg: *mut core::ffi::c_char = core::ptr::null_mut();
1602#[no_mangle] pub static mut optind: i32 = 1;
1603#[no_mangle] pub static mut opterr: i32 = 1;
1604#[no_mangle] pub static mut optopt: i32 = '?' as i32;
1605#[no_mangle] pub static mut optreset: i32 = 0;
1606
1607// ============================================================
1608// Timer functions
1609// ============================================================
1610
1611#[no_mangle]
1612pub unsafe extern "C" fn getitimer(which: i32, curr_value: *mut core::ffi::c_void) -> i32 {
1613    syscall_ret(<Arch as Syscalls>::syscall2(SYS_GETITIMER, which as i64, curr_value as i64)) as i32
1614}
1615
1616#[no_mangle]
1617pub unsafe extern "C" fn setitimer(which: i32, new_value: *const core::ffi::c_void, old_value: *mut core::ffi::c_void) -> i32 {
1618    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SETITIMER, which as i64, new_value as i64, old_value as i64)) as i32
1619}
1620
1621// ============================================================
1622// System info functions
1623// ============================================================
1624
1625#[no_mangle]
1626pub unsafe extern "C" fn getloadavg(loadavg: *mut f64, nelem: i32) -> i32 {
1627    let fd = crate::open(b"/proc/loadavg\0".as_ptr() as *const core::ffi::c_char, 0, 0);
1628    if fd < 0 { return -1; }
1629    let mut buf: [u8; 64] = [0; 64];
1630    let n = crate::read(fd, buf.as_mut_ptr() as *mut core::ffi::c_void, 63);
1631    crate::close(fd);
1632    if n <= 0 { return -1; }
1633    buf[n as usize] = 0;
1634    if loadavg.is_null() || nelem <= 0 { return -1; }
1635    let mut p = buf.as_ptr() as *const core::ffi::c_char;
1636    let mut i = 0usize;
1637    while i < nelem as usize && i < 3 {
1638        let mut end: *mut core::ffi::c_char = core::ptr::null_mut();
1639        let val = crate::strtod(p, &mut end);
1640        if end as *const core::ffi::c_char == p { break; }
1641        *loadavg.add(i) = val;
1642        i += 1;
1643        p = end;
1644        while *p == b' ' as c_char || *p == b'\t' as c_char || *p == b'\n' as c_char {
1645            p = p.add(1);
1646        }
1647    }
1648    i as i32
1649}
1650
1651#[no_mangle]
1652pub unsafe extern "C" fn get_nprocs() -> i32 {
1653    sysconf(84) as i32 // _SC_NPROCESSORS_ONLN
1654}
1655
1656#[no_mangle]
1657pub unsafe extern "C" fn get_nprocs_conf() -> i32 {
1658    sysconf(83) as i32 // _SC_NPROCESSORS_CONF
1659}
1660
1661#[no_mangle]
1662pub unsafe extern "C" fn get_avphys_pages() -> i64 {
1663    sysconf(86) // _SC_AVPHYS_PAGES
1664}
1665
1666#[no_mangle]
1667pub unsafe extern "C" fn get_phys_pages() -> i64 {
1668    sysconf(85) // _SC_PHYS_PAGES
1669}
1670
1671// ============================================================
1672// Capability functions
1673// ============================================================
1674
1675#[no_mangle]
1676pub unsafe extern "C" fn capget(header: *mut core::ffi::c_void, data: *mut core::ffi::c_void) -> i32 {
1677    syscall_ret(<Arch as Syscalls>::syscall2(SYS_CAPGET, header as i64, data as i64)) as i32
1678}
1679
1680#[no_mangle]
1681pub unsafe extern "C" fn capset(header: *const core::ffi::c_void, data: *const core::ffi::c_void) -> i32 {
1682    syscall_ret(<Arch as Syscalls>::syscall2(SYS_CAPSET, header as i64, data as i64)) as i32
1683}
1684
1685#[no_mangle]
1686pub unsafe extern "C" fn arch_prctl(code: i32, addr: u64) -> i32 {
1687    #[cfg(target_arch = "x86_64")]
1688    { syscall_ret(<Arch as Syscalls>::syscall2(crate::SYS_ARCH_PRCTL, code as i64, addr as i64)) as i32 }
1689    #[cfg(not(target_arch = "x86_64"))]
1690    { let _ = (code, addr); ERRNO!() = ENOSYS; -1 }
1691}
1692
1693// ============================================================
1694// termios functions
1695// ============================================================
1696
1697#[no_mangle]
1698pub unsafe extern "C" fn cfgetospeed(termios_p: *const core::ffi::c_void) -> u32 {
1699    unsafe { *(termios_p as *const u32).add(1) }
1700}
1701
1702#[no_mangle]
1703pub unsafe extern "C" fn cfgetispeed(termios_p: *const core::ffi::c_void) -> u32 {
1704    unsafe { *(termios_p as *const u32).add(2) }
1705}
1706
1707#[no_mangle]
1708pub unsafe extern "C" fn cfsetospeed(termios_p: *mut core::ffi::c_void, speed: u32) -> i32 {
1709    unsafe { *(termios_p as *mut u32).add(1) = speed; }
1710    0
1711}
1712
1713#[no_mangle]
1714pub unsafe extern "C" fn cfsetispeed(termios_p: *mut core::ffi::c_void, speed: u32) -> i32 {
1715    unsafe { *(termios_p as *mut u32).add(2) = speed; }
1716    0
1717}
1718
1719#[no_mangle]
1720pub unsafe extern "C" fn cfmakeraw(termios_p: *mut core::ffi::c_void) {
1721    unsafe {
1722        let p = termios_p as *mut u32;
1723        *p.add(0) &= !0o37;
1724        *p.add(0) &= !(0o4000 | 0o2000 | 0o10000 | 0o20);
1725        *p.add(0) |= 0o60;
1726        *p.add(3) &= !0o17;
1727        *p.add(3) &= !(0o1000 | 0o40000000);
1728        *p.add(1) = 1;
1729        *p.add(2) = 0;
1730    }
1731}
1732
1733// ============================================================
1734// Resource functions
1735// ============================================================
1736
1737#[no_mangle]
1738pub unsafe extern "C" fn getrusage(who: i32, usage: *mut core::ffi::c_void) -> i32 {
1739    syscall_ret(<Arch as Syscalls>::syscall2(SYS_GETRUSAGE, who as i64, usage as i64)) as i32
1740}
1741
1742#[no_mangle]
1743pub unsafe extern "C" fn getpriority(which: i32, who: u32) -> i32 {
1744    // The kernel encodes nice as 20 - nice; musl converts it back.
1745    let r = <Arch as Syscalls>::syscall2(SYS_GETPRIORITY, which as i64, who as i64);
1746    if r < 0 { ERRNO!() = (-r) as c_int; return -1; }
1747    20 - r as i32
1748}
1749
1750#[no_mangle]
1751pub unsafe extern "C" fn setpriority(which: i32, who: u32, prio: i32) -> i32 {
1752    syscall_ret(<Arch as Syscalls>::syscall3(SYS_SETPRIORITY, which as i64, who as i64, prio as i64)) as i32
1753}
1754
1755#[no_mangle]
1756pub unsafe extern "C" fn prlimit(pid: u32, resource: i32, new_limit: *const core::ffi::c_void, old_limit: *mut core::ffi::c_void) -> i32 {
1757    syscall_ret(<Arch as Syscalls>::syscall4(SYS_PRLIMIT64, pid as i64, resource as i64, new_limit as i64, old_limit as i64)) as i32
1758}
1759
1760// ============================================================
1761// fork/clone functions
1762// ============================================================
1763
1764#[no_mangle]
1765pub unsafe extern "C" fn clone(
1766    func: usize,
1767    stack: *mut u8,
1768    flags: c_int,
1769    arg: *mut c_void,
1770    mut args: ...
1771) -> c_int {
1772    // musl-compatible signature: clone(fn, stack, flags, arg, ptid, tls, ctid).
1773    const CLONE_PIDFD_C: c_int = 0x00001000;
1774    const CLONE_VM_C: c_int = 0x00000100;
1775    const CLONE_THREAD_C: c_int = 0x00010000;
1776    const CLONE_SETTLS_C: c_int = 0x00080000;
1777    const CLONE_PARENT_SETTID_C: c_int = 0x00100000;
1778    const CLONE_CHILD_SETTID_C: c_int = 0x01000000;
1779    const CLONE_CHILD_CLEARTID_C: c_int = 0x00200000;
1780    let _ = CLONE_VM_C;
1781    if flags & (CLONE_THREAD_C | CLONE_SETTLS_C | CLONE_CHILD_CLEARTID_C) != 0
1782        || stack.is_null()
1783    {
1784        ERRNO!() = EINVAL;
1785        return -1;
1786    }
1787    let mut ptid: *mut c_int = core::ptr::null_mut();
1788    let mut tls: c_ulong = 0;
1789    let mut ctid: *mut c_int = core::ptr::null_mut();
1790    if flags & (CLONE_PIDFD_C | CLONE_PARENT_SETTID_C | CLONE_CHILD_SETTID_C) != 0 {
1791        ptid = args.next_arg::<*mut c_int>();
1792    }
1793    if flags & CLONE_CHILD_SETTID_C != 0 {
1794        tls = args.next_arg::<c_ulong>();
1795        ctid = args.next_arg::<*mut c_int>();
1796    }
1797    let r = __keel_clone(func, stack, flags as c_ulong, arg, ptid, tls, ctid);
1798    syscall_ret(r) as c_int
1799}
1800
1801#[no_mangle]
1802pub unsafe extern "C" fn _Fork() -> i32 {
1803    crate::fork()
1804}
1805
1806#[no_mangle]
1807pub unsafe extern "C" fn forkpty(amaster: *mut i32, name: *mut core::ffi::c_char, termp: *const core::ffi::c_void, winp: *const core::ffi::c_void) -> i32 {
1808    let mut master: i32 = 0;
1809    let mut slave: i32 = 0;
1810    if openpty(&mut master, &mut slave, name, termp, winp) < 0 { return -1; }
1811    let pid = crate::fork();
1812    if pid < 0 { crate::close(master); crate::close(slave); return -1; }
1813    if pid == 0 {
1814        crate::close(master);
1815        if login_tty(slave) < 0 { crate::_exit(1); }
1816    } else {
1817        crate::close(slave);
1818        if !amaster.is_null() { *amaster = master; }
1819    }
1820    pid
1821}
1822
1823#[no_mangle]
1824pub unsafe extern "C" fn openpty(amaster: *mut i32, aslave: *mut i32, name: *mut core::ffi::c_char, termp: *const core::ffi::c_void, winp: *const core::ffi::c_void) -> i32 {
1825    // O_RDWR | O_NOCTTY.  0o2000 is O_APPEND, which opens the slave read-only
1826    // and makes every write through the pty fail.
1827    let flags = 0o2 | 0o400;
1828    let master = crate::open(b"/dev/ptmx\0".as_ptr() as *const core::ffi::c_char, flags, 0);
1829    if master < 0 { return -1; }
1830    grantpt(master);
1831    if unlockpt(master) < 0 { crate::close(master); return -1; }
1832    let slave_name = ptsname(master);
1833    if slave_name.is_null() { crate::close(master); return -1; }
1834    let slave = crate::open(slave_name, flags, 0);
1835    if slave < 0 { crate::close(master); return -1; }
1836    if !termp.is_null() && crate::tcsetattr(slave, 0, termp) < 0 {
1837        crate::close(master); crate::close(slave); return -1;
1838    }
1839    if !winp.is_null() && <Arch as Syscalls>::syscall3(SYS_IOCTL, slave as i64, 0x5414, winp as i64) < 0 {
1840        crate::close(master); crate::close(slave); return -1;
1841    }
1842    if !amaster.is_null() { *amaster = master; }
1843    if !aslave.is_null() { *aslave = slave; }
1844    if !name.is_null() {
1845        // The caller's buffer is documented as at least 128 bytes; keep the
1846        // copy bounded anyway.
1847        let mut i = 0usize;
1848        while i < 127 && *slave_name.add(i) != 0 {
1849            *name.add(i) = *slave_name.add(i);
1850            i += 1;
1851        }
1852        *name.add(i) = 0;
1853    }
1854    0
1855}
1856
1857#[no_mangle]
1858pub unsafe extern "C" fn login_tty(fd: i32) -> i32 {
1859    if crate::setsid() < 0 { return -1; }
1860    if <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, 0x540E, 0) < 0 { return -1; }
1861    if crate::dup2(fd, 0) < 0 || crate::dup2(fd, 1) < 0 || crate::dup2(fd, 2) < 0 {
1862        return -1;
1863    }
1864    if fd > 2 { crate::close(fd); }
1865    0
1866}
1867
1868// ============================================================
1869// dl functions
1870// ============================================================
1871
1872#[no_mangle]
1873pub unsafe extern "C" fn __dls2b() {}
1874#[no_mangle]
1875pub unsafe extern "C" fn __dls3() {}
1876#[no_mangle]
1877pub unsafe extern "C" fn _dlstart() {}
1878
1879// ============================================================
1880// string functions
1881// ============================================================
1882
1883#[no_mangle]
1884pub unsafe extern "C" fn index(s: *const core::ffi::c_char, c: i32) -> *mut core::ffi::c_char {
1885    let mut i = 0;
1886    loop {
1887        let ch = *s.add(i);
1888        if ch == c as core::ffi::c_char { return s.add(i) as *mut core::ffi::c_char; }
1889        if ch == 0 { return core::ptr::null_mut(); }
1890        i += 1;
1891    }
1892}
1893
1894#[no_mangle]
1895pub unsafe extern "C" fn rindex(s: *const core::ffi::c_char, c: i32) -> *mut core::ffi::c_char {
1896    let mut last = core::ptr::null_mut();
1897    let mut i = 0;
1898    loop {
1899        let ch = *s.add(i);
1900        if ch == 0 { return last; }
1901        if ch == c as core::ffi::c_char { last = s.add(i) as *mut core::ffi::c_char; }
1902        i += 1;
1903    }
1904}
1905
1906// ============================================================
1907// intmax functions
1908// ============================================================
1909
1910#[no_mangle]
1911pub unsafe extern "C" fn imaxabs(j: i64) -> i64 {
1912    j.abs()
1913}
1914
1915#[no_mangle]
1916pub unsafe extern "C" fn imaxdiv(numer: i64, denom: i64) -> (i64, i64) {
1917    (numer / denom, numer % denom)
1918}
1919
1920// ============================================================
1921// herror functions
1922// ============================================================
1923
1924#[no_mangle]
1925pub static mut h_errno: i32 = 0;
1926
1927#[no_mangle]
1928pub unsafe extern "C" fn __h_errno_location() -> *mut i32 {
1929    h_errno_ptr()
1930}
1931
1932#[no_mangle]
1933pub unsafe extern "C" fn herror(s: *const core::ffi::c_char) {
1934    if !s.is_null() { crate::write(2, s as *const core::ffi::c_void, crate::strlen(s)); }
1935    crate::write(2, b": ".as_ptr() as *const core::ffi::c_void, 2);
1936    let msg = hstrerror(*__h_errno_location());
1937    crate::write(2, msg as *const core::ffi::c_void, crate::strlen(msg));
1938    crate::write(2, b"\n".as_ptr() as *const core::ffi::c_void, 1);
1939}
1940
1941#[no_mangle]
1942pub unsafe extern "C" fn hstrerror(err: i32) -> *const core::ffi::c_char {
1943    let msg: &[u8] = match err {
1944        1 => b"Host not found\0",
1945        2 => b"Try again\0",
1946        3 => b"Non-recoverable error\0",
1947        4 => b"Address not available\0",
1948        _ => b"Unknown error\0",
1949    };
1950    msg.as_ptr() as *const core::ffi::c_char
1951}
1952
1953// ============================================================
1954// gettid function
1955// ============================================================
1956
1957#[no_mangle]
1958pub unsafe extern "C" fn gettid() -> i32 {
1959    syscall_ret(<Arch as Syscalls>::syscall0(SYS_GETTID)) as i32
1960}
1961
1962// ============================================================
1963// ioctl functions
1964// ============================================================
1965
1966#[no_mangle]
1967pub unsafe extern "C" fn ioctl(fd: i32, request: u32, mut args: ...) -> i32 {
1968    let arg = args.next_arg::<i64>();
1969    syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, request as i64, arg)) as i32
1970}
1971
1972#[no_mangle]
1973pub unsafe extern "C" fn ioperm(from: u64, num: u64, turn_on: i32) -> i32 {
1974    #[cfg(target_arch = "x86_64")]
1975    { syscall_ret(<Arch as Syscalls>::syscall3(SYS_IOPERM, from as i64, num as i64, turn_on as i64)) as i32 }
1976    #[cfg(not(target_arch = "x86_64"))]
1977    {
1978        let _ = (from, num, turn_on);
1979        ERRNO!() = ENOSYS;
1980        -1
1981    }
1982}
1983
1984#[no_mangle]
1985pub unsafe extern "C" fn iopl(level: i32) -> i32 {
1986    #[cfg(target_arch = "x86_64")]
1987    { syscall_ret(<Arch as Syscalls>::syscall1(SYS_IOPL, level as i64)) as i32 }
1988    #[cfg(not(target_arch = "x86_64"))]
1989    {
1990        let _ = level;
1991        ERRNO!() = ENOSYS;
1992        -1
1993    }
1994}
1995
1996// ============================================================
1997// xattr functions
1998// ============================================================
1999
2000#[no_mangle]
2001pub unsafe extern "C" fn getxattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char, value: *mut u8, size: usize) -> isize {
2002    syscall_ret(<Arch as Syscalls>::syscall4(SYS_GETXATTR, path as i64, name as i64, value as i64, size as i64)) as isize
2003}
2004
2005#[no_mangle]
2006pub unsafe extern "C" fn lgetxattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char, value: *mut u8, size: usize) -> isize {
2007    syscall_ret(<Arch as Syscalls>::syscall4(SYS_LGETXATTR, path as i64, name as i64, value as i64, size as i64)) as isize
2008}
2009
2010#[no_mangle]
2011pub unsafe extern "C" fn fgetxattr(fd: i32, name: *const core::ffi::c_char, value: *mut u8, size: usize) -> isize {
2012    syscall_ret(<Arch as Syscalls>::syscall4(SYS_FGETXATTR, fd as i64, name as i64, value as i64, size as i64)) as isize
2013}
2014
2015#[no_mangle]
2016pub unsafe extern "C" fn listxattr(path: *const core::ffi::c_char, list: *mut u8, size: usize) -> isize {
2017    syscall_ret(<Arch as Syscalls>::syscall3(SYS_LISTXATTR, path as i64, list as i64, size as i64)) as isize
2018}
2019
2020#[no_mangle]
2021pub unsafe extern "C" fn llistxattr(path: *const core::ffi::c_char, list: *mut u8, size: usize) -> isize {
2022    syscall_ret(<Arch as Syscalls>::syscall3(SYS_LLISTXATTR, path as i64, list as i64, size as i64)) as isize
2023}
2024
2025#[no_mangle]
2026pub unsafe extern "C" fn flistxattr(fd: i32, list: *mut u8, size: usize) -> isize {
2027    syscall_ret(<Arch as Syscalls>::syscall3(SYS_FLISTXATTR, fd as i64, list as i64, size as i64)) as isize
2028}
2029
2030#[no_mangle]
2031pub unsafe extern "C" fn setxattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char, value: *const u8, size: usize, flags: i32) -> i32 {
2032    syscall_ret(<Arch as Syscalls>::syscall5(SYS_SETXATTR, path as i64, name as i64, value as i64, size as i64, flags as i64)) as i32
2033}
2034
2035#[no_mangle]
2036pub unsafe extern "C" fn lsetxattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char, value: *const u8, size: usize, flags: i32) -> i32 {
2037    syscall_ret(<Arch as Syscalls>::syscall5(SYS_LSETXATTR, path as i64, name as i64, value as i64, size as i64, flags as i64)) as i32
2038}
2039
2040#[no_mangle]
2041pub unsafe extern "C" fn fsetxattr(fd: i32, name: *const core::ffi::c_char, value: *const u8, size: usize, flags: i32) -> i32 {
2042    syscall_ret(<Arch as Syscalls>::syscall5(SYS_FSETXATTR, fd as i64, name as i64, value as i64, size as i64, flags as i64)) as i32
2043}
2044
2045#[no_mangle]
2046pub unsafe extern "C" fn removexattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char) -> i32 {
2047    syscall_ret(<Arch as Syscalls>::syscall2(SYS_REMOVEXATTR, path as i64, name as i64)) as i32
2048}
2049
2050#[no_mangle]
2051pub unsafe extern "C" fn lremovexattr(path: *const core::ffi::c_char, name: *const core::ffi::c_char) -> i32 {
2052    syscall_ret(<Arch as Syscalls>::syscall2(SYS_LREMOVEXATTR, path as i64, name as i64)) as i32
2053}
2054
2055#[no_mangle]
2056pub unsafe extern "C" fn fremovexattr(fd: i32, name: *const core::ffi::c_char) -> i32 {
2057    syscall_ret(<Arch as Syscalls>::syscall2(SYS_FREMOVEXATTR, fd as i64, name as i64)) as i32
2058}
2059
2060// ============================================================
2061// adjtime functions
2062// ============================================================
2063
2064#[repr(C)]
2065struct TimexBuf {
2066    modes: c_uint,
2067    offset: c_long,
2068    freq: c_long,
2069    maxerror: c_long,
2070    esterror: c_long,
2071    status: c_int,
2072    constant: c_long,
2073    precision: c_long,
2074    tolerance: c_long,
2075    time: timeval,
2076    tick: c_long,
2077    ppsfreq: c_long,
2078    jitter: c_long,
2079    shift: c_int,
2080    stabil: c_long,
2081    jitcnt: c_long,
2082    calcnt: c_long,
2083    errcnt: c_long,
2084    stbcnt: c_long,
2085    tai: c_int,
2086    padding: [c_int; 11],
2087}
2088
2089#[no_mangle]
2090pub unsafe extern "C" fn adjtime(delta: *const timeval, olddelta: *mut timeval) -> i32 {
2091    // Port of musl's linux/adjtime.c.
2092    let mut tx: TimexBuf = core::mem::zeroed();
2093    if !delta.is_null() {
2094        if (*delta).tv_sec > 1000 || (*delta).tv_usec > 1_000_000_000 {
2095            ERRNO!() = EINVAL;
2096            return -1;
2097        }
2098        tx.offset = (*delta).tv_sec * 1_000_000 + (*delta).tv_usec;
2099        tx.modes = 0x8001; // ADJ_OFFSET_SINGLESHOT
2100    }
2101    let r = <Arch as Syscalls>::syscall1(SYS_ADJTIMEX, &mut tx as *mut TimexBuf as i64);
2102    if r < 0 {
2103        ERRNO!() = (-r) as c_int;
2104        return -1;
2105    }
2106    if !olddelta.is_null() {
2107        let mut sec = tx.offset / 1_000_000;
2108        let mut usec = tx.offset % 1_000_000;
2109        if usec < 0 {
2110            sec -= 1;
2111            usec += 1_000_000;
2112        }
2113        (*olddelta).tv_sec = sec;
2114        (*olddelta).tv_usec = usec;
2115    }
2116    0
2117}
2118
2119#[no_mangle]
2120pub unsafe extern "C" fn adjtimex(buf: *mut core::ffi::c_void) -> i32 {
2121    syscall_ret(<Arch as Syscalls>::syscall1(SYS_ADJTIMEX, buf as i64)) as i32
2122}
2123
2124// ============================================================
2125// pty functions
2126// ============================================================
2127
2128// Linux devpts ioctls (asm-generic/ioctls.h): TIOCGPTN = _IOR('T', 0x30, int),
2129// TIOCSPTLCK = _IOW('T', 0x31, int). The old 0x541x values are the window-size
2130// and process-group requests, which both return the wrong data and write more
2131// bytes than the int the caller supplied.
2132#[cfg(target_arch = "powerpc64")]
2133const TIOCGPTN: i64 = 0x40045430;
2134#[cfg(not(target_arch = "powerpc64"))]
2135const TIOCGPTN: i64 = 0x80045430;
2136#[cfg(target_arch = "powerpc64")]
2137const TIOCSPTLCK: i64 = 0x80045431;
2138#[cfg(not(target_arch = "powerpc64"))]
2139const TIOCSPTLCK: i64 = 0x40045431;
2140
2141#[no_mangle]
2142pub unsafe extern "C" fn grantpt(_fd: i32) -> i32 {
2143    // devpts creates the slave already owned by the caller.
2144    0
2145}
2146
2147#[no_mangle]
2148pub unsafe extern "C" fn unlockpt(fd: i32) -> i32 {
2149    let mut unlock: i32 = 0;
2150    let r = <Arch as Syscalls>::syscall3(
2151        SYS_IOCTL,
2152        fd as i64,
2153        TIOCSPTLCK,
2154        &mut unlock as *mut i32 as i64,
2155    );
2156    syscall_ret(r) as c_int
2157}
2158
2159#[no_mangle]
2160pub unsafe extern "C" fn ptsname(fd: i32) -> *mut core::ffi::c_char {
2161    static mut BUF: [core::ffi::c_char; 32] = [0; 32];
2162    let mut pty_nr: i32 = 0;
2163    if <Arch as Syscalls>::syscall3(SYS_IOCTL, fd as i64, TIOCGPTN, &mut pty_nr as *mut i32 as i64) < 0 {
2164        return core::ptr::null_mut();
2165    }
2166    // Format /dev/pts/N
2167    BUF[0] = '/' as c_char; BUF[1] = 'd' as c_char; BUF[2] = 'e' as c_char; BUF[3] = 'v' as c_char;
2168    BUF[4] = '/' as c_char; BUF[5] = 'p' as c_char; BUF[6] = 't' as c_char; BUF[7] = 's' as c_char;
2169    BUF[8] = '/' as c_char;
2170    let i = 9 + write_decimal(BUF.as_mut_ptr().add(9), pty_nr as usize);
2171    if i >= BUF.len() { return core::ptr::null_mut(); }
2172    BUF[i] = 0;
2173    BUF.as_mut_ptr()
2174}
2175
2176// ============================================================
2177// ftw functions
2178// ============================================================
2179
2180// ============================================================
2181// ctype locale functions
2182// ============================================================
2183
2184#[no_mangle]
2185pub unsafe extern "C" fn __isalnum_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2186    (c >= '0' as i32 && c <= '9' as i32 || c >= 'A' as i32 && c <= 'Z' as i32 || c >= 'a' as i32 && c <= 'z' as i32) as i32
2187}
2188
2189#[no_mangle]
2190pub unsafe extern "C" fn __isalpha_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2191    (c >= 'A' as i32 && c <= 'Z' as i32 || c >= 'a' as i32 && c <= 'z' as i32) as i32
2192}
2193
2194#[no_mangle]
2195pub unsafe extern "C" fn __isblank_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2196    (c == ' ' as i32 || c == '\t' as i32) as i32
2197}
2198
2199#[no_mangle]
2200pub unsafe extern "C" fn __iscntrl_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2201    (c < 0x20 || c == 0x7f) as i32
2202}
2203
2204#[no_mangle]
2205pub unsafe extern "C" fn __isdigit_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2206    (c >= '0' as i32 && c <= '9' as i32) as i32
2207}
2208
2209#[no_mangle]
2210pub unsafe extern "C" fn __isgraph_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2211    (c > 0x20 && c < 0x7f) as i32
2212}
2213
2214#[no_mangle]
2215pub unsafe extern "C" fn __islower_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2216    (c >= 'a' as i32 && c <= 'z' as i32) as i32
2217}
2218
2219#[no_mangle]
2220pub unsafe extern "C" fn __isprint_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2221    (c >= 0x20 && c < 0x7f) as i32
2222}
2223
2224#[no_mangle]
2225pub unsafe extern "C" fn __ispunct_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2226    (c >= 0x21 && c <= 0x2f || c >= 0x3a && c <= 0x40 || c >= 0x5b && c <= 0x60 || c >= 0x7b && c <= 0x7e) as i32
2227}
2228
2229#[no_mangle]
2230pub unsafe extern "C" fn __isspace_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2231    (c == ' ' as i32 || c >= 0x09 && c <= 0x0d) as i32
2232}
2233
2234#[no_mangle]
2235pub unsafe extern "C" fn __isupper_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2236    (c >= 'A' as i32 && c <= 'Z' as i32) as i32
2237}
2238
2239#[no_mangle]
2240pub unsafe extern "C" fn __isxdigit_l(c: i32, _loc: *mut core::ffi::c_void) -> i32 {
2241    (c >= '0' as i32 && c <= '9' as i32 || c >= 'A' as i32 && c <= 'F' as i32 || c >= 'a' as i32 && c <= 'f' as i32) as i32
2242}
2243
2244// ============================================================
2245// wide char locale functions
2246// ============================================================
2247
2248#[no_mangle]
2249pub unsafe extern "C" fn __iswalnum_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2250    (c >= '0' as u32 && c <= '9' as u32 || c >= 'A' as u32 && c <= 'Z' as u32 || c >= 'a' as u32 && c <= 'z' as u32) as i32
2251}
2252
2253#[no_mangle]
2254pub unsafe extern "C" fn __iswalpha_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2255    (c >= 'A' as u32 && c <= 'Z' as u32 || c >= 'a' as u32 && c <= 'z' as u32) as i32
2256}
2257
2258#[no_mangle]
2259pub unsafe extern "C" fn __iswblank_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2260    (c == ' ' as u32 || c == '\t' as u32) as i32
2261}
2262
2263#[no_mangle]
2264pub unsafe extern "C" fn __iswcntrl_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2265    (c < 0x20 || c == 0x7f) as i32
2266}
2267
2268#[no_mangle]
2269pub unsafe extern "C" fn __iswdigit_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2270    (c >= '0' as u32 && c <= '9' as u32) as i32
2271}
2272
2273#[no_mangle]
2274pub unsafe extern "C" fn __iswgraph_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2275    (c > 0x20 && c < 0x7f) as i32
2276}
2277
2278#[no_mangle]
2279pub unsafe extern "C" fn __iswlower_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2280    (c >= 'a' as u32 && c <= 'z' as u32) as i32
2281}
2282
2283#[no_mangle]
2284pub unsafe extern "C" fn __iswprint_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2285    (c >= 0x20 && c < 0x7f) as i32
2286}
2287
2288#[no_mangle]
2289pub unsafe extern "C" fn __iswpunct_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2290    (c >= 0x21 && c <= 0x2f || c >= 0x3a && c <= 0x40 || c >= 0x5b && c <= 0x60 || c >= 0x7b && c <= 0x7e) as i32
2291}
2292
2293#[no_mangle]
2294pub unsafe extern "C" fn __iswspace_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2295    (c == ' ' as u32 || c >= 0x09 && c <= 0x0d) as i32
2296}
2297
2298#[no_mangle]
2299pub unsafe extern "C" fn __iswupper_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2300    (c >= 'A' as u32 && c <= 'Z' as u32) as i32
2301}
2302
2303#[no_mangle]
2304pub unsafe extern "C" fn __iswxdigit_l(c: u32, _loc: *mut core::ffi::c_void) -> i32 {
2305    (c >= '0' as u32 && c <= '9' as u32 || c >= 'A' as u32 && c <= 'F' as u32 || c >= 'a' as u32 && c <= 'f' as u32) as i32
2306}
2307
2308#[no_mangle]
2309pub unsafe extern "C" fn __iswctype_l(c: u32, ctype: u32, _loc: *mut core::ffi::c_void) -> i32 {
2310    crate::iswctype(c as wint_t, ctype as wctype_t)
2311}
2312
2313#[no_mangle]
2314pub unsafe extern "C" fn __towlower_l(c: u32, _loc: *mut core::ffi::c_void) -> u32 {
2315    crate::towlower(c)
2316}
2317
2318#[no_mangle]
2319pub unsafe extern "C" fn __towupper_l(c: u32, _loc: *mut core::ffi::c_void) -> u32 {
2320    crate::towupper(c)
2321}
2322
2323#[no_mangle]
2324pub unsafe extern "C" fn __towctrans_l(c: u32, _trans: u32, _loc: *mut core::ffi::c_void) -> u32 {
2325    c
2326}
2327
2328// ============================================================
2329// _r functions
2330// ============================================================
2331
2332// ============================================================
2333// stdio internal functions
2334// ============================================================
2335
2336include!("compat_misc.rs");