@@ -43,16 +43,20 @@ const MAX_GROUPS: usize = libc::KERN_NGROUPS_MAX as usize;
4343#[ cfg( target_os = "macos" ) ]
4444const MAX_GROUPS : usize = 16 ;
4545
46- /// newgrp — change to a new group
47- #[ derive( Parser , Debug ) ]
48- #[ command( author, version, about, long_about) ]
46+ #[ derive( Parser ) ]
47+ #[ command( version, about = "newgrp — change to a new group" ) ]
4948struct Args {
50- /// Change the environment to what would be expected if the user actually logged in again (letter `l`).
51- #[ arg( short = 'l' ) ]
49+ #[ arg(
50+ short = 'l' ,
51+ help = "Change the environment to what would be expected if the user actually logged in again (letter 'l')."
52+ ) ]
5253 login : bool ,
5354
54- /// Specifies the group ID or group name. This is a positional argument that must be provided.
55- #[ arg( value_name = "GROUP" , required = true ) ]
55+ #[ arg(
56+ value_name = "GROUP" ,
57+ required = true ,
58+ help = "Specifies the group ID or group name. This is a positional argument that must be provided."
59+ ) ]
5660 group : String ,
5761}
5862
@@ -496,10 +500,10 @@ fn find_matching_group(group_identifier: &str, groups: &Vec<Group>) -> Option<Gr
496500///
497501fn logger ( name : & str , gid : u32 ) {
498502 // Get the current login name
499- let loginname = get_current_login ( ) . unwrap_or ( "???" . to_string ( ) ) ;
503+ let loginname = plib :: curuser :: login_name ( ) ;
500504
501505 // Get the current tty device name
502- let tty = get_current_tty ( ) . unwrap_or ( "???" . to_string ( ) ) ;
506+ let tty = plib :: curuser :: tty ( ) ;
503507
504508 // Log the message
505509 eprintln ! (
@@ -508,39 +512,6 @@ fn logger(name: &str, gid: u32) {
508512 ) ;
509513}
510514
511- /// Retrieves the current login name.
512- fn get_current_login ( ) -> Option < String > {
513- unsafe {
514- let login_ptr = libc:: getlogin ( ) ;
515- if !login_ptr. is_null ( ) {
516- CStr :: from_ptr ( login_ptr)
517- . to_str ( )
518- . ok ( )
519- . map ( |s| s. to_string ( ) )
520- } else {
521- None
522- }
523- }
524- }
525-
526- /// Retrieves the current tty name.
527- fn get_current_tty ( ) -> Option < String > {
528- unsafe {
529- let tty_ptr = libc:: ttyname ( 0 ) ;
530- if !tty_ptr. is_null ( ) {
531- CStr :: from_ptr ( tty_ptr) . to_str ( ) . ok ( ) . map ( |s| {
532- if s. starts_with ( "/dev/" ) {
533- s[ 5 ..] . to_string ( )
534- } else {
535- s. to_string ( )
536- }
537- } )
538- } else {
539- None
540- }
541- }
542- }
543-
544515/// Checks permissions for accessing a specified group based on the provided user credentials.
545516///
546517/// 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> {
822793}
823794
824795fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
796+ setlocale ( LocaleCategory :: LcAll , "" ) ;
797+ textdomain ( PROJECT_NAME ) ?;
798+ bind_textdomain_codeset ( PROJECT_NAME , "UTF-8" ) ?;
799+ let mut exit_code = 0 ;
800+
825801 let args = Args :: try_parse ( ) . unwrap_or_else ( |err| {
826802 if err. kind ( ) == ErrorKind :: DisplayHelp || err. kind ( ) == ErrorKind :: DisplayVersion {
827803 // Print help or version message
@@ -835,11 +811,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
835811 std:: process:: exit ( 1 ) ;
836812 } ) ;
837813
838- setlocale ( LocaleCategory :: LcAll , "" ) ;
839- textdomain ( PROJECT_NAME ) ?;
840- bind_textdomain_codeset ( PROJECT_NAME , "UTF-8" ) ?;
841- let mut exit_code = 0 ;
842-
843814 if let Err ( err) = newgrp ( args) {
844815 exit_code = 1 ;
845816 eprint ! ( "{}" , err) ;
0 commit comments