Skip to content

Commit

Permalink
Don't check the errno value from isatty. (#468)
Browse files Browse the repository at this point in the history
We don't actually do anything different for different errno values,
other than panic on unknown ones, and that isn't that isn't adding
much value compared to the cost of being an extra surprise when
porting to new OS's.

Fixes #467.
  • Loading branch information
sunfishcode committed Dec 1, 2022
1 parent 1c81957 commit ce30bd9
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/backend/libc/termios/syscalls.rs
Expand Up @@ -10,13 +10,13 @@ use crate::fd::BorrowedFd;
#[cfg(feature = "procfs")]
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
use crate::ffi::CStr;
#[cfg(not(target_os = "wasi"))]
use crate::io;
#[cfg(not(target_os = "wasi"))]
use crate::process::{Pid, RawNonZeroPid};
#[cfg(not(target_os = "wasi"))]
use crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize};
use core::mem::MaybeUninit;
use libc_errno::errno;

#[cfg(not(target_os = "wasi"))]
pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
Expand Down Expand Up @@ -142,26 +142,12 @@ pub(crate) fn cfsetspeed(termios: &mut Termios, speed: Speed) -> io::Result<()>
}

pub(crate) fn isatty(fd: BorrowedFd<'_>) -> bool {
let res = unsafe { c::isatty(borrowed_fd(fd)) };
if res == 0 {
match errno().0 {
#[cfg(not(any(target_os = "android", target_os = "linux")))]
c::ENOTTY => false,

// Old Linux versions reportedly return `EINVAL`.
// <https://man7.org/linux/man-pages/man3/isatty.3.html#ERRORS>
#[cfg(any(target_os = "android", target_os = "linux"))]
c::ENOTTY | c::EINVAL => false,

// Darwin mysteriously returns `EOPNOTSUPP` sometimes.
#[cfg(any(target_os = "ios", target_os = "macos"))]
c::EOPNOTSUPP => false,

err => panic!("unexpected error from isatty: {:?}", err),
}
} else {
true
}
// Use the return value of `isatty` alone. We don't check `errno` because
// we return `bool` rather than `io::Result<bool>`, because we assume
// `BorrrowedFd` protects us from `EBADF`, and any other reasonably
// anticipated errno value would end up interpreted as "assume it's not a
// terminal" anyway.
unsafe { c::isatty(borrowed_fd(fd)) != 0 }
}

#[cfg(feature = "procfs")]
Expand Down

0 comments on commit ce30bd9

Please sign in to comment.