aboutsummaryrefslogtreecommitdiff
path: root/rtty.c
blob: 42c9324c736b1272eaeb2c7967ffd7d79e258520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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(;;){
		/* 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);
			if(!nout) continue;
			bufout[nout] = 0;

			/* Ignore repetition of typed command. */
			for(bi = bufin, bo = bufout;;){
				if(*bo == '\n')
					break;
				if(*bo == 13){
					bo++;
					continue;
				}
				if(*bi != *bo){
					printf("%s", bufout);
					fflush(stdout);
					break;
				}
				bi++; bo++;
			}
			bufin[0] = 0;
		}

		/* 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);
		}
	}

}