From 6351ac63d2adef1c9e390a10fae4a7e25dd64b0b Mon Sep 17 00:00:00 2001 From: Ava Affine Date: Tue, 20 Feb 2024 12:45:34 -0800 Subject: [PATCH] cd now tracks PWD Signed-off-by: Ava Affine --- src/stl/posix.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/stl/posix.rs b/src/stl/posix.rs index 0257fad..d8e5230 100644 --- a/src/stl/posix.rs +++ b/src/stl/posix.rs @@ -41,7 +41,7 @@ use { io::Result as IOResult, rc::Rc, fs::{ - File, OpenOptions + File, OpenOptions, canonicalize, }, path::Path, process::{ @@ -715,7 +715,7 @@ fn fg_callback( const CD_DOCSTRING: &str = "Expects 1 argument (a string). Changes to a new directory"; -fn cd_callback(ast: &Seg, _syms: &mut SymTable) -> Result { +fn cd_callback(ast: &Seg, syms: &mut SymTable) -> Result { if let Ctr::String(ref dir) = *ast.car { let dirp = Path::new(dir); if let Err(s) = set_current_dir(dirp) { @@ -723,7 +723,24 @@ fn cd_callback(ast: &Seg, _syms: &mut SymTable) -> Result { ("cd", s.to_string()) .into())) } else { - Ok(Ctr::None) + if let Ok(abs) = canonicalize(dirp) { + syms.insert( + String::from("PWD"), + Symbol::from_ast( + &String::from("PWD"), + &String::from("current working directory"), + &Seg::from_mono(Box::new( + Ctr::String(abs.to_string_lossy().into()))), + None, + )); + + // Run configurable user function + + Ok(Ctr::None) + } else { + Err(start_trace( + ("cd", "cannot make absolute path").into())) + } } } else { Err(start_trace(