Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ feat_common_core = [
"tr",
"true",
"truncate",
"tty",
"tsort",
"unexpand",
"uniq",
Expand Down Expand Up @@ -291,7 +292,6 @@ feat_require_unix_core = [
"stat",
"stty",
"timeout",
"tty",
]
# "feat_require_unix_utmpx" == set of utilities requiring unix utmp/utmpx support
# * ref: <https://wiki.musl-libc.org/faq.html#Q:-Why-is-the-utmp/wtmp-functionality-only-implemented-as-stubs?>
Expand All @@ -317,7 +317,6 @@ feat_os_unix_fuchsia = [
"mknod",
"nice",
"pathchk",
"tty",
"uname",
"unlink",
]
Expand Down
19 changes: 12 additions & 7 deletions src/uu/tty/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _ = uucore::signals::disable_pipe_errors();

let silent = matches.get_flag(options::SILENT);
let is_tty = std::io::stdin().is_terminal();

// If silent, we don't need the name, only whether or not stdin is a tty.
if silent {
return if std::io::stdin().is_terminal() {
Ok(())
} else {
Err(1.into())
};
return if is_tty { Ok(()) } else { Err(1.into()) };
}

let mut stdout = std::io::stdout();

#[cfg(unix)]
let name = nix::unistd::ttyname(std::io::stdin());
#[cfg(not(unix))] // todo: maximize cygwin compatibility
let name = if is_tty {
Ok(std::ffi::OsString::from("/dev/tty"))
} else {
Err(())
};

let write_result = if let Ok(name) = name {
stdout.write_all_os(name.as_os_str())
stdout
.write_all_os(name.as_os_str())
.and_then(|_| writeln!(stdout))
} else {
set_exit_code(1);
writeln!(stdout, "{}", translate!("tty-not-a-tty"))
Expand Down
13 changes: 10 additions & 3 deletions tests/by-util/test_tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#[cfg(unix)]
use std::fs::File;

#[cfg(unix)]
use uutests::new_ucmd;

#[test]
#[cfg(not(windows))]
#[cfg(unix)]
fn test_dev_null() {
new_ucmd!()
.set_stdin(File::open("/dev/null").unwrap())
Expand All @@ -16,7 +17,7 @@ fn test_dev_null() {
}

#[test]
#[cfg(not(windows))]
#[cfg(unix)]
fn test_dev_null_silent() {
new_ucmd!()
.args(&["-s"])
Expand All @@ -26,39 +27,45 @@ fn test_dev_null_silent() {
}

#[test]
#[cfg(unix)]
fn test_close_stdin() {
let mut child = new_ucmd!().run_no_wait();
child.close_stdin();
child.wait().unwrap().code_is(1).stdout_is("not a tty\n");
}

#[test]
#[cfg(unix)]
fn test_close_stdin_silent() {
let mut child = new_ucmd!().arg("-s").run_no_wait();
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}

#[test]
#[cfg(unix)]
fn test_close_stdin_silent_long() {
let mut child = new_ucmd!().arg("--silent").run_no_wait();
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}

#[test]
#[cfg(unix)]
fn test_close_stdin_silent_alias() {
let mut child = new_ucmd!().arg("--quiet").run_no_wait();
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}

#[test]
#[cfg(unix)]
fn test_wrong_argument() {
new_ucmd!().args(&["a"]).fails_with_code(2);
}

#[test]
#[cfg(unix)]
fn test_help() {
new_ucmd!().args(&["--help"]).succeeds();
}
Expand Down
Loading