From 604de7c4de6f51a6bcd495ccda11602c12804493 Mon Sep 17 00:00:00 2001 From: Will Shuttleworth Date: Tue, 27 May 2025 16:43:06 -0400 Subject: [PATCH] stty: fix output redirection --- src/uu/stty/src/stty.rs | 48 +++++++++++------------------------------ 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/src/uu/stty/src/stty.rs b/src/uu/stty/src/stty.rs index 5cc24a596..8114b6b27 100644 --- a/src/uu/stty/src/stty.rs +++ b/src/uu/stty/src/stty.rs @@ -15,11 +15,11 @@ use nix::sys::termios::{ }; use nix::{ioctl_read_bad, ioctl_write_ptr_bad}; use std::fs::File; -use std::io::{self, Stdout, stdout}; +use std::io::Result; use std::ops::ControlFlow; -use std::os::fd::{AsFd, BorrowedFd}; +use std::os::fd::AsFd; use std::os::unix::fs::OpenOptionsExt; -use std::os::unix::io::{AsRawFd, RawFd}; +use std::os::unix::io::AsRawFd; use uucore::error::{UResult, USimpleError}; use uucore::{format_usage, help_about, help_usage}; @@ -94,35 +94,12 @@ mod options { struct Options<'a> { all: bool, save: bool, - file: Device, + file: File, settings: Option>, } -enum Device { - File(File), - Stdout(Stdout), -} - -impl AsFd for Device { - fn as_fd(&self) -> BorrowedFd<'_> { - match self { - Self::File(f) => f.as_fd(), - Self::Stdout(stdout) => stdout.as_fd(), - } - } -} - -impl AsRawFd for Device { - fn as_raw_fd(&self) -> RawFd { - match self { - Self::File(f) => f.as_raw_fd(), - Self::Stdout(stdout) => stdout.as_raw_fd(), - } - } -} - impl<'a> Options<'a> { - fn from(matches: &'a ArgMatches) -> io::Result { + fn from(matches: &'a ArgMatches) -> Result { Ok(Self { all: matches.get_flag(options::ALL), save: matches.get_flag(options::SAVE), @@ -136,13 +113,14 @@ impl<'a> Options<'a> { // will clean up the FD for us on exit, so it doesn't // matter. The alternative would be to have an enum of // BorrowedFd/OwnedFd to handle both cases. - Some(f) => Device::File( - std::fs::OpenOptions::new() - .read(true) - .custom_flags(O_NONBLOCK) - .open(f)?, - ), - None => Device::Stdout(stdout()), + Some(f) => std::fs::OpenOptions::new() + .read(true) + .custom_flags(O_NONBLOCK) + .open(f)?, + None => std::fs::OpenOptions::new() + .read(true) + .custom_flags(O_NONBLOCK) + .open("/dev/tty")?, }, settings: matches .get_many::(options::SETTINGS)