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