main termhelper.c
 1#include <sys/types.h>
 2#include <sys/stat.h>
 3#include <fcntl.h>
 4#include <unistd.h>
 5#include <stdlib.h>
 6#include "terminal.h"
 7
 8char buf[4096];
 9
10void copy_data(int to, int from)
11{
12	ssize_t bytes = read(from, buf, sizeof(buf));
13	while (bytes > 0)
14	{
15		ssize_t written = write(to, buf, bytes);
16		if (written == -1) {
17			exit(1);
18		}
19		bytes -= written;
20	}
21}
22
23int main(int argc, char **argv)
24{
25	//these will block so order is important
26	int input_fd = open(INPUT_PATH, O_WRONLY);
27	int output_fd = open(OUTPUT_PATH, O_RDONLY);
28	fd_set read_fds;
29	FD_ZERO(&read_fds);
30	for (;;)
31	{
32		FD_SET(STDIN_FILENO, &read_fds);
33		FD_SET(output_fd, &read_fds);
34		select(output_fd+1, &read_fds, NULL, NULL, NULL);
35		
36		if (FD_ISSET(STDIN_FILENO, &read_fds)) {
37			copy_data(input_fd, STDIN_FILENO);
38		}
39		if (FD_ISSET(output_fd, &read_fds)) {
40			copy_data(STDOUT_FILENO, output_fd);
41		}
42	}
43	return 0;
44}