The pty crate provides pty::fork(). That makes a parent process fork with new pseudo-terminal (PTY).
This crate depends on followings:
libc libraryAdd this to your Cargo.toml:
```toml [dependencies]
pty = "0.1" ```
and this to your crate root:
rust
extern crate pty;
pty::fork() -> io::Result<(Child, Master)>This function returns two values. pty::Child represents a child process. pty::Master represents master of a PTY.
For example, the following code spawns tty(1) command by pty::fork().
```rust extern crate libc; extern crate pty;
use std::io::Read;
fn main()
{
    match pty::fork() {
        Ok((child, mut ptymaster)) => {
            if child.pid() == 0 {
                // Child process just exec tty
                let mut ptrs: Vec<*const libc::cchar> = Vec::with_capacity(1);
            ptrs.push(std::ffi::CString::new("tty").unwrap().as_ptr());
            ptrs.push(std::ptr::null());
            unsafe { libc::execvp(*ptrs.as_ptr(), ptrs.as_mut_ptr()) };
        }
        else {
            // Read entire output via PTY master
            let mut string = String::new();
            match pty_master.read_to_string(&mut string) {
                Ok(_nread)  => {
                    println!("child tty is: {}", string.trim());
                },
                Err(e) => panic!("read error: {}", e)
            }
            child.wait();
            pty_master.close();
        }
    },
    Err(e) => panic!("pty::fork error: {}", e)
}
} ```
When run this, we get new PTY in the child process.
$ tty
/dev/pts/5
$ cargo run
    Running `target/debug/example`
child tty is: /dev/pts/8
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Copyright (c) 2015 Hika Hibariya
Distributed under the MIT License.