diff options
author | Eric S. Raymond <esr@thyrsus.com> | 1997-05-13 23:50:46 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 1997-05-13 23:50:46 +0000 |
commit | 610d851200f4621e9936d22f87335b956658718a (patch) | |
tree | 7dc89119df9132f976e5a342a095990d589a52a0 /socket.c | |
parent | 451d98c467f141e7802b91d26869b6e38ee868fc (diff) | |
download | fetchmail-610d851200f4621e9936d22f87335b956658718a.tar.gz fetchmail-610d851200f4621e9936d22f87335b956658718a.tar.bz2 fetchmail-610d851200f4621e9936d22f87335b956658718a.zip |
Return correct length of data read.
svn path=/trunk/; revision=993
Diffstat (limited to 'socket.c')
-rw-r--r-- | socket.c | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -116,29 +116,39 @@ int SockWrite(int sock, char *buf, int len) int SockRead(int sock, char *buf, int len) { char *p, *bp = buf; - int n; + int n, readlen; if (--len < 1) return(-1); do { + /* + * The reason for these gymnastics is that we want two things: + * (1) to read \n-terminated lines, + * (2) to return the true length of data read, even if the + * data coming in has embedded NULS. + */ + readlen = 0; + /* return value of 0 is EOF, < 0 is error */ if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0) return(-1); if ((p = memchr(bp, '\n', n)) != NULL) { - if (read(sock, bp, ++p - bp) == -1) + if ((n = read(sock, bp, ++p - bp)) == -1) return(-1); + readlen += n; *p = '\0'; - return strlen(buf); + return readlen; } if ((n = read(sock, bp, n)) == -1) return(-1); + readlen += n; bp += n; len -= n; } while (len); *bp = '\0'; - return strlen(buf); + return readlen; } int SockPeek(int sock) |