diff options
author | John Ankarström <john@ankarstrom.se> | 2021-07-14 14:22:35 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-07-14 14:24:15 +0200 |
commit | 7fe5629a1ab9d1f67ad1d1eabbfb65003ff82622 (patch) | |
tree | 0f8b4dd3f5af8b47d5f309ccb4b499f7a98c372f | |
parent | 646ae6344ae040220b75744c8fa9d955a46fe681 (diff) | |
download | rtty-7fe5629a1ab9d1f67ad1d1eabbfb65003ff82622.tar.gz |
Add r1.c
-rw-r--r-- | r1.c | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,76 @@ +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/select.h> + +#define MAXBUF 2048 + +fd_set rfds0, rfds1; +struct timeval timeout; + +int +main() +{ + char buf[MAXBUF]; + FILE *in, *out; + int c, i, n, ofd, state; + + if(!(in = fopen("/var/tmp/r.in", "w"))) + err(1, "fopen"); + if(!(out = fopen("/var/tmp/r.out", "r"))) + err(1, "fopen"); + ofd = fileno(out); + + FD_ZERO(&rfds0); + FD_SET(0, &rfds0); + + FD_ZERO(&rfds1); + FD_SET(ofd, &rfds1); + + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + for(state = 0; printf("."); state = !state) + switch(state){ + case 0: + if(!select(0+1, &rfds0, NULL, NULL, &timeout)) + break; + printf("0 ready\n"); + break; + case 1: + if(!select(ofd+1, &rfds1, NULL, NULL, &timeout)) + break; + printf("1 ready\n"); + break; + } + +#if 0 + for(;;){ + printf("."); + FD_ZERO(&read_set); + FD_SET(0, &read_set); + FD_SET(ofd, &read_set); + switch(select(ofd+1, &read_set, NULL, NULL, &timeout)){ + case -1: + err(1, "select"); + break; + case 0: + break; + default: + if(FD_ISSET(0, &read_set)){ + fgets(buf, MAXBUF, stdin); + buf[strcspn(buf, "\n")] = 0; + printf("in: %s\n", buf); + }else if(FD_ISSET(ofd, &read_set)){ + fgets(buf, MAXBUF, stdin); + buf[strcspn(buf, "\n")] = 0; + printf("out: %s\n", buf); + }else{ + printf("?\n"); + } + } + } +#endif + +} |