@@ -43,16 +43,20 @@ const MAX_GROUPS: usize = libc::KERN_NGROUPS_MAX as usize;
43
43
#[ cfg( target_os = "macos" ) ]
44
44
const MAX_GROUPS : usize = 16 ;
45
45
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" ) ]
49
48
struct 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
+ ) ]
52
53
login : bool ,
53
54
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
+ ) ]
56
60
group : String ,
57
61
}
58
62
@@ -496,10 +500,10 @@ fn find_matching_group(group_identifier: &str, groups: &Vec<Group>) -> Option<Gr
496
500
///
497
501
fn logger ( name : & str , gid : u32 ) {
498
502
// Get the current login name
499
- let loginname = get_current_login ( ) . unwrap_or ( "???" . to_string ( ) ) ;
503
+ let loginname = plib :: curuser :: login_name ( ) ;
500
504
501
505
// Get the current tty device name
502
- let tty = get_current_tty ( ) . unwrap_or ( "???" . to_string ( ) ) ;
506
+ let tty = plib :: curuser :: tty ( ) ;
503
507
504
508
// Log the message
505
509
eprintln ! (
@@ -508,39 +512,6 @@ fn logger(name: &str, gid: u32) {
508
512
) ;
509
513
}
510
514
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
-
544
515
/// Checks permissions for accessing a specified group based on the provided user credentials.
545
516
///
546
517
/// 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> {
822
793
}
823
794
824
795
fn 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
+
825
801
let args = Args :: try_parse ( ) . unwrap_or_else ( |err| {
826
802
if err. kind ( ) == ErrorKind :: DisplayHelp || err. kind ( ) == ErrorKind :: DisplayVersion {
827
803
// Print help or version message
@@ -835,11 +811,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
835
811
std:: process:: exit ( 1 ) ;
836
812
} ) ;
837
813
838
- setlocale ( LocaleCategory :: LcAll , "" ) ;
839
- textdomain ( PROJECT_NAME ) ?;
840
- bind_textdomain_codeset ( PROJECT_NAME , "UTF-8" ) ?;
841
- let mut exit_code = 0 ;
842
-
843
814
if let Err ( err) = newgrp ( args) {
844
815
exit_code = 1 ;
845
816
eprint ! ( "{}" , err) ;
0 commit comments