Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tty: move from libc to nix #3838

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/uu/tty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ path = "src/tty.rs"

[dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
libc = "0.2.126"
nix = "0.25"
atty = "0.2"
uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["fs"] }

Expand Down
49 changes: 23 additions & 26 deletions src/uu/tty/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// spell-checker:ignore (ToDO) ttyname filedesc

use clap::{crate_version, Arg, Command};
use std::ffi::CStr;
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;

static ABOUT: &str = "Print the file name of the terminal connected to standard input.";
Expand All @@ -24,42 +24,39 @@ mod options {

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args.collect_lossy();

let matches = uu_app().get_matches_from(args);

let silent = matches.contains_id(options::SILENT);

// Call libc function ttyname
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()
// If silent, we don't need the name, only whether or not stdin is a tty.
if silent {
return if atty::is(atty::Stream::Stdin) {
Ok(())
} else {
"".to_owned()
}
Err(1.into())
};
};

let mut stdout = std::io::stdout();

if !silent {
let write_result = if !tty.chars().all(|c| c.is_whitespace()) {
writeln!(stdout, "{}", tty)
} else {
// Get the ttyname via nix
let name = nix::unistd::ttyname(std::io::stdin().as_raw_fd());

let write_result = match name {
Ok(name) => writeln!(stdout, "{}", name.display()),
Err(_) => {
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);
}
}
};

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) {
Ok(())
} else {
Err(libc::EXIT_FAILURE.into())
}
Ok(())
}

pub fn uu_app<'a>() -> Command<'a> {
Expand Down