summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto E. Vargas Caballero <k0ga@shike2.com>2020-04-10 22:25:46 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2020-04-11 15:23:23 +0200
commite52319cc7d153e4f59b38c4fb4c0556e118d4775 (patch)
tree086acfcdc8c8d1e869a5920419b1760809272eb7
parentAdd support for scroll(1) (diff)
downloadst-e52319cc7d153e4f59b38c4fb4c0556e118d4775.tar.gz
st-e52319cc7d153e4f59b38c4fb4c0556e118d4775.zip
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.
-rw-r--r--st.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/st.c b/st.c
index 5f2352a..81973ee 100644
--- a/st.c
+++ b/st.c
@@ -823,17 +823,24 @@ ttyread(void)
823 int ret; 823 int ret;
824 824
825 /* append read bytes to unprocessed bytes */ 825 /* append read bytes to unprocessed bytes */
826 if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0) 826 ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
827 die("couldn't read from shell: %s\n", strerror(errno));
828 buflen += ret;
829 827
830 written = twrite(buf, buflen, 0); 828 switch (ret) {
831 buflen -= written; 829 case 0:
832 /* keep any uncomplete utf8 char for the next call */ 830 fputs("Found EOF in input\n", stderr);
833 if (buflen > 0) 831 exit(0);
834 memmove(buf, buf + written, buflen); 832 case -1:
833 die("couldn't read from shell: %s\n", strerror(errno));
834 default:
835 buflen += ret;
836 written = twrite(buf, buflen, 0);
837 buflen -= written;
838 /* keep any uncomplete utf8 char for the next call */
839 if (buflen > 0)
840 memmove(buf, buf + written, buflen);
841 return ret;
835 842
836 return ret; 843 }
837} 844}
838 845
839void 846void