From f759568da7341e27cf72ae2c0f92638b86a02235 Mon Sep 17 00:00:00 2001 From: Furby Haxx Date: Tue, 7 Jan 2025 16:42:44 +0100 Subject: [PATCH 1/4] Fix zero size for SSH terminal --- src/nix.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/nix.rs b/src/nix.rs index c5b00b8..e10cbdf 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -4,7 +4,7 @@ use std::io::IsTerminal; use self::{ super::Size, - libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}, + libc::{c_ushort, CString, ioctl, O_RDONLY, STDOUT_FILENO, TIOCGWINSZ}, }; /// A representation of the size of the current terminal @@ -31,7 +31,24 @@ pub fn get() -> Option { x: 0, y: 0, }; - let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut us) }; + + let fd = if let Ok(ssh_term) = 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) }; + if r == 0 { Some(Size { rows: us.rows, From ae2a7a2c392d9aa4acb7ea690d5324dd15196702 Mon Sep 17 00:00:00 2001 From: Furby Haxx Date: Tue, 7 Jan 2025 16:46:03 +0100 Subject: [PATCH 2/4] fix wrong imports --- src/nix.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nix.rs b/src/nix.rs index e10cbdf..153f3a1 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, CString, ioctl, O_RDONLY, STDOUT_FILENO, TIOCGWINSZ}, + libc::{ioctl, O_RDONLY, STDOUT_FILENO, TIOCGWINSZ}, }; /// A representation of the size of the current terminal From 3b43d89425f6ca8da0cb0935d35205a94df2d176 Mon Sep 17 00:00:00 2001 From: Furby Haxx Date: Tue, 7 Jan 2025 16:47:40 +0100 Subject: [PATCH 3/4] fix imports (again) --- src/nix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix.rs b/src/nix.rs index 153f3a1..ca0ae3c 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -34,7 +34,7 @@ pub fn get() -> Option { y: 0, }; - let fd = if let Ok(ssh_term) = env::var("SSH_TTY") { + 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"); From fb9507150bd329e337dbfc04e1a333754ae323fb Mon Sep 17 00:00:00 2001 From: Vital Reichmuth Date: Tue, 7 Jan 2025 22:58:32 +0100 Subject: [PATCH 4/4] Closing open file descriptor. --- src/nix.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/nix.rs b/src/nix.rs index ca0ae3c..c8ec2cf 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -50,6 +50,11 @@ pub fn get() -> Option { }; 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 {