From b3445f29a5dba8238770e615c8ce5d468d5efeb7 Mon Sep 17 00:00:00 2001 From: jinwjinl <2532191107@qq.com> Date: Fri, 12 Dec 2025 16:17:09 +0800 Subject: [PATCH 1/3] fix can't read absolute path and add situation of reading home_dir --- src/commands/cd.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/commands/cd.rs b/src/commands/cd.rs index 5e9e412..fbc1ef1 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -14,6 +14,10 @@ use std::{env, path::Path}; +fn home_dir() -> Option { + env::var("HOME").ok().map(std::path::PathBuf::from) +} + pub fn command(args: &[&str]) -> Result<(), String> { if args.len() != 1 { return Err("Usage: cd ".to_string()); @@ -21,12 +25,34 @@ pub fn command(args: &[&str]) -> Result<(), String> { let target = args[0]; let target_path = match target { "." => env::current_dir().map_err(|e| format!("Unable to get current directory: {}", e))?, + "~" => { + // solely ~ means home directory + home_dir() + .ok_or_else(|| "Unable to determine home directory".to_string())? + } + path if path.starts_with("~/") => { + // starts with ~/ + let home = home_dir() + .ok_or_else(|| "Unable to determine home directory".to_string())?; + //remove the ~/ + let rest = &path[2..]; + home.join(rest) + } path => { - let current_dir = env::current_dir() - .map_err(|e| format!("Unable to get current directory: {}", e))?; - let mut path_buf = current_dir; - for component in Path::new(path).components() { + + let path_obj = Path::new(path); + let mut path_buf = if path_obj.is_absolute() { + std::path::PathBuf::new() // if absolute, start from root + } else { + env::current_dir() + .map_err(|e| format!("Unable to get current directory: {}", e))? + }; + + for component in path_obj.components() { match component { + std::path::Component::RootDir => { + path_buf.push("/"); + } std::path::Component::ParentDir => { if !path_buf.pop() { return Err("Already at root directory".to_string()); From 196a9fd10a8d0902c25597d39c329407b42afc0c Mon Sep 17 00:00:00 2001 From: jinwjinl <2532191107@qq.com> Date: Fri, 12 Dec 2025 16:38:38 +0800 Subject: [PATCH 2/3] fix formatting problem --- src/commands/cd.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/commands/cd.rs b/src/commands/cd.rs index fbc1ef1..c3d9a42 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -14,7 +14,7 @@ use std::{env, path::Path}; -fn home_dir() -> Option { +fn get_home() -> Option { env::var("HOME").ok().map(std::path::PathBuf::from) } @@ -26,15 +26,13 @@ pub fn command(args: &[&str]) -> Result<(), String> { let target_path = match target { "." => env::current_dir().map_err(|e| format!("Unable to get current directory: {}", e))?, "~" => { - // solely ~ means home directory - home_dir() + // Single ~ means home directory + get_home() .ok_or_else(|| "Unable to determine home directory".to_string())? } path if path.starts_with("~/") => { - // starts with ~/ - let home = home_dir() + let home = get_hoome() .ok_or_else(|| "Unable to determine home directory".to_string())?; - //remove the ~/ let rest = &path[2..]; home.join(rest) } @@ -42,7 +40,7 @@ pub fn command(args: &[&str]) -> Result<(), String> { let path_obj = Path::new(path); let mut path_buf = if path_obj.is_absolute() { - std::path::PathBuf::new() // if absolute, start from root + std::path::PathBuf::new() } else { env::current_dir() .map_err(|e| format!("Unable to get current directory: {}", e))? From e3ef641e44891a7650102fcbe0235863ac4ce6be Mon Sep 17 00:00:00 2001 From: jinwjinl <2532191107@qq.com> Date: Wed, 17 Dec 2025 17:11:26 +0800 Subject: [PATCH 3/3] fix formatting problem --- src/commands/cd.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/commands/cd.rs b/src/commands/cd.rs index c3d9a42..ec88ce7 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -26,24 +26,21 @@ pub fn command(args: &[&str]) -> Result<(), String> { let target_path = match target { "." => env::current_dir().map_err(|e| format!("Unable to get current directory: {}", e))?, "~" => { - // Single ~ means home directory - get_home() - .ok_or_else(|| "Unable to determine home directory".to_string())? + // Single ~ means home directory. + get_home().ok_or_else(|| "Unable to determine home directory".to_string())? } path if path.starts_with("~/") => { - let home = get_hoome() - .ok_or_else(|| "Unable to determine home directory".to_string())?; - let rest = &path[2..]; + let home = + get_home().ok_or_else(|| "Unable to determine home directory".to_string())?; + let rest = &path[2..]; home.join(rest) } path => { - let path_obj = Path::new(path); let mut path_buf = if path_obj.is_absolute() { - std::path::PathBuf::new() + std::path::PathBuf::new() } else { - env::current_dir() - .map_err(|e| format!("Unable to get current directory: {}", e))? + env::current_dir().map_err(|e| format!("Unable to get current directory: {}", e))? }; for component in path_obj.components() {