Fork with new pseudo-terminal (PTY)
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples switch to Command::new so it consistent with other examples/tests May 30, 2017
src Replace ffi with definitions from libc Jun 11, 2017
tests switch uses to prelude May 31, 2017
.editorconfig Add .editorconfig Nov 7, 2015
.gitignore improve Error Handling and POO representation, remove posix_openpt Aug 26, 2016
.travis.yml trim parent_tty, run tty check only if parent is a tty (travis runs t… May 29, 2017
CHANGELOG.md
Cargo.toml Version 0.2.2 Jun 25, 2017
LICENSE.txt It works Jul 2, 2015
README.md Fix the URL for documentation (fixes #11) Jun 4, 2017
rustfmt.toml hard tab = false Sep 4, 2016

README.md

PTY

Crate docs-badge license-badge travis-badge

The pty crate provides pty::fork(). That makes a parent process fork with new pseudo-terminal (PTY).

This crate depends on followings:

  • libc library
  • POSIX environment

Usage

Add this to your Cargo.toml:

[dependencies]
pty = "0.2"

and this to your crate root:

extern crate pty;

pty::fork()

This function returns pty::Child. It represents the child process and its PTY.

For example, the following code spawns tty(1) command by pty::fork() and outputs the result of the command.

extern crate pty;
extern crate libc;

use std::ffi::CString;
use std::io::Read;
use std::ptr;

use pty::fork::*;

fn main() {
  let fork = Fork::from_ptmx().unwrap();

  if let Some(mut master) = fork.is_parent().ok() {
    // Read output via PTY master
    let mut output = String::new();

    match master.read_to_string(&mut output) {
      Ok(_nread) => println!("child tty is: {}", output.trim()),
      Err(e)     => panic!("read error: {}", e),
    }
  }
  else {
    // Child process just exec `tty`
    Command::new("tty").status().expect("could not execute tty");
  }
}

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

Documentation

API documentation for latest version: http://hibariya.github.io/pty-rs/pty/index.html

Contributing

  1. Fork it ( https://github.com/hibariya/pty-rs/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

Copyright (c) 2015 Hika Hibariya

Distributed under the MIT License.