wez Sun Oct 13 22:28:36 2002 EDT
Modified files:
/php4/ext/standard file.c
/php4/main streams.c
Log:
- fgets($fp) (with no length parameter) now uses a buffer as long as the
the next line available from the $fp. Previously, there was a 1KB limit.
(Wez)
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.268 php4/ext/standard/file.c:1.269
--- php4/ext/standard/file.c:1.268 Sat Oct 5 06:59:35 2002
+++ php4/ext/standard/file.cSun Oct 13 22:28:35 2002
-21,7 +21,7
+--+
*/
-/* $Id: file.c,v 1.268 2002/10/05 10:59:35 wez Exp $ */
+/* $Id: file.c,v 1.269 2002/10/14 02:28:35 wez Exp $ */
/* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
-1247,8 +1247,8
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
php_stream_from_zval(stream, arg1);
- if (argc1) {
+ 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
exit_failed:
RETVAL_FALSE;
- efree(buf);
+ if (buf)
+ efree(buf);
}
/* }}} */
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.103 php4/main/streams.c:1.104
--- php4/main/streams.c:1.103 Sun Oct 13 19:43:46 2002
+++ php4/main/streams.c Sun Oct 13 22:28:35 2002
-20,7 +20,7
+--+
*/
-/* $Id: streams.c,v 1.103 2002/10/13 23:43:46 wez Exp $ */
+/* $Id: streams.c,v 1.104 2002/10/14 02:28:35 wez Exp $ */
#define _GNU_SOURCE
#include php.h
-666,8 +666,8
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
* 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
stream-readpos += cpysz;
buf += cpysz;
maxlen -= cpysz;
+ total_copied += cpysz;
- did_copy = 1;
if (done) {
break;
}
-758,8 +759,11
}
}
- if (!did_copy)
+ if (total_copied == 0) {
+ if (grow_mode)
+ assert(bufstart != NULL);
return NULL;
+ }
buf[0] = '\0';
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php