int setup_tty(int fd)
{
struct termios tmio;
-
+
int ret = tcgetattr(fd, &tmio);
if(ret) {
perror("tcgetattr()");
tmio.c_lflag &= ~ECHO;
tmio.c_lflag |= CLOCAL;
+ tmio.c_iflag &= ~ICRNL;
+ tmio.c_iflag &= ~IGNCR;
+ tmio.c_iflag |= IGNBRK | BRKINT;
+
+ tmio.c_cflag |= CLOCAL;
+
ret = tcsetattr(fd, TCSANOW, &tmio);
if(ret) {
perror("tcsetattr()");
return ret;
}
-
+
ret = tcflush(fd, TCIFLUSH);
if(ret) {
perror("tcflush()");
return 1;
}
- int fd = open(argv[1], O_RDONLY);
+ int fd = open(argv[1], O_RDONLY | O_NONBLOCK | O_NOCTTY);
if(fd < 0) {
perror("open()");
return 2;
if(setup_tty(fd)) return 3;
+ fd_set rfds, efds;
char buf[100];
for(;;) {
- int r = read(fd, buf, sizeof(buf));
- if(r <=0 ) return r;
-
- int i;
+ FD_ZERO(&rfds);
+ FD_SET(fd, &rfds);
+ FD_ZERO(&efds);
+ FD_SET(1, &efds);
+
+ int s = select(fd+1, &rfds, NULL, &efds, NULL);
+ if(s < 0) {
+ perror("select()");
+ return s;
+ }
+ if(FD_ISSET(1, &efds)) return 0;
+ if(!FD_ISSET(fd, &rfds)) continue;
+
+ ssize_t r = read(fd, buf, sizeof(buf));
+ if(r <= 0) {
+ perror("read()");
+ return r;
+ }
+
+ ssize_t i;
for(i=0; i < r;) {
- int w = write(fd, &(buf[i]), r - i);
- if(w < 0) return w;
+ ssize_t w = write(1, &(buf[i]), r - i);
+ if(w < 0) {
+ perror("write()");
+ return w;
+ }
i+=w;
}
}