diff options
-rw-r--r-- | termsettitle.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/termsettitle.c b/termsettitle.c index 8ed1b9f..2ff9883 100644 --- a/termsettitle.c +++ b/termsettitle.c @@ -17,6 +17,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/select.h> #include <termios.h> #include <unistd.h> @@ -27,22 +28,38 @@ int da(int fd, char *buf, int real) { char c; - int i, r; + fd_set fds; + int i, r, responses; + struct timeval timeout; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 0; + responses = 0; + i = 0; if(real) write(fd, "\033P\033[>c\033\\", 8); else write(fd, "\033[>c", 4); + +read: while(r = read(fd, &c, 1)){ if(!r){ fprintf(stderr, "bad terminal\n"); return -1; } - buf[i] = c; - if(c==99){ - buf[++i] = '\0'; - return i; - } - i++; + buf[i++] = c; + if(c==99) break; } + buf[i] = '\0'; + responses++; + + /* ensure there is only one real terminal */ + if(real && select(fd+1, &fds, NULL, NULL, &timeout)>0) + goto read; + if(responses>1) + buf[0] = '\0'; + return i; } |