relibc: implement getprogname() in stdlib

BSD extension used by diffutils, coreutils, and other GNU software.
Returns the basename of program_invocation_name.
This commit is contained in:
Red Bear OS
2026-07-11 13:42:06 +03:00
parent 45ecf48235
commit 03e71cd749
+17
View File
@@ -457,6 +457,23 @@ pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char {
.unwrap_or(ptr::null_mut())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn getprogname() -> *const c_char {
let full = crate::platform::program_invocation_name;
if full.is_null() {
return c"".as_ptr() as *const c_char;
}
let mut base = full;
let mut i = full;
while unsafe { *i } != 0 {
if unsafe { *i } == b'/' as c_char {
base = unsafe { i.add(1) };
}
i = unsafe { i.add(1) };
}
base
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getsubopt.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn getsubopt(