commit e52319cc7d153e4f59b38c4fb4c0556e118d4775 from: Roberto E. Vargas Caballero via: Hiltjo Posthuma date: Sat Apr 11 13:23:23 2020 UTC 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. commit - 21e0d6e8b8d20903494386e7e6f43201b3761154 commit + e52319cc7d153e4f59b38c4fb4c0556e118d4775 blob - 5f2352af2f10fc7e97d6d127f5d2e8244cb02722 blob + 81973eed38d5a2a3a9efdd83d3508eb2ef819438 --- st.c +++ st.c @@ -823,17 +823,24 @@ ttyread(void) int ret; /* append read bytes to unprocessed bytes */ - if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0) + ret = read(cmdfd, buf+buflen, LEN(buf)-buflen); + + 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)); - buflen += ret; + 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; - 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