Skip to content

Commit 1e43aae

Browse files
authored
Rollup merge of rust-lang#96193 - djkoloski:fuchsia_current_exe, r=tmandry
[fuchsia] Add implementation for `current_exe` This implementation returns a best attempt at the current exe path. On fuchsia, fdio will always use `argv[0]` as the process name and if it is not set then an error will be returned. Because this is not guaranteed to be the case, this implementation returns an error if `argv` does not contain any elements.
2 parents 976c6b2 + eb6b6a8 commit 1e43aae

File tree

1 file changed

+21
-1
lines changed
  • library/std/src/sys/unix

1 file changed

+21
-1
lines changed

library/std/src/sys/unix/os.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
427427
crate::fs::read_to_string("sys:exe").map(PathBuf::from)
428428
}
429429

430-
#[cfg(any(target_os = "fuchsia", target_os = "l4re"))]
430+
#[cfg(target_os = "l4re")]
431431
pub fn current_exe() -> io::Result<PathBuf> {
432432
use crate::io::ErrorKind;
433433
Err(io::const_io_error!(ErrorKind::Unsupported, "Not yet implemented!"))
@@ -451,6 +451,26 @@ pub fn current_exe() -> io::Result<PathBuf> {
451451
super::unsupported::unsupported()
452452
}
453453

454+
#[cfg(target_os = "fuchsia")]
455+
pub fn current_exe() -> io::Result<PathBuf> {
456+
use crate::io::ErrorKind;
457+
458+
#[cfg(test)]
459+
use realstd::env;
460+
461+
#[cfg(not(test))]
462+
use crate::env;
463+
464+
let exe_path = env::args().next().ok_or(io::const_io_error!(
465+
ErrorKind::Uncategorized,
466+
"an executable path was not found because no arguments were provided through argv"
467+
))?;
468+
let path = PathBuf::from(exe_path);
469+
470+
// Prepend the current working directory to the path if it's not absolute.
471+
if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) }
472+
}
473+
454474
pub struct Env {
455475
iter: vec::IntoIter<(OsString, OsString)>,
456476
}

0 commit comments

Comments
 (0)