Crates.io | pty2 |
lib.rs | pty2 |
version | 0.1.0 |
source | src |
created_at | 2022-12-19 17:39:48.888736 |
updated_at | 2022-12-19 17:39:48.888736 |
description | Fork with new pseudo-terminal (PTY) |
homepage | https://github.com/littledivy/pty-rs |
repository | https://github.com/littledivy/pty-rs |
max_upload_size | |
id | 741436 |
size | 34,134 |
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
:
[dependencies]
pty = "0.2"
and this to your crate root:
extern crate pty;
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
API documentation for latest version: http://hibariya.github.io/pty-rs/pty/index.html
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.