tty: correct exit code for write errrors

This commit is contained in:
Michael Debertol
2021-06-16 17:55:48 +02:00
parent aeaf2cebfb
commit 4c5ee1dbd7
2 changed files with 20 additions and 3 deletions
+11 -3
View File
@@ -14,6 +14,7 @@ extern crate uucore;
use clap::{crate_version, App, Arg};
use std::ffi::CStr;
use std::io::Write;
use uucore::InvalidEncodingHandling;
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
@@ -66,11 +67,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}
};
let mut stdout = std::io::stdout();
if !silent {
if !tty.chars().all(|c| c.is_whitespace()) {
println!("{}", tty);
let write_result = if !tty.chars().all(|c| c.is_whitespace()) {
writeln!(stdout, "{}", tty)
} else {
println!("not a tty");
writeln!(stdout, "not a tty")
};
if write_result.is_err() || stdout.flush().is_err() {
// Don't return to prevent a panic later when another flush is attempted
// because the `uucore_procs::main` macro inserts a flush after execution for every utility.
std::process::exit(3);
}
}
+9
View File
@@ -63,3 +63,12 @@ fn test_close_stdin_silent_alias() {
fn test_wrong_argument() {
new_ucmd!().args(&["a"]).fails().code_is(2);
}
#[test]
#[cfg(not(windows))]
fn test_stdout_fail() {
let mut child = new_ucmd!().run_no_wait();
drop(child.stdout.take());
let status = child.wait().unwrap();
assert_eq!(status.code(), Some(3));
}