Feat: pwd

This commit is contained in:
RustedTerrier 2021-08-28 00:58:20 +02:00
parent c34accce65
commit 61f1545ff2
1 changed files with 36 additions and 0 deletions

36
src/pwd.rs Normal file
View File

@ -0,0 +1,36 @@
use std::{env, fs, process::exit};
fn main() {
let mut args: Vec<String> = env::args().collect();
args.remove(0);
let mut output = Output::Logical;
for i in args {
if &i[..] == "-L" {
output = Output::Logical;
} else if &i[..] == "-P" {
output = Output::Physical;
}
}
match output {
| Output::Logical => println!("{}", get_logical_dir()),
| Output::Physical => {
println!("{}", fs::canonicalize(get_logical_dir()).unwrap().to_string_lossy())
}
}
}
fn get_logical_dir() -> String {
// current_dir follows symlinks which isn't what we want with
// logical pwd.
match env::var("PWD") {
| Ok(d) => d,
| Err(e) => {
eprintln!("Could not read environment variable $PWD: {}", e);
exit(1);
}
}
}
enum Output {
Logical,
Physical
}