37 lines
775 B
Rust
37 lines
775 B
Rust
|
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)
|
||
|
}
|
||
|
}
|