#[must_use]
pub fn login_shell() -> String {
#[cfg(windows)]
{
std::env::var("COMSPEC")
.ok()
.filter(|shell| !shell.trim().is_empty())
.unwrap_or_else(|| "cmd.exe".into())
}
#[cfg(unix)]
{
login_shell_with(
std::env::var("SHELL").ok(),
login_shell_from_passwd(),
crate::tools::runs,
)
}
#[cfg(not(any(unix, windows)))]
{
"/bin/sh".into()
}
}
#[cfg(unix)]
#[must_use]
pub fn login_shell_with(
shell_env: Option<String>,
passwd_shell: Option<String>,
runs: impl Fn(&str) -> bool,
) -> String {
let named = |shell: Option<String>| shell.filter(|shell| !shell.is_empty() && runs(shell));
named(shell_env)
.or_else(|| named(passwd_shell))
.unwrap_or_else(|| "/bin/sh".into())
}
#[cfg(unix)]
#[must_use]
pub fn login_shell_from_passwd() -> Option<String> {
use std::ffi::CStr;
unsafe {
let pw = libc::getpwuid(crate::uid());
if pw.is_null() || (*pw).pw_shell.is_null() {
return None;
}
Some(
CStr::from_ptr((*pw).pw_shell)
.to_string_lossy()
.into_owned(),
)
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
fn has(shells: &'static [&'static str]) -> impl Fn(&str) -> bool {
move |shell| shells.contains(&shell)
}
#[test]
fn the_shell_env_wins_then_the_passwd_shell_then_sh() {
let fish = || Some("/usr/bin/fish".to_owned());
let zsh = || Some("/usr/bin/zsh".to_owned());
let bash = || Some("/bin/bash".to_owned());
assert_eq!(
login_shell_with(fish(), bash(), has(&["/usr/bin/fish"])),
"/usr/bin/fish",
"$SHELL wins when it is set and can run"
);
assert_eq!(
login_shell_with(None, zsh(), has(&["/usr/bin/zsh"])),
"/usr/bin/zsh",
"with $SHELL unset the recorded login shell starts, not /bin/sh"
);
assert_eq!(
login_shell_with(Some(String::new()), zsh(), has(&["/usr/bin/zsh"])),
"/usr/bin/zsh"
);
assert_eq!(
login_shell_with(fish(), bash(), has(&["/bin/bash"])),
"/bin/bash"
);
assert_eq!(login_shell_with(fish(), None, has(&[])), "/bin/sh");
let passwd = login_shell_from_passwd().expect("this account has a login shell recorded");
assert!(
passwd.starts_with('/'),
"the passwd login shell is absolute: {passwd:?}"
);
assert!(crate::tools::runs("/bin/sh"));
assert!(crate::tools::runs("sh"), "a bare name is looked up in PATH");
assert!(!crate::tools::runs("/nonexistent/uninstalled-shell"));
assert!(!crate::tools::runs("tilo-no-such-program"));
}
}