Skip to content

Commit 61af439

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

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/uu/stty/src/stty.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
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};
21-
use nix::libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort};
22+
use nix::libc::{O_NONBLOCK, TCGETS2, TIOCGWINSZ, TIOCSWINSZ, c_ushort, termios2};
2223
use nix::sys::termios::{
2324
ControlFlags, InputFlags, LocalFlags, OutputFlags, SetArg, SpecialCharacterIndices as S,
24-
Termios, cfgetospeed, cfsetispeed, cfsetospeed, tcgetattr, tcsetattr,
25+
Termios, cfsetispeed, cfsetospeed, tcgetattr, tcsetattr,
2526
};
2627
use nix::{ioctl_read_bad, ioctl_write_ptr_bad};
2728
use std::cmp::Ordering;
@@ -613,16 +614,27 @@ fn print_terminal_size(
613614
window_size: Option<&TermSize>,
614615
term_size: Option<&TermSize>,
615616
) -> nix::Result<()> {
616-
let speed = cfgetospeed(termios);
617+
// GNU linked against glibc 2.42 provides us baudrate 51 which panics cfgetospeed
618+
#[cfg(not(target_os = "linux"))]
619+
let speed = nix::sys::termios::cfgetospeed(termios);
620+
#[cfg(target_os = "linux")]
621+
ioctl_read_bad!(tcgets2, TCGETS2, termios2);
622+
#[cfg(target_os = "linux")]
623+
let speed = {
624+
let mut t2 = unsafe { std::mem::zeroed::<termios2>() };
625+
unsafe { tcgets2(opts.file.as_raw_fd(), &mut t2)? };
626+
t2.c_ospeed
627+
};
628+
617629
let mut printer = WrappedPrinter::new(window_size);
618630

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

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

0 commit comments

Comments
 (0)