-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhich.rs
More file actions
39 lines (29 loc) · 779 Bytes
/
Copy pathwhich.rs
File metadata and controls
39 lines (29 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::env;
use std::fs;
use std::path::Path;
fn main() {
let mut args = env::args();
if args.len() < 2 {
eprintln!("Usage: which COMMAND [-a]");
std::process::exit(1);
}
let command = args.nth(1).unwrap();
let show_all = args.any(|arg| arg == "-a");
let path_string = env::var("PATH").expect("PATH is not set");
let paths: Vec<&str> = path_string.split(":").collect();
let mut found = false;
for path in paths {
let full_path = Path::new(path).join(&command);
if full_path.exists() {
found = true;
println!("{}", full_path.to_str().unwrap());
if !show_all {
break;
}
}
}
if !found {
eprintln!("{}: command not found", command);
std::process::exit(1);
}
}