Re: feof
| From: | Sascha Schumann | Date: | Wed, 19 May 1999 15:34:50 +0000 |
| Subject: | Re: feof | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-5791@lists.php.net to get a copy of this message | ||
On Wed, May 19, 1999 at 04:40:27PM +0200, Sascha Schumann wrote:
> On Tue, May 18, 1999 at 01:50:03AM +0200, Sascha Schumann wrote:
> > Hello all,
> >
> > running the short script
>
> Argh. I was bitten by the impacts of the new _php3_sock_fgets. feof() does a
> direct recv()/feof() system call and does not use the buffer provided of
> _php3_sock_fgets. Could someone change that (Jim?).
I've attached a diff which fixes the problem. I don't know exactly whether the
eof test is correct:
if(sockbuf) {
ret = (sockbuf->writepos - sockbuf->readpos) == 0 ? 1 : 0;
}
Someone more familiar with the buffer stuff should probably look over the
changes.
--
Regards,
Sascha Schumann
Consultant
Index: file.c =================================================================== RCS file: /repository/php3/functions/file.c,v retrieving revision 1.209 diff -u -r1.209 file.c --- file.c 1999/05/16 15:14:36 1.209 +++ file.c 1999/05/19 14:32:57 @@ -656,7 +656,6 @@ int id, type; int issock=0; int socketd=0, *sock; - unsigned int temp; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { @@ -675,7 +674,7 @@ /* we're at the eof if the file doesn't exist */ RETURN_TRUE; } - if ((issock?!(recv(socketd,(char *)&temp,1,MSG_PEEK)):feof(fp))) { + if ((issock?(_php3_sock_eof(socketd)):feof(fp))) { RETURN_TRUE; } else { RETURN_FALSE; Index: fsock.c =================================================================== RCS file: /repository/php3/functions/fsock.c,v retrieving revision 1.94 diff -u -r1.94 fsock.c --- fsock.c 1999/04/06 09:53:40 1.94 +++ fsock.c 1999/05/19 14:32:57 @@ -97,6 +97,8 @@ static struct php3i_sockbuf *phpsockbuf; +typedef struct php3i_sockbuf php3i_sockbuf; + static int php3_minit_fsock(INIT_FUNC_ARGS); static int php3_mshutdown_fsock(void); static int php3_rshutdown_fsock(void); @@ -295,23 +297,43 @@ * (buffered data is not persistent) * - php3_fopen_url_wrapper() is still doing single-byte lookahead/read */ -/* {{{ _php3_sock_fgets() */ -int _php3_sock_fgets(char *buf, int maxlen, int socket) +static php3i_sockbuf *_php3_sock_findsock(int socket) { - struct php3i_sockbuf *sockbuf = NULL, *tmpsockbuf; - int bytesread, toread, len, buflen, count = 0; - char *nl; + /* FIXME: O(n) could be improved */ - tmpsockbuf = phpsockbuf; - while (tmpsockbuf) { - if (tmpsockbuf->socket == socket) { - sockbuf = tmpsockbuf; + php3i_sockbuf *buf = NULL, *tmp; + + for(tmp = phpsockbuf; tmp; tmp = tmp->next) + if(tmp->socket == socket) { + buf = tmp; break; } - tmpsockbuf = tmpsockbuf->next; + + return buf; +} + +int _php3_sock_eof(int socket) +{ + php3i_sockbuf *sockbuf; + int ret = 0; + + sockbuf = _php3_sock_findsock(socket); + if(sockbuf) { + ret = (sockbuf->writepos - sockbuf->readpos) == 0 ? 1 : 0; } + return ret; +} +/* {{{ _php3_sock_fgets() */ +int _php3_sock_fgets(char *buf, int maxlen, int socket) +{ + struct php3i_sockbuf *sockbuf; + int bytesread, toread, len, buflen, count = 0; + char *nl; + + sockbuf = _php3_sock_findsock(socket); + if (sockbuf) { toread = sockbuf->writepos - sockbuf->readpos; if (toread > maxlen) { Index: fsock.h =================================================================== RCS file: /repository/php3/functions/fsock.h,v retrieving revision 1.16 diff -u -r1.16 fsock.h --- fsock.h 1999/01/24 23:53:19 1.16 +++ fsock.h 1999/05/19 14:32:57 @@ -41,5 +41,6 @@ extern int _php3_sock_fgets(char *buf, int maxlen, int socket); extern int _php3_sock_fread(char *buf, int maxlen, int socket); extern int _php3_is_persistent_sock(int); +int _php3_sock_eof(int socket); #endif /* _FSOCK_H */
Index: file.c =================================================================== RCS file: /repository/php3/functions/file.c,v retrieving revision 1.209 diff -u -r1.209 file.c --- file.c 1999/05/16 15:14:36 1.209 +++ file.c 1999/05/19 14:32:57 @@ -656,7 +656,6 @@ int id, type; int issock=0; int socketd=0, *sock; - unsigned int temp; TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { @@ -675,7 +674,7 @@ /* we're at the eof if the file doesn't exist */ RETURN_TRUE; } - if ((issock?!(recv(socketd,(char *)&temp,1,MSG_PEEK)):feof(fp))) { + if ((issock?(_php3_sock_eof(socketd)):feof(fp))) { RETURN_TRUE; } else { RETURN_FALSE; Index: fsock.c =================================================================== RCS file: /repository/php3/functions/fsock.c,v retrieving revision 1.94 diff -u -r1.94 fsock.c --- fsock.c 1999/04/06 09:53:40 1.94 +++ fsock.c 1999/05/19 14:32:57 @@ -97,6 +97,8 @@ static struct php3i_sockbuf *phpsockbuf; +typedef struct php3i_sockbuf php3i_sockbuf; + static int php3_minit_fsock(INIT_FUNC_ARGS); static int php3_mshutdown_fsock(void); static int php3_rshutdown_fsock(void); @@ -295,23 +297,43 @@ * (buffered data is not persistent) * - php3_fopen_url_wrapper() is still doing single-byte lookahead/read */ -/* {{{ _php3_sock_fgets() */ -int _php3_sock_fgets(char *buf, int maxlen, int socket) +static php3i_sockbuf *_php3_sock_findsock(int socket) { - struct php3i_sockbuf *sockbuf = NULL, *tmpsockbuf; - int bytesread, toread, len, buflen, count = 0; - char *nl; + /* FIXME: O(n) could be improved */ - tmpsockbuf = phpsockbuf; - while (tmpsockbuf) { - if (tmpsockbuf->socket == socket) { - sockbuf = tmpsockbuf; + php3i_sockbuf *buf = NULL, *tmp; + + for(tmp = phpsockbuf; tmp; tmp = tmp->next) + if(tmp->socket == socket) { + buf = tmp; break; } - tmpsockbuf = tmpsockbuf->next; + + return buf; +} + +int _php3_sock_eof(int socket) +{ + php3i_sockbuf *sockbuf; + int ret = 0; + + sockbuf = _php3_sock_findsock(socket); + if(sockbuf) { + ret = (sockbuf->writepos - sockbuf->readpos) == 0 ? 1 : 0; } + return ret; +} +/* {{{ _php3_sock_fgets() */ +int _php3_sock_fgets(char *buf, int maxlen, int socket) +{ + struct php3i_sockbuf *sockbuf; + int bytesread, toread, len, buflen, count = 0; + char *nl; + + sockbuf = _php3_sock_findsock(socket); + if (sockbuf) { toread = sockbuf->writepos - sockbuf->readpos; if (toread > maxlen) { Index: fsock.h =================================================================== RCS file: /repository/php3/functions/fsock.h,v retrieving revision 1.16 diff -u -r1.16 fsock.h --- fsock.h 1999/01/24 23:53:19 1.16 +++ fsock.h 1999/05/19 14:32:57 @@ -41,5 +41,6 @@ extern int _php3_sock_fgets(char *buf, int maxlen, int socket); extern int _php3_sock_fread(char *buf, int maxlen, int socket); extern int _php3_is_persistent_sock(int); +int _php3_sock_eof(int socket); #endif /* _FSOCK_H */