diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 9f1b559..3747d44 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -32,13 +32,14 @@ pub mod shitbox; pub mod sleep; mod sync; pub mod r#true; +pub mod whoami; pub mod yes; pub use { self::hostname::Hostname, base32::Base32, base64::Base64, basename::Basename, bootstrap::Bootstrap, dirname::Dirname, echo::Echo, factor::Factor, fold::Fold, head::Head, mountpoint::Mountpoint, nologin::Nologin, nproc::Nproc, r#false::False, r#true::True, rev::Rev, - shitbox::Shitbox, sleep::Sleep, yes::Yes, + shitbox::Shitbox, sleep::Sleep, whoami::Whoami, yes::Yes, }; pub trait Cmd: fmt::Debug + Sync { @@ -67,12 +68,13 @@ pub fn get(name: &str) -> Option> { "shitbox" => Some(Box::new(Shitbox::default())), "sleep" => Some(Box::new(Sleep::default())), "true" => Some(Box::new(True::default())), + "whoami" => Some(Box::new(Whoami::default())), "yes" => Some(Box::new(Yes::default())), _ => None, } } -pub static COMMANDS: [&'static str; 19] = [ +pub static COMMANDS: [&'static str; 20] = [ "base32", "base64", "basename", @@ -91,5 +93,6 @@ pub static COMMANDS: [&'static str; 19] = [ "sleep", "shitbox", "true", + "whoami", "yes", ]; diff --git a/src/cmd/whoami/mod.rs b/src/cmd/whoami/mod.rs new file mode 100644 index 0000000..2071e5a --- /dev/null +++ b/src/cmd/whoami/mod.rs @@ -0,0 +1,36 @@ +use std::ffi::CStr; + +use { + clap::Command, + libc::{geteuid, getpwuid}, + super::Cmd, +}; + +#[derive(Debug, Default)] +pub struct Whoami; + +impl Cmd for Whoami { + fn name(&self) -> &str { + "whoami" + } + + fn cli(&self) -> clap::Command { + Command::new("whoami") + .about("print effective user name") + .author("Nathan Fisher") + } + + fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + let pwnam = unsafe { + let pw = *getpwuid(geteuid()); + CStr::from_ptr((pw).pw_name) + }; + let pwnam = pwnam.to_str()?; + println!("{pwnam}"); + Ok(()) + } + + fn path(&self) -> Option { + Some(crate::Path::UsrBin) + } +}