add misc/bicat.c
[fleet.git] / misc / 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 #include <stdio.h>
9
10 int main (int argc, char **argv) {
11   char buf[20];
12   int fd;
13   int retval;
14   int numread;
15   FILE* log;
16   fd = open(argv[1], O_RDWR);
17   log = fopen("bicat.log", "w");
18   for(;;) {
19     fd_set rfds;
20     struct timeval tv;
21
22     FD_ZERO(&rfds);
23     FD_SET(STDIN_FILENO, &rfds);
24     FD_SET(fd, &rfds);
25
26     tv.tv_sec = 5;
27     tv.tv_usec = 0;
28
29     retval = select(fd+1, &rfds, NULL, NULL, &tv);
30
31     if (retval == -1)
32       perror("select()");
33     else if (retval) {
34       if (FD_ISSET(fd, &rfds)) {
35         numread = read(fd, buf, 1);
36         if (numread==0) return;
37         if (numread<0) perror("read()");
38         //fprintf(log, "fpga->host: %d\n", buf[0]);
39         write(STDOUT_FILENO, buf, numread);
40         if ((buf[0] & (3<<6)) == 0) {
41           buf[0] = (1<<6) | 1;
42           write(fd, buf, 1);
43         }
44       } else if (FD_ISSET(STDIN_FILENO, &rfds)) {
45         numread = read(STDIN_FILENO, buf, 1);
46         if (numread==0) return;
47         if (numread<0) perror("read()");
48         //fprintf(log, "host->fpga: %d\n", buf[0]);
49         write(fd, buf, numread);
50       } else if (FD_ISSET(fd, &rfds)) {
51         perror("huh?\n");
52       }
53     } else {
54       //fprintf(log, "sleep\n");
55     }
56     //fflush(log);
57   }
58 }