Skip to content

Commit 6f6d62f

Browse files
Kai LuobzEq
authored andcommitted
Implement current_exe for AIX
1 parent ed61c13 commit 6f6d62f

File tree

1 file changed

+40
-0
lines changed
  • library/std/src/sys/unix

1 file changed

+40
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,46 @@ pub fn current_exe() -> io::Result<PathBuf> {
471471
if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) }
472472
}
473473

474+
#[cfg(target_os) = "aix")]
475+
pub fn current_exe() -> io::Result<PathBuf> {
476+
use crate::io::ErrorKind;
477+
478+
#[cfg(test)]
479+
use realstd::env;
480+
481+
#[cfg(not(test))]
482+
use crate::env;
483+
484+
let exe_path = env::args().next().ok_or(io::const_io_error!(
485+
ErrorKind::Uncategorized,
486+
"an executable path was not found because no arguments were provided through argv"
487+
))?;
488+
let path = PathBuf::from(exe_path);
489+
if path.is_absolute() {
490+
return path.canonicalize();
491+
}
492+
// Search PWD to infer current_exe.
493+
if let Some(pstr) = path.to_str() && pstr.contains("/") {
494+
return getcwd().map(|cwd| cwd.join(path))?.canonicalize();
495+
}
496+
// Search PATH to infer current_exe.
497+
if let Some(p) = getenv(OsStr::from_bytes("PATH".as_bytes())) {
498+
for search_path in split_paths(&p) {
499+
let pb = search_path.join(&path);
500+
if pb.is_file() && let Ok(metadata) = crate::fs::metadata(&pb) {
501+
if metadata.permissions().mode() & 0o111 != 0 {
502+
return pb.canonicalize();
503+
}
504+
} else {
505+
continue;
506+
}
507+
}
508+
}
509+
return Err(io::const_io_error!(
510+
ErrorKind::Uncategorized,
511+
"an executable path was not found"));
512+
}
513+
474514
pub struct Env {
475515
iter: vec::IntoIter<(OsString, OsString)>,
476516
}

0 commit comments

Comments
 (0)