From: Ilia Alshanetsky Date: Tue, 12 Oct 2004 23:25:24 +0000 (+0000) Subject: MFH: Added optional offset parameter to stream_get_contents(). X-Git-Tag: PRE_NEW_VM_GEN_PATCH~39 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2390ca71f1cb412cf65f40c6a510d10f66735d39;p=php MFH: Added optional offset parameter to stream_get_contents(). --- diff --git a/NEWS b/NEWS index 644b5ceecb..93b39c1d8d 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2004, PHP 5.1.0 +- Added optional offset parameter to stream_get_contents(). (Ilia) - Improved performance of: . general execution/compilation. (Andi, Thies, Sterling, Dmitry, Marcus) . switch statement. (Dmitry) diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 91093a7653..2853ac1ea9 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -354,22 +354,27 @@ PHP_FUNCTION(stream_socket_recvfrom) } /* }}} */ -/* {{{ proto long stream_get_contents(resource source [, long maxlen ]) +/* {{{ proto long stream_get_contents(resource source [, long maxlen [, long offset]]) Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */ PHP_FUNCTION(stream_get_contents) { php_stream *stream; zval *zsrc; - long maxlen = PHP_STREAM_COPY_ALL; + long maxlen = PHP_STREAM_COPY_ALL, pos = 0; int len, newlen; char *contents = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zsrc, &maxlen) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zsrc, &maxlen, &pos) == FAILURE) { RETURN_FALSE; } php_stream_from_zval(stream, &zsrc); + if (pos > 0 && php_stream_seek(stream, pos, SEEK_SET) < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to %ld position in the stream.", pos); + RETURN_FALSE; + } + if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) { if (PG(magic_quotes_runtime)) {