Skip to content

Commit

Permalink
Add various pty functions
Browse files Browse the repository at this point in the history
  * grantpt
  * ptsname/ptsname_r
  * posix_openpt
  * unlockpt
  • Loading branch information
Susurrus committed Mar 15, 2017
1 parent 5c90289 commit a247129
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub mod mount;
#[cfg(target_os = "linux")]
pub mod mqueue;

pub mod pty;

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod poll;

Expand Down
72 changes: 72 additions & 0 deletions src/pty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::ffi::CStr;
use std::os::unix::io::RawFd;

use libc;

use {Errno, Error, fcntl, Result};

pub fn grantpt(fd: RawFd) -> Result<()> {
if unsafe { libc::grantpt(fd) } < 0 {
return Err(Error::last().into());
}

Ok(())
}

pub fn posix_openpt(flags: fcntl::OFlag) -> Result<RawFd> {
let fd = unsafe {
libc::posix_openpt(flags.bits())
};

if fd < 0 {
return Err(Error::last().into());
}

Ok(fd)
}

pub fn ptsname(fd: RawFd) -> Result<String> {
let name_ptr = unsafe { libc::ptsname(fd) };
if name_ptr.is_null() {
return Err(Errno::ENOTTY.into());
}

let name_cstr = unsafe { CStr::from_ptr(name_ptr) };
let name = match name_cstr.to_str() {
Ok(s) => s.to_string(),
Err(_) => {
// FIXME: Unsure what error I should report here.
return Err(Error::last().into());
}
};
Ok(name)
}

#[cfg(any(target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia"))]
pub fn ptsname_r(fd: RawFd) -> Result<String> {
let mut name_buf = [0 as libc::c_char; 64];
if unsafe { libc::ptsname_r(fd, name_buf.as_mut_ptr(), name_buf.len()) } != 0 {
return Err(Error::last().into());
}

let name_cstr = unsafe { CStr::from_ptr(name_buf.as_ptr()) };
let name = match name_cstr.to_str() {
Ok(s) => s.to_string(),
Err(_) => {
// FIXME: Unsure what error I should report here.
return Err(Error::last().into());
}
};
Ok(name)
}

pub fn unlockpt(fd: RawFd) -> Result<()> {
if unsafe { libc::unlockpt(fd) } < 0 {
return Err(Error::last().into());
}

Ok(())
}

0 comments on commit a247129

Please sign in to comment.