Add whoami applet

This commit is contained in:
Nathan Fisher 2023-01-09 18:39:38 -05:00
parent 0513cdef2e
commit 315ee56e25
2 changed files with 41 additions and 2 deletions

View File

@ -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<Box<dyn Cmd>> {
"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",
];

36
src/cmd/whoami/mod.rs Normal file
View File

@ -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<dyn std::error::Error>> {
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<crate::Path> {
Some(crate::Path::UsrBin)
}
}