Add logname applet

This commit is contained in:
Nathan Fisher 2023-01-21 19:34:02 -05:00
parent d92cb9f208
commit eeb9a61fa3
3 changed files with 36 additions and 2 deletions

View File

@ -21,6 +21,7 @@ code between applets, making for an overall smaller binary.
- base64
- basename
- bootstrap
- chgrp
- chmod
- chown
- clear
@ -35,9 +36,12 @@ code between applets, making for an overall smaller binary.
- hostname
- link
- mkfifo
- mknod
- mountpoint
- nologin
- nproc
- printenv
- pwd
- rev
- rm
- rmdir

27
src/cmd/logname/mod.rs Normal file
View File

@ -0,0 +1,27 @@
use std::ffi::CStr;
use super::Cmd;
use clap::Command;
#[derive(Debug, Default)]
pub struct Logname;
impl Cmd for Logname {
fn cli(&self) -> clap::Command {
Command::new("logname")
.about("print user's login name")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
}
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
let logname = unsafe { CStr::from_ptr(libc::getlogin()) };
let logname = logname.to_str()?;
println!("{logname}");
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}

View File

@ -24,6 +24,7 @@ mod head;
mod hostname;
mod link;
mod ln;
mod logname;
mod ls;
mod mkfifo;
mod mknod;
@ -81,6 +82,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"groups" => Some(Box::new(groups::Groups::default())),
"head" => Some(Box::new(head::Head::default())),
"link" => Some(Box::new(link::Link::default())),
"logname" => Some(Box::new(logname::Logname::default())),
"mkfifo" => Some(Box::new(mkfifo::MkFifo::default())),
"mknod" => Some(Box::new(mknod::MkNod::default())),
"mountpoint" => Some(Box::new(mountpoint::Mountpoint::default())),
@ -103,13 +105,13 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
}
}
pub static COMMANDS: [&str; 36] = [
pub static COMMANDS: [&str; 37] = [
"base32",
"base64",
"basename",
"bootstrap",
"chmod",
"chgrp",
"chmod",
"chown",
"clear",
"cut",
@ -122,6 +124,7 @@ pub static COMMANDS: [&str; 36] = [
"head",
"hostname",
"link",
"logname",
"mkfifo",
"mknod",
"mountpoint",