os current_exe using same approach as linux to get always the full absolute path
but in case of failure (e.g. prcfs not mounted) still using getexecname.
This commit is contained in:
parent
b289bb7fdf
commit
cb4519e59c
@ -368,20 +368,24 @@ pub fn current_exe() -> io::Result<PathBuf> {
|
|||||||
|
|
||||||
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
|
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
|
||||||
pub fn current_exe() -> io::Result<PathBuf> {
|
pub fn current_exe() -> io::Result<PathBuf> {
|
||||||
extern "C" {
|
if let Ok(path) = crate::fs::read_link("/proc/self/path/a.out") {
|
||||||
fn getexecname() -> *const c_char;
|
Ok(path)
|
||||||
}
|
} else {
|
||||||
unsafe {
|
extern "C" {
|
||||||
let path = getexecname();
|
fn getexecname() -> *const c_char;
|
||||||
if path.is_null() {
|
}
|
||||||
Err(io::Error::last_os_error())
|
unsafe {
|
||||||
} else {
|
let path = getexecname();
|
||||||
let filename = CStr::from_ptr(path).to_bytes();
|
if path.is_null() {
|
||||||
let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename));
|
Err(io::Error::last_os_error())
|
||||||
|
} else {
|
||||||
|
let filename = CStr::from_ptr(path).to_bytes();
|
||||||
|
let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename));
|
||||||
|
|
||||||
// Prepend a current working directory to the path if
|
// Prepend a current working directory to the path if
|
||||||
// it doesn't contain an absolute pathname.
|
// it doesn't contain an absolute pathname.
|
||||||
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
|
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user