Skip to content

Commit bd291ac

Browse files
committed
Fix the error values the libc backend ttyname implementation.
libc'c `ttyname_r` returns its error code rather than setting `errno`; fix the wrapper code accordingly.
1 parent 980e47f commit bd291ac

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/imp/libc/termios/syscalls.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ pub(crate) fn isatty(fd: BorrowedFd<'_>) -> bool {
150150
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
151151
pub(crate) fn ttyname(dirfd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
152152
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+
}
159158
}
160159
}

tests/termios/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66

77
#[cfg(not(windows))]
88
mod isatty;
9+
#[cfg(not(windows))]
10+
mod ttyname;

tests/termios/ttyname.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)