1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#[macro_use]
extern crate uucore;
use clap::{App, Arg};
use std::ffi::CStr;
use uucore::fs::is_stdin_interactive;
static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
mod options {
pub const SILENT: &str = "silent";
}
fn get_usage() -> String {
format!("{0} [OPTION]...", executable!())
}
pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args.collect_str();
let usage = get_usage();
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.arg(
Arg::with_name(options::SILENT)
.long(options::SILENT)
.visible_alias("quiet")
.short("s")
.help("print nothing, only return an exit status")
.required(false),
)
.get_matches_from(args);
let silent = matches.is_present(options::SILENT);
let tty = unsafe {
let ptr = libc::ttyname(libc::STDIN_FILENO);
if !ptr.is_null() {
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
} else {
"".to_owned()
}
};
if !silent {
if !tty.chars().all(|c| c.is_whitespace()) {
println!("{}", tty);
} else {
println!("not a tty");
}
}
return if is_stdin_interactive() {
libc::EXIT_SUCCESS
} else {
libc::EXIT_FAILURE
};
}