diff options
Diffstat (limited to 'src/builtins.rs')
-rw-r--r-- | src/builtins.rs | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/builtins.rs b/src/builtins.rs index ad41e59..ac83efa 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,17 +1,27 @@ use std::path::Path; use std::process::Command; -pub fn cd(new_dir: &str) -> std::path::PathBuf { - let root = match std::fs::canonicalize(Path::new(new_dir)) { +pub fn cd( + args: &[&str], + home: &std::path::PathBuf, +) -> Result<std::path::PathBuf, std::string::String> { + if args.len() > 1 { + return Err("Too many arguments passed to cd".to_string()); + } + + if args.len() == 0 { + return Ok(home.to_path_buf()); + } + + let root = match std::fs::canonicalize(Path::new(args[0])) { Ok(p) => p, Err(_) => Path::new("/").to_path_buf(), }; - if let Err(e) = std::env::set_current_dir(&root) { - eprintln!("{}", e); + match std::env::set_current_dir(&root) { + Ok(_) => Ok(root.to_path_buf()), + Err(e) => Err(format!("Could not set current dir: {}", e)), } - - root.to_path_buf() } pub fn run(command: &str, args: &[&str]) { |