diff options
Diffstat (limited to 'main/streams/streams.c')
| -rwxr-xr-x | main/streams/streams.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/main/streams/streams.c b/main/streams/streams.c index e3f79816a..e19430608 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c 299466 2010-05-18 19:39:39Z pajoye $ */ +/* $Id: streams.c 305379 2010-11-15 18:22:52Z cataphract $ */ #define _GNU_SOURCE #include "php.h" @@ -1093,12 +1093,17 @@ PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC) PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC) { + if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) { + /* flush to commit data written to the fopencookie FILE* */ + fflush(stream->stdiocast); + } + /* handle the case where we are in the buffer */ if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) { switch(whence) { case SEEK_CUR: - if (offset > 0 && offset < stream->writepos - stream->readpos) { - stream->readpos += offset; + if (offset > 0 && offset <= stream->writepos - stream->readpos) { + stream->readpos += offset; /* if offset = ..., then readpos = writepos */ stream->position += offset; stream->eof = 0; return 0; @@ -1106,7 +1111,7 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ break; case SEEK_SET: if (offset > stream->position && - offset < stream->position + stream->writepos - stream->readpos) { + offset <= stream->position + stream->writepos - stream->readpos) { stream->readpos += offset - stream->position; stream->position = offset; stream->eof = 0; @@ -1149,14 +1154,12 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ /* emulate forward moving seeks with reads */ if (whence == SEEK_CUR && offset > 0) { char tmp[1024]; - while(offset >= sizeof(tmp)) { - if (php_stream_read(stream, tmp, sizeof(tmp)) == 0) { + size_t didread; + while(offset > 0) { + if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) == 0) { return -1; } - offset -= sizeof(tmp); - } - if (offset && (php_stream_read(stream, tmp, offset) == 0)) { - return -1; + offset -= didread; } stream->eof = 0; return 0; |
