about summary refs log tree commit diff
path: root/library/std
diff options
context:
space:
mode:
authorDavid Koloski <dkoloski@google.com>2022-04-18 18:22:32 -0400
committerDavid Koloski <dkoloski@google.com>2022-04-19 16:50:24 -0400
commiteb6b6a877efe765c77700a30cd45f5b1e6365d62 (patch)
treecb145412311315434564d4193c207753850b162b /library/std
parent8305398d7ae6128811ec2b3223939bcd067530c2 (diff)
downloadrust-eb6b6a877efe765c77700a30cd45f5b1e6365d62.tar.gz
rust-eb6b6a877efe765c77700a30cd45f5b1e6365d62.zip
[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.
Diffstat (limited to 'library/std')
-rw-r--r--library/std/src/sys/unix/os.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs
index 1be733ba106..92bea9346d8 100644
--- a/library/std/src/sys/unix/os.rs
+++ b/library/std/src/sys/unix/os.rs
@@ -427,7 +427,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
     crate::fs::read_to_string("sys:exe").map(PathBuf::from)
 }
 
-#[cfg(any(target_os = "fuchsia", target_os = "l4re"))]
+#[cfg(target_os = "l4re")]
 pub fn current_exe() -> io::Result<PathBuf> {
     use crate::io::ErrorKind;
     Err(io::const_io_error!(ErrorKind::Unsupported, "Not yet implemented!"))
@@ -451,6 +451,26 @@ pub fn current_exe() -> io::Result<PathBuf> {
     super::unsupported::unsupported()
 }
 
+#[cfg(target_os = "fuchsia")]
+pub fn current_exe() -> io::Result<PathBuf> {
+    use crate::io::ErrorKind;
+
+    #[cfg(test)]
+    use realstd::env;
+
+    #[cfg(not(test))]
+    use crate::env;
+
+    let exe_path = env::args().next().ok_or(io::const_io_error!(
+        ErrorKind::Uncategorized,
+        "an executable path was not found because no arguments were provided through argv"
+    ))?;
+    let path = PathBuf::from(exe_path);
+
+    // Prepend the current working directory to the path if it's not absolute.
+    if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) }
+}
+
 pub struct Env {
     iter: vec::IntoIter<(OsString, OsString)>,
 }