CVS update: php3/functions
| From: | ssb | Date: | Tue, 23 Mar 1999 01:50:01 +0000 |
| Subject: | CVS update: php3/functions | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-4729@lists.php.net to get a copy of this message | ||
Date: Monday March 22, 1999 @ 20:50
Author: ssb
Update of /repository/php3/functions
In directory asf:/u/temp/cvs-serv15006
Modified Files:
fsock.c
Log Message:
implemented a read buffer for fgets()
Index: php3/functions/fsock.c
diff -c php3/functions/fsock.c:1.88 php3/functions/fsock.c:1.89
*** php3/functions/fsock.c:1.88 Mon Mar 22 18:41:07 1999
--- php3/functions/fsock.c Mon Mar 22 20:50:00 1999
***************
*** 27,33 ****
| Jim Winstead (jimw@php.net) |
+----------------------------------------------------------------------+
*/
! /* $Id: fsock.c,v 1.88 1999/03/22 23:41:07 ssb Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
--- 27,33 ----
| Jim Winstead (jimw@php.net) |
+----------------------------------------------------------------------+
*/
! /* $Id: fsock.c,v 1.89 1999/03/23 01:50:00 ssb Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
***************
*** 279,308 ****
}
/* }}} */
int _php3_sock_fgets(char *buf, int maxlen, int socket)
{
! int retval, count = 0, toread, i;
! char *ptr;
!
while (count < maxlen) {
toread = maxlen - count;
! retval = recv(socket, buf, toread, 0);
! if (retval <= 0) {
break;
}
! ptr = (char *)memchr(buf, '\n', retval);
! if (ptr != NULL) {
! i = ptr - buf;
! if (count + i < maxlen) {
! i++;
}
! count += i;
! buf += i;
}
! count += retval;
! buf += retval;
}
sock_fgets_exit:
*buf = '\0';
return count;
}
--- 279,396 ----
}
/* }}} */
+ struct php3i_sockbuf {
+ int socket;
+ char *readbuf;
+ size_t readbuflen;
+ size_t readpos;
+ size_t writepos;
+ struct php3i_sockbuf *prev;
+ struct php3i_sockbuf *next;
+ };
+
+ static struct php3i_sockbuf *phpsockbuf = NULL;
+
int _php3_sock_fgets(char *buf, int maxlen, int socket)
{
! struct php3i_sockbuf *sockbuf = NULL, *tmpsockbuf;
! int bytesread, toread, len, buflen, count = 0, remove = 0;
! char *nl;
!
! tmpsockbuf = phpsockbuf;
! while (tmpsockbuf) {
! if (tmpsockbuf->socket == socket) {
! sockbuf = tmpsockbuf;
! break;
! }
! tmpsockbuf++;
! }
!
! if (sockbuf) {
! toread = sockbuf->writepos - sockbuf->readpos;
! if (toread > maxlen) {
! toread = maxlen;
! }
! if ((nl = memchr(sockbuf->readbuf + sockbuf->readpos, '\n', toread)) != NULL) {
! toread = (nl - (sockbuf->readbuf + sockbuf->readpos)) + 1;
! }
! memcpy(buf, sockbuf->readbuf + sockbuf->readpos, toread);
! sockbuf->readpos += toread;
! count += toread;
! buf += toread;
! /* if a newline was not found, or if the buffer has been
! * exhausted, the buffer may be unlinked
! */
! if (nl == NULL || sockbuf->readpos >= sockbuf->writepos) {
! remove = 1;
! }
! if (nl != NULL) {
! /* if a newline was found, skip the recv() loop */
! goto sock_fgets_exit;
! }
! }
!
! nl = NULL;
! buflen = 0;
while (count < maxlen) {
toread = maxlen - count;
! bytesread = recv(socket, buf, toread, 0);
! if (bytesread <= 0) {
break;
+ }
+ if ((nl = memchr(buf, '\n', bytesread)) != NULL) {
+ len = (nl - buf) + 1;
+ count += len;
+ buf += len;
+ if (len < bytesread) {
+ buflen = bytesread - len;
+ break;
+ }
+ } else {
+ count += bytesread;
+ buf += bytesread;
}
! }
!
! if (buflen > 0) { /* there was data after the "\n" ... */
! if (sockbuf == NULL) {
! sockbuf = emalloc(sizeof(struct php3i_sockbuf));
! sockbuf->socket = socket;
! sockbuf->readbuf = emalloc(maxlen);
! sockbuf->readbuflen = maxlen;
! sockbuf->readpos = sockbuf->writepos = 0;
! sockbuf->next = phpsockbuf;
! if (phpsockbuf != NULL) {
! phpsockbuf->prev = sockbuf;
}
! phpsockbuf = sockbuf;
! } else {
! int needlen = sockbuf->writepos + buflen;
! if (needlen > sockbuf->readbuflen) {
! sockbuf->readbuflen += maxlen;
! sockbuf->readbuf = erealloc(sockbuf->readbuf, sockbuf->readbuflen);
! }
}
! memcpy(sockbuf->readbuf + sockbuf->writepos, buf, buflen);
! sockbuf->writepos += buflen;
! remove = 0;
}
+
sock_fgets_exit:
+ if (sockbuf && remove) {
+ if (sockbuf->prev) {
+ sockbuf->prev->next = sockbuf->next;
+ } else {
+ phpsockbuf = sockbuf->next;
+ }
+ if (sockbuf->next) {
+ sockbuf->next->prev = sockbuf->prev;
+ }
+ if (sockbuf->readbuf != NULL) {
+ efree(sockbuf->readbuf);
+ }
+ efree(sockbuf);
+ }
*buf = '\0';
return count;
}
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net