summaryrefslogtreecommitdiffstats
path: root/usr.bin/ftp/fetch.c
diff options
context:
space:
mode:
authorjca <jca@openbsd.org>2020-02-13 15:47:33 +0000
committerjca <jca@openbsd.org>2020-02-13 15:47:33 +0000
commitd6fcb86628bc561beaa960d9caed4a738f426bd2 (patch)
treeab32765f0c789d694afa7e124a6dd92133a49de1 /usr.bin/ftp/fetch.c
parentResolve a few issues with interrupt handling (diff)
downloadwireguard-openbsd-d6fcb86628bc561beaa960d9caed4a738f426bd2.tar.xz
wireguard-openbsd-d6fcb86628bc561beaa960d9caed4a738f426bd2.zip
Fixes and tweaks for read/write loop in url_get()
Changes already present in file_get() - no need to special case write(2) returning 0 - clearer loop condition - fix read error detection and properly save errno
Diffstat (limited to 'usr.bin/ftp/fetch.c')
-rw-r--r--usr.bin/ftp/fetch.c18
1 files changed, 8 insertions, 10 deletions
diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c
index c5954a57df6..cea7a4efa68 100644
--- a/usr.bin/ftp/fetch.c
+++ b/usr.bin/ftp/fetch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fetch.c,v 1.187 2020/01/21 05:02:53 beck Exp $ */
+/* $OpenBSD: fetch.c,v 1.188 2020/02/13 15:47:33 jca Exp $ */
/* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
/*-
@@ -1045,18 +1045,14 @@ noslash:
if (error == -1)
goto cleanup_url_get;
} else {
- i = 0;
- len = 1;
- while (len > 0) {
- len = fread(buf, 1, buflen, fin);
+ while ((len = fread(buf, 1, buflen, fin)) > 0) {
bytes += len;
- for (cp = buf, wlen = len; wlen > 0; wlen -= i, cp += i) {
- if ((i = write(out, cp, wlen)) == -1) {
+ for (cp = buf; len > 0; len -= wlen, cp += wlen) {
+ if ((wlen = write(out, cp, len)) == -1) {
warn("Writing %s", savefile);
signal(SIGINFO, oldinti);
goto cleanup_url_get;
- } else if (i == 0)
- break;
+ }
}
if (hash && !progress) {
while (bytes >= hashbytes) {
@@ -1066,6 +1062,7 @@ noslash:
(void)fflush(ttyout);
}
}
+ save_errno = errno;
signal(SIGINFO, oldinti);
if (hash && !progress && bytes > 0) {
if (bytes < mark)
@@ -1073,7 +1070,8 @@ noslash:
(void)putc('\n', ttyout);
(void)fflush(ttyout);
}
- if (len != 0) {
+ if (len == 0 && ferror(fin)) {
+ errno = save_errno;
warnx("Reading from socket: %s", sockerror(tls));
goto cleanup_url_get;
}