From 5076dcc350e558f7f49ea142751358b7ea9dc21d Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 23 Jan 2023 10:17:17 -0500 Subject: [PATCH] Add `hostid` applet --- README.md | 1 + src/cmd/hostid/mod.rs | 27 +++++++++++++++++++++++++++ src/cmd/mod.rs | 6 +++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/cmd/hostid/mod.rs diff --git a/README.md b/README.md index b14ded0..2010407 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ code between applets, making for an overall smaller binary. - fold - groups - head +- hostid - hostname - link - logname diff --git a/src/cmd/hostid/mod.rs b/src/cmd/hostid/mod.rs new file mode 100644 index 0000000..736a8f9 --- /dev/null +++ b/src/cmd/hostid/mod.rs @@ -0,0 +1,27 @@ +use super::Cmd; +use clap::Command; + +#[derive(Debug, Default)] +pub struct Hostid; + +impl Cmd for Hostid { + fn cli(&self) -> clap::Command { + Command::new("hostid") + .about("print the numeric identifier for the current host") + .author("Nathan Fisher") + .version(env!("CARGO_PKG_VERSION")) + } + + fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + let hostid = unsafe { + libc::gethostid() + }; + let hostid: String = format!("{hostid:x}").chars().skip(8).collect(); + println!("{}", hostid); + Ok(()) + } + + fn path(&self) -> Option { + Some(crate::Path::UsrBin) + } +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 8ddb803..6914942 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -22,6 +22,7 @@ mod fold; mod getty; mod groups; mod head; +mod hostid; mod hostname; mod link; mod ln; @@ -86,6 +87,8 @@ pub fn get(name: &str) -> Option> { "fold" => Some(Box::new(fold::Fold::default())), "groups" => Some(Box::new(groups::Groups::default())), "head" => Some(Box::new(head::Head::default())), + "hostid" => Some(Box::new(hostid::Hostid::default())), + "hostname" => Some(Box::new(hostname::Hostname::default())), "link" => Some(Box::new(link::Link::default())), "logname" => Some(Box::new(logname::Logname::default())), "mkfifo" => Some(Box::new(mkfifo::MkFifo::default())), @@ -113,7 +116,7 @@ pub fn get(name: &str) -> Option> { } } -pub static COMMANDS: [&str; 41] = [ +pub static COMMANDS: [&str; 42] = [ "base32", "base64", "basename", @@ -131,6 +134,7 @@ pub static COMMANDS: [&str; 41] = [ "fold", "groups", "head", + "hostid", "hostname", "link", "logname",