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.
This commit is contained in:
bakkeby 2020-04-20 10:29:26 +02:00
parent 2090981be3
commit fe6e6324d7
1 changed files with 16 additions and 10 deletions

14
st.c
View File

@ -889,18 +889,24 @@ ttyread(void)
int ret; int ret;
/* append read bytes to unprocessed bytes */ /* append read bytes to unprocessed bytes */
if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0) ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
die("couldn't read from shell: %s\n", strerror(errno));
buflen += 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); written = twrite(buf, buflen, 0);
buflen -= written; buflen -= written;
/* keep any uncomplete utf8 char for the next call */ /* keep any uncomplete utf8 char for the next call */
if (buflen > 0) if (buflen > 0)
memmove(buf, buf + written, buflen); memmove(buf, buf + written, buflen);
return ret; return ret;
} }
}
void void
ttywrite(const char *s, size_t n, int may_echo) ttywrite(const char *s, size_t n, int may_echo)