diff --git a/Cargo.toml b/Cargo.toml index 887efb6..6e691a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "echo", "env", "wc", + "whoami", ] [workspace.dependencies] diff --git a/whoami/Cargo.toml b/whoami/Cargo.toml new file mode 100644 index 0000000..596a353 --- /dev/null +++ b/whoami/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "whoami" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { workspace = true } +users = "0.11.0" diff --git a/whoami/src/main.rs b/whoami/src/main.rs new file mode 100644 index 0000000..325cd23 --- /dev/null +++ b/whoami/src/main.rs @@ -0,0 +1,15 @@ +/// Get the current user +/// +/// Went the route of using the existing users crate rather than +/// re-implementing this via the libc crate. +use users::{get_current_uid, get_user_by_uid}; + +fn main() { + let user = get_user_by_uid(get_current_uid()); + if let Some(u) = user { + let name = u.name().to_str(); + if name.is_some() { + println!("{}", name.unwrap()); + } + } +}