Created
February 18, 2014 17:12
-
-
Save zimbatm/9075262 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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