Skip to content

Commit

Permalink
Refactor yes to remove libc dependency
Browse files Browse the repository at this point in the history
Yes, using libc while using nix waas a bit redundant.
  • Loading branch information
anastygnome committed Mar 23, 2023
1 parent 9090dd7 commit 509da03
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/uu/yes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions src/uu/yes/src/yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::borrow::Cow;
use std::io::{self, 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};

Expand Down Expand Up @@ -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(io::Error::new(io::ErrorKind::Other, "")),
}
Ok(())
}

#[cfg(not(unix))]
Expand Down

0 comments on commit 509da03

Please sign in to comment.