sesh_shared/
user.rs

1use anyhow::Result;
2use std::{env, ffi};
3
4use crate::error::CResult;
5
6struct Passwd {
7    pub shell: String,
8}
9
10fn get_passwd() -> Result<Passwd> {
11    unsafe {
12        let passwd = libc::getpwuid(libc::getuid()).to_result()?;
13        let shell = ffi::CStr::from_ptr(passwd.pw_shell).to_str()?.to_string();
14        Ok(Passwd { shell })
15    }
16}
17
18pub fn get_shell() -> String {
19    env::var("SHELL")
20        .or_else(|_| get_passwd().map(|passwd| passwd.shell))
21        .unwrap_or_else(|_| "/bin/sh".to_string())
22}