diff --git a/find_exe.rs b/find_exe.rs new file mode 100644 index 0000000..cdb6a36 --- /dev/null +++ b/find_exe.rs @@ -0,0 +1,34 @@ +use std::env; +use std::path::Path; +use std::path::PathBuf; + +fn main() { + let args = env::args(); + if args.len() != 2 { + println!("Missing executable name"); + return; + } + + let command = PathBuf::from(&args.last().unwrap()); + println!("Searching for {:?}...", command); + + if command.exists() { + println!("Command is here!"); + return; + } + + println!("Exe is in {:?}", find_in_path(&command)); +} + +fn find_in_path(command: &Path) -> Option { + env::split_paths(&env::var_os("PATH")?) + .filter_map(|dir| { + let full_path = dir.join(&command); + if full_path.exists() { + Some(full_path) + } else { + None + } + }) + .next() +}