Fix inverted is_console_session() logic

The $TERM matching logic was inverted.
This commit is contained in:
Mahmoud Al-Qudsi 2023-05-02 14:53:10 -05:00
parent c94fce75e5
commit d3abd5d600

View File

@ -1891,39 +1891,35 @@ pub const fn assert_sync<T: Sync>() {}
/// session. We err on the side of assuming it's not a console session. This approach isn't /// session. We err on the side of assuming it's not a console session. This approach isn't
/// bullet-proof and that's OK. /// bullet-proof and that's OK.
pub fn is_console_session() -> bool { pub fn is_console_session() -> bool {
*CONSOLE_SESSION static IS_CONSOLE_SESSION: Lazy<bool> = Lazy::new(|| {
} use std::os::unix::ffi::OsStrExt;
static CONSOLE_SESSION: Lazy<bool> = Lazy::new(|| { const PATH_MAX: usize = libc::PATH_MAX as usize;
const path_max: usize = libc::PATH_MAX as _; let mut tty_name = [0u8; PATH_MAX];
let mut tty_name: [u8; path_max] = [0; path_max]; unsafe {
if unsafe { if libc::ttyname_r(STDIN_FILENO, tty_name.as_mut_ptr().cast(), tty_name.len()) != 0 {
libc::ttyname_r(
STDIN_FILENO,
std::ptr::addr_of_mut!(tty_name).cast(),
path_max,
)
} != 0
{
return false; return false;
} }
// Test that the tty matches /dev/(console|dcons|tty[uv\d]) }
let len = "/dev/tty".len(); // Check if the tty matches /dev/(console|dcons|tty[uv\d])
const LEN: usize = b"/dev/tty".len();
( (
( (
tty_name.starts_with(b"/dev/tty") && tty_name.starts_with(b"/dev/tty") &&
([b'u', b'v'].contains(&tty_name[len]) || tty_name[len].is_ascii_digit()) ([b'u', b'v'].contains(&tty_name[LEN]) || tty_name[LEN].is_ascii_digit())
) || ) ||
tty_name.starts_with(b"/dev/dcons\0") || tty_name.starts_with(b"/dev/dcons\0") ||
tty_name.starts_with(b"/dev/console\0")) tty_name.starts_with(b"/dev/console\0"))
// and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` // and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` or `sun-color`.
&& match env::var("TERM") { && match env::var_os("TERM") {
Ok(term) => ["-", "sun-color"].contains(&term.as_str()), Some(term) => !term.as_bytes().contains(&b'-'),
Err(env::VarError::NotPresent) => true, None => true,
Err(_) => false,
} }
}); });
*IS_CONSOLE_SESSION
}
/// Asserts that a slice is alphabetically sorted by a [`&wstr`] `name` field. /// Asserts that a slice is alphabetically sorted by a [`&wstr`] `name` field.
/// ///
/// Mainly useful for static asserts/const eval. /// Mainly useful for static asserts/const eval.