Skip to content

Commit 09e313b

Browse files
committed
stty: Don't panic when GNU provided stty 51 us
1 parent 12193c5 commit 09e313b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/uu/stty/src/stty.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@
1212
// spell-checker:ignore sigquit sigtstp
1313
// spell-checker:ignore cbreak decctlq evenp litout oddp tcsadrain exta extb NCCS cfsetispeed
1414
// spell-checker:ignore notaflag notacombo notabaud
15+
// spell-checker:ignore baudrate TCGETS
1516

1617
mod flags;
1718

1819
use crate::flags::AllFlags;
1920
use crate::flags::COMBINATION_SETTINGS;
2021
use clap::{Arg, ArgAction, ArgMatches, Command};
2122
use nix::libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort};
23+
24+
#[cfg(target_os = "linux")]
25+
use nix::libc::{TCGETS2, termios2};
26+
2227
use nix::sys::termios::{
2328
ControlFlags, InputFlags, LocalFlags, OutputFlags, SetArg, SpecialCharacterIndices as S,
24-
Termios, cfgetospeed, cfsetispeed, cfsetospeed, tcgetattr, tcsetattr,
29+
Termios, cfsetispeed, cfsetospeed, tcgetattr, tcsetattr,
2530
};
2631
use nix::{ioctl_read_bad, ioctl_write_ptr_bad};
2732
use std::cmp::Ordering;
@@ -613,16 +618,27 @@ fn print_terminal_size(
613618
window_size: Option<&TermSize>,
614619
term_size: Option<&TermSize>,
615620
) -> nix::Result<()> {
616-
let speed = cfgetospeed(termios);
621+
// GNU linked against glibc 2.42 provides us baudrate 51 which panics cfgetospeed
622+
#[cfg(not(target_os = "linux"))]
623+
let speed = nix::sys::termios::cfgetospeed(termios);
624+
#[cfg(target_os = "linux")]
625+
ioctl_read_bad!(tcgets2, TCGETS2, termios2);
626+
#[cfg(target_os = "linux")]
627+
let speed = {
628+
let mut t2 = unsafe { std::mem::zeroed::<termios2>() };
629+
unsafe { tcgets2(opts.file.as_raw_fd(), &mut t2)? };
630+
t2.c_ospeed
631+
};
632+
617633
let mut printer = WrappedPrinter::new(window_size);
618634

619-
// BSDs use a u32 for the baud rate, so we can simply print it.
620-
#[cfg(bsd)]
635+
// BSDs and Linux use a u32 for the baud rate, so we can simply print it.
636+
#[cfg(any(target_os = "linux", bsd))]
621637
printer.print(&translate!("stty-output-speed", "speed" => speed));
622638

623639
// Other platforms need to use the baud rate enum, so printing the right value
624640
// becomes slightly more complicated.
625-
#[cfg(not(bsd))]
641+
#[cfg(not(any(target_os = "linux", bsd)))]
626642
for (text, baud_rate) in BAUD_RATES {
627643
if *baud_rate == speed {
628644
printer.print(&translate!("stty-output-speed", "speed" => (*text)));

0 commit comments

Comments
 (0)