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