From 234af89ffb8d11bcd5388d95a6dfd984772e2acd Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 27 Sep 2021 07:39:06 -0700 Subject: [PATCH 1/2] Use libc bindings instead of manual extern blocks to take advantage of platform wrappers in libc crate. --- src/sys/unix/attr.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/sys/unix/attr.rs b/src/sys/unix/attr.rs index d25e942..9a5c886 100644 --- a/src/sys/unix/attr.rs +++ b/src/sys/unix/attr.rs @@ -1,29 +1,19 @@ use std::{io, mem}; use super::{cvt, Termios}; -use super::libc::c_int; pub fn get_terminal_attr() -> io::Result { - extern "C" { - pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int; - } unsafe { let mut termios = mem::zeroed(); - cvt(tcgetattr(1, &mut termios))?; + cvt(libc::tcgetattr(1, &mut termios))?; Ok(termios) } } pub fn set_terminal_attr(termios: &Termios) -> io::Result<()> { - extern "C" { - pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int; - } - cvt(unsafe { tcsetattr(1, 0, termios) }).and(Ok(())) + cvt(unsafe { libc::tcsetattr(1, 0, termios) }).and(Ok(())) } pub fn raw_terminal_attr(termios: &mut Termios) { - extern "C" { - pub fn cfmakeraw(termptr: *mut Termios); - } - unsafe { cfmakeraw(termios) } + unsafe { libc::cfmakeraw(termios) } } From 4fafb44f8925a7f0df8e1f439f9b04d2593454c0 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 27 Sep 2021 07:43:32 -0700 Subject: [PATCH 2/2] Use constants from libc instead of magic numbers. --- src/sys/unix/attr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys/unix/attr.rs b/src/sys/unix/attr.rs index 9a5c886..a83e6c1 100644 --- a/src/sys/unix/attr.rs +++ b/src/sys/unix/attr.rs @@ -5,13 +5,13 @@ use super::{cvt, Termios}; pub fn get_terminal_attr() -> io::Result { unsafe { let mut termios = mem::zeroed(); - cvt(libc::tcgetattr(1, &mut termios))?; + cvt(libc::tcgetattr(libc::STDOUT_FILENO, &mut termios))?; Ok(termios) } } pub fn set_terminal_attr(termios: &Termios) -> io::Result<()> { - cvt(unsafe { libc::tcsetattr(1, 0, termios) }).and(Ok(())) + cvt(unsafe { libc::tcsetattr(libc::STDOUT_FILENO, libc::TCSANOW, termios) }).and(Ok(())) } pub fn raw_terminal_attr(termios: &mut Termios) {