11use std:: { ffi:: CStr , fmt:: Write as _, io, mem:: MaybeUninit } ;
22
3- use crate :: { UtsName , colors:: COLORS , syscall:: read_file_fast} ;
3+ use crate :: { colors:: COLORS , last_os_error, syscall:: read_file_fast, UtsName } ;
4+
5+ #[ inline]
6+ #[ cold]
7+ const fn unknown_user ( ) -> & ' static str { "unknown_user" }
8+
9+ #[ inline]
10+ #[ cold]
11+ const fn unknown_host ( ) -> & ' static str { "unknown_host" }
412
513#[ must_use]
614#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
715pub fn get_username_and_hostname ( utsname : & UtsName ) -> String {
816 let username = unsafe {
917 let ptr = libc:: getenv ( c"USER" . as_ptr ( ) ) ;
1018 if ptr. is_null ( ) {
11- " unknown_user"
19+ unknown_user ( )
1220 } else {
13- CStr :: from_ptr ( ptr) . to_str ( ) . unwrap_or ( " unknown_user" )
21+ CStr :: from_ptr ( ptr) . to_str ( ) . unwrap_or_else ( |_| unknown_user ( ) )
1422 }
1523 } ;
16- let hostname = utsname. nodename ( ) . to_str ( ) . unwrap_or ( " unknown_host" ) ;
24+ let hostname = utsname. nodename ( ) . to_str ( ) . unwrap_or_else ( |_| unknown_host ( ) ) ;
1725
1826 let capacity = COLORS . yellow . len ( )
1927 + username. len ( )
@@ -35,13 +43,17 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
3543 result
3644}
3745
46+ #[ inline]
47+ #[ cold]
48+ const fn unknown_shell ( ) -> & ' static str { "unknown_shell" }
49+
3850#[ must_use]
3951#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
4052pub fn get_shell ( ) -> String {
4153 unsafe {
4254 let ptr = libc:: getenv ( c"SHELL" . as_ptr ( ) ) ;
4355 if ptr. is_null ( ) {
44- return " unknown_shell" . into ( ) ;
56+ return unknown_shell ( ) . into ( ) ;
4557 }
4658
4759 let bytes = CStr :: from_ptr ( ptr) . to_bytes ( ) ;
@@ -63,7 +75,7 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
6375 let path = b"/\0 " ;
6476
6577 if unsafe { libc:: statvfs ( path. as_ptr ( ) . cast ( ) , vfs. as_mut_ptr ( ) ) } != 0 {
66- return Err ( io :: Error :: last_os_error ( ) ) ;
78+ return last_os_error ( ) ;
6779 }
6880
6981 let vfs = unsafe { vfs. assume_init ( ) } ;
0 commit comments