diff options
author | Kelvin Ly <kelvin.ly1618@gmail.com> | 2016-11-02 09:26:29 -0400 |
---|---|---|
committer | Kelvin Ly <kelvin.ly1618@gmail.com> | 2017-06-12 08:34:16 -0400 |
commit | 45b7b1bd1aee2335ed38e0f1d677e7e0edd2c4e7 (patch) | |
tree | a1a7f86475c65e7a104c0ec610ca79f52c4de848 /src/pty.rs | |
parent | f37b0d3a39ef0dde1e0e0eb79b6a891c4b534d6d (diff) | |
download | nix-45b7b1bd1aee2335ed38e0f1d677e7e0edd2c4e7.zip |
Add openpty()
Diffstat (limited to 'src/pty.rs')
-rw-r--r-- | src/pty.rs | 56 |
1 files changed, 54 insertions, 2 deletions
@@ -1,12 +1,25 @@ //! Create master and slave virtual pseudo-terminals (PTYs) +use libc; + +pub use libc::pid_t as SessionId; +pub use libc::winsize as Winsize; + use std::ffi::CStr; use std::mem; use std::os::unix::prelude::*; -use libc; +use sys::termios::Termios; +use {Errno, Result, Error, fcntl}; + +/// Representation of a master/slave pty pair +/// +/// This is returned by `openpty` +pub struct OpenptyResult { + pub master: RawFd, + pub slave: RawFd, +} -use {Error, fcntl, Result}; /// Representation of the Master device in a master/slave pty pair /// @@ -162,3 +175,42 @@ pub fn unlockpt(fd: &PtyMaster) -> Result<()> { Ok(()) } + + +/// Create a new pseudoterminal, returning the slave and master file descriptors +/// in `OpenptyResult` +/// (see [openpty](http://man7.org/linux/man-pages/man3/openpty.3.html)). +/// +/// If `winsize` is not `None`, the window size of the slave will be set to +/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's +/// terminal settings of the slave will be set to the values in `termios`. +#[inline] +pub fn openpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b Termios>>>(winsize: T, termios: U) -> Result<OpenptyResult> { + use std::ptr; + + let mut slave: libc::c_int = -1; + let mut master: libc::c_int = -1; + let c_termios = match termios.into() { + Some(termios) => termios as *const Termios, + None => ptr::null() as *const Termios, + }; + let c_winsize = match winsize.into() { + Some(ws) => ws as *const Winsize, + None => ptr::null() as *const Winsize, + }; + let ret = unsafe { + libc::openpty( + &mut master as *mut libc::c_int, + &mut slave as *mut libc::c_int, + ptr::null_mut(), + c_termios as *mut libc::termios, + c_winsize as *mut Winsize) + }; + + Errno::result(ret)?; + + Ok(OpenptyResult { + master: master, + slave: slave, + }) +} |