#[cfg(target_os = "linux")]
pub fn process_cwd(pid: u32) -> Option<std::path::PathBuf> {
std::fs::read_link(format!("/proc/{pid}/cwd")).ok()
}
#[cfg(not(target_os = "linux"))]
pub fn process_cwd(_pid: u32) -> Option<std::path::PathBuf> {
None
}
#[cfg(unix)]
pub fn session_cwd(shell_pid: Option<u32>) -> Option<std::path::PathBuf> {
let shell_pid = shell_pid?;
if let Some(fg) = crate::agent::process::foreground_pid(shell_pid)
&& let Some(cwd) = process_cwd(fg)
{
return Some(cwd);
}
process_cwd(shell_pid)
}
#[cfg(not(unix))]
pub fn session_cwd(_shell_pid: Option<u32>) -> Option<std::path::PathBuf> {
None
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::*;
#[test]
fn process_cwd_matches_self() {
let cwd = process_cwd(std::process::id());
assert_eq!(cwd, Some(std::env::current_dir().unwrap()));
}
#[test]
fn process_cwd_none_for_dead_pid() {
assert_eq!(process_cwd(u32::MAX), None);
}
}