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 24, 2023
1 parent cc77a95 commit 2c012b8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 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
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
12 changes: 7 additions & 5 deletions src/uu/yes/src/yes.rs
Expand Up @@ -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};

Check failure on line 11 in src/uu/yes/src/yes.rs

View workflow job for this annotation

GitHub Actions / Style/lint (windows-latest, feat_os_windows)

ERROR: `cargo clippy`: unused imports: `ErrorKind`, `Error` (file:'src\uu\yes\src/yes.rs', line:11)

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(Error::from(ErrorKind::Other)),

Check warning on line 79 in src/uu/yes/src/yes.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/yes/src/yes.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
}
Ok(())
}

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

0 comments on commit 2c012b8

Please sign in to comment.