mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
parent
88d0bb01c0
commit
3714e2201b
3 changed files with 100 additions and 50 deletions
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/tty.rs"
|
path = "src/tty.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] }
|
uucore = { version=">=0.0.7", package="uucore", path="../../uucore", features=["fs"] }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
|
@ -12,68 +12,62 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use uucore::fs::is_stdin_interactive;
|
use uucore::fs::is_stdin_interactive;
|
||||||
|
|
||||||
extern "C" {
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
fn ttyname(filedesc: libc::c_int) -> *const libc::c_char;
|
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
|
||||||
|
|
||||||
|
mod options {
|
||||||
|
pub const SILENT: &str = "silent";
|
||||||
}
|
}
|
||||||
|
|
||||||
static NAME: &str = "tty";
|
fn get_usage() -> String {
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
format!("{0} [OPTION]...", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
let usage = get_usage();
|
||||||
|
|
||||||
let mut opts = getopts::Options::new();
|
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);
|
||||||
|
|
||||||
opts.optflag("s", "silent", "print nothing, only return an exit status");
|
let silent = matches.is_present(options::SILENT);
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
// Call libc function ttyname
|
||||||
Ok(m) => m,
|
let tty = unsafe {
|
||||||
Err(f) => crash!(2, "{}", f),
|
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 matches.opt_present("help") {
|
if !silent {
|
||||||
println!("{} {}", NAME, VERSION);
|
if !tty.chars().all(|c| c.is_whitespace()) {
|
||||||
println!();
|
println!("{}", tty);
|
||||||
println!("Usage:");
|
|
||||||
println!(" {} [OPTION]...", NAME);
|
|
||||||
println!();
|
|
||||||
print!(
|
|
||||||
"{}",
|
|
||||||
opts.usage("Print the file name of the terminal connected to standard input.")
|
|
||||||
);
|
|
||||||
} else if matches.opt_present("version") {
|
|
||||||
println!("{} {}", NAME, VERSION);
|
|
||||||
} else {
|
|
||||||
let silent = matches.opt_present("s");
|
|
||||||
|
|
||||||
let tty = unsafe {
|
|
||||||
let ptr = 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 {
|
} else {
|
||||||
libc::EXIT_FAILURE
|
println!("not a tty");
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
return if is_stdin_interactive() {
|
||||||
|
libc::EXIT_SUCCESS
|
||||||
|
} else {
|
||||||
|
libc::EXIT_FAILURE
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,57 @@
|
||||||
// ToDO: add tests
|
use crate::common::util::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
fn test_dev_null() {
|
||||||
|
new_ucmd!()
|
||||||
|
.pipe_in("</dev/null")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("not a tty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
fn test_dev_null_silent() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-s"])
|
||||||
|
.pipe_in("</dev/null")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_close_stdin() {
|
||||||
|
new_ucmd!().pipe_in("<&-").fails().stdout_is("not a tty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_close_stdin_silent() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-s"])
|
||||||
|
.pipe_in("<&-")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_close_stdin_silent_long() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--silent"])
|
||||||
|
.pipe_in("<&-")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_close_stdin_silent_alias() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--quiet"])
|
||||||
|
.pipe_in("<&-")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_wrong_argument() {
|
||||||
|
new_ucmd!().args(&["a"]).fails();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue