Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
}
let query = &parts[1];

let builtin = ["exit", "echo", "type", "pwd"];
let builtin = ["exit", "echo", "type", "pwd", "cd"];

if builtin.contains(query) {
println!("{} is a rshell builtin", query);
Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() {
println!("{}: not found", query);
}
}
Err(e) => eprintln!("Couldn't read PATH environment variable: {}", e),
Err(e) => eprintln!("cd: Unable to read PATH environment variable: {}", e),
}
}
}
Expand All @@ -83,15 +83,32 @@ fn main() {

let args = &parts[1..];
let full_path = args.join(" ");

let path = Path::new(&full_path);
if !path.exists() {
println!("cd: {}: No such file or directory", full_path);
} else if !path.is_dir() {
println!("cd: {}: Not a directory", full_path);
let path_str = full_path.as_str();
if path_str == "~" || path_str.starts_with("~/") {
match env::var("HOME") {
Ok(val) => {
let expanded = if path_str == "~" {
val.clone()
} else {
path_str.replacen("~", &val, 1)
};

if let Err(e) = env::set_current_dir(&expanded) {
eprintln!("cd: {} {}", &expanded, e)
}
}
Err(e) => eprintln!("cd: Unable to read HOME environment variable: {}", e),
}
} else {
if let Err(e) = env::set_current_dir(&full_path) {
eprintln!("cd: {} {}", path.display(), e);
if !path.exists() {
println!("cd: {}: No such file or directory", full_path);
} else if !path.is_dir() {
println!("cd: {}: Not a directory", full_path);
} else {
if let Err(e) = env::set_current_dir(&full_path) {
eprintln!("cd: {} {}", path.display(), e);
}
}
}
}
Expand Down