File tree Expand file tree Collapse file tree 3 files changed +29
-6
lines changed Expand file tree Collapse file tree 3 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -150,11 +150,10 @@ pub(crate) fn isatty(fd: BorrowedFd<'_>) -> bool {
150
150
#[ cfg( not( any( target_os = "fuchsia" , target_os = "wasi" ) ) ) ]
151
151
pub ( crate ) fn ttyname ( dirfd : BorrowedFd < ' _ > , buf : & mut [ u8 ] ) -> io:: Result < usize > {
152
152
unsafe {
153
- ret ( c:: ttyname_r (
154
- borrowed_fd ( dirfd) ,
155
- buf. as_mut_ptr ( ) . cast ( ) ,
156
- buf. len ( ) ,
157
- ) ) ?;
158
- Ok ( CStr :: from_ptr ( buf. as_ptr ( ) . cast ( ) ) . to_bytes ( ) . len ( ) )
153
+ // `ttyname_r` returns its error status rather than using `errno`.
154
+ match c:: ttyname_r ( borrowed_fd ( dirfd) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) ) {
155
+ 0 => Ok ( CStr :: from_ptr ( buf. as_ptr ( ) . cast ( ) ) . to_bytes ( ) . len ( ) ) ,
156
+ err => Err ( io:: Errno :: from_raw_os_error ( err) ) ,
157
+ }
159
158
}
160
159
}
Original file line number Diff line number Diff line change 6
6
7
7
#[ cfg( not( windows) ) ]
8
8
mod isatty;
9
+ #[ cfg( not( windows) ) ]
10
+ mod ttyname;
Original file line number Diff line number Diff line change
1
+ use rustix:: io;
2
+ use rustix:: termios:: ttyname;
3
+ use std:: fs:: File ;
4
+
5
+ #[ test]
6
+ fn test_ttyname_ok ( ) {
7
+ let file = File :: open ( "/dev/stdin" ) . unwrap ( ) ;
8
+ assert ! ( ttyname( & file, Vec :: new( ) )
9
+ . unwrap( )
10
+ . into_string( )
11
+ . unwrap( )
12
+ . starts_with( "/dev/" ) ) ;
13
+ }
14
+
15
+ #[ test]
16
+ fn test_ttyname_not_tty ( ) {
17
+ let file = File :: open ( "Cargo.toml" ) . unwrap ( ) ;
18
+ assert_eq ! ( ttyname( & file, Vec :: new( ) ) . unwrap_err( ) , io:: Errno :: NOTTY ) ;
19
+
20
+ let file = File :: open ( "/dev/null" ) . unwrap ( ) ;
21
+ assert_eq ! ( ttyname( & file, Vec :: new( ) ) . unwrap_err( ) , io:: Errno :: NOTTY ) ;
22
+ }
You can’t perform that action at this time.
0 commit comments