summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/standard/file.c55
-rwxr-xr-xmain/php_streams.h1
-rwxr-xr-xmain/streams.c13
3 files changed, 46 insertions, 23 deletions
diff --git a/ext/standard/file.c b/ext/standard/file.c
index 0bf654b2b2..2e8c2c37de 100644
--- a/ext/standard/file.c
+++ b/ext/standard/file.c
@@ -461,15 +461,14 @@ PHP_FUNCTION(file)
{
char *filename;
int filename_len;
- char *slashed, *target_buf;
+ char *slashed, *target_buf, *p, *s, *e;
register int i = 0;
- int len;
+ int target_len, len;
char eol_marker = '\n';
zend_bool use_include_path = 0;
- zend_bool reached_eof = 0;
php_stream *stream;
- /* Parse arguments */
+ /* Parse arguments */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b",
&filename, &filename_len, &use_include_path) == FAILURE) {
return;
@@ -485,21 +484,39 @@ PHP_FUNCTION(file)
/* Initialize return array */
array_init(return_value);
- /* Now loop through the file and do the magic quotes thing if needed */
- while (1) {
- target_buf = php_stream_gets(stream, NULL, 0);
- if (target_buf == NULL) {
- break;
- }
-
- if (PG(magic_quotes_runtime)) {
- /* 1 = free source string */
- slashed = php_addslashes(target_buf, strlen(target_buf), &len, 1 TSRMLS_CC);
- add_next_index_stringl(return_value, slashed, len, 0);
- } else {
- add_next_index_string(return_value, target_buf, 0);
- }
- }
+ if ((target_len = php_stream_copy_to_mem(stream, &target_buf, PHP_STREAM_COPY_ALL, 0))) {
+ s = target_buf;
+ e = target_buf + target_len;
+
+ if (!(p = php_stream_locate_eol(stream, target_buf, target_len TSRMLS_CC))) {
+ p = e;
+ goto parse_eol;
+ }
+
+ if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
+ eol_marker = '\r';
+ }
+
+ do {
+ p++;
+ parse_eol:
+ if (PG(magic_quotes_runtime)) {
+ slashed = php_addslashes(s, (p-s), &len, 1 TSRMLS_CC);
+ add_index_stringl(return_value, i++, slashed, len, 0);
+ } else {
+ add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0);
+ }
+ s = p;
+ } while ((p = memchr(p, eol_marker, (e-p))));
+
+ /* handle any left overs of files without new lines */
+ if (s != e) {
+ p = e;
+ goto parse_eol;
+ }
+ }
+
+ efree(target_buf);
php_stream_close(stream);
}
/* }}} */
diff --git a/main/php_streams.h b/main/php_streams.h
index 9a5f12c8fb..0f85166ef6 100755
--- a/main/php_streams.h
+++ b/main/php_streams.h
@@ -503,6 +503,7 @@ PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *w
PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC);
PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC);
+PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC);
#define php_stream_open_wrapper(path, mode, options, opened) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_CC TSRMLS_CC)
#define php_stream_open_wrapper_ex(path, mode, options, opened, context) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), (context) STREAMS_CC TSRMLS_CC)
diff --git a/main/streams.c b/main/streams.c
index 8b6a40c1e1..e71a42585c 100755
--- a/main/streams.c
+++ b/main/streams.c
@@ -626,14 +626,19 @@ PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_D
return stream->ops->stat(stream, ssb TSRMLS_CC);
}
-static char *php_stream_locate_eol(php_stream *stream TSRMLS_DC)
+PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC)
{
size_t avail;
char *cr, *lf, *eol = NULL;
char *readptr;
- readptr = stream->readbuf + stream->readpos;
- avail = stream->writepos - stream->readpos;
+ if (!buf) {
+ readptr = stream->readbuf + stream->readpos;
+ avail = stream->writepos - stream->readpos;
+ } else {
+ readptr = buf;
+ avail = buf_len;
+ }
/* Look for EOL */
if (stream->flags & PHP_STREAM_FLAG_DETECT_EOL) {
@@ -699,7 +704,7 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
int done = 0;
readptr = stream->readbuf + stream->readpos;
- eol = php_stream_locate_eol(stream TSRMLS_CC);
+ eol = php_stream_locate_eol(stream, NULL, 0 TSRMLS_CC);
if (eol) {
cpysz = eol - readptr + 1;