diff options
author | Wez Furlong <wez@php.net> | 2002-10-19 13:11:48 +0000 |
---|---|---|
committer | Wez Furlong <wez@php.net> | 2002-10-19 13:11:48 +0000 |
commit | 829f1855fdc70b2db862f400423327b75de0bdf1 (patch) | |
tree | 0596c78c4425d717f0a15fb3b925a0d029a80860 | |
parent | 8998bce403f1f9e91e043a4fa9b18c0a70248978 (diff) | |
download | php-git-829f1855fdc70b2db862f400423327b75de0bdf1.tar.gz |
made fgets() binary safe.
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.
-rw-r--r-- | ext/standard/file.c | 11 | ||||
-rwxr-xr-x | main/php_streams.h | 6 | ||||
-rwxr-xr-x | main/streams.c | 5 |
3 files changed, 14 insertions, 8 deletions
diff --git a/ext/standard/file.c b/ext/standard/file.c index 2e8c2c37de..bcc11c7365 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -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; diff --git a/main/php_streams.h b/main/php_streams.h index 0f85166ef6..6861671360 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -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); diff --git a/main/streams.c b/main/streams.c index 9e61916bd9..e820e53d18 100755 --- a/main/streams.c +++ b/main/streams.c @@ -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; } |