Skip to main content

c/
compat_abi.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// musl ABI compatibility helpers
6// ============================================================
7
8#[no_mangle]
9pub unsafe extern "C" fn stpcpy(dest: *mut c_char, src: *const c_char) -> *mut c_char {
10    let mut d = dest;
11    let mut s = src;
12    while *s != 0 {
13        *d = *s;
14        d = d.add(1);
15        s = s.add(1);
16    }
17    *d = 0;
18    d
19}
20
21#[no_mangle]
22pub unsafe extern "C" fn stpncpy(dest: *mut c_char, src: *const c_char, n: usize) -> *mut c_char {
23    let mut i = 0;
24    while i < n && *src.add(i) != 0 {
25        *dest.add(i) = *src.add(i);
26        i += 1;
27    }
28    let end = dest.add(i);
29    while i < n {
30        *dest.add(i) = 0;
31        i += 1;
32    }
33    end
34}
35
36#[no_mangle]
37pub unsafe extern "C" fn strchrnul(s: *const c_char, c: c_int) -> *mut c_char {
38    let mut p = s;
39    while *p != 0 && *p as c_int != c {
40        p = p.add(1);
41    }
42    p as *mut c_char
43}
44
45#[no_mangle]
46pub unsafe extern "C" fn fstatat(dirfd: c_int, path: *const c_char, buf: *mut Stat, flags: c_int) -> c_int {
47    let r = sys_newfstatat(dirfd, path, buf as *mut u8, flags);
48    syscall_ret(r) as c_int
49}
50
51#[no_mangle]
52pub unsafe extern "C" fn execvpe(
53    file: *const c_char,
54    argv: *const *const c_char,
55    envp: *const *const c_char,
56) -> c_int {
57    ERRNO!() = ENOENT;
58    if file.is_null() || *file == 0 {
59        return -1;
60    }
61    let mut p = file;
62    while *p != 0 {
63        if *p == b'/' as c_char {
64            return execve(file, argv, envp);
65        }
66        p = p.add(1);
67    }
68    let file_len = crate::strlen(file);
69    let path = crate::getenv(b"PATH\0".as_ptr() as *const c_char);
70    let default_path = b"/usr/local/bin:/bin:/usr/bin\0";
71    let path = if path.is_null() { default_path.as_ptr() as *const c_char } else { path };
72
73    let mut seen_eacces = false;
74    let mut buf = [0 as c_char; PATH_MAX_V];
75    let mut dir = path;
76    loop {
77        let mut end = dir;
78        while *end != 0 && *end != b':' as c_char { end = end.add(1); }
79        let dlen = end.offset_from(dir) as usize;
80        // An empty PATH component means the current directory (no slash).
81        let need_slash = dlen > 0;
82        let total = dlen + need_slash as usize + file_len;
83        if total < buf.len() {
84            let mut i = 0;
85            while i < dlen { buf[i] = *dir.add(i); i += 1; }
86            if need_slash { buf[i] = b'/' as c_char; i += 1; }
87            let mut j = 0;
88            while j < file_len { buf[i + j] = *file.add(j); j += 1; }
89            buf[i + j] = 0;
90            execve(buf.as_ptr(), argv, envp);
91            match ERRNO!() {
92                13 => seen_eacces = true, // EACCES
93                2 | 20 => {}              // ENOENT, ENOTDIR: try next
94                _ => return -1,
95            }
96        }
97        if *end == 0 { break; }
98        dir = end.add(1);
99    }
100    ERRNO!() = if seen_eacces { 13 } else { 2 };
101    -1
102}
103
104// Locale-aware entry points. keel currently has a single global locale,
105// so these are source/ABI-compatible wrappers around the normal functions.
106#[no_mangle]
107pub unsafe extern "C" fn strcoll_l(s1: *const c_char, s2: *const c_char, _loc: locale_t) -> c_int {
108    strcoll(s1, s2)
109}
110
111#[no_mangle]
112pub unsafe extern "C" fn strxfrm_l(dest: *mut c_char, src: *const c_char, n: usize, _loc: locale_t) -> usize {
113    strxfrm(dest, src, n)
114}
115
116#[no_mangle]
117pub unsafe extern "C" fn strerror_l(errnum: c_int, _loc: locale_t) -> *mut c_char {
118    strerror(errnum)
119}
120
121#[no_mangle]
122pub unsafe extern "C" fn strcasecmp_l(s1: *const c_char, s2: *const c_char, _loc: locale_t) -> c_int {
123    strcasecmp(s1, s2)
124}
125
126#[no_mangle]
127pub unsafe extern "C" fn strncasecmp_l(s1: *const c_char, s2: *const c_char, n: usize, _loc: locale_t) -> c_int {
128    strncasecmp(s1, s2, n)
129}
130
131#[no_mangle]
132pub unsafe extern "C" fn strftime_l(s: *mut c_char, max: usize, fmt: *const c_char, tm: *const tm, _loc: locale_t) -> usize {
133    strftime(s, max, fmt, tm)
134}
135
136#[no_mangle]
137pub unsafe extern "C" fn nl_langinfo_l(item: c_int, _loc: locale_t) -> *mut c_char {
138    nl_langinfo(item)
139}
140
141#[no_mangle]
142pub unsafe extern "C" fn wcscoll_l(s1: *const u32, s2: *const u32, _loc: locale_t) -> c_int {
143    wcscoll(s1, s2)
144}
145
146#[no_mangle]
147pub unsafe extern "C" fn wcsxfrm_l(dst: *mut wchar_t, src: *const wchar_t, n: usize, _loc: locale_t) -> usize {
148    wcsxfrm(dst, src, n)
149}
150
151#[no_mangle]
152pub unsafe extern "C" fn wcsftime_l(s: *mut u32, max: usize, fmt: *const u32, tm: *const c_void, _loc: locale_t) -> usize {
153    wcsftime(s, max, fmt, tm)
154}
155
156#[no_mangle]
157pub unsafe extern "C" fn wctype_l(name: *const c_char, _loc: locale_t) -> wctype_t {
158    wctype(name)
159}
160
161#[no_mangle]
162pub unsafe extern "C" fn wctrans_l(name: *const c_char, _loc: locale_t) -> wctrans_t {
163    wctrans(name)
164}
165
166#[no_mangle]
167pub unsafe extern "C" fn towlower_l(c: wint_t, _loc: locale_t) -> wint_t {
168    towlower(c)
169}
170
171#[no_mangle]
172pub unsafe extern "C" fn towupper_l(c: wint_t, _loc: locale_t) -> wint_t {
173    towupper(c)
174}
175
176#[no_mangle]
177pub unsafe extern "C" fn towctrans_l(c: wint_t, desc: wctrans_t, _loc: locale_t) -> wint_t {
178    towctrans(c, desc)
179}
180
181#[no_mangle] pub unsafe extern "C" fn iswalnum_l(c: wint_t, _l: locale_t) -> c_int { iswalnum(c) }
182#[no_mangle] pub unsafe extern "C" fn iswalpha_l(c: wint_t, _l: locale_t) -> c_int { iswalpha(c) }
183#[no_mangle] pub unsafe extern "C" fn iswblank_l(c: wint_t, _l: locale_t) -> c_int { iswblank(c) }
184#[no_mangle] pub unsafe extern "C" fn iswcntrl_l(c: wint_t, _l: locale_t) -> c_int { iswcntrl(c) }
185#[no_mangle] pub unsafe extern "C" fn iswdigit_l(c: wint_t, _l: locale_t) -> c_int { iswdigit(c) }
186#[no_mangle] pub unsafe extern "C" fn iswgraph_l(c: wint_t, _l: locale_t) -> c_int { iswgraph(c) }
187#[no_mangle] pub unsafe extern "C" fn iswlower_l(c: wint_t, _l: locale_t) -> c_int { iswlower(c) }
188#[no_mangle] pub unsafe extern "C" fn iswprint_l(c: wint_t, _l: locale_t) -> c_int { iswprint(c) }
189#[no_mangle] pub unsafe extern "C" fn iswpunct_l(c: wint_t, _l: locale_t) -> c_int { iswpunct(c) }
190#[no_mangle] pub unsafe extern "C" fn iswspace_l(c: wint_t, _l: locale_t) -> c_int { iswspace(c) }
191#[no_mangle] pub unsafe extern "C" fn iswupper_l(c: wint_t, _l: locale_t) -> c_int { iswupper(c) }
192#[no_mangle] pub unsafe extern "C" fn iswxdigit_l(c: wint_t, _l: locale_t) -> c_int { iswxdigit(c) }
193#[no_mangle] pub unsafe extern "C" fn iswctype_l(c: wint_t, desc: wctype_t, _l: locale_t) -> c_int { iswctype(c, desc) }
194
195// ISO C99 scanf ABI aliases expected by musl binaries.
196#[no_mangle] pub unsafe extern "C" fn __isoc99_scanf(fmt: *const c_char, args: ...) -> c_int { vfscanf(stdin, fmt, args) }
197#[no_mangle] pub unsafe extern "C" fn __isoc99_fscanf(f: *mut FILE, fmt: *const c_char, args: ...) -> c_int { vfscanf(f, fmt, args) }
198#[no_mangle] pub unsafe extern "C" fn __isoc99_sscanf(s: *const c_char, fmt: *const c_char, args: ...) -> c_int { vsscanf(s, fmt, args) }
199#[no_mangle] pub unsafe extern "C" fn __isoc99_vscanf(fmt: *const c_char, args: VaList) -> c_int { vfscanf(stdin, fmt, args) }
200#[no_mangle] pub unsafe extern "C" fn __isoc99_vfscanf(f: *mut FILE, fmt: *const c_char, args: VaList) -> c_int { vfscanf(f, fmt, args) }
201#[no_mangle] pub unsafe extern "C" fn __isoc99_vsscanf(s: *const c_char, fmt: *const c_char, args: VaList) -> c_int { vsscanf(s, fmt, args) }
202#[no_mangle] pub unsafe extern "C" fn __isoc99_wscanf(fmt: *const wchar_t, args: ...) -> c_int { vfwscanf(stdin, fmt, args) }
203#[no_mangle] pub unsafe extern "C" fn __isoc99_fwscanf(f: *mut FILE, fmt: *const wchar_t, args: ...) -> c_int { vfwscanf(f, fmt, args) }
204#[no_mangle] pub unsafe extern "C" fn __isoc99_swscanf(s: *const wchar_t, fmt: *const wchar_t, args: ...) -> c_int { vswscanf(s, fmt, args) }
205#[no_mangle] pub unsafe extern "C" fn __isoc99_vwscanf(fmt: *const wchar_t, args: VaList) -> c_int { vfwscanf(stdin, fmt, args) }
206#[no_mangle] pub unsafe extern "C" fn __isoc99_vfwscanf(f: *mut FILE, fmt: *const wchar_t, args: VaList) -> c_int { vfwscanf(f, fmt, args) }
207#[no_mangle] pub unsafe extern "C" fn __isoc99_vswscanf(s: *const wchar_t, fmt: *const wchar_t, args: VaList) -> c_int { vswscanf(s, fmt, args) }
208
209// Unlocked stdio entry points.  keel's stdio does not take FILE locks
210// internally (see docs/limitations.md), so these are aliases for the normal
211// entry points.
212#[no_mangle] pub unsafe extern "C" fn clearerr_unlocked(f: *mut FILE) { clearerr(f) }
213#[no_mangle] pub unsafe extern "C" fn feof_unlocked(f: *mut FILE) -> c_int { feof(f) }
214#[no_mangle] pub unsafe extern "C" fn ferror_unlocked(f: *mut FILE) -> c_int { ferror(f) }
215#[no_mangle] pub unsafe extern "C" fn fileno_unlocked(f: *mut FILE) -> c_int { fileno(f) }
216#[no_mangle] pub unsafe extern "C" fn fflush_unlocked(f: *mut FILE) -> c_int { fflush(f) }
217#[no_mangle] pub unsafe extern "C" fn fgetc_unlocked(f: *mut FILE) -> c_int { fgetc(f) }
218#[no_mangle] pub unsafe extern "C" fn fgets_unlocked(s: *mut c_char, n: c_int, f: *mut FILE) -> *mut c_char { fgets(s, n, f) }
219#[no_mangle] pub unsafe extern "C" fn fputc_unlocked(c: c_int, f: *mut FILE) -> c_int { fputc(c, f) }
220#[no_mangle] pub unsafe extern "C" fn fputs_unlocked(s: *const c_char, f: *mut FILE) -> c_int { fputs(s, f) }
221#[no_mangle] pub unsafe extern "C" fn fread_unlocked(p: *mut c_void, size: usize, n: usize, f: *mut FILE) -> usize { fread(p, size, n, f) }
222#[no_mangle] pub unsafe extern "C" fn fwrite_unlocked(p: *const c_void, size: usize, n: usize, f: *mut FILE) -> usize { fwrite(p, size, n, f) }
223#[no_mangle] pub unsafe extern "C" fn fgetwc_unlocked(f: *mut FILE) -> wint_t { fgetwc(f) }
224#[no_mangle] pub unsafe extern "C" fn fgetws_unlocked(s: *mut u32, n: c_int, f: *mut c_void) -> *mut u32 { fgetws(s, n, f) }
225#[no_mangle] pub unsafe extern "C" fn fputwc_unlocked(c: wchar_t, f: *mut FILE) -> wint_t { fputwc(c, f) }
226#[no_mangle] pub unsafe extern "C" fn fputws_unlocked(s: *const wchar_t, f: *mut FILE) -> c_int { fputws(s, f) }
227#[no_mangle] pub unsafe extern "C" fn getwc_unlocked(f: *mut c_void) -> wint_t { getwc(f) }
228#[no_mangle] pub unsafe extern "C" fn putwc_unlocked(c: u32, f: *mut c_void) -> wint_t { putwc(c, f) }
229#[no_mangle] pub unsafe extern "C" fn getwchar_unlocked() -> wint_t { getwchar() }
230#[no_mangle] pub unsafe extern "C" fn putwchar_unlocked(c: wchar_t) -> wint_t { putwchar(c) }
231#[no_mangle] pub unsafe extern "C" fn fpurge(f: *mut FILE) { clearerr(f) }
232#[no_mangle] pub unsafe extern "C" fn _IO_getc(f: *mut FILE) -> c_int { getc(f) }
233#[no_mangle] pub unsafe extern "C" fn _IO_putc(c: c_int, f: *mut FILE) -> c_int { putc(c, f) }
234#[no_mangle] pub unsafe extern "C" fn _IO_getc_unlocked(f: *mut FILE) -> c_int { getc(f) }
235#[no_mangle] pub unsafe extern "C" fn _IO_putc_unlocked(c: c_int, f: *mut FILE) -> c_int { putc(c, f) }
236#[no_mangle] pub unsafe extern "C" fn _IO_feof_unlocked(f: *mut FILE) -> c_int { feof(f) }
237#[no_mangle] pub unsafe extern "C" fn _IO_ferror_unlocked(f: *mut FILE) -> c_int { ferror(f) }
238
239// ABI aliases used by musl and older C libraries.
240#[no_mangle] pub unsafe extern "C" fn __getauxval(t: c_ulong) -> c_ulong { getauxval(t) }
241#[no_mangle] pub unsafe extern "C" fn __longjmp(env: *const c_ulong, val: c_int) -> ! { longjmp(env, val) }
242#[no_mangle] pub unsafe extern "C" fn bsd_signal(sig: c_int, handler: usize) -> usize { signal(sig, handler) }
243#[no_mangle] pub unsafe extern "C" fn __sysv_signal(sig: c_int, handler: usize) -> usize { signal(sig, handler) }
244#[no_mangle] pub unsafe extern "C" fn eaccess(path: *const c_char, mode: c_int) -> c_int { access(path, mode) }
245#[no_mangle] pub unsafe extern "C" fn __xpg_basename(path: *mut c_char) -> *mut c_char { basename(path) }
246#[no_mangle] pub unsafe extern "C" fn __xpg_strerror_r(errnum: c_int, buf: *mut c_char, len: usize) -> c_int { strerror_r(errnum, buf, len) }
247#[no_mangle] pub unsafe extern "C" fn __posix_getopt(argc: c_int, argv: *const *const c_char, optstring: *const c_char) -> c_int { getopt(argc, argv, optstring) }
248
249// ============================================================
250// Remaining musl ABI entry points
251// ============================================================
252
253#[no_mangle]
254pub unsafe extern "C" fn cfsetspeed(termios_p: *mut c_void, speed: u32) -> c_int {
255    let r = cfsetispeed(termios_p, speed);
256    if r != 0 { return r; }
257    cfsetospeed(termios_p, speed)
258}
259
260#[no_mangle]
261pub unsafe extern "C" fn futimesat(dirfd: c_int, path: *const c_char, times: *const timeval) -> c_int {
262    if times.is_null() {
263        return utimensat(dirfd, path, core::ptr::null(), 0);
264    }
265    let tv = times as *const [timeval; 2];
266    let mut ts: [timespec; 2] = [
267        timespec { tv_sec: (*tv)[0].tv_sec, tv_nsec: (*tv)[0].tv_usec * 1000 },
268        timespec { tv_sec: (*tv)[1].tv_sec, tv_nsec: (*tv)[1].tv_usec * 1000 },
269    ];
270    for t in &mut ts {
271        if t.tv_nsec < 0 || t.tv_nsec >= 1_000_000_000 {
272            ERRNO!() = EINVAL;
273            return -1;
274        }
275    }
276    utimensat(dirfd, path, ts.as_ptr(), 0)
277}
278
279#[no_mangle]
280pub unsafe extern "C" fn membarrier(cmd: c_int, flags: c_int, cpu_id: c_int) -> c_int {
281    <Arch as Syscalls>::syscall3(SYS_MEMBARRIER, cmd as i64, flags as i64, cpu_id as i64) as c_int
282}
283
284#[no_mangle]
285pub unsafe extern "C" fn mremap(
286    old_address: *mut c_void,
287    old_size: usize,
288    new_size: usize,
289    flags: c_int,
290    mut args: ...
291) -> *mut c_void {
292    let new_address = if flags & 2 != 0 { args.next_arg::<*mut c_void>() } else { core::ptr::null_mut() };
293    let r = <Arch as Syscalls>::syscall5(
294        SYS_MREMAP,
295        old_address as i64,
296        old_size as i64,
297        new_size as i64,
298        flags as i64,
299        new_address as i64,
300    );
301    if r < 0 {
302        ERRNO!() = (-r) as c_int;
303        return (-1isize) as *mut c_void;
304    }
305    r as *mut c_void
306}
307
308#[no_mangle]
309pub unsafe extern "C" fn ptsname_r(fd: c_int, buf: *mut c_char, buflen: usize) -> c_int {
310    let name = ptsname(fd);
311    if name.is_null() { return -1; }
312    let n = crate::strlen(name);
313    if n + 1 > buflen {
314        ERRNO!() = ERANGE;
315        return ERANGE;
316    }
317    core::ptr::copy_nonoverlapping(name, buf, n + 1);
318    0
319}
320
321#[no_mangle]
322pub unsafe extern "C" fn posix_getdents(fd: c_int, buf: *mut c_void, len: usize, flags: c_int) -> isize {
323    if flags != 0 {
324        ERRNO!() = EINVAL;
325        return -1;
326    }
327    let r = <Arch as Syscalls>::syscall3(SYS_GETDENTS64, fd as i64, buf as i64, len as i64);
328    if r < 0 { ERRNO!() = (-r) as c_int; -1 } else { r as isize }
329}
330
331const SYS_RISCV_FLUSH_ICACHE: i64 = 259;
332#[no_mangle]
333pub unsafe extern "C" fn riscv_flush_icache(start: *mut c_void, end: *mut c_void, flags: c_ulong) -> c_int {
334    #[cfg(target_arch = "riscv64")]
335    {
336        let r = <Arch as Syscalls>::syscall3(SYS_RISCV_FLUSH_ICACHE, start as i64, end as i64, flags as i64);
337        syscall_ret(r) as c_int
338    }
339    #[cfg(not(target_arch = "riscv64"))]
340    {
341        let _ = (start, end, flags);
342        0
343    }
344}
345
346#[no_mangle]
347pub unsafe extern "C" fn __riscv_flush_icache(start: *mut c_void, end: *mut c_void, flags: c_ulong) -> c_int {
348    riscv_flush_icache(start, end, flags)
349}
350
351#[no_mangle]
352pub unsafe extern "C" fn mkostemps(template: *mut c_char, suffixlen: c_int, flags: c_int) -> c_int {
353    mkstemps_flags(template, suffixlen, flags)
354}
355
356#[no_mangle]
357pub unsafe extern "C" fn statfs(path: *const c_char, buf: *mut c_void) -> c_int {
358    let r = <Arch as Syscalls>::syscall2(SYS_STATFS, path as i64, buf as i64);
359    syscall_ret(r) as c_int
360}
361
362#[no_mangle]
363pub unsafe extern "C" fn fstatfs(fd: c_int, buf: *mut c_void) -> c_int {
364    let r = <Arch as Syscalls>::syscall2(SYS_FSTATFS, fd as i64, buf as i64);
365    syscall_ret(r) as c_int
366}
367
368#[no_mangle]
369pub unsafe extern "C" fn sysinfo(info: *mut c_void) -> c_int {
370    let r = <Arch as Syscalls>::syscall1(SYS_SYSINFO, info as i64);
371    syscall_ret(r) as c_int
372}
373
374#[no_mangle]
375pub unsafe extern "C" fn pthread_tryjoin_np(thread: PthreadT, retval: *mut *mut c_void) -> c_int {
376    let slot = thread as *mut Thread;
377    if slot.is_null() { return EINVAL; }
378    if (*slot).detach_state == DT_DETACHED { return EINVAL; }
379    let tid = core::ptr::read_volatile(&raw const (*slot).tid);
380    if tid > 0 { return EBUSY; }
381    if tid < 0 { return EINVAL; }
382    pthread_join(thread, retval)
383}
384
385#[no_mangle]
386pub unsafe extern "C" fn pthread_timedjoin_np(thread: PthreadT, retval: *mut *mut c_void, abstime: *const timespec) -> c_int {
387    let slot = thread as *mut Thread;
388    if slot.is_null() { return EINVAL; }
389    if thread == pthread_self() { return EDEADLK; }
390    if (*slot).detach_state == DT_DETACHED { return EINVAL; }
391    loop {
392        let r = pthread_tryjoin_np(thread, retval);
393        if r != EBUSY { return r; }
394        let tid = core::ptr::read_volatile(&raw const (*slot).tid);
395        if tid <= 0 { continue; }
396        let e = futex_timedwait(&raw mut (*slot).tid, tid, abstime);
397        if e == ETIMEDOUT { return ETIMEDOUT; }
398    }
399}
400
401
402// utmp/utmpx are backed by the classic /var/run/utmp file.  musl itself
403// leaves these as no-ops; keel provides the real file-backed API.
404#[repr(C)]
405pub struct UtmpExit {
406    pub e_termination: i16,
407    pub e_exit: i16,
408}
409
410#[repr(C)]
411pub struct Utmpx {
412    pub ut_type: i16,
413    pub __ut_pad1: i16,
414    pub ut_pid: i32,
415    pub ut_line: [c_char; 32],
416    pub ut_id: [c_char; 4],
417    pub ut_user: [c_char; 32],
418    pub ut_host: [c_char; 256],
419    pub ut_exit: UtmpExit,
420    pub ut_session: i32,
421    pub __ut_pad2: i32,
422    pub ut_tv: timeval,
423    pub ut_addr_v6: [u32; 4],
424    pub __unused: [u8; 20],
425}
426
427const UTMP_SIZE: usize = 400;
428const _: [(); UTMP_SIZE] = [(); core::mem::size_of::<Utmpx>()];
429
430static mut UTMP_FD: c_int = -1;
431static mut UTMP_PATH: [u8; 256] = [0; 256];
432static mut UTMP_PATH_SET: bool = false;
433#[repr(align(8))]
434struct UtmpBuf([u8; UTMP_SIZE]);
435static mut UTMP_BUF: UtmpBuf = UtmpBuf([0; UTMP_SIZE]);
436
437unsafe fn utmp_path_ptr() -> *const c_char {
438    if !UTMP_PATH_SET {
439        let default = b"/var/run/utmp\0";
440        let mut i = 0;
441        while i < default.len() && i < UTMP_PATH.len() {
442            UTMP_PATH[i] = default[i];
443            i += 1;
444        }
445        UTMP_PATH_SET = true;
446    }
447    UTMP_PATH.as_ptr() as *const c_char
448}
449
450unsafe fn utmp_open_read() -> c_int {
451    if UTMP_FD >= 0 {
452        crate::close(UTMP_FD);
453    }
454    UTMP_FD = crate::open(utmp_path_ptr(), 0, 0);
455    UTMP_FD
456}
457
458unsafe fn utmp_open_append() -> c_int {
459    crate::open(utmp_path_ptr(), O_WRONLY | O_CREAT | O_APPEND, 0o644)
460}
461
462#[no_mangle]
463pub unsafe extern "C" fn setutent() {
464    let _ = utmp_open_read();
465}
466
467#[no_mangle]
468pub unsafe extern "C" fn endutent() {
469    if UTMP_FD >= 0 {
470        crate::close(UTMP_FD);
471        UTMP_FD = -1;
472    }
473}
474
475#[no_mangle]
476pub unsafe extern "C" fn getutent() -> *mut c_void {
477    if UTMP_FD < 0 && utmp_open_read() < 0 {
478        return core::ptr::null_mut();
479    }
480    let n = crate::read(UTMP_FD, UTMP_BUF.0.as_mut_ptr() as *mut c_void, UTMP_SIZE);
481    if n == UTMP_SIZE as isize {
482        UTMP_BUF.0.as_mut_ptr() as *mut c_void
483    } else {
484        core::ptr::null_mut()
485    }
486}
487
488unsafe fn utmp_id_eq(a: &[c_char; 4], b: &[c_char; 4]) -> bool {
489    a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]
490}
491
492unsafe fn utmp_line_eq(a: &[c_char; 32], b: &[c_char; 32]) -> bool {
493    let mut i = 0;
494    while i < 32 {
495        if a[i] != b[i] { return false; }
496        if a[i] == 0 { return true; }
497        i += 1;
498    }
499    true
500}
501
502#[no_mangle]
503pub unsafe extern "C" fn getutid(ut: *const c_void) -> *mut c_void {
504    if ut.is_null() { return core::ptr::null_mut(); }
505    let want = &*(ut as *const Utmpx);
506    loop {
507        let p = getutent();
508        if p.is_null() { return p; }
509        let cur = &*(p as *const Utmpx);
510        let matched = match want.ut_type {
511            1 | 2 | 3 | 4 => cur.ut_type == want.ut_type,
512            _ => utmp_id_eq(&cur.ut_id, &want.ut_id)
513                && (want.ut_type == 0 || cur.ut_type == want.ut_type),
514        };
515        if matched { return p; }
516    }
517}
518
519#[no_mangle]
520pub unsafe extern "C" fn getutline(ut: *const c_void) -> *mut c_void {
521    if ut.is_null() { return core::ptr::null_mut(); }
522    let want = &*(ut as *const Utmpx);
523    loop {
524        let p = getutent();
525        if p.is_null() { return p; }
526        if utmp_line_eq(&(*(p as *const Utmpx)).ut_line, &want.ut_line) {
527            return p;
528        }
529    }
530}
531
532#[no_mangle]
533pub unsafe extern "C" fn pututline(ut: *const c_void) -> *mut c_void {
534    if ut.is_null() { return core::ptr::null_mut(); }
535    let fd = utmp_open_append();
536    if fd < 0 { return core::ptr::null_mut(); }
537    let n = crate::write(fd, ut, UTMP_SIZE);
538    crate::close(fd);
539    if n == UTMP_SIZE as isize { ut as *mut c_void } else { core::ptr::null_mut() }
540}
541
542#[no_mangle]
543pub unsafe extern "C" fn updwtmp(file: *const c_char, ut: *const c_void) {
544    if file.is_null() || ut.is_null() { return; }
545    let fd = crate::open(file, O_WRONLY | O_CREAT | O_APPEND, 0o644);
546    if fd >= 0 {
547        let _ = crate::write(fd, ut, UTMP_SIZE);
548        crate::close(fd);
549    }
550}
551
552unsafe fn utmp_set_name(file: *const c_char) -> c_int {
553    if file.is_null() { return -1; }
554    let len = crate::strlen(file);
555    if len + 1 > UTMP_PATH.len() { return -1; }
556    if UTMP_FD >= 0 {
557        crate::close(UTMP_FD);
558        UTMP_FD = -1;
559    }
560    core::ptr::copy_nonoverlapping(file, UTMP_PATH.as_mut_ptr() as *mut c_char, len + 1);
561    UTMP_PATH_SET = true;
562    0
563}
564
565#[no_mangle]
566pub unsafe extern "C" fn utmpname(file: *const c_char) -> c_int { utmp_set_name(file) }
567
568#[no_mangle]
569pub unsafe extern "C" fn utmpxname(file: *const c_char) -> c_int { utmp_set_name(file) }
570
571
572// Data symbols and internal aliases still referenced by some musl binaries.
573#[no_mangle] pub static mut ___environ: *mut *mut c_char = core::ptr::null_mut();
574#[no_mangle] pub static mut _environ: *mut *mut c_char = core::ptr::null_mut();
575#[no_mangle] pub unsafe extern "C" fn __freelocale(loc: locale_t) { freelocale(loc) }
576#[no_mangle] pub unsafe extern "C" fn __getdelim(lineptr: *mut *mut c_char, n: *mut usize, delim: c_int, stream: *mut FILE) -> isize { getdelim(lineptr, n, delim, stream) }
577#[no_mangle] pub unsafe extern "C" fn __strtod_l(s: *const c_char, end: *mut *mut c_char, loc: locale_t) -> f64 { strtod_l(s, end, loc) }
578#[no_mangle] pub unsafe extern "C" fn __strtof_l(s: *const c_char, end: *mut *mut c_char, loc: locale_t) -> f32 { strtof_l(s, end, loc) }
579#[cfg(target_arch = "x86_64")]
580#[no_mangle] pub unsafe extern "C" fn __strtold_l(s: *const c_char, end: *mut *mut c_char, loc: locale_t) -> f64 { strtold_l(s, end, loc) }
581#[cfg(not(target_arch = "x86_64"))]
582#[no_mangle] pub unsafe extern "C" fn __strtold_l(s: *const c_char, end: *mut *mut c_char, loc: locale_t) -> f128 { strtold_l(s, end, loc) }
583
584#[no_mangle] pub unsafe extern "C" fn __strtol_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> c_long { strtol(s, end, base) }
585#[no_mangle] pub unsafe extern "C" fn __strtoul_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> c_ulong { strtoul(s, end, base) }
586#[no_mangle] pub unsafe extern "C" fn __strtoll_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> c_longlong { strtoll(s, end, base) }
587#[no_mangle] pub unsafe extern "C" fn __strtoull_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> c_ulonglong { strtoull(s, end, base) }
588#[no_mangle] pub unsafe extern "C" fn __strtoimax_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> i64 { strtoimax(s, end, base) }
589#[no_mangle] pub unsafe extern "C" fn __strtoumax_internal(s: *const c_char, end: *mut *mut c_char, base: c_int) -> u64 { strtoumax(s, end, base) }
590