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 - base64
- basename - basename
- bootstrap - bootstrap
- chgrp
- chmod - chmod
- chown - chown
- clear - clear
@ -35,9 +36,12 @@ code between applets, making for an overall smaller binary.
- hostname - hostname
- link - link
- mkfifo - mkfifo
- mknod
- mountpoint - mountpoint
- nologin - nologin
- nproc - nproc
- printenv
- pwd
- rev - rev
- rm - rm
- rmdir - 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 hostname;
mod link; mod link;
mod ln; mod ln;
mod logname;
mod ls; mod ls;
mod mkfifo; mod mkfifo;
mod mknod; mod mknod;
@ -81,6 +82,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"groups" => Some(Box::new(groups::Groups::default())), "groups" => Some(Box::new(groups::Groups::default())),
"head" => Some(Box::new(head::Head::default())), "head" => Some(Box::new(head::Head::default())),
"link" => Some(Box::new(link::Link::default())), "link" => Some(Box::new(link::Link::default())),
"logname" => Some(Box::new(logname::Logname::default())),
"mkfifo" => Some(Box::new(mkfifo::MkFifo::default())), "mkfifo" => Some(Box::new(mkfifo::MkFifo::default())),
"mknod" => Some(Box::new(mknod::MkNod::default())), "mknod" => Some(Box::new(mknod::MkNod::default())),
"mountpoint" => Some(Box::new(mountpoint::Mountpoint::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", "base32",
"base64", "base64",
"basename", "basename",
"bootstrap", "bootstrap",
"chmod",
"chgrp", "chgrp",
"chmod",
"chown", "chown",
"clear", "clear",
"cut", "cut",
@ -122,6 +124,7 @@ pub static COMMANDS: [&str; 36] = [
"head", "head",
"hostname", "hostname",
"link", "link",
"logname",
"mkfifo", "mkfifo",
"mknod", "mknod",
"mountpoint", "mountpoint",