diff --git a/users/Cargo.toml b/users/Cargo.toml index 6e68f000..5debb689 100644 --- a/users/Cargo.toml +++ b/users/Cargo.toml @@ -14,7 +14,7 @@ gettext-rs.workspace = true libc.workspace = true chrono.workspace = true syslog = "6.1" -libcrypt-rs = "0.1.2" +libcrypt-rs = "0.1" thiserror = "1.0" binrw = "0.14" diff --git a/users/newgrp.rs b/users/newgrp.rs index c681ef45..4e640f71 100644 --- a/users/newgrp.rs +++ b/users/newgrp.rs @@ -43,16 +43,20 @@ const MAX_GROUPS: usize = libc::KERN_NGROUPS_MAX as usize; #[cfg(target_os = "macos")] const MAX_GROUPS: usize = 16; -/// newgrp — change to a new group -#[derive(Parser, Debug)] -#[command(author, version, about, long_about)] +#[derive(Parser)] +#[command(version, about = "newgrp — change to a new group")] struct Args { - /// Change the environment to what would be expected if the user actually logged in again (letter `l`). - #[arg(short = 'l')] + #[arg( + short = 'l', + help = "Change the environment to what would be expected if the user actually logged in again (letter 'l')." + )] login: bool, - /// Specifies the group ID or group name. This is a positional argument that must be provided. - #[arg(value_name = "GROUP", required = true)] + #[arg( + value_name = "GROUP", + required = true, + help = "Specifies the group ID or group name. This is a positional argument that must be provided." + )] group: String, } @@ -496,10 +500,10 @@ fn find_matching_group(group_identifier: &str, groups: &Vec) -> Option Option { - unsafe { - let login_ptr = libc::getlogin(); - if !login_ptr.is_null() { - CStr::from_ptr(login_ptr) - .to_str() - .ok() - .map(|s| s.to_string()) - } else { - None - } - } -} - -/// Retrieves the current tty name. -fn get_current_tty() -> Option { - unsafe { - let tty_ptr = libc::ttyname(0); - if !tty_ptr.is_null() { - CStr::from_ptr(tty_ptr).to_str().ok().map(|s| { - if s.starts_with("/dev/") { - s[5..].to_string() - } else { - s.to_string() - } - }) - } else { - None - } - } -} - /// Checks permissions for accessing a specified group based on the provided user credentials. /// /// This function determines whether the user associated with the given password structure @@ -822,6 +793,11 @@ fn set_login_environment(user: &str) -> Result<(), io::Error> { } fn main() -> Result<(), Box> { + setlocale(LocaleCategory::LcAll, ""); + textdomain(PROJECT_NAME)?; + bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + let mut exit_code = 0; + let args = Args::try_parse().unwrap_or_else(|err| { if err.kind() == ErrorKind::DisplayHelp || err.kind() == ErrorKind::DisplayVersion { // Print help or version message @@ -835,11 +811,6 @@ fn main() -> Result<(), Box> { std::process::exit(1); }); - setlocale(LocaleCategory::LcAll, ""); - textdomain(PROJECT_NAME)?; - bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - let mut exit_code = 0; - if let Err(err) = newgrp(args) { exit_code = 1; eprint!("{}", err);