commit fe6e6324d717e01deff6b05ad3e96cd09998d348
parent 2090981be3d3e5ca1083c004a08f54aced2d0720
Author: bakkeby <bakkeby@gmail.com>
Date: Mon, 20 Apr 2020 10:29:26 +0200
ttyread: test for EOF while reading tty
When a read operation returns 0 then it means that we arrived to the end of the
file, and new reads will return 0 unless you do some other operation such as
lseek(). This case happens with USB-232 adapters when they are unplugged.
Diffstat:
| M | st.c | | | 26 | ++++++++++++++++---------- |
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/st.c b/st.c
@@ -889,17 +889,23 @@ ttyread(void)
int ret;
/* append read bytes to unprocessed bytes */
- if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
- die("couldn't read from shell: %s\n", strerror(errno));
- buflen += ret;
-
- written = twrite(buf, buflen, 0);
- buflen -= written;
- /* keep any uncomplete utf8 char for the next call */
- if (buflen > 0)
- memmove(buf, buf + written, buflen);
+ ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
- return ret;
+ switch (ret) {
+ case 0:
+ fputs("Found EOF in input\n", stderr);
+ exit(0);
+ case -1:
+ die("couldn't read from shell: %s\n", strerror(errno));
+ default:
+ buflen += ret;
+ written = twrite(buf, buflen, 0);
+ buflen -= written;
+ /* keep any uncomplete utf8 char for the next call */
+ if (buflen > 0)
+ memmove(buf, buf + written, buflen);
+ return ret;
+ }
}
void