Skip to content

Commit

Permalink
Rollup merge of rust-lang#87631 - :solarish_upd_fs, r=joshtriplett
Browse files Browse the repository at this point in the history
os current_exe using same approach as linux to get always the full ab…

…solute path
  • Loading branch information
Manishearth authored Oct 5, 2021
2 parents 003d8d3 + cb4519e commit eb1bb97
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,24 @@ pub fn current_exe() -> io::Result<PathBuf> {

#[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn current_exe() -> io::Result<PathBuf> {
extern "C" {
fn getexecname() -> *const c_char;
}
unsafe {
let path = getexecname();
if path.is_null() {
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));
if let Ok(path) = crate::fs::read_link("/proc/self/path/a.out") {
Ok(path)
} else {
extern "C" {
fn getexecname() -> *const c_char;
}
unsafe {
let path = getexecname();
if path.is_null() {
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
// it doesn't contain an absolute pathname.
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
// Prepend a current working directory to the path if
// it doesn't contain an absolute pathname.
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
}
}
}
}
Expand Down

0 comments on commit eb1bb97

Please sign in to comment.