1
1
//! Get system identification
2
2
use std:: mem;
3
+ use std:: os:: unix:: ffi:: OsStrExt ;
4
+ use std:: ffi:: OsStr ;
3
5
use libc:: c_char;
4
6
use crate :: { Errno , Result } ;
5
7
@@ -10,27 +12,27 @@ pub struct UtsName(libc::utsname);
10
12
11
13
impl UtsName {
12
14
/// Name of the operating system implementation.
13
- pub fn sysname ( & self ) -> & [ u8 ] {
15
+ pub fn sysname ( & self ) -> & OsStr {
14
16
cast_and_trim ( & self . 0 . sysname )
15
17
}
16
18
17
19
/// Network name of this machine.
18
- pub fn nodename ( & self ) -> & [ u8 ] {
20
+ pub fn nodename ( & self ) -> & OsStr {
19
21
cast_and_trim ( & self . 0 . nodename )
20
22
}
21
23
22
24
/// Release level of the operating system.
23
- pub fn release ( & self ) -> & [ u8 ] {
25
+ pub fn release ( & self ) -> & OsStr {
24
26
cast_and_trim ( & self . 0 . release )
25
27
}
26
28
27
29
/// Version level of the operating system.
28
- pub fn version ( & self ) -> & [ u8 ] {
30
+ pub fn version ( & self ) -> & OsStr {
29
31
cast_and_trim ( & self . 0 . version )
30
32
}
31
33
32
34
/// Machine hardware platform.
33
- pub fn machine ( & self ) -> & [ u8 ] {
35
+ pub fn machine ( & self ) -> & OsStr {
34
36
cast_and_trim ( & self . 0 . machine )
35
37
}
36
38
}
@@ -44,30 +46,32 @@ pub fn uname() -> Result<UtsName> {
44
46
}
45
47
}
46
48
47
- fn cast_and_trim ( slice : & [ c_char ] ) -> & [ u8 ] {
49
+ fn cast_and_trim ( slice : & [ c_char ] ) -> & OsStr {
48
50
let length = slice. iter ( ) . position ( |& byte| byte == 0 ) . unwrap_or ( slice. len ( ) ) ;
49
- unsafe {
51
+ let bytes = unsafe {
50
52
std:: slice:: from_raw_parts ( slice. as_ptr ( ) . cast ( ) , length)
51
- }
53
+ } ;
54
+
55
+ OsStr :: from_bytes ( bytes)
52
56
}
53
57
54
58
#[ cfg( test) ]
55
59
mod test {
56
60
#[ cfg( target_os = "linux" ) ]
57
61
#[ test]
58
62
pub fn test_uname_linux ( ) {
59
- assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , b "Linux") ;
63
+ assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , "Linux" ) ;
60
64
}
61
65
62
66
#[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
63
67
#[ test]
64
68
pub fn test_uname_darwin ( ) {
65
- assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , b "Darwin") ;
69
+ assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , "Darwin" ) ;
66
70
}
67
71
68
72
#[ cfg( target_os = "freebsd" ) ]
69
73
#[ test]
70
74
pub fn test_uname_freebsd ( ) {
71
- assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , b "FreeBSD") ;
75
+ assert_eq ! ( super :: uname( ) . unwrap( ) . sysname( ) , "FreeBSD" ) ;
72
76
}
73
77
}
0 commit comments