<commit_before># PTY

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`:

```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 pty_master)) => {
            if child.pid() == 0 {
                // Child process just exec `tty`
                let mut ptrs: Vec<*const libc::c_char> = 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.

```<commit_msg>Split essential code<commit_after># PTY

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`:

```toml
[dependencies]

pty = "0.1"
```

and this to your crate root:

```rust
extern crate pty;
```

### pty::fork()

This function returns two values. `pty::Child` represents a child process. `pty::Master` represents master of a PTY.

```rust
let (child_process, pty_master) = pty::fork();
```

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

```rust
extern crate libc;
extern crate pty;

use std::io::Read;

fn main()
{
    match pty::fork() {
        Ok((child_process, mut pty_master)) => {
            if child_process.pid() == 0 {
                // Child process just exec `tty`
                let mut ptrs: Vec<*const libc::c_char> = 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_process.wait();
                pty_master.close();
            }
        },
        Err(e) => panic!("pty::fork error: {}", e)
    }
}
```

When run this, we get new PTY in the child process.

```
{'fraction_non_alphanumeric': 0.1521493212669683, 'fraction_numerical': 0.002828054298642534, 'mean_word_length': 2.038659793814433, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1127050', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1077, 'n_words': 312}