/// Creates a new pseudo terminal in /dev/pts/ and returns the name and the master / slave file
/// descriptors.
///
/// # Examples
/// ```
/// let (master, slave, name) = openpty::openpty(None, None, None)
///     .expect("Creating pty failed");
/// ```
Creates a new pseudo terminal in /dev/pts/ and returns the name and the master / slave file
descriptors.
Creates a new pseudo terminal in / dev / pts / and returns the name and the master / slave file descriptors .
pub fn openpty(
    termios: Option<&libc::termios>,
    winsize: Option<&libc::winsize>,
    name: Option<String>,
) -> Result<(File, File, String), Box<dyn Error>> {
    let master = OpenOptions::new()
        .read(true)
        .write(true)
        .custom_flags(libc::O_NOCTTY)
        .open("/dev/ptmx")?;
    let mut pts_number = 0;
    unsafe {
        ioctl::tiocsptlck(master.as_raw_fd(), &(pts_number as libc::c_int))?;
        ioctl::tiocgptn(master.as_raw_fd(), &mut pts_number)?;
    }
    let name = name.unwrap_or_else(|| format!("/dev/pts/{}", pts_number));
    let slave = OpenOptions::new()
        .read(true)
        .write(true)
        .custom_flags(libc::O_NOCTTY)
        .open(&name)?;
    if let Some(tio) = termios {
        Errno::result(unsafe { libc::tcsetattr(slave.as_raw_fd(), libc::TCSANOW, tio) })?;
    }
    if let Some(ws) = winsize {
        unsafe {
            ioctl::tiocswinsz(slave.as_raw_fd(), ws)?;
        }
    }
    Ok((master, slave, name))
}
pub fn openpty ( termios : Option < & libc :: termios > , winsize : Option < & libc :: winsize > , name : Option < String > , ) -> Result < ( File , File , String ) , Box < dyn Error > > { let master = OpenOptions :: new ( ) . read ( true ) . write ( true ) . custom_flags ( libc :: O_NOCTTY ) . open ( " " ) ? ; let mut pts_number = 0 ; unsafe { ioctl :: tiocsptlck ( master . as_raw_fd ( ) , & ( pts_number as libc :: c_int ) ) ? ; ioctl :: tiocgptn ( master . as_raw_fd ( ) , & mut pts_number ) ? ; } let name = name . unwrap_or_else ( | | format ! ( " " pts_number ) ) ; let slave = OpenOptions :: new ( ) . read ( true ) . write ( true ) . custom_flags ( libc :: O_NOCTTY ) . open ( & name ) ? ; if let Some ( tio ) = termios { Errno :: result ( unsafe { libc :: tcsetattr ( slave . as_raw_fd ( ) , libc :: TCSANOW , tio ) } ) ? ; } if let Some ( ws ) = winsize { unsafe { ioctl :: tiocswinsz ( slave . as_raw_fd ( ) , ws ) ? ; } } Ok ( ( master , slave , name ) ) }
Creates a new pseudo terminal in /dev/pts/ and returns the name and the master / slave file
descriptors.
Creates a new pseudo terminal in / dev / pts / and returns the name and the master / slave file descriptors .
termios
Option<&libc::termios>
winsize
Option<&libc::winsize>
name
Option<String>
examples
```
let (master, slave, name) = openpty::openpty(None, None, None)
.expect("Creating pty failed");
```
` ` ` let ( master slave name ) = openpty :: openpty ( None None None ) . expect ( " Creating pty failed " ) ; ` ` `