Skip to main content

c/
regression_stubs.rs

1// Stubs and minimal implementations for symbols required by libc-test
2// regression cases that currently fail to link.
3
4const _SC_PAGE_SIZE: c_int = 30;
5
6#[no_mangle]
7pub unsafe extern "C" fn mmap(
8    addr: *mut c_void,
9    len: usize,
10    prot: c_int,
11    flags: c_int,
12    fd: c_int,
13    off: i64,
14) -> *mut c_void {
15    crate::syscall(
16        SYS_MMAP,
17        addr as c_long,
18        len as c_long,
19        prot as c_long,
20        flags as c_long,
21        fd as c_long,
22        off as c_long,
23    ) as *mut c_void
24}
25
26#[no_mangle]
27pub unsafe extern "C" fn munmap(addr: *mut c_void, len: usize) -> c_int {
28    crate::syscall(SYS_MUNMAP, addr as c_long, len as c_long, 0, 0, 0, 0) as c_int
29}
30
31#[no_mangle]
32pub unsafe extern "C" fn pread(fd: c_int, buf: *mut c_void, count: usize, offset: i64) -> isize {
33    crate::syscall(
34        SYS_PREAD64,
35        fd as c_long,
36        buf as c_long,
37        count as c_long,
38        offset as c_long,
39        0,
40        0,
41    ) as isize
42}
43
44#[no_mangle]
45pub unsafe extern "C" fn sysconf(name: c_int) -> c_long {
46    match name {
47        _SC_PAGE_SIZE => 4096,
48        _ => {
49            crate::__errno_location().write(EINVAL);
50            -1
51        }
52    }
53}
54
55#[no_mangle]
56pub unsafe extern "C" fn execle(path: *const c_char, arg: *const c_char, mut args: ...) -> c_int {
57    let mut argv: [*const c_char; 128] = [core::ptr::null(); 128];
58    argv[0] = arg;
59    let mut n = 1usize;
60    loop {
61        let a: *const c_char = args.next_arg();
62        if a.is_null() {
63            break;
64        }
65        if n >= argv.len() - 1 {
66            crate::__errno_location().write(E2BIG);
67            return -1;
68        }
69        argv[n] = a;
70        n += 1;
71    }
72    argv[n] = core::ptr::null();
73    let envp: *const *const c_char = args.next_arg();
74    crate::syscall(
75        SYS_EXECVE,
76        path as c_long,
77        argv.as_ptr() as c_long,
78        envp as c_long,
79        0,
80        0,
81        0,
82    ) as c_int
83}
84
85#[no_mangle]
86pub unsafe extern "C" fn flockfile(f: *mut crate::FILE) {
87    if !f.is_null() {
88        (*f).lockcount += 1;
89    }
90}
91
92#[no_mangle]
93pub unsafe extern "C" fn funlockfile(f: *mut crate::FILE) {
94    if !f.is_null() {
95        if (*f).lockcount > 1 {
96            (*f).lockcount -= 1;
97        } else {
98            (*f).lockcount = 0;
99        }
100    }
101}
102
103#[no_mangle]
104pub unsafe extern "C" fn __libc_current_sigrtmin() -> c_int {
105    34
106}
107
108#[no_mangle]
109pub unsafe extern "C" fn strdup(s: *const c_char) -> *mut c_char {
110    if s.is_null() {
111        return core::ptr::null_mut();
112    }
113    let len = crate::strlen(s);
114    let p = crate::malloc(len + 1) as *mut c_char;
115    if p.is_null() {
116        return core::ptr::null_mut();
117    }
118    crate::memcpy(p as *mut c_void, s as *const c_void, len + 1);
119    p
120}
121
122static mut MKTEMP_COUNTER: c_uint = 0;
123
124unsafe fn mktemp_internal(
125    template: *mut c_char,
126    mkdir_fn: Option<unsafe extern "C" fn(*const c_char, c_uint) -> c_int>,
127) -> *mut c_char {
128    if template.is_null() {
129        crate::__errno_location().write(EINVAL);
130        return core::ptr::null_mut();
131    }
132    let len = crate::strlen(template);
133    if len < 6 {
134        crate::__errno_location().write(EINVAL);
135        return core::ptr::null_mut();
136    }
137    let mut xcount = 0usize;
138    while xcount < 6 {
139        let ch = *template.add(len - 1 - xcount) as u8;
140        if ch != b'X' {
141            break;
142        }
143        xcount += 1;
144    }
145    if xcount != 6 {
146        crate::__errno_location().write(EINVAL);
147        return core::ptr::null_mut();
148    }
149    let _pid = crate::getpid();
150    let mut c = MKTEMP_COUNTER;
151    for _ in 0..1000 {
152        let mut n = c;
153        for i in 0..6 {
154            let digit = (n % 36) as u8;
155            let ch = if digit < 10 { b'0' + digit } else { b'a' + digit - 10 };
156            *template.add(len - 6 + i) = ch as c_char;
157            n /= 36;
158        }
159        c = c.wrapping_add(1);
160        if mkdir_fn.is_some() {
161            if crate::mkdir(template, 0o700) == 0 {
162                MKTEMP_COUNTER = c;
163                return template;
164            }
165            let e = crate::__errno_location().read();
166            if e != EEXIST && e != EINTR {
167                break;
168            }
169        } else {
170            MKTEMP_COUNTER = c;
171            return template;
172        }
173    }
174    MKTEMP_COUNTER = c;
175    template
176}
177
178#[no_mangle]
179pub unsafe extern "C" fn mkdtemp(template: *mut c_char) -> *mut c_char {
180    mktemp_internal(template, Some(crate::mkdir))
181}
182
183#[no_mangle]
184pub unsafe extern "C" fn mktemp(template: *mut c_char) -> *mut c_char {
185    mktemp_internal(template, None)
186}
187
188#[no_mangle]
189pub unsafe extern "C" fn getpwnam_r(
190    _name: *const c_char,
191    _pwd: *mut c_void,
192    _buf: *mut c_char,
193    _buflen: usize,
194    result: *mut *mut c_void,
195) -> c_int {
196    if !result.is_null() {
197        *result = core::ptr::null_mut();
198    }
199    0
200}