Skip to main content

faster_path/
extname.rs

1use libc::c_char;
2use std::ffi::{CStr, CString};
3use path_parsing::extract_last_path_segment;
4
5#[no_mangle]
6pub extern "C" fn extname(c_pth: *const c_char) -> *const c_char {
7  if c_pth.is_null() {
8    return c_pth
9  }
10
11  let name = extract_last_path_segment(unsafe { CStr::from_ptr(c_pth) }.to_str().unwrap());
12
13  if let Some(dot_i) = name.rfind('.') {
14    if dot_i > 0 && dot_i < name.len() - 1 && name[..dot_i].chars().rev().next().unwrap() != '.' {
15      return CString::new(&name[dot_i..]).unwrap().into_raw()
16    }
17  }
18
19  CString::new("").unwrap().into_raw()
20}