diff --git a/src/nix.rs b/src/nix.rs index c5b00b8..c8ec2cf 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -2,9 +2,11 @@ extern crate libc; use std::io::IsTerminal; +use std::ffi::{c_ushort, CString}; + use self::{ super::Size, - libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}, + libc::{ioctl, O_RDONLY, STDOUT_FILENO, TIOCGWINSZ}, }; /// A representation of the size of the current terminal @@ -31,7 +33,29 @@ pub fn get() -> Option { x: 0, y: 0, }; - let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut us) }; + + let fd = if let Ok(ssh_term) = std::env::var("SSH_TTY") { + // Convert path to a C-compatible string + let c_path = CString::new(ssh_term).expect("Failed to convert path to CString"); + + // Open the terminal device + let fd = unsafe { libc::open(c_path.as_ptr(), O_RDONLY) }; + if fd < 0 { + return None; // Failed to open the terminal device + } + + fd + } else { + STDOUT_FILENO + }; + + let r = unsafe { ioctl(fd, TIOCGWINSZ, &mut us) }; + + // Closing the open file descriptor + if fd != STDOUT_FILENO { + unsafe { libc::close(fd); } + } + if r == 0 { Some(Size { rows: us.rows,