mirror of
https://git.proxmox.com/git/proxmox
synced 2025-06-15 21:51:19 +00:00
sys: Use safe wrapper for libc::isatty
Use the `std::io::IsTerminal` trait introduced in Rust 1.70. Internally it calls `libc::isatty`, see [1, 2]. Note that it switches the comparison from `== 1` to `!= 0` which shouldn't make a difference assuming that libc::isatty upholds the promises made in its man page. The MSRV was set on the workspace to reflect this change. [1] https://doc.rust-lang.org/src/std/io/stdio.rs.html#1079 [2] https://doc.rust-lang.org/src/std/sys/unix/io.rs.html#79 Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
This commit is contained in:
parent
7126249102
commit
430df21720
@ -44,6 +44,7 @@ license = "AGPL-3"
|
|||||||
repository = "https://git.proxmox.com/?p=proxmox.git"
|
repository = "https://git.proxmox.com/?p=proxmox.git"
|
||||||
homepage = "https://proxmox.com"
|
homepage = "https://proxmox.com"
|
||||||
exclude = [ "debian" ]
|
exclude = [ "debian" ]
|
||||||
|
rust-version = "1.70"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
# any features enabled here are enabled on all members using 'workspace = true'!
|
# any features enabled here are enabled on all members using 'workspace = true'!
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::io::{self, Read, Write};
|
use std::io::{self, IsTerminal, Read, Write};
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
use std::os::unix::io::{AsRawFd, OwnedFd};
|
use std::os::unix::io::{AsRawFd, OwnedFd};
|
||||||
|
|
||||||
@ -26,19 +26,15 @@ pub fn stdout_terminal_size() -> (usize, usize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the current stdout is a tty.
|
/// Returns whether the current stdout is a tty.
|
||||||
/// # Safety
|
#[deprecated(note = "Use std::io::stdout().is_terminal()")]
|
||||||
///
|
|
||||||
/// uses unsafe call to libc::isatty
|
|
||||||
pub fn stdout_isatty() -> bool {
|
pub fn stdout_isatty() -> bool {
|
||||||
unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
|
std::io::stdout().is_terminal()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the current stdin is a tty.
|
/// Returns whether the current stdin is a tty.
|
||||||
/// # Safety
|
#[deprecated(note = "Use std::io::stdin().is_terminal()")]
|
||||||
///
|
|
||||||
/// uses unsafe call to libc::isatty
|
|
||||||
pub fn stdin_isatty() -> bool {
|
pub fn stdin_isatty() -> bool {
|
||||||
unsafe { libc::isatty(std::io::stdin().as_raw_fd()) == 1 }
|
std::io::stdin().is_terminal()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum TtyOutput {
|
pub enum TtyOutput {
|
||||||
@ -75,7 +71,7 @@ impl TtyOutput {
|
|||||||
/// Get an output file descriptor for the current terminal.
|
/// Get an output file descriptor for the current terminal.
|
||||||
pub fn open() -> io::Result<Option<Self>> {
|
pub fn open() -> io::Result<Option<Self>> {
|
||||||
let stdout = std::io::stdout();
|
let stdout = std::io::stdout();
|
||||||
if unsafe { libc::isatty(stdout.as_raw_fd()) } == 1 {
|
if stdout.is_terminal() {
|
||||||
Ok(Some(TtyOutput::Stdout(stdout)))
|
Ok(Some(TtyOutput::Stdout(stdout)))
|
||||||
} else {
|
} else {
|
||||||
match crate::fd::open(
|
match crate::fd::open(
|
||||||
@ -97,7 +93,7 @@ impl TtyOutput {
|
|||||||
/// first.
|
/// first.
|
||||||
pub fn read_password(query: &str) -> Result<Vec<u8>, Error> {
|
pub fn read_password(query: &str) -> Result<Vec<u8>, Error> {
|
||||||
let input = std::io::stdin();
|
let input = std::io::stdin();
|
||||||
if unsafe { libc::isatty(input.as_raw_fd()) } != 1 {
|
if !input.is_terminal() {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
input.read_line(&mut out)?;
|
input.read_line(&mut out)?;
|
||||||
return Ok(out.into_bytes());
|
return Ok(out.into_bytes());
|
||||||
|
Loading…
Reference in New Issue
Block a user