aboutsummaryrefslogtreecommitdiff
path: root/rio.c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-14 18:43:20 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-14 18:43:20 +0200
commit9f34ef6a0b1fd5545112498e2363d784cee8e42e (patch)
treee3a6569f9921018d04e39577e6b4d121d7e856e6 /rio.c
parent1d764bd32582e48082e1a2ed07f079ddc17fdf8a (diff)
downloadrtty-9f34ef6a0b1fd5545112498e2363d784cee8e42e.tar.gz
Rename rtty to rio, rd to rtty
Diffstat (limited to 'rio.c')
-rw-r--r--rio.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/rio.c b/rio.c
new file mode 100644
index 0000000..88ef214
--- /dev/null
+++ b/rio.c
@@ -0,0 +1,88 @@
+#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], *bi, *bo;
+ 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");
+
+ FD_ZERO(&rfds0);
+ FD_ZERO(&rfds1);
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 1;
+
+ for(;;){
+ /*
+ * User input is read from standard in and copied the
+ * input pipe. It is saved in bufin for later use.
+ */
+ 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);
+ }
+
+ /*
+ * System output is read from the output pipe and copied
+ * to standard out for the user to see.
+ */
+ FD_SET(fdout, &rfds1);
+ if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){
+ nout = read(fdout, bufout, MAXBUF);
+ if(!nout) continue;
+ bufout[nout] = 0;
+
+ /*
+ * Bufin and bufout are copied to the temporary
+ * pointers bi and bo. Bo is incremented in order
+ * to skip any potential repetition of the command
+ * given by the user on standard in. (It is assumed
+ * that the typed command fits in bufin, i.e. is
+ * not larger than MAXBUF.)
+ */
+ bi = bufin;
+ bo = bufout;
+ for(;;){
+ if(*bo == '\n'){
+ bo++;
+ break;
+ }
+ if(*bo == 13){
+ bo++;
+ continue;
+ }
+ if(*bi != *bo){
+ bo = bufout;
+ break;
+ }
+ bi++; bo++;
+ }
+
+ /*
+ * Bo is printed and bufin is cleared, so as not
+ * to perform the skip again, incorrectly.
+ */
+ printf("%s", bo);
+ fflush(stdout);
+ bufin[0] = 0;
+ }
+ }
+
+}