From cdd146b8954d704a07a4445f00ef8ed0f65da096 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 22 Jan 2014 22:41:53 +0100 Subject: [PATCH] Add std::os::self_exe_name() --- src/libstd/os.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 93762a3cdd5c..1b55427fc2db 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -337,9 +337,9 @@ pub fn dll_filename(base: &str) -> ~str { format!("{}{}{}", consts::DLL_PREFIX, base, consts::DLL_SUFFIX) } -/// Optionally returns the filesystem path to the current executable which is +/// Optionally returns the filesystem path of the current executable which is /// running. If any failure occurs, None is returned. -pub fn self_exe_path() -> Option { +pub fn self_exe_name() -> Option { #[cfg(target_os = "freebsd")] fn load_self() -> Option<~[u8]> { @@ -402,7 +402,14 @@ pub fn self_exe_path() -> Option { } } - load_self().and_then(|path| Path::new_opt(path).map(|mut p| { p.pop(); p })) + load_self().and_then(Path::new_opt) +} + +/// Optionally returns the filesystem path to the current executable which is +/// running. Like self_exe_name() but without the binary's name. +/// If any failure occurs, None is returned. +pub fn self_exe_path() -> Option { + self_exe_name().map(|mut p| { p.pop(); p }) } /** @@ -1310,6 +1317,17 @@ mod tests { assert_eq!(getenv(n), option::Some(s)); } + #[test] + fn test_self_exe_name() { + let path = os::self_exe_name(); + assert!(path.is_some()); + let path = path.unwrap(); + debug!("{:?}", path.clone()); + + // Hard to test this function + assert!(path.is_absolute()); + } + #[test] fn test_self_exe_path() { let path = os::self_exe_path();