iliaa           Fri Oct 18 16:39:49 2002 EDT

  Modified files:              
    /php4/main  streams.c php_streams.h 
    /php4/ext/standard  file.c 
  Log:
  Fixed bug #19971 (optimized the file() function).
  The file() function is now also binary safe.
  
  
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.109 php4/main/streams.c:1.110
--- php4/main/streams.c:1.109   Fri Oct 18 08:15:04 2002
+++ php4/main/streams.c Fri Oct 18 16:39:49 2002
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: streams.c,v 1.109 2002/10/18 12:15:04 wez Exp $ */
+/* $Id: streams.c,v 1.110 2002/10/18 20:39:49 iliaa Exp $ */
 
 #define _GNU_SOURCE
 #include "php.h"
@@ -626,14 +626,19 @@
        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 @@
                        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;
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.54 php4/main/php_streams.h:1.55
--- php4/main/php_streams.h:1.54        Sat Oct  5 06:35:13 2002
+++ php4/main/php_streams.h     Fri Oct 18 16:39:49 2002
@@ -503,6 +503,7 @@
 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)
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.271 php4/ext/standard/file.c:1.272
--- php4/ext/standard/file.c:1.271      Fri Oct 18 12:55:47 2002
+++ php4/ext/standard/file.c    Fri Oct 18 16:39:49 2002
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: file.c,v 1.271 2002/10/18 16:55:47 wez Exp $ */
+/* $Id: file.c,v 1.272 2002/10/18 20:39:49 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
 
@@ -461,15 +461,14 @@
 {
        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 @@
        /* 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);
 }
 /* }}} */



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to