initial commit
[bicat.git] / bicat.c
1 #include <unistd.h>
2 #include <sys/select.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 int main (int argc, char **argv) {
10   char buf[20];
11   int fd;
12   int retval;
13   int numread;
14   fd = open(argv[1], O_RDWR);
15   for(;;) {
16     fd_set rfds;
17     struct timeval tv;
18
19     FD_ZERO(&rfds);
20     FD_SET(STDIN_FILENO, &rfds);
21     FD_SET(fd, &rfds);
22
23     tv.tv_sec = 5;
24     tv.tv_usec = 0;
25
26     retval = select(fd+1, &rfds, NULL, NULL, &tv);
27
28     if (retval == -1)
29       perror("select()");
30     else if (retval) {
31       if (FD_ISSET(fd, &rfds)) {
32         numread = read(fd, buf, 1);
33         if (numread==0) return;
34         if (numread<0) perror("read()");
35         write(STDOUT_FILENO, buf, numread);
36       } else if (FD_ISSET(STDIN_FILENO, &rfds)) {
37         numread = read(STDIN_FILENO, buf, 1);
38         if (numread==0) return;
39         if (numread<0) perror("read()");
40         write(fd, buf, numread);
41       } else if (FD_ISSET(fd, &rfds)) {
42         perror("huh?\n");
43       }
44     } else {
45       //printf("No data within five seconds.\n");
46     }
47   }
48 }