diff options
author | John Ankarström <john@ankarstrom.se> | 2021-07-14 11:08:56 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-07-14 14:24:15 +0200 |
commit | 57c74154571d14dcebb92ad0f9ff6369728163da (patch) | |
tree | f49390e897a3249745b2a433824f828a8c120f3f | |
parent | 42f56290e11c8d997968ddf0a93e940696b73381 (diff) | |
download | rtty-57c74154571d14dcebb92ad0f9ff6369728163da.tar.gz |
Add rtty.c
-rw-r--r-- | rtty.c | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -0,0 +1,59 @@ +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/select.h> +#include <unistd.h> + +#define MAXBUF 2048 + +int +main() +{ + char bufin[MAXBUF], bufout[MAXBUF]; + fd_set rfds0, rfds1; + int c, fdin, fdout, flags, i, nin, nout, state; + struct timeval tv; + + if(!(fdin = open("/var/tmp/r.in", O_WRONLY))) + err(1, "open"); + if(!(fdout = open("/var/tmp/r.out", O_RDONLY))) + err(1, "open"); + + flags = fcntl(0, F_GETFL, 0); + flags |= O_NONBLOCK; + fcntl(0, F_SETFL, flags); + + FD_ZERO(&rfds0); + FD_ZERO(&rfds1); + + tv.tv_sec = 0; + tv.tv_usec = 1; + + for(;;){ + /* Read from named pipe (out) and print on standard in. */ + FD_SET(fdout, &rfds1); + if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){ + nout = read(fdout, bufout, MAXBUF); + bufout[nout] = 0; + + /* Ignore repetition of typed command. */ + if(strcmp(bufout, bufin) == 0) + bufin[0] = 0; + else{ + printf("%s", bufout); + fflush(stdout); + } + } + + /* Read from standard in and print on named pipe (in). */ + FD_SET(0, &rfds0); + if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ + nout = read(0, bufin, MAXBUF); + bufin[nout] = 0; + dprintf(fdin, "%s", bufin); + fflush(stdout); + } + } + +} |