diff --git a/Cargo.lock b/Cargo.lock index c86edc12a2c..60034306961 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3311,7 +3311,6 @@ name = "uu_yes" version = "0.0.17" dependencies = [ "clap", - "libc", "nix", "uucore", ] diff --git a/src/uu/yes/Cargo.toml b/src/uu/yes/Cargo.toml index cf120f2e0ba..4d8ac1ffdfc 100644 --- a/src/uu/yes/Cargo.toml +++ b/src/uu/yes/Cargo.toml @@ -16,11 +16,10 @@ path = "src/yes.rs" [dependencies] clap = { workspace=true } -libc = { workspace=true } uucore = { workspace=true, features=["pipes"] } -[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] -nix = { workspace=true } +[target.'cfg(unix)'.dependencies] +nix = { workspace=true, features = ["signal"] } [[bin]] name = "yes" diff --git a/src/uu/yes/src/yes.rs b/src/uu/yes/src/yes.rs index 714bdcac514..26c79dbd2bd 100644 --- a/src/uu/yes/src/yes.rs +++ b/src/uu/yes/src/yes.rs @@ -8,9 +8,11 @@ /* last synced with: yes (GNU coreutils) 8.13 */ use std::borrow::Cow; -use std::io::{self, Result, Write}; +use std::io::{self, Error, ErrorKind, Result, Write}; use clap::{Arg, ArgAction, Command}; +#[cfg(unix)] +use nix::sys::signal::{signal, SigHandler::SigDfl, Signal::SIGPIPE}; use uucore::error::{UResult, USimpleError}; use uucore::{format_usage, help_about, help_usage}; @@ -71,11 +73,11 @@ fn prepare_buffer<'a>(input: &'a str, buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8 #[cfg(unix)] fn enable_pipe_errors() -> Result<()> { - let ret = unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) }; - if ret == libc::SIG_ERR { - return Err(io::Error::new(io::ErrorKind::Other, "")); + // SAFETY: this function is safe as long as we do not use a custom SigHandler -- we use the default one. + match unsafe { signal(SIGPIPE, SigDfl) } { + Ok(_) => Ok(()), + _ => Err(Error::from(ErrorKind::Other)), } - Ok(()) } #[cfg(not(unix))]