2023-01-09 18:39:38 -05:00
|
|
|
use {
|
2023-01-10 18:28:33 -05:00
|
|
|
super::Cmd,
|
2023-01-09 18:39:38 -05:00
|
|
|
clap::Command,
|
|
|
|
libc::{geteuid, getpwuid},
|
2023-01-13 01:08:32 -05:00
|
|
|
std::ffi::CStr,
|
2023-01-09 18:39:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Whoami;
|
|
|
|
|
|
|
|
impl Cmd for Whoami {
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("whoami")
|
|
|
|
.about("print effective user name")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-09 18:39:38 -05:00
|
|
|
let pwnam = unsafe {
|
|
|
|
let pw = *getpwuid(geteuid());
|
|
|
|
CStr::from_ptr((pw).pw_name)
|
|
|
|
};
|
|
|
|
let pwnam = pwnam.to_str()?;
|
|
|
|
println!("{pwnam}");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::UsrBin)
|
2023-01-09 18:39:38 -05:00
|
|
|
}
|
|
|
|
}
|