From 201c590badb75468adb11c88abe3149046a178b5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 12 Oct 2020 23:38:59 -0400 Subject: ttyname was merged back into nix --- src/bin/rbw/actions.rs | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs index 2c7d699..ec6ba99 100644 --- a/src/bin/rbw/actions.rs +++ b/src/bin/rbw/actions.rs @@ -1,6 +1,5 @@ use anyhow::Context as _; use std::io::Read as _; -use std::os::unix::ffi::OsStringExt as _; pub fn login() -> anyhow::Result<()> { simple_action(rbw::protocol::Action::Login) @@ -30,7 +29,7 @@ pub fn quit() -> anyhow::Result<()> { std::fs::File::open(pidfile)?.read_to_string(&mut pid)?; let pid = nix::unistd::Pid::from_raw(pid.parse()?); sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Quit, @@ -54,7 +53,7 @@ pub fn decrypt( ) -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Decrypt { @@ -79,7 +78,7 @@ pub fn encrypt( ) -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Encrypt { @@ -101,7 +100,7 @@ pub fn encrypt( pub fn version() -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Version, @@ -121,7 +120,7 @@ fn simple_action(action: rbw::protocol::Action) -> anyhow::Result<()> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action, @@ -149,25 +148,6 @@ fn connect() -> anyhow::Result { }) } -// XXX copied (with small modifications) from the as-yet-unreleased nix -// version, should use that directly once 0.18 is released -fn ttyname( - fd: std::os::unix::io::RawFd, -) -> anyhow::Result { - const PATH_MAX: usize = libc::PATH_MAX as usize; - let mut buf = vec![0_u8; PATH_MAX]; - let c_buf = buf.as_mut_ptr() as *mut libc::c_char; - - let ret = unsafe { libc::ttyname_r(fd, c_buf, buf.len()) }; - if ret != 0 { - return Err(anyhow::anyhow!("ttyname_r failed: {}", ret)); - } - - let nul = buf.iter().position(|c| *c == b'\0').unwrap(); - buf.truncate(nul); - Ok(std::ffi::OsString::from_vec(buf).into()) -} - fn wait_for_exit(pid: nix::unistd::Pid) -> anyhow::Result<()> { loop { if nix::sys::signal::kill(pid, None).is_err() { -- cgit v1.2.3-54-g00ecf