aboutsummaryrefslogtreecommitdiffstats
path: root/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'socket.c')
-rw-r--r--socket.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/socket.c b/socket.c
index af5018e4..1dd11a02 100644
--- a/socket.c
+++ b/socket.c
@@ -143,19 +143,27 @@ int SockWrite(char *buf, int size, int len, FILE *sockfp)
char *SockGets(char *buf, int len, FILE *sockfp)
{
- int rdlen = 0;
- char *cp = buf;
-
- while (--len)
- {
- if (read(fileno(sockfp), cp, 1) != 1)
- return((char *)NULL);
- else
- rdlen++;
- if (*cp++ == '\n')
- break;
- }
- *cp = 0;
+ char *p, *bp = buf;
+ int n;
+
+ if (--len < 1)
+ return NULL;
+ do {
+ if ((n = recv(fileno(sockfp), bp, len, MSG_PEEK)) == -1)
+ return NULL;
+ if ((p = memchr(bp, '\n', n)) != NULL)
+ {
+ if (read(fileno(sockfp), bp, ++p - bp) == -1)
+ return NULL;
+ *p = '\0';
+ return buf;
+ }
+ if ((n = read(fileno(sockfp), bp, n)) == -1)
+ return NULL;
+ bp += n;
+ len -= n;
+ } while (len);
+ *bp = '\0';
return buf;
}