Skip to content

Instantly share code, notes, and snippets.

@heftig
Created October 27, 2011 09:29
Show Gist options
  • Save heftig/1319162 to your computer and use it in GitHub Desktop.
Save heftig/1319162 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pty.h>
#include <sys/select.h>
static void copy(int from, int to) {
char buf[4096];
ssize_t size;
size = read(from, &buf, sizeof(buf));
switch (size) {
case -1:
perror("read");
exit(1);
case 0:
exit(0);
default:
write(to, &buf, size);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) { exit(1); }
int amaster;
pid_t pid = forkpty(&amaster, NULL, NULL, NULL);
if (pid < 0) { perror("forkpty"); exit(1); }
if (!pid) {
execvp(argv[1], &argv[1]);
perror("execv");
exit(1);
}
while (1) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(amaster, &fds);
int ret = select(amaster+1, &fds, NULL, NULL, NULL);
if (ret < 0) { perror("select"); exit(1); }
if (ret > 0) {
if (FD_ISSET(STDIN_FILENO, &fds)) copy(STDIN_FILENO, amaster);
if (FD_ISSET(amaster, &fds)) copy(amaster, STDOUT_FILENO);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment