Skip to content

Instantly share code, notes, and snippets.

@croepha
Last active November 12, 2017 03:58
Show Gist options
  • Save croepha/136ae70e336a91cdc8eb767f3d421bce to your computer and use it in GitHub Desktop.
Save croepha/136ae70e336a91cdc8eb767f3d421bce to your computer and use it in GitHub Desktop.
// clang++ -Wno-writable-strings pty_reader.cpp -lutil -o pty_reader
#include <stdio.h>
#include <unistd.h>
#include <pty.h>
int main(){
fprintf(stdout, "READER...\n");
fflush(stdout);
int master = 0;
pid_t child_pid = forkpty(&master, NULL, NULL, NULL);
if (child_pid == -1){
fprintf(stdout, "failed to fork\n");
return(0);
}
if (child_pid == 0){
char *argv[] = {
"sh",
"-c",
"./exerciser",
0
};
if (execv("/bin/sh", argv) == -1){
fprintf(stdout, "failed to execv\n");
return(0);
}
}
else{
char buffer[1024];
int capacity = sizeof(buffer);
for (;;){
ssize_t num = read(master, buffer, capacity);
if (num == -1){
fprintf(stdout, "read error\n"); // This likely means that the process has closed the pty
return(0);
}
else if (num == 0){
break;
}
else{
fprintf(stdout, "%.*s", (int)num, buffer);
fflush(stdout);
}
}
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment