Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created February 18, 2014 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimbatm/9075262 to your computer and use it in GitHub Desktop.
Save zimbatm/9075262 to your computer and use it in GitHub Desktop.
use std::path::Path;
static PATHS: [&'static str, .. 4] = ["/bin", "/sbin", "/usr/bin", "/usr/sbin"];
// Tries to find an executable
// TODO: Look for the executable bit for the select user/group
fn look_path(program: ~str, root: ~Path) -> Option<Path> {
let cmd = Path::new(program.clone());
if cmd.is_absolute() || program.contains("/") { // TODO: Use path::SEP
Some(cmd)
} else {
let paths = PATHS.map(|p| Path::new(p.as_bytes()).join(cmd.clone()));
match paths.iter().find(|p| root.join(*p).is_file()) {
Some(m) => Some(m.clone()),
None => None
}
}
}
fn main() {
match look_path(~"ls", ~Path::new("/")) {
Some(cmd) => println!("command found: {}", cmd.as_str().unwrap()),
None => println!("No command found")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment