wez Sun Dec 22 13:21:55 2002 EDT Added files: (Branch: PHP_4_3) /php4/ext/standard/tests/file bug21131.phpt
Modified files: /php4/ext/standard/tests/file userstreams.phpt /php4/main streams.c Log: MFH fix for Bug #21131 Index: php4/ext/standard/tests/file/userstreams.phpt diff -u php4/ext/standard/tests/file/userstreams.phpt:1.8 php4/ext/standard/tests/file/userstreams.phpt:1.8.2.1 --- php4/ext/standard/tests/file/userstreams.phpt:1.8 Tue Oct 29 09:36:49 2002 +++ php4/ext/standard/tests/file/userstreams.phpt Sun Dec 22 13:21:54 2002 @@ -97,7 +97,11 @@ $split = parse_url($path); $this->varname = $split["host"]; - $this->position = 0; + + if (strchr($mode, 'a')) + $this->position = strlen($GLOBALS[$this->varname]); + else + $this->position = 0; return true; } @@ -301,6 +305,15 @@ if ($fail_count == 0) { echo "FGETS: OK\n"; } + +/* One final test to see if the position is respected when opened for append */ +$fp = fopen("test://lyrics", "a+"); +rewind($fp); +var_dump(ftell($fp)); +$data = fgets($fp); +fclose($fp); +echo $data . "\n"; + ?> --EXPECT-- Not Registered @@ -308,3 +321,5 @@ Registered SEEK: OK FGETS: OK +int(0) +...and the road becomes my bride Index: php4/main/streams.c diff -u php4/main/streams.c:1.125.2.21 php4/main/streams.c:1.125.2.22 --- php4/main/streams.c:1.125.2.21 Thu Dec 19 15:34:34 2002 +++ php4/main/streams.c Sun Dec 22 13:21:55 2002 @@ -20,7 +20,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c,v 1.125.2.21 2002/12/19 20:34:34 wez Exp $ */ +/* $Id: streams.c,v 1.125.2.22 2002/12/22 18:21:55 wez Exp $ */ #define _GNU_SOURCE #include "php.h" @@ -1317,8 +1317,12 @@ stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); - if (stream && self->is_pipe) { - stream->flags |= PHP_STREAM_FLAG_NO_SEEK; + if (stream) { + if (self->is_pipe) { + stream->flags |= PHP_STREAM_FLAG_NO_SEEK; + } else { + stream->position = ftell(file); + } } return stream; @@ -2412,6 +2416,16 @@ } } } + + if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) +== 0 && strchr(mode, 'a')) { + fpos_t newpos = 0; + + /* if opened for append, we need to revise our idea of the initial +file position */ + if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos TSRMLS_CC)) { + stream->position = newpos; + } + } + if (stream == NULL && (options & REPORT_ERRORS)) { display_wrapper_errors(wrapper, path, "failed to create stream" TSRMLS_CC); } Index: php4/ext/standard/tests/file/bug21131.phpt +++ php4/ext/standard/tests/file/bug21131.phpt --TEST-- Bug #21131: fopen($filename, "a+") has broken position --FILE-- <?php # vim600:syn=php: $filename = tempnam("/tmp", "phpt"); $fp = fopen($filename, "w") or die("can't open $filename for append"); fwrite($fp, "foobar"); fclose($fp); $fp = fopen($filename, "a+"); var_dump(ftell($fp)); rewind($fp); var_dump(ftell($fp)); fpassthru($fp); fclose($fp); unlink($filename); ?> --EXPECT-- int(6) int(0) foobar -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php