Skip to content

Commit 48864c8

Browse files
committed
tty: Build for Windows
1 parent 3dec656 commit 48864c8

3 files changed

Lines changed: 24 additions & 10 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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
#[cfg(unix)]
56
use std::fs::File;
6-
7+
#[cfg(unix)]
78
use uutests::new_ucmd;
89

910
#[test]
10-
#[cfg(not(windows))]
11+
#[cfg(unix)]
1112
fn test_dev_null() {
1213
new_ucmd!()
1314
.set_stdin(File::open("/dev/null").unwrap())
@@ -16,7 +17,7 @@ fn test_dev_null() {
1617
}
1718

1819
#[test]
19-
#[cfg(not(windows))]
20+
#[cfg(unix)]
2021
fn test_dev_null_silent() {
2122
new_ucmd!()
2223
.args(&["-s"])
@@ -26,39 +27,45 @@ fn test_dev_null_silent() {
2627
}
2728

2829
#[test]
30+
#[cfg(unix)]
2931
fn test_close_stdin() {
3032
let mut child = new_ucmd!().run_no_wait();
3133
child.close_stdin();
3234
child.wait().unwrap().code_is(1).stdout_is("not a tty\n");
3335
}
3436

3537
#[test]
38+
#[cfg(unix)]
3639
fn test_close_stdin_silent() {
3740
let mut child = new_ucmd!().arg("-s").run_no_wait();
3841
child.close_stdin();
3942
child.wait().unwrap().code_is(1).no_stdout();
4043
}
4144

4245
#[test]
46+
#[cfg(unix)]
4347
fn test_close_stdin_silent_long() {
4448
let mut child = new_ucmd!().arg("--silent").run_no_wait();
4549
child.close_stdin();
4650
child.wait().unwrap().code_is(1).no_stdout();
4751
}
4852

4953
#[test]
54+
#[cfg(unix)]
5055
fn test_close_stdin_silent_alias() {
5156
let mut child = new_ucmd!().arg("--quiet").run_no_wait();
5257
child.close_stdin();
5358
child.wait().unwrap().code_is(1).no_stdout();
5459
}
5560

5661
#[test]
62+
#[cfg(unix)]
5763
fn test_wrong_argument() {
5864
new_ucmd!().args(&["a"]).fails_with_code(2);
5965
}
6066

6167
#[test]
68+
#[cfg(unix)]
6269
fn test_help() {
6370
new_ucmd!().args(&["--help"]).succeeds();
6471
}

0 commit comments

Comments
 (0)