mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
tty: correct exit code for write errrors
This commit is contained in:
parent
aeaf2cebfb
commit
4c5ee1dbd7
2 changed files with 20 additions and 3 deletions
|
@ -14,6 +14,7 @@ extern crate uucore;
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
use std::io::Write;
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
|
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 !silent {
|
||||||
if !tty.chars().all(|c| c.is_whitespace()) {
|
let write_result = if !tty.chars().all(|c| c.is_whitespace()) {
|
||||||
println!("{}", tty);
|
writeln!(stdout, "{}", tty)
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,3 +63,12 @@ fn test_close_stdin_silent_alias() {
|
||||||
fn test_wrong_argument() {
|
fn test_wrong_argument() {
|
||||||
new_ucmd!().args(&["a"]).fails().code_is(2);
|
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));
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue