diff options
| author | Roberto E. Vargas Caballero <k0ga@shike2.com> | 2020-04-10 22:25:46 +0200 |
|---|---|---|
| committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2020-04-11 15:23:23 +0200 |
| commit | e52319cc7d153e4f59b38c4fb4c0556e118d4775 (patch) | |
| tree | 086acfcdc8c8d1e869a5920419b1760809272eb7 /st.c | |
| parent | Add support for scroll(1) (diff) | |
| download | st-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.
Diffstat (limited to 'st.c')
| -rw-r--r-- | st.c | 25 |
1 files changed, 16 insertions, 9 deletions
| @@ -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 | ||
| 839 | void | 846 | void |
