Skip to content

Commit

Permalink
Implement tee -p
Browse files Browse the repository at this point in the history
This amounts to enabling pipe errors when -p is not specified, but only
on unixish platforms.
  • Loading branch information
eds-collabora authored and sylvestre committed Jun 22, 2022
1 parent 75edeea commit 4e64299
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/uu/tee/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ mod options {
pub const APPEND: &str = "append";
pub const IGNORE_INTERRUPTS: &str = "ignore-interrupts";
pub const FILE: &str = "file";
pub const IGNORE_PIPE_ERRORS: &str = "ignore-pipe-errors";
}

#[allow(dead_code)]
struct Options {
append: bool,
ignore_interrupts: bool,
files: Vec<String>,
ignore_pipe_errors: bool,
}

#[uucore::main]
Expand All @@ -47,6 +49,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.values_of(options::FILE)
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(),
ignore_pipe_errors: matches.is_present(options::IGNORE_PIPE_ERRORS),
};

match tee(&options) {
Expand Down Expand Up @@ -79,6 +82,11 @@ pub fn uu_app<'a>() -> Command<'a> {
.multiple_occurrences(true)
.value_hint(clap::ValueHint::FilePath),
)
.arg(
Arg::new(options::IGNORE_PIPE_ERRORS)
.short('p')
.help("diagnose errors writing to non-pipes (ignored on non-Unix platforms)"),
)
}

#[cfg(unix)]
Expand All @@ -96,10 +104,28 @@ fn ignore_interrupts() -> Result<()> {
Ok(())
}

#[cfg(unix)]
fn enable_pipe_errors() -> Result<()> {
let ret = unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) };
if ret == libc::SIG_ERR {
return Err(Error::new(ErrorKind::Other, ""));
}
Ok(())
}

#[cfg(not(unix))]
fn enable_pipe_errors() -> Result<()> {
// Do nothing.
Ok(())
}

fn tee(options: &Options) -> Result<()> {
if options.ignore_interrupts {
ignore_interrupts()?;
}
if !options.ignore_pipe_errors {
enable_pipe_errors()?;
}
let mut writers: Vec<NamedWriter> = options
.files
.clone()
Expand Down

0 comments on commit 4e64299

Please sign in to comment.