]> granicus.if.org Git - php/commitdiff
made fgets() binary safe.
authorWez Furlong <wez@php.net>
Sat, 19 Oct 2002 13:11:48 +0000 (13:11 +0000)
committerWez Furlong <wez@php.net>
Sat, 19 Oct 2002 13:11:48 +0000 (13:11 +0000)
php_stream_gets is now a macro which calls php_stream_get_line. The latter
has an option argument to return the number of bytes in the line.
Functions like fgetcsv(), fgetss() can be made binary safe by calling
php_stream_get_line directly.

# HEADS UP: You will need to make clean after updating your CVS, as the
# binary signature has changed.

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

index 2e8c2c37de51d283b0b0718a70bc3ffbd26d3ffb..bcc11c73654af3897c5ad8e3227c7a37097a54d0 100644 (file)
@@ -1239,6 +1239,7 @@ PHPAPI PHP_FUNCTION(fgets)
        int len;
        char *buf = NULL;
        int argc = ZEND_NUM_ARGS();
+       size_t line_len = 0;
        php_stream *stream;
 
        if (argc<1 || argc>2 || zend_get_parameters_ex(argc, &arg1, &arg2) == FAILURE) {
@@ -1249,7 +1250,7 @@ PHPAPI PHP_FUNCTION(fgets)
 
        if (argc == 1) {
                /* ask streams to give us a buffer of an appropriate size */
-               buf = php_stream_gets(stream, NULL, 0);
+               buf = php_stream_get_line(stream, NULL, 0, &line_len);
                if (buf == NULL)
                        goto exit_failed;
        } else if (argc > 1) {
@@ -1262,19 +1263,19 @@ PHPAPI PHP_FUNCTION(fgets)
                }
 
                buf = ecalloc(len + 1, sizeof(char));
-               if (php_stream_gets(stream, buf, len) == NULL)
+               if (php_stream_get_line(stream, buf, len, &line_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_STRVAL_P(return_value) = php_addslashes(buf, line_len, &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
                Z_TYPE_P(return_value) = IS_STRING;
        } else {
-               ZVAL_STRING(return_value, buf, 0);
+               ZVAL_STRINGL(return_value, buf, line_len, 0);
                /* 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);
+                       Z_STRVAL_P(return_value) = erealloc(buf, line_len + 1);
                }
        }
        return;
index 0f85166ef6dc123afbfc0c55275049600b703791..6861671360b95762d8927d9beffa27df85f74b3c 100755 (executable)
@@ -359,8 +359,10 @@ PHPAPI int _php_stream_putc(php_stream *stream, int c TSRMLS_DC);
 PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC);
 #define php_stream_flush(stream)       _php_stream_flush((stream), 0 TSRMLS_CC)
 
-PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC);
-#define php_stream_gets(stream, buf, maxlen)   _php_stream_gets((stream), (buf), (maxlen) TSRMLS_CC)
+PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len TSRMLS_DC);
+#define php_stream_gets(stream, buf, maxlen)   _php_stream_get_line((stream), (buf), (maxlen), NULL TSRMLS_CC)
+
+#define php_stream_get_line(stream, buf, maxlen, retlen) _php_stream_get_line((stream), (buf), (maxlen), (retlen) TSRMLS_CC)
 
 /* CAREFUL! this is equivalent to puts NOT fputs! */
 PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC);
index 9e61916bd9a3cf972bb3826402c0899c191e112c..e820e53d18a19011e7f67895fb78a77d610b3c24 100755 (executable)
@@ -668,7 +668,8 @@ PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len
 /* If buf == NULL, the buffer will be allocated automatically and will be of an
  * appropriate length to hold the line, regardless of the line length, memory
  * permitting */
-PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
+PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
+               size_t *returned_len TSRMLS_DC)
 {
        size_t avail = 0;
        size_t current_buf_size = 0;
@@ -772,6 +773,8 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
        }
        
        buf[0] = '\0';
+       if (returned_len)
+               *returned_len = total_copied;
 
        return bufstart;
 }