mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 04:57:45 +00:00
Merge pull request #3838 from tertsdiepraam/tty-nix
`tty`: move from `libc` to `nix`
This commit is contained in:
commit
a3e3542602
3 changed files with 27 additions and 30 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -3030,7 +3030,7 @@ version = "0.0.14"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"atty",
|
"atty",
|
||||||
"clap 3.2.17",
|
"clap 3.2.17",
|
||||||
"libc",
|
"nix",
|
||||||
"uucore",
|
"uucore",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ path = "src/tty.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
|
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
|
||||||
libc = "0.2.126"
|
nix = "0.25"
|
||||||
atty = "0.2"
|
atty = "0.2"
|
||||||
uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["fs"] }
|
uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["fs"] }
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
// spell-checker:ignore (ToDO) ttyname filedesc
|
// spell-checker:ignore (ToDO) ttyname filedesc
|
||||||
|
|
||||||
use clap::{crate_version, Arg, Command};
|
use clap::{crate_version, Arg, Command};
|
||||||
use std::ffi::CStr;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use uucore::error::UResult;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use uucore::error::{set_exit_code, UResult};
|
||||||
use uucore::format_usage;
|
use uucore::format_usage;
|
||||||
|
|
||||||
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
|
static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
|
||||||
|
@ -24,42 +24,39 @@ mod options {
|
||||||
|
|
||||||
#[uucore::main]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let args = args.collect_lossy();
|
|
||||||
|
|
||||||
let matches = uu_app().get_matches_from(args);
|
let matches = uu_app().get_matches_from(args);
|
||||||
|
|
||||||
let silent = matches.contains_id(options::SILENT);
|
let silent = matches.contains_id(options::SILENT);
|
||||||
|
|
||||||
// Call libc function ttyname
|
// If silent, we don't need the name, only whether or not stdin is a tty.
|
||||||
let tty = unsafe {
|
if silent {
|
||||||
let ptr = libc::ttyname(libc::STDIN_FILENO);
|
return if atty::is(atty::Stream::Stdin) {
|
||||||
if !ptr.is_null() {
|
Ok(())
|
||||||
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
|
|
||||||
} else {
|
} else {
|
||||||
"".to_owned()
|
Err(1.into())
|
||||||
}
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut stdout = std::io::stdout();
|
let mut stdout = std::io::stdout();
|
||||||
|
|
||||||
if !silent {
|
// Get the ttyname via nix
|
||||||
let write_result = if !tty.chars().all(|c| c.is_whitespace()) {
|
let name = nix::unistd::ttyname(std::io::stdin().as_raw_fd());
|
||||||
writeln!(stdout, "{}", tty)
|
|
||||||
} else {
|
|
||||||
writeln!(stdout, "not a tty")
|
|
||||||
};
|
|
||||||
if write_result.is_err() || stdout.flush().is_err() {
|
|
||||||
// Don't return to prevent a panic later when another flush is attempted
|
|
||||||
// because the `uucore_procs::main` macro inserts a flush after execution for every utility.
|
|
||||||
std::process::exit(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if atty::is(atty::Stream::Stdin) {
|
let write_result = match name {
|
||||||
Ok(())
|
Ok(name) => writeln!(stdout, "{}", name.display()),
|
||||||
} else {
|
Err(_) => {
|
||||||
Err(libc::EXIT_FAILURE.into())
|
set_exit_code(1);
|
||||||
}
|
writeln!(stdout, "not a tty")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if write_result.is_err() || stdout.flush().is_err() {
|
||||||
|
// Don't return to prevent a panic later when another flush is attempted
|
||||||
|
// because the `uucore_procs::main` macro inserts a flush after execution for every utility.
|
||||||
|
std::process::exit(3);
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app<'a>() -> Command<'a> {
|
pub fn uu_app<'a>() -> Command<'a> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue