]> granicus.if.org Git - php/commitdiff
@- fgets($fp) (with no length parameter) now uses a buffer as long as the
authorWez Furlong <wez@php.net>
Mon, 14 Oct 2002 02:28:35 +0000 (02:28 +0000)
committerWez Furlong <wez@php.net>
Mon, 14 Oct 2002 02:28:35 +0000 (02:28 +0000)
@  the next line available from the $fp. Previously, there was a 1KB limit.
@  (Wez)

ext/standard/file.c
main/streams.c

index ab778e4c787523ba2a4101828c9d8a38763ff884..b264a2a637b0e4585348dca589d4f71820302c7e 100644 (file)
@@ -1247,8 +1247,8 @@ PHP_FUNCTION(stream_set_timeout)
 PHPAPI PHP_FUNCTION(fgets)
 {
        zval **arg1, **arg2;
-       int len = 1024;
-       char *buf;
+       int len;
+       char *buf = NULL;
        int argc = ZEND_NUM_ARGS();
        php_stream *stream;
 
@@ -1258,30 +1258,33 @@ PHPAPI PHP_FUNCTION(fgets)
 
        php_stream_from_zval(stream, arg1);
 
-       if (argc>1) {
+       if (argc == 1) {
+               /* ask streams to give us a buffer of an appropriate size */
+               buf = php_stream_gets(stream, NULL, 0);
+               if (buf == NULL)
+                       goto exit_failed;
+       } else if (argc > 1) {
                convert_to_long_ex(arg2);
                len = Z_LVAL_PP(arg2);
-       }
-
-       if (len < 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter may not be negative");
-               RETURN_FALSE;
-       }
-
-       buf = emalloc(sizeof(char) * (len + 1));
-       /* needed because recv doesnt put a null at the end*/
-       memset(buf, 0, len+1);
 
-       if (php_stream_gets(stream, buf, len) == NULL)
-               goto exit_failed;
+               if (len < 0) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter may not be negative");
+                       RETURN_FALSE;
+               }
 
+               buf = ecalloc(len + 1, sizeof(char));
+               if (php_stream_gets(stream, buf, len) == NULL)
+                       goto exit_failed;
+       }
+       
        if (PG(magic_quotes_runtime)) {
                Z_STRVAL_P(return_value) = php_addslashes(buf, 0, &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
                Z_TYPE_P(return_value) = IS_STRING;
        } else {
                ZVAL_STRING(return_value, buf, 0);
-               /* resize buffer if it's much larger than the result */
-               if (Z_STRLEN_P(return_value) < len / 2) {
+               /* resize buffer if it's much larger than the result.
+                * Only needed if the user requested a buffer size. */
+               if (argc > 1 && Z_STRLEN_P(return_value) < len / 2) {
                        Z_STRVAL_P(return_value) = erealloc(buf, Z_STRLEN_P(return_value) + 1);
                }
        }
@@ -1289,7 +1292,8 @@ PHPAPI PHP_FUNCTION(fgets)
 
 exit_failed:
        RETVAL_FALSE;
-       efree(buf);
+       if (buf)
+               efree(buf);
 }
 /* }}} */
 
index 177e78ba2b80333fe980bddc67a9798ae21c7ac7..743fcac2331559426f9475d08a528af8711b73b2 100755 (executable)
@@ -666,8 +666,8 @@ static char *php_stream_locate_eol(php_stream *stream TSRMLS_DC)
 PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
 {
        size_t avail = 0;
-       int did_copy = 0;
        size_t current_buf_size = 0;
+       size_t total_copied = 0;
        int grow_mode = 0;
        char *bufstart = buf;
 
@@ -718,6 +718,7 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
                                 * hard to follow */
                                bufstart = erealloc(bufstart, current_buf_size + cpysz + 1);
                                current_buf_size += cpysz + 1;
+                               buf = bufstart + total_copied;
                        } else {
                                if (cpysz >= maxlen - 1) {
                                        cpysz = maxlen - 1;
@@ -731,8 +732,8 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
                        stream->readpos += cpysz;
                        buf += cpysz;
                        maxlen -= cpysz;
+                       total_copied += cpysz;
 
-                       did_copy = 1;
                        if (done) {
                                break;
                        }
@@ -758,8 +759,11 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
                }
        }
        
-       if (!did_copy)
+       if (total_copied == 0) {
+               if (grow_mode)
+                       assert(bufstart != NULL);
                return NULL;
+       }
        
        buf[0] = '\0';