diff --git a/src/gmnlm.c b/src/gmnlm.c
index 5d7892b83f7914da46b08100014db6211e36173a..245ec85fa59fbcabde7e49c7d4d6e978aefa5f9a 100644
--- a/src/gmnlm.c
+++ b/src/gmnlm.c
@@ -908,10 +908,21 @@ int row = 0, col = 0;
ioctl(fileno(browser->tty), TIOCGWINSZ, &ws);
char buf[BUFSIZ];
- int n;
- while ((n = BIO_read(resp->bio, buf, sizeof(buf)) != 0)) {
- while (n) {
- n -= fwrite(buf, 1, n, browser->tty);
+ for (int n = 1; n > 0;) {
+ n = BIO_read(resp->bio, buf, BUFSIZ);
+ if (n == -1) {
+ fprintf(stderr, "Error: read\n");
+ return 1;
+ }
+ ssize_t w = 0;
+ while (w < (ssize_t)n) {
+ ssize_t x = fwrite(&buf[w], 1, n - w, browser->tty);
+ if (ferror(browser->tty)) {
+ fprintf(stderr, "Error: write: %s\n",
+ strerror(errno));
+ return 1;
+ }
+ w += x;
}
}