1use libc::c_char;
2use std::ffi::{CStr, CString};
3use path_parsing::extract_last_path_segment;
4
5#[no_mangle]
6pub extern "C" fn basename(c_pth: *const c_char, c_ext: *const c_char) -> *const c_char {
7 if c_pth.is_null() || c_ext.is_null() {
10 return c_pth;
11 }
12 let pth = unsafe { CStr::from_ptr(c_pth) }.to_str().unwrap();
13 let ext = unsafe { CStr::from_ptr(c_ext) }.to_str().unwrap();
14
15 let mut name = extract_last_path_segment(pth);
16
17 if ext == ".*" {
18 if let Some(dot_i) = name.rfind('.') {
19 name = &name[0..dot_i];
20 }
21 } else if name.ends_with(ext) {
22 name = &name[..name.len() - ext.len()];
23 };
24
25 CString::new(name).unwrap().into_raw()
26}