Skip to content

Commit 34ecedf

Browse files
committed
tty: Build for Windows
1 parent 3dec656 commit 34ecedf

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ feat_Tier1 = [
171171
"hostname",
172172
"nproc",
173173
"sync",
174+
"tty",
174175
"uname",
175176
"whoami",
176177
]
@@ -212,6 +213,7 @@ feat_wasm = [
212213
"tee",
213214
"true",
214215
"truncate",
216+
"tty",
215217
"unexpand",
216218
"uniq",
217219
"unlink",

src/uu/tty/src/tty.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2727
let _ = uucore::signals::disable_pipe_errors();
2828

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

3132
// If silent, we don't need the name, only whether or not stdin is a tty.
3233
if silent {
33-
return if std::io::stdin().is_terminal() {
34-
Ok(())
35-
} else {
36-
Err(1.into())
37-
};
34+
return if is_tty { Ok(()) } else { Err(1.into()) };
3835
}
3936

4037
let mut stdout = std::io::stdout();
41-
38+
#[cfg(unix)]
4239
let name = nix::unistd::ttyname(std::io::stdin());
40+
#[cfg(not(unix))] // todo: maximize cygwin compatibility
41+
let name = if is_tty {
42+
Ok(std::ffi::OsString::from("/dev/tty"))
43+
} else {
44+
Err(())
45+
};
4346

4447
let write_result = if let Ok(name) = name {
45-
stdout.write_all_os(name.as_os_str())
48+
stdout
49+
.write_all_os(name.as_os_str())
50+
.and_then(|_| writeln!(stdout))
4651
} else {
4752
set_exit_code(1);
4853
writeln!(stdout, "{}", translate!("tty-not-a-tty"))

tests/by-util/test_tty.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fs::File;
77
use uutests::new_ucmd;
88

99
#[test]
10-
#[cfg(not(windows))]
10+
#[cfg(unix)]
1111
fn test_dev_null() {
1212
new_ucmd!()
1313
.set_stdin(File::open("/dev/null").unwrap())
@@ -16,7 +16,7 @@ fn test_dev_null() {
1616
}
1717

1818
#[test]
19-
#[cfg(not(windows))]
19+
#[cfg(unix)]
2020
fn test_dev_null_silent() {
2121
new_ucmd!()
2222
.args(&["-s"])
@@ -26,39 +26,45 @@ fn test_dev_null_silent() {
2626
}
2727

2828
#[test]
29+
#[cfg(unix)]
2930
fn test_close_stdin() {
3031
let mut child = new_ucmd!().run_no_wait();
3132
child.close_stdin();
3233
child.wait().unwrap().code_is(1).stdout_is("not a tty\n");
3334
}
3435

3536
#[test]
37+
#[cfg(unix)]
3638
fn test_close_stdin_silent() {
3739
let mut child = new_ucmd!().arg("-s").run_no_wait();
3840
child.close_stdin();
3941
child.wait().unwrap().code_is(1).no_stdout();
4042
}
4143

4244
#[test]
45+
#[cfg(unix)]
4346
fn test_close_stdin_silent_long() {
4447
let mut child = new_ucmd!().arg("--silent").run_no_wait();
4548
child.close_stdin();
4649
child.wait().unwrap().code_is(1).no_stdout();
4750
}
4851

4952
#[test]
53+
#[cfg(unix)]
5054
fn test_close_stdin_silent_alias() {
5155
let mut child = new_ucmd!().arg("--quiet").run_no_wait();
5256
child.close_stdin();
5357
child.wait().unwrap().code_is(1).no_stdout();
5458
}
5559

5660
#[test]
61+
#[cfg(unix)]
5762
fn test_wrong_argument() {
5863
new_ucmd!().args(&["a"]).fails_with_code(2);
5964
}
6065

6166
#[test]
67+
#[cfg(unix)]
6268
fn test_help() {
6369
new_ucmd!().args(&["--help"]).succeeds();
6470
}

0 commit comments

Comments
 (0)