#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/stat.h>

struct myfd {
	int fdin;
	int fdout;
};

void *term2pipe(void *arg) {
	struct myfd *myfd = arg;
	size_t n;
	char buf[1024];
	for (;;) {
		n = read(STDIN_FILENO, buf, 1024);
		if (n <= 0) break;
		write (myfd->fdout, buf, n);
	}
	return NULL;
}

void *pipe2term(void *arg) {
	struct myfd *myfd = arg;
	size_t n;
	char buf[1024];
	for (;;) {
		n = read(myfd->fdin, buf, 1024);
		if (n <= 0) break;
		write (STDOUT_FILENO, buf, n);
	}
	return NULL;
}

int main(int argc, char * argv[]){
	struct myfd myfd;
	pthread_t t2p;
	pthread_t p2t;
	myfd.fdin = open(argv[1], O_RDONLY | O_NONBLOCK);
	myfd.fdout = open(argv[2], O_WRONLY);
	pthread_create(&t2p, NULL, term2pipe, &myfd);
	pthread_create(&p2t, NULL, pipe2term, &myfd);
	pthread_join(t2p, NULL);
	pthread_join(p2t, NULL);
}
