From b85d71b67a2743272f46c85bf479b1051171193d Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 17 Dec 1996 20:53:10 +0000 Subject: [PATCH] SockGets is gone. svn path=/trunk/; revision=650 --- driver.c | 24 ++++++++++++++++++++++-- imap.c | 8 ++++---- pop2.c | 2 +- pop3.c | 6 +++--- smtp.c | 7 ++++--- socket.c | 23 ++--------------------- socket.h | 15 ++------------- 7 files changed, 38 insertions(+), 47 deletions(-) diff --git a/driver.c b/driver.c index 5d8abfb7..6ef734bf 100644 --- a/driver.c +++ b/driver.c @@ -276,7 +276,7 @@ static FILE *smtp_open(struct query *ctl) /* if no socket to this host is already set up, try to open one */ if (ctl->smtp_sockfp == (FILE *)NULL) { - if ((ctl->smtp_sockfp = Socket(ctl->smtphost, SMTP_PORT)) == (FILE *)NULL) + if ((ctl->smtp_sockfp = sockopen(ctl->smtphost, SMTP_PORT)) == (FILE *)NULL) return((FILE *)NULL); else if (SMTP_ok(ctl->smtp_sockfp) != SM_OK || SMTP_helo(ctl->smtp_sockfp, ctl->servernames->id) != SM_OK) @@ -289,6 +289,26 @@ static FILE *smtp_open(struct query *ctl) return(ctl->smtp_sockfp); } +static int SockGets(char *buf, int len, FILE *sockfp) +/* get a LF-terminated line, removing \r characters */ +{ + int rdlen = 0; + + while (--len) + { + if ((*buf = fgetc(sockfp)) == EOF) + return -1; + else + rdlen++; + if (*buf == '\n') + break; + if (*buf != '\r') /* remove all CRs */ + buf++; + } + *buf = 0; + return rdlen; +} + static int gen_readmsg (sockfp, len, delimited, ctl) /* read message content and ship to SMTP or MDA */ FILE *sockfp; /* to which the server is connected */ @@ -861,7 +881,7 @@ const struct method *proto; /* protocol method table */ FILE *sockfp; /* open a socket to the mail server */ - if ((sockfp = Socket(ctl->servernames->id, + if ((sockfp = sockopen(ctl->servernames->id, ctl->port ? ctl->port : protocol->port)) == NULL) { error(0, errno, "connecting to host"); diff --git a/imap.c b/imap.c index 7887453c..0b7dcd2f 100644 --- a/imap.c +++ b/imap.c @@ -25,7 +25,7 @@ int imap_ok (FILE *sockfp, char *argbuf) seen = 0; do { - if (SockGets(buf, sizeof(buf), sockfp) < 0) + if (!fgets(buf, sizeof(buf), sockfp)) return(PS_SOCKET); if (outlevel == O_VERBOSE) @@ -118,7 +118,7 @@ static int imap_getsizes(FILE *sockfp, int count, int *sizes) char buf [POPBUFSIZE+1]; gen_send(sockfp, "FETCH 1:%d RFC822.SIZE", count); - while (SockGets(buf, sizeof(buf), sockfp) >= 0) + while (fgets(buf, sizeof(buf), sockfp)) { int num, size; @@ -165,7 +165,7 @@ static int imap_fetch(FILE *sockfp, int number, int *lenp) /* looking for FETCH response */ do { - if (SockGets(buf, sizeof(buf), sockfp) < 0) + if (!fgets(buf, sizeof(buf), sockfp)) return(PS_SOCKET); } while (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2); @@ -181,7 +181,7 @@ static int imap_trail(FILE *sockfp, struct query *ctl, int number) { char buf [POPBUFSIZE+1]; - if (SockGets(buf, sizeof(buf), sockfp) < 0) + if (!fgets(buf, sizeof(buf), sockfp)) return(PS_SOCKET); else return(0); diff --git a/pop2.c b/pop2.c index 32cba243..c2e5ecc4 100644 --- a/pop2.c +++ b/pop2.c @@ -23,7 +23,7 @@ int pop2_ok (FILE *sockfp, char *argbuf) char buf [POPBUFSIZE+1]; pound_arg = equal_arg = -1; - if (SockGets(buf, sizeof(buf), sockfp) >= 0) { + if (fgets(buf, sizeof(buf), sockfp)) { if (outlevel == O_VERBOSE) error(0, 0, "POP2< %s", buf); diff --git a/pop3.c b/pop3.c index fb621a33..9b8616e4 100644 --- a/pop3.c +++ b/pop3.c @@ -30,7 +30,7 @@ int pop3_ok (FILE *sockfp, char *argbuf) char buf [POPBUFSIZE+1]; char *bufp; - if (SockGets(buf, sizeof(buf), sockfp) >= 0) { + if (fgets(buf, sizeof(buf), sockfp)) { if (outlevel == O_VERBOSE) error(0, 0, "POP3< %s", buf); @@ -165,7 +165,7 @@ static int pop3_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp) int num; *newp = 0; - while (SockGets(buf, sizeof(buf), sockfp) >= 0) + while (fgets(buf, sizeof(buf), sockfp)) { if (outlevel == O_VERBOSE) error(0, 0, "POP3< %s", buf); @@ -196,7 +196,7 @@ static int pop3_getsizes(FILE *sockfp, int count, int *sizes) { char buf [POPBUFSIZE+1]; - while (SockGets(buf, sizeof(buf), sockfp) >= 0) + while (fgets(buf, sizeof(buf), sockfp)) { int num, size; diff --git a/smtp.c b/smtp.c index 0133e5a9..877e2e5c 100644 --- a/smtp.c +++ b/smtp.c @@ -95,11 +95,12 @@ int SMTP_eom(FILE *sockfp) int SMTP_ok(FILE *sockfp) /* returns status of SMTP connection */ { - int n; - char buf[SMTPBUFSIZE]; + char buf[SMTPBUFSIZE], *ip; - while ((n = SockGets(buf, sizeof(buf)-1, sockfp)) > 0) + while ((ip = fgets(buf, sizeof(buf)-1, sockfp))) { + int n = strlen(ip); + if (n < 4) return SM_ERROR; buf[n] = '\0'; diff --git a/socket.c b/socket.c index cfb93284..dbab9254 100644 --- a/socket.c +++ b/socket.c @@ -44,9 +44,10 @@ * * #define INTERNAL_BUFSIZE 2048 * + * Note that stdio's 1024-byte default is just fine. */ -FILE *Socket(char *host, int clientPort) +FILE *sockopen(char *host, int clientPort) { int sock; unsigned long inaddr; @@ -79,24 +80,4 @@ FILE *Socket(char *host, int clientPort) return fdopen(sock, "r+"); } - -int SockGets(char *buf, int len, FILE *sockfp) -{ - int rdlen = 0; - - while (--len) - { - if ((*buf = fgetc(sockfp)) == EOF) - return -1; - else - rdlen++; - if (*buf == '\n') - break; - if (*buf != '\r') /* remove all CRs */ - buf++; - } - *buf = 0; - return rdlen; -} - /* socket.c ends here */ diff --git a/socket.h b/socket.h index a69f36bc..11d9e296 100644 --- a/socket.h +++ b/socket.h @@ -7,18 +7,7 @@ #ifndef SOCKET__ #define SOCKET__ -/* -Create a new client socket -returns (FILE *)NULL on error -*/ -FILE *Socket(char *host, int clientPort); - -/* -Get a string terminated by an '\n', delete any '\r' and the '\n'. -Pass it a valid socket, a buffer for the string, and -the length of the buffer (including the trailing \0) -returns 0 for success. -*/ -int SockGets(char *buf, int len, FILE *sockfp); +/* Create a new client socket; returns (FILE *)NULL on error */ +FILE *sockopen(char *host, int clientPort); #endif /* SOCKET__ */ -- 2.43.2