PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - file.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1002 1167 85.9 %
Date: 2014-07-13 Functions: 49 51 96.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
      16             :    |          Stig Bakken <ssb@php.net>                                   |
      17             :    |          Andi Gutmans <andi@zend.com>                                |
      18             :    |          Zeev Suraski <zeev@zend.com>                                |
      19             :    | PHP 4.0 patches by Thies C. Arntzen (thies@thieso.net)               |
      20             :    | PHP streams by Wez Furlong (wez@thebrainroom.com)                    |
      21             :    +----------------------------------------------------------------------+
      22             : */
      23             : 
      24             : /* $Id$ */
      25             : 
      26             : /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
      27             : 
      28             : /* {{{ includes */
      29             : 
      30             : #include "php.h"
      31             : #include "php_globals.h"
      32             : #include "ext/standard/flock_compat.h"
      33             : #include "ext/standard/exec.h"
      34             : #include "ext/standard/php_filestat.h"
      35             : #include "php_open_temporary_file.h"
      36             : #include "ext/standard/basic_functions.h"
      37             : #include "php_ini.h"
      38             : #include "php_smart_str.h"
      39             : 
      40             : #include <stdio.h>
      41             : #include <stdlib.h>
      42             : #include <errno.h>
      43             : #include <sys/types.h>
      44             : #include <sys/stat.h>
      45             : #include <fcntl.h>
      46             : 
      47             : #ifdef PHP_WIN32
      48             : # include <io.h>
      49             : # define O_RDONLY _O_RDONLY
      50             : # include "win32/param.h"
      51             : # include "win32/winutil.h"
      52             : # include "win32/fnmatch.h"
      53             : #else
      54             : # if HAVE_SYS_PARAM_H
      55             : #  include <sys/param.h>
      56             : # endif
      57             : # if HAVE_SYS_SELECT_H
      58             : #  include <sys/select.h>
      59             : # endif
      60             : # if defined(NETWARE) && defined(USE_WINSOCK)
      61             : #  include <novsock2.h>
      62             : # else
      63             : #  include <sys/socket.h>
      64             : #  include <netinet/in.h>
      65             : #  include <netdb.h>
      66             : # endif
      67             : # if HAVE_ARPA_INET_H
      68             : #  include <arpa/inet.h>
      69             : # endif
      70             : #endif
      71             : 
      72             : #include "ext/standard/head.h"
      73             : #include "safe_mode.h"
      74             : #include "php_string.h"
      75             : #include "file.h"
      76             : 
      77             : #if HAVE_PWD_H
      78             : # ifdef PHP_WIN32
      79             : #  include "win32/pwd.h"
      80             : # else
      81             : #  include <pwd.h>
      82             : # endif
      83             : #endif
      84             : 
      85             : #ifdef HAVE_SYS_TIME_H
      86             : # include <sys/time.h>
      87             : #endif
      88             : 
      89             : #include "fsock.h"
      90             : #include "fopen_wrappers.h"
      91             : #include "streamsfuncs.h"
      92             : #include "php_globals.h"
      93             : 
      94             : #ifdef HAVE_SYS_FILE_H
      95             : # include <sys/file.h>
      96             : #endif
      97             : 
      98             : #if MISSING_FCLOSE_DECL
      99             : extern int fclose(FILE *);
     100             : #endif
     101             : 
     102             : #ifdef HAVE_SYS_MMAN_H
     103             : # include <sys/mman.h>
     104             : #endif
     105             : 
     106             : #include "scanf.h"
     107             : #include "zend_API.h"
     108             : 
     109             : #ifdef ZTS
     110             : int file_globals_id;
     111             : #else
     112             : php_file_globals file_globals;
     113             : #endif
     114             : 
     115             : #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32)
     116             : # ifndef _GNU_SOURCE
     117             : #  define _GNU_SOURCE
     118             : # endif
     119             : # include <fnmatch.h>
     120             : #endif
     121             : 
     122             : #ifdef HAVE_WCHAR_H
     123             : # include <wchar.h>
     124             : #endif
     125             : 
     126             : #ifndef S_ISDIR
     127             : # define S_ISDIR(mode)  (((mode)&S_IFMT) == S_IFDIR)
     128             : #endif
     129             : /* }}} */
     130             : 
     131             : #define PHP_STREAM_TO_ZVAL(stream, arg) \
     132             :         php_stream_from_zval_no_verify(stream, arg); \
     133             :         if (stream == NULL) {   \
     134             :                 RETURN_FALSE;   \
     135             :         }
     136             : 
     137             : /* {{{ ZTS-stuff / Globals / Prototypes */
     138             : 
     139             : /* sharing globals is *evil* */         
     140             : static int le_stream_context = FAILURE;
     141             : 
     142        5985 : PHPAPI int php_le_stream_context(void)
     143             : {
     144        5985 :         return le_stream_context;
     145             : }
     146             : /* }}} */
     147             : 
     148             : /* {{{ Module-Stuff
     149             : */
     150        4499 : static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
     151             : {
     152        4499 :         php_stream_context *context = (php_stream_context*)rsrc->ptr;
     153        4499 :         if (context->options) {
     154        4499 :                 zval_ptr_dtor(&context->options);
     155        4499 :                 context->options = NULL;
     156             :         }
     157        4499 :         php_stream_context_free(context);
     158        4499 : }
     159             : 
     160       19341 : static void file_globals_ctor(php_file_globals *file_globals_p TSRMLS_DC)
     161             : {
     162       19341 :         FG(pclose_ret) = 0;
     163       19341 :         FG(user_stream_current_filename) = NULL;
     164       19341 :         FG(def_chunk_size) = PHP_SOCK_CHUNK_SIZE;
     165       19341 :         FG(wrapper_errors) = NULL;
     166       19341 : }
     167             : 
     168       19376 : static void file_globals_dtor(php_file_globals *file_globals_p TSRMLS_DC)
     169             : {
     170       19376 : }
     171             : 
     172             : PHP_INI_BEGIN()
     173             :         STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals)
     174             :         PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, NULL)
     175             :         STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals)
     176             :         STD_PHP_INI_ENTRY("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateLong, auto_detect_line_endings, php_file_globals, file_globals)
     177             : PHP_INI_END()
     178             : 
     179       19341 : PHP_MINIT_FUNCTION(file)
     180             : {
     181       19341 :         le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number);
     182             : 
     183             : #ifdef ZTS
     184             :         ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);
     185             : #else
     186       19341 :         file_globals_ctor(&file_globals TSRMLS_CC);
     187             : #endif
     188             : 
     189       19341 :         REGISTER_INI_ENTRIES();
     190             : 
     191       19341 :         REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
     192       19341 :         REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
     193       19341 :         REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
     194       19341 :         REGISTER_LONG_CONSTANT("LOCK_SH", PHP_LOCK_SH, CONST_CS | CONST_PERSISTENT);
     195       19341 :         REGISTER_LONG_CONSTANT("LOCK_EX", PHP_LOCK_EX, CONST_CS | CONST_PERSISTENT);
     196       19341 :         REGISTER_LONG_CONSTANT("LOCK_UN", PHP_LOCK_UN, CONST_CS | CONST_PERSISTENT);
     197       19341 :         REGISTER_LONG_CONSTANT("LOCK_NB", PHP_LOCK_NB, CONST_CS | CONST_PERSISTENT);
     198             : 
     199       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_CONNECT",                       PHP_STREAM_NOTIFY_CONNECT,                      CONST_CS | CONST_PERSISTENT);
     200       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_REQUIRED", PHP_STREAM_NOTIFY_AUTH_REQUIRED,        CONST_CS | CONST_PERSISTENT);
     201       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_RESULT",           PHP_STREAM_NOTIFY_AUTH_RESULT,          CONST_CS | CONST_PERSISTENT);
     202       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_MIME_TYPE_IS",  PHP_STREAM_NOTIFY_MIME_TYPE_IS,         CONST_CS | CONST_PERSISTENT);
     203       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FILE_SIZE_IS",  PHP_STREAM_NOTIFY_FILE_SIZE_IS,         CONST_CS | CONST_PERSISTENT);
     204       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_REDIRECTED",            PHP_STREAM_NOTIFY_REDIRECTED,           CONST_CS | CONST_PERSISTENT);
     205       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_PROGRESS",              PHP_STREAM_NOTIFY_PROGRESS,                     CONST_CS | CONST_PERSISTENT);
     206       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FAILURE",                       PHP_STREAM_NOTIFY_FAILURE,                      CONST_CS | CONST_PERSISTENT);
     207       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_COMPLETED",             PHP_STREAM_NOTIFY_COMPLETED,            CONST_CS | CONST_PERSISTENT);
     208       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_RESOLVE",                       PHP_STREAM_NOTIFY_RESOLVE,                      CONST_CS | CONST_PERSISTENT);
     209             : 
     210       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_INFO", PHP_STREAM_NOTIFY_SEVERITY_INFO,        CONST_CS | CONST_PERSISTENT);
     211       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN", PHP_STREAM_NOTIFY_SEVERITY_WARN,        CONST_CS | CONST_PERSISTENT);
     212       19341 :         REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR",  PHP_STREAM_NOTIFY_SEVERITY_ERR,         CONST_CS | CONST_PERSISTENT);
     213             : 
     214       19341 :         REGISTER_LONG_CONSTANT("STREAM_FILTER_READ",                  PHP_STREAM_FILTER_READ,                         CONST_CS | CONST_PERSISTENT);
     215       19341 :         REGISTER_LONG_CONSTANT("STREAM_FILTER_WRITE",                 PHP_STREAM_FILTER_WRITE,                        CONST_CS | CONST_PERSISTENT);
     216       19341 :         REGISTER_LONG_CONSTANT("STREAM_FILTER_ALL",                           PHP_STREAM_FILTER_ALL,                          CONST_CS | CONST_PERSISTENT);
     217             : 
     218       19341 :         REGISTER_LONG_CONSTANT("STREAM_CLIENT_PERSISTENT",            PHP_STREAM_CLIENT_PERSISTENT,           CONST_CS | CONST_PERSISTENT);
     219       19341 :         REGISTER_LONG_CONSTANT("STREAM_CLIENT_ASYNC_CONNECT", PHP_STREAM_CLIENT_ASYNC_CONNECT,        CONST_CS | CONST_PERSISTENT);
     220       19341 :         REGISTER_LONG_CONSTANT("STREAM_CLIENT_CONNECT",                       PHP_STREAM_CLIENT_CONNECT,      CONST_CS | CONST_PERSISTENT);
     221             : 
     222       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_CLIENT",           STREAM_CRYPTO_METHOD_SSLv2_CLIENT,      CONST_CS|CONST_PERSISTENT);
     223       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_CLIENT",           STREAM_CRYPTO_METHOD_SSLv3_CLIENT,      CONST_CS|CONST_PERSISTENT);
     224       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_CLIENT",  STREAM_CRYPTO_METHOD_SSLv23_CLIENT,     CONST_CS|CONST_PERSISTENT);
     225       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_CLIENT",             STREAM_CRYPTO_METHOD_TLS_CLIENT,        CONST_CS|CONST_PERSISTENT);
     226       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_SERVER",           STREAM_CRYPTO_METHOD_SSLv2_SERVER,      CONST_CS|CONST_PERSISTENT);
     227       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_SERVER",           STREAM_CRYPTO_METHOD_SSLv3_SERVER,      CONST_CS|CONST_PERSISTENT);
     228       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_SERVER",  STREAM_CRYPTO_METHOD_SSLv23_SERVER,     CONST_CS|CONST_PERSISTENT);
     229       19341 :         REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_SERVER",             STREAM_CRYPTO_METHOD_TLS_SERVER,        CONST_CS|CONST_PERSISTENT);
     230             : 
     231       19341 :         REGISTER_LONG_CONSTANT("STREAM_SHUT_RD",      STREAM_SHUT_RD,         CONST_CS|CONST_PERSISTENT);
     232       19341 :         REGISTER_LONG_CONSTANT("STREAM_SHUT_WR",      STREAM_SHUT_WR,         CONST_CS|CONST_PERSISTENT);
     233       19341 :         REGISTER_LONG_CONSTANT("STREAM_SHUT_RDWR",    STREAM_SHUT_RDWR,       CONST_CS|CONST_PERSISTENT);
     234             : 
     235             : #ifdef PF_INET
     236       19341 :         REGISTER_LONG_CONSTANT("STREAM_PF_INET", PF_INET, CONST_CS|CONST_PERSISTENT);
     237             : #elif defined(AF_INET)
     238             :         REGISTER_LONG_CONSTANT("STREAM_PF_INET", AF_INET, CONST_CS|CONST_PERSISTENT);
     239             : #endif
     240             : 
     241             : #ifdef PF_INET6
     242       19341 :         REGISTER_LONG_CONSTANT("STREAM_PF_INET6", PF_INET6, CONST_CS|CONST_PERSISTENT);
     243             : #elif defined(AF_INET6)
     244             :         REGISTER_LONG_CONSTANT("STREAM_PF_INET6", AF_INET6, CONST_CS|CONST_PERSISTENT);
     245             : #endif
     246             : 
     247             : #ifdef PF_UNIX
     248       19341 :         REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", PF_UNIX, CONST_CS|CONST_PERSISTENT);
     249             : #elif defined(AF_UNIX)
     250             :         REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", AF_UNIX, CONST_CS|CONST_PERSISTENT);
     251             : #endif
     252             : 
     253             : #ifdef IPPROTO_IP
     254             :         /* most people will use this one when calling socket() or socketpair() */
     255       19341 :         REGISTER_LONG_CONSTANT("STREAM_IPPROTO_IP", IPPROTO_IP, CONST_CS|CONST_PERSISTENT);
     256             : #endif
     257             : 
     258             : #ifdef IPPROTO_TCP
     259       19341 :         REGISTER_LONG_CONSTANT("STREAM_IPPROTO_TCP", IPPROTO_TCP, CONST_CS|CONST_PERSISTENT);
     260             : #endif
     261             : 
     262             : #ifdef IPPROTO_UDP
     263       19341 :         REGISTER_LONG_CONSTANT("STREAM_IPPROTO_UDP", IPPROTO_UDP, CONST_CS|CONST_PERSISTENT);
     264             : #endif
     265             : 
     266             : #ifdef IPPROTO_ICMP
     267       19341 :         REGISTER_LONG_CONSTANT("STREAM_IPPROTO_ICMP", IPPROTO_ICMP, CONST_CS|CONST_PERSISTENT);
     268             : #endif
     269             : 
     270             : #ifdef IPPROTO_RAW
     271       19341 :         REGISTER_LONG_CONSTANT("STREAM_IPPROTO_RAW", IPPROTO_RAW, CONST_CS|CONST_PERSISTENT);
     272             : #endif
     273             : 
     274       19341 :         REGISTER_LONG_CONSTANT("STREAM_SOCK_STREAM", SOCK_STREAM, CONST_CS|CONST_PERSISTENT);
     275       19341 :         REGISTER_LONG_CONSTANT("STREAM_SOCK_DGRAM", SOCK_DGRAM, CONST_CS|CONST_PERSISTENT);
     276             : 
     277             : #ifdef SOCK_RAW
     278       19341 :         REGISTER_LONG_CONSTANT("STREAM_SOCK_RAW", SOCK_RAW, CONST_CS|CONST_PERSISTENT);
     279             : #endif
     280             : 
     281             : #ifdef SOCK_SEQPACKET
     282       19341 :         REGISTER_LONG_CONSTANT("STREAM_SOCK_SEQPACKET", SOCK_SEQPACKET, CONST_CS|CONST_PERSISTENT);
     283             : #endif
     284             : 
     285             : #ifdef SOCK_RDM
     286       19341 :         REGISTER_LONG_CONSTANT("STREAM_SOCK_RDM", SOCK_RDM, CONST_CS|CONST_PERSISTENT);
     287             : #endif
     288             : 
     289       19341 :         REGISTER_LONG_CONSTANT("STREAM_PEEK", STREAM_PEEK, CONST_CS | CONST_PERSISTENT);
     290       19341 :         REGISTER_LONG_CONSTANT("STREAM_OOB",  STREAM_OOB, CONST_CS | CONST_PERSISTENT);
     291             : 
     292       19341 :         REGISTER_LONG_CONSTANT("STREAM_SERVER_BIND",                  STREAM_XPORT_BIND,                                      CONST_CS | CONST_PERSISTENT);
     293       19341 :         REGISTER_LONG_CONSTANT("STREAM_SERVER_LISTEN",                        STREAM_XPORT_LISTEN,                            CONST_CS | CONST_PERSISTENT);
     294             : 
     295       19341 :         REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH",                       PHP_FILE_USE_INCLUDE_PATH,                      CONST_CS | CONST_PERSISTENT);
     296       19341 :         REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES",                       PHP_FILE_IGNORE_NEW_LINES,                      CONST_CS | CONST_PERSISTENT);
     297       19341 :         REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES",                       PHP_FILE_SKIP_EMPTY_LINES,                      CONST_CS | CONST_PERSISTENT);
     298       19341 :         REGISTER_LONG_CONSTANT("FILE_APPEND",                                 PHP_FILE_APPEND,                                        CONST_CS | CONST_PERSISTENT);
     299       19341 :         REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT",             PHP_FILE_NO_DEFAULT_CONTEXT,            CONST_CS | CONST_PERSISTENT);
     300             : 
     301       19341 :         REGISTER_LONG_CONSTANT("FILE_TEXT",                                           0,                                                                      CONST_CS | CONST_PERSISTENT);
     302       19341 :         REGISTER_LONG_CONSTANT("FILE_BINARY",                                 0,                                                                      CONST_CS | CONST_PERSISTENT);
     303             : 
     304             : #ifdef HAVE_FNMATCH
     305       19341 :         REGISTER_LONG_CONSTANT("FNM_NOESCAPE", FNM_NOESCAPE, CONST_CS | CONST_PERSISTENT);
     306       19341 :         REGISTER_LONG_CONSTANT("FNM_PATHNAME", FNM_PATHNAME, CONST_CS | CONST_PERSISTENT);
     307       19341 :         REGISTER_LONG_CONSTANT("FNM_PERIOD",   FNM_PERIOD,   CONST_CS | CONST_PERSISTENT);
     308             : # ifdef FNM_CASEFOLD /* a GNU extension */ /* TODO emulate if not available */
     309       19341 :         REGISTER_LONG_CONSTANT("FNM_CASEFOLD", FNM_CASEFOLD, CONST_CS | CONST_PERSISTENT);
     310             : # endif
     311             : #endif
     312             : 
     313       19341 :         return SUCCESS;
     314             : }
     315             : /* }}} */
     316             : 
     317       19376 : PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */
     318             : {
     319             : #ifndef ZTS
     320       19376 :         file_globals_dtor(&file_globals TSRMLS_CC);
     321             : #endif
     322       19376 :         return SUCCESS;
     323             : }
     324             : /* }}} */
     325             : 
     326             : static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
     327             : 
     328             : /* {{{ proto bool flock(resource fp, int operation [, int &wouldblock])
     329             :    Portable file locking */
     330         194 : PHP_FUNCTION(flock)
     331             : {
     332         194 :         zval *arg1, *arg3 = NULL;
     333             :         int act;
     334             :         php_stream *stream;
     335         194 :         long operation = 0;
     336             : 
     337         194 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &arg1, &operation, &arg3) == FAILURE) {
     338          12 :                 return;
     339             :         }
     340             : 
     341         182 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
     342             : 
     343         180 :         act = operation & 3;
     344         180 :         if (act < 1 || act > 3) {
     345           5 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal operation argument");
     346           5 :                 RETURN_FALSE;
     347             :         }
     348             : 
     349         319 :         if (arg3 && PZVAL_IS_REF(arg3)) {
     350         366 :                 convert_to_long_ex(&arg3);
     351         144 :                 Z_LVAL_P(arg3) = 0;
     352             :         }
     353             : 
     354             :         /* flock_values contains all possible actions if (operation & 4) we won't block on the lock */
     355         175 :         act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0);
     356         175 :         if (php_stream_lock(stream, act)) {
     357          12 :                 if (operation && errno == EWOULDBLOCK && arg3 && PZVAL_IS_REF(arg3)) {
     358           0 :                         Z_LVAL_P(arg3) = 1;
     359             :                 }
     360          12 :                 RETURN_FALSE;
     361             :         }
     362         163 :         RETURN_TRUE;
     363             : }
     364             : /* }}} */
     365             : 
     366             : #define PHP_META_UNSAFE ".\\+*?[^]$() "
     367             : 
     368             : /* {{{ proto array get_meta_tags(string filename [, bool use_include_path])
     369             :    Extracts all meta tag content attributes from a file and returns an array */
     370           8 : PHP_FUNCTION(get_meta_tags)
     371             : {
     372             :         char *filename;
     373             :         int filename_len;
     374           8 :         zend_bool use_include_path = 0;
     375           8 :         int in_tag = 0, done = 0;
     376           8 :         int looking_for_val = 0, have_name = 0, have_content = 0;
     377           8 :         int saw_name = 0, saw_content = 0;
     378           8 :         char *name = NULL, *value = NULL, *temp = NULL;
     379             :         php_meta_tags_token tok, tok_last;
     380             :         php_meta_tags_data md;
     381             : 
     382             :         /* Initiailize our structure */
     383           8 :         memset(&md, 0, sizeof(md));
     384             : 
     385             :         /* Parse arguments */
     386           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &use_include_path) == FAILURE) {
     387           0 :                 return;
     388             :         }
     389             : 
     390           8 :         if (strlen(filename) != filename_len) {
     391           0 :                 RETURN_FALSE;
     392             :         }
     393             : 
     394           8 :         md.stream = php_stream_open_wrapper(filename, "rb",
     395             :                         (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS,
     396             :                         NULL);
     397           8 :         if (!md.stream) {
     398           0 :                 RETURN_FALSE;
     399             :         }
     400             : 
     401           8 :         array_init(return_value);
     402             : 
     403           8 :         tok_last = TOK_EOF;
     404             : 
     405         220 :         while (!done && (tok = php_next_meta_token(&md TSRMLS_CC)) != TOK_EOF) {
     406         204 :                 if (tok == TOK_ID) {
     407          45 :                         if (tok_last == TOK_OPENTAG) {
     408          17 :                                 md.in_meta = !strcasecmp("meta", md.token_data);
     409          30 :                         } else if (tok_last == TOK_SLASH && in_tag) {
     410           2 :                                 if (strcasecmp("head", md.token_data) == 0) {
     411             :                                         /* We are done here! */
     412           2 :                                         done = 1;
     413             :                                 }
     414          26 :                         } else if (tok_last == TOK_EQUAL && looking_for_val) {
     415           0 :                                 if (saw_name) {
     416           0 :                                         STR_FREE(name);
     417             :                                         /* Get the NAME attr (Single word attr, non-quoted) */
     418           0 :                                         temp = name = estrndup(md.token_data, md.token_len);
     419             : 
     420           0 :                                         while (temp && *temp) {
     421           0 :                                                 if (strchr(PHP_META_UNSAFE, *temp)) {
     422           0 :                                                         *temp = '_';
     423             :                                                 }
     424           0 :                                                 temp++;
     425             :                                         }
     426             : 
     427           0 :                                         have_name = 1;
     428           0 :                                 } else if (saw_content) {
     429           0 :                                         STR_FREE(value);
     430             :                                         /* Get the CONTENT attr (Single word attr, non-quoted) */
     431           0 :                                         if (PG(magic_quotes_runtime)) {
     432           0 :                                                 value = php_addslashes(md.token_data, 0, &md.token_len, 0 TSRMLS_CC);
     433             :                                         } else {
     434           0 :                                                 value = estrndup(md.token_data, md.token_len);
     435             :                                         }
     436             : 
     437           0 :                                         have_content = 1;
     438             :                                 }
     439             : 
     440           0 :                                 looking_for_val = 0;
     441             :                         } else {
     442          26 :                                 if (md.in_meta) {
     443          26 :                                         if (strcasecmp("name", md.token_data) == 0) {
     444          13 :                                                 saw_name = 1;
     445          13 :                                                 saw_content = 0;
     446          13 :                                                 looking_for_val = 1;
     447          13 :                                         } else if (strcasecmp("content", md.token_data) == 0) {
     448          13 :                                                 saw_name = 0;
     449          13 :                                                 saw_content = 1;
     450          13 :                                                 looking_for_val = 1;
     451             :                                         }
     452             :                                 }
     453             :                         }
     454         185 :                 } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) {
     455          26 :                         if (saw_name) {
     456          13 :                                 STR_FREE(name);
     457             :                                 /* Get the NAME attr (Quoted single/double) */
     458          13 :                                 temp = name = estrndup(md.token_data, md.token_len);
     459             : 
     460         136 :                                 while (temp && *temp) {
     461         110 :                                         if (strchr(PHP_META_UNSAFE, *temp)) {
     462           2 :                                                 *temp = '_';
     463             :                                         }
     464         110 :                                         temp++;
     465             :                                 }
     466             : 
     467          13 :                                 have_name = 1;
     468          13 :                         } else if (saw_content) {
     469          13 :                                 STR_FREE(value);
     470             :                                 /* Get the CONTENT attr (Single word attr, non-quoted) */
     471          13 :                                 if (PG(magic_quotes_runtime)) {
     472           0 :                                         value = php_addslashes(md.token_data, 0, &md.token_len, 0 TSRMLS_CC);
     473             :                                 } else {
     474          13 :                                         value = estrndup(md.token_data, md.token_len);
     475             :                                 }
     476             : 
     477          13 :                                 have_content = 1;
     478             :                         }
     479             : 
     480          26 :                         looking_for_val = 0;
     481         133 :                 } else if (tok == TOK_OPENTAG) {
     482          25 :                         if (looking_for_val) {
     483           0 :                                 looking_for_val = 0;
     484           0 :                                 have_name = saw_name = 0;
     485           0 :                                 have_content = saw_content = 0;
     486             :                         }
     487          25 :                         in_tag = 1;
     488         108 :                 } else if (tok == TOK_CLOSETAG) {
     489          13 :                         if (have_name) {
     490             :                                 /* For BC */
     491          10 :                                 php_strtolower(name, strlen(name));
     492          10 :                                 if (have_content) {
     493          10 :                                         add_assoc_string(return_value, name, value, 1);
     494             :                                 } else {
     495           0 :                                         add_assoc_string(return_value, name, "", 1);
     496             :                                 }
     497             : 
     498          10 :                                 efree(name);
     499          10 :                                 STR_FREE(value);
     500           3 :                         } else if (have_content) {
     501           0 :                                 efree(value);
     502             :                         }
     503             : 
     504          13 :                         name = value = NULL;
     505             : 
     506             :                         /* Reset all of our flags */
     507          13 :                         in_tag = looking_for_val = 0;
     508          13 :                         have_name = saw_name = 0;
     509          13 :                         have_content = saw_content = 0;
     510          13 :                         md.in_meta = 0;
     511             :                 }
     512             : 
     513         204 :                 tok_last = tok;
     514             : 
     515         204 :                 if (md.token_data)
     516          71 :                         efree(md.token_data);
     517             : 
     518         204 :                 md.token_data = NULL;
     519             :         }
     520             : 
     521           8 :         STR_FREE(value);
     522           8 :         STR_FREE(name);
     523           8 :         php_stream_close(md.stream);
     524             : }
     525             : /* }}} */
     526             : 
     527             : /* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context [, long offset [, long maxlen]]]])
     528             :    Read the entire file into a string */
     529        2329 : PHP_FUNCTION(file_get_contents)
     530             : {
     531             :         char *filename;
     532             :         int filename_len;
     533             :         char *contents;
     534        2329 :         zend_bool use_include_path = 0;
     535             :         php_stream *stream;
     536             :         int len;
     537        2329 :         long offset = -1;
     538        2329 :         long maxlen = PHP_STREAM_COPY_ALL;
     539        2329 :         zval *zcontext = NULL;
     540        2329 :         php_stream_context *context = NULL;
     541             : 
     542             :         /* Parse arguments */
     543        2329 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|br!ll", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen) == FAILURE) {
     544          61 :                 return;
     545             :         }
     546             : 
     547        2268 :         if (strlen(filename) != filename_len) {
     548           3 :                 RETURN_FALSE;
     549             :         }
     550             : 
     551        2265 :         if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
     552           5 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "length must be greater than or equal to zero");
     553           5 :                 RETURN_FALSE;
     554             :         }
     555             : 
     556        2260 :         context = php_stream_context_from_zval(zcontext, 0);
     557             : 
     558        2260 :         stream = php_stream_open_wrapper_ex(filename, "rb",
     559             :                                 (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS,
     560             :                                 NULL, context);
     561        2260 :         if (!stream) {
     562          49 :                 RETURN_FALSE;
     563             :         }
     564             : 
     565        2211 :         if (offset > 0 && php_stream_seek(stream, offset, SEEK_SET) < 0) {
     566           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to position %ld in the stream", offset);
     567           1 :                 php_stream_close(stream);
     568           1 :                 RETURN_FALSE;
     569             :         }
     570             : 
     571        2210 :         if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {
     572             : 
     573        1833 :                 if (PG(magic_quotes_runtime)) {
     574           3 :                         contents = php_addslashes(contents, len, &len, 1 TSRMLS_CC); /* 1 = free source string */
     575             :                 }
     576             : 
     577        1833 :                 RETVAL_STRINGL(contents, len, 0);
     578         377 :         } else if (len == 0) {
     579         377 :                 RETVAL_EMPTY_STRING();
     580             :         } else {
     581           0 :                 RETVAL_FALSE;
     582             :         }
     583             : 
     584        2210 :         php_stream_close(stream);
     585             : }
     586             : /* }}} */
     587             : 
     588             : /* {{{ proto int file_put_contents(string file, mixed data [, int flags [, resource context]])
     589             :    Write/Create a file with contents data and return the number of bytes written */
     590       24439 : PHP_FUNCTION(file_put_contents)
     591             : {
     592             :         php_stream *stream;
     593             :         char *filename;
     594             :         int filename_len;
     595             :         zval *data;
     596       24439 :         int numbytes = 0;
     597       24439 :         long flags = 0;
     598       24439 :         zval *zcontext = NULL;
     599       24439 :         php_stream_context *context = NULL;
     600       24439 :         php_stream *srcstream = NULL;
     601       24439 :         char mode[3] = "wb";
     602             : 
     603       24439 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|lr!", &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) {
     604          29 :                 return;
     605             :         }
     606             : 
     607       24410 :         if (strlen(filename) != filename_len) {
     608           1 :                 RETURN_FALSE;
     609             :         }
     610             : 
     611       24409 :         if (Z_TYPE_P(data) == IS_RESOURCE) {
     612           1 :                 php_stream_from_zval(srcstream, &data);
     613             :         }
     614             : 
     615       24408 :         context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
     616             : 
     617       24408 :         if (flags & PHP_FILE_APPEND) {
     618           8 :                 mode[0] = 'a';
     619       24400 :         } else if (flags & LOCK_EX) {
     620             :                 /* check to make sure we are dealing with a regular file */
     621           0 :                 if (php_memnstr(filename, "://", sizeof("://") - 1, filename + filename_len)) {
     622           0 :                         if (strncasecmp(filename, "file://", sizeof("file://") - 1)) {
     623           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Exclusive locks may only be set for regular files");
     624           0 :                                 RETURN_FALSE;
     625             :                         }
     626             :                 }
     627           0 :                 mode[0] = 'c';
     628             :         }
     629       24408 :         mode[2] = '\0';
     630             : 
     631       24408 :         stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
     632       24408 :         if (stream == NULL) {
     633          20 :                 RETURN_FALSE;
     634             :         }
     635             : 
     636       24388 :         if (flags & LOCK_EX && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) {
     637           0 :                 php_stream_close(stream);
     638           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Exclusive locks are not supported for this stream");
     639           0 :                 RETURN_FALSE;
     640             :         }
     641             : 
     642       24388 :         if (mode[0] == 'c') {
     643           0 :                 php_stream_truncate_set_size(stream, 0);
     644             :         }
     645             : 
     646       24388 :         switch (Z_TYPE_P(data)) {
     647             :                 case IS_RESOURCE: {
     648             :                         size_t len;
     649           0 :                         if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
     650           0 :                                 numbytes = -1;
     651             :                         } else {
     652           0 :                                 numbytes = len;
     653             :                         }
     654           0 :                         break;
     655             :                 }
     656             :                 case IS_NULL:
     657             :                 case IS_LONG:
     658             :                 case IS_DOUBLE:
     659             :                 case IS_BOOL:
     660             :                 case IS_CONSTANT:
     661         108 :                         convert_to_string_ex(&data);
     662             : 
     663             :                 case IS_STRING:
     664       24378 :                         if (Z_STRLEN_P(data)) {
     665       20265 :                                 numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));
     666       20265 :                                 if (numbytes != Z_STRLEN_P(data)) {
     667           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRLEN_P(data));
     668           0 :                                         numbytes = -1;
     669             :                                 }
     670             :                         }
     671       24378 :                         break;
     672             : 
     673             :                 case IS_ARRAY:
     674           6 :                         if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
     675             :                                 int bytes_written;
     676             :                                 zval **tmp;
     677             :                                 HashPosition pos;
     678             : 
     679           5 :                                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
     680          76 :                                 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &tmp, &pos) == SUCCESS) {
     681          66 :                                         if (Z_TYPE_PP(tmp) != IS_STRING) {
     682          48 :                                                 SEPARATE_ZVAL(tmp);
     683          12 :                                                 convert_to_string(*tmp);
     684             :                                         }
     685          66 :                                         if (Z_STRLEN_PP(tmp)) {
     686          65 :                                                 numbytes += Z_STRLEN_PP(tmp);
     687          65 :                                                 bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     688          65 :                                                 if (bytes_written < 0 || bytes_written != Z_STRLEN_PP(tmp)) {
     689           0 :                                                         if (bytes_written < 0) {
     690           0 :                                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write %d bytes to %s", Z_STRLEN_PP(tmp), filename);
     691             :                                                         } else {
     692           0 :                                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", bytes_written, Z_STRLEN_PP(tmp));
     693             :                                                         }
     694           0 :                                                         numbytes = -1;
     695           0 :                                                         break;
     696             :                                                 }
     697             :                                         }
     698          66 :                                         zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos);
     699             :                                 }
     700             :                         }
     701           6 :                         break;
     702             : 
     703             :                 case IS_OBJECT:
     704           4 :                         if (Z_OBJ_HT_P(data) != NULL) {
     705             :                                 zval out;
     706             : 
     707           4 :                                 if (zend_std_cast_object_tostring(data, &out, IS_STRING TSRMLS_CC) == SUCCESS) {
     708           2 :                                         numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
     709           2 :                                         if (numbytes != Z_STRLEN(out)) {
     710           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRLEN(out));
     711           0 :                                                 numbytes = -1;
     712             :                                         }
     713           2 :                                         zval_dtor(&out);
     714           2 :                                         break;
     715             :                                 }
     716             :                         }
     717             :                 default:
     718           2 :                         numbytes = -1;
     719             :                         break;
     720             :         }
     721       24388 :         php_stream_close(stream);
     722             : 
     723       24388 :         if (numbytes < 0) {
     724           2 :                 RETURN_FALSE;
     725             :         }
     726             : 
     727       24386 :         RETURN_LONG(numbytes);
     728             : }
     729             : /* }}} */
     730             : 
     731             : #define PHP_FILE_BUF_SIZE       80
     732             : 
     733             : /* {{{ proto array file(string filename [, int flags[, resource context]])
     734             :    Read entire file into an array */
     735         250 : PHP_FUNCTION(file)
     736             : {
     737             :         char *filename;
     738             :         int filename_len;
     739         250 :         char *slashed, *target_buf=NULL, *p, *s, *e;
     740         250 :         register int i = 0;
     741             :         int target_len, len;
     742         250 :         char eol_marker = '\n';
     743         250 :         long flags = 0;
     744             :         zend_bool use_include_path;
     745             :         zend_bool include_new_line;
     746             :         zend_bool skip_blank_lines;
     747             :         php_stream *stream;
     748         250 :         zval *zcontext = NULL;
     749         250 :         php_stream_context *context = NULL;
     750             : 
     751             :         /* Parse arguments */
     752         250 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr!", &filename, &filename_len, &flags, &zcontext) == FAILURE) {
     753          45 :                 return;
     754             :         }
     755             : 
     756         205 :         if (strlen(filename) != filename_len) {
     757           0 :                 RETURN_FALSE;
     758             :         }
     759             : 
     760         205 :         if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
     761          12 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%ld' flag is not supported", flags);
     762          12 :                 RETURN_FALSE;
     763             :         }
     764             : 
     765         193 :         use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
     766         193 :         include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
     767         193 :         skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;
     768             : 
     769         193 :         context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
     770             : 
     771         193 :         stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
     772         193 :         if (!stream) {
     773          32 :                 RETURN_FALSE;
     774             :         }
     775             : 
     776             :         /* Initialize return array */
     777         161 :         array_init(return_value);
     778             : 
     779         161 :         if ((target_len = php_stream_copy_to_mem(stream, &target_buf, PHP_STREAM_COPY_ALL, 0))) {
     780         158 :                 s = target_buf;
     781         158 :                 e = target_buf + target_len;
     782             : 
     783         158 :                 if (!(p = php_stream_locate_eol(stream, target_buf, target_len TSRMLS_CC))) {
     784          20 :                         p = e;
     785          20 :                         goto parse_eol;
     786             :                 }
     787             : 
     788         138 :                 if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
     789           0 :                         eol_marker = '\r';
     790             :                 }
     791             : 
     792             :                 /* for performance reasons the code is duplicated, so that the if (include_new_line)
     793             :                  * will not need to be done for every single line in the file. */
     794         138 :                 if (include_new_line) {
     795             :                         do {
     796        2188 :                                 p++;
     797             : parse_eol:
     798        2264 :                                 if (PG(magic_quotes_runtime)) {
     799             :                                         /* s is in target_buf which is freed at the end of the function */
     800           0 :                                         slashed = php_addslashes(s, (p-s), &len, 0 TSRMLS_CC);
     801           0 :                                         add_index_stringl(return_value, i++, slashed, len, 0);
     802             :                                 } else {
     803        2264 :                                         add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0);
     804             :                                 }
     805        2264 :                                 s = p;
     806        2264 :                         } while ((p = memchr(p, eol_marker, (e-p))));
     807             :                 } else {
     808             :                         do {
     809          47 :                                 int windows_eol = 0;
     810          47 :                                 if (p != target_buf && eol_marker == '\n' && *(p - 1) == '\r') {
     811           6 :                                         windows_eol++;
     812             :                                 }
     813          47 :                                 if (skip_blank_lines && !(p-s-windows_eol)) {
     814           1 :                                         s = ++p;
     815           1 :                                         continue;
     816             :                                 }
     817          46 :                                 if (PG(magic_quotes_runtime)) {
     818             :                                         /* s is in target_buf which is freed at the end of the function */
     819           0 :                                         slashed = php_addslashes(s, (p-s-windows_eol), &len, 0 TSRMLS_CC);
     820           0 :                                         add_index_stringl(return_value, i++, slashed, len, 0);
     821             :                                 } else {
     822          46 :                                         add_index_stringl(return_value, i++, estrndup(s, p-s-windows_eol), p-s-windows_eol, 0);
     823             :                                 }
     824          46 :                                 s = ++p;
     825          47 :                         } while ((p = memchr(p, eol_marker, (e-p))));
     826             :                 }
     827             : 
     828             :                 /* handle any left overs of files without new lines */
     829         214 :                 if (s != e) {
     830          56 :                         p = e;
     831          56 :                         goto parse_eol;
     832             :                 }
     833             :         }
     834             : 
     835         161 :         if (target_buf) {
     836         158 :                 efree(target_buf);
     837             :         }
     838         161 :         php_stream_close(stream);
     839             : }
     840             : /* }}} */
     841             : 
     842             : /* {{{ proto string tempnam(string dir, string prefix)
     843             :    Create a unique filename in a directory */
     844         764 : PHP_FUNCTION(tempnam)
     845             : {
     846             :         char *dir, *prefix;
     847             :         int dir_len, prefix_len;
     848             :         size_t p_len;
     849             :         char *opened_path;
     850             :         char *p;
     851             :         int fd;
     852             : 
     853         764 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &dir, &dir_len, &prefix, &prefix_len) == FAILURE) {
     854           9 :                 return;
     855             :         }
     856             : 
     857         755 :         if (strlen(dir) != dir_len) {
     858           1 :                 RETURN_FALSE;
     859             :         }
     860             : 
     861         754 :         if (strlen(prefix) != prefix_len) {
     862           1 :                 RETURN_FALSE;
     863             :         }
     864             : 
     865         753 :         if (PG(safe_mode) &&(!php_checkuid(dir, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
     866           0 :                 RETURN_FALSE;
     867             :         }
     868             : 
     869         753 :         if (php_check_open_basedir(dir TSRMLS_CC)) {
     870           7 :                 RETURN_FALSE;
     871             :         }
     872             : 
     873         746 :         php_basename(prefix, prefix_len, NULL, 0, &p, &p_len TSRMLS_CC);
     874         746 :         if (p_len > 64) {
     875           0 :                 p[63] = '\0';
     876             :         }
     877             :         
     878         746 :         RETVAL_FALSE;
     879             : 
     880         746 :         if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, 1 TSRMLS_CC)) >= 0) {
     881         745 :                 close(fd);
     882         745 :                 RETVAL_STRING(opened_path, 0);
     883             :         }
     884         746 :         efree(p);
     885             : }
     886             : /* }}} */
     887             : 
     888             : /* {{{ proto resource tmpfile(void)
     889             :    Create a temporary file that will be deleted automatically after use */
     890          73 : PHP_NAMED_FUNCTION(php_if_tmpfile)
     891             : {
     892             :         php_stream *stream;
     893             : 
     894          73 :         if (zend_parse_parameters_none() == FAILURE) {
     895           0 :                 return;
     896             :         }
     897             : 
     898          73 :         stream = php_stream_fopen_tmpfile();
     899             : 
     900          73 :         if (stream) {
     901          73 :                 php_stream_to_zval(stream, return_value);
     902             :         } else {
     903           0 :                 RETURN_FALSE;
     904             :         }
     905             : }
     906             : /* }}} */
     907             : 
     908             : /* {{{ proto resource fopen(string filename, string mode [, bool use_include_path [, resource context]])
     909             :    Open a file or a URL and return a file pointer */
     910       23135 : PHP_NAMED_FUNCTION(php_if_fopen)
     911             : {
     912             :         char *filename, *mode;
     913             :         int filename_len, mode_len;
     914       23135 :         zend_bool use_include_path = 0;
     915       23135 :         zval *zcontext = NULL;
     916             :         php_stream *stream;
     917       23135 :         php_stream_context *context = NULL;
     918             : 
     919       23135 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|br", &filename, &filename_len, &mode, &mode_len, &use_include_path, &zcontext) == FAILURE) {
     920          43 :                 RETURN_FALSE;
     921             :         }
     922             : 
     923       23092 :         if (strlen(filename) != filename_len) {
     924           0 :                 RETURN_FALSE;
     925             :         }
     926             : 
     927       23092 :         context = php_stream_context_from_zval(zcontext, 0);
     928             : 
     929       23092 :         stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
     930             : 
     931       23091 :         if (stream == NULL) {
     932          92 :                 RETURN_FALSE;
     933             :         }
     934             : 
     935       22999 :         php_stream_to_zval(stream, return_value);
     936             : }
     937             : /* }}} */
     938             : 
     939             : /* {{{ proto bool fclose(resource fp)
     940             :    Close an open file pointer */
     941       42386 : PHPAPI PHP_FUNCTION(fclose)
     942             : {
     943             :         zval *arg1;
     944             :         php_stream *stream;
     945             : 
     946       42386 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
     947          31 :                 RETURN_FALSE;
     948             :         }
     949             : 
     950       42355 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
     951             : 
     952       42354 :         if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
     953           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid stream resource", stream->rsrc_id);
     954           0 :                 RETURN_FALSE;
     955             :         }
     956             : 
     957       42354 :         if (!stream->is_persistent) {
     958       42353 :                 zend_list_delete(stream->rsrc_id);
     959             :         } else {
     960           1 :                 php_stream_pclose(stream);
     961             :         }
     962             : 
     963       42354 :         RETURN_TRUE;
     964             : }
     965             : /* }}} */
     966             : 
     967             : /* {{{ proto resource popen(string command, string mode)
     968             :    Execute a command and open either a read or a write pipe to it */
     969           8 : PHP_FUNCTION(popen)
     970             : {
     971             :         char *command, *mode;
     972             :         int command_len, mode_len;
     973             :         FILE *fp;
     974             :         php_stream *stream;
     975           8 :         char *posix_mode, *b, *buf = 0, *tmp;
     976             : 
     977           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &command, &command_len, &mode, &mode_len) == FAILURE) {
     978           2 :                 return;
     979             :         }
     980             : 
     981           6 :         posix_mode = estrndup(mode, mode_len);
     982             : #ifndef PHP_WIN32
     983             :         {
     984           6 :                 char *z = memchr(posix_mode, 'b', mode_len);
     985           6 :                 if (z) {
     986           1 :                         memmove(z, z + 1, mode_len - (z - posix_mode));
     987             :                 }
     988             :         }
     989             : #endif
     990           6 :         if (PG(safe_mode)){
     991           0 :                 b = strchr(command, ' ');
     992           0 :                 if (!b) {
     993           0 :                         b = strrchr(command, '/');
     994             :                 } else {
     995             :                         char *c;
     996             : 
     997           0 :                         c = command;
     998           0 :                         while((*b != '/') && (b != c)) {
     999           0 :                                 b--;
    1000             :                         }
    1001           0 :                         if (b == c) {
    1002           0 :                                 b = NULL;
    1003             :                         }
    1004             :                 }
    1005             : 
    1006           0 :                 if (b) {
    1007           0 :                         spprintf(&buf, 0, "%s%s", PG(safe_mode_exec_dir), b);
    1008             :                 } else {
    1009           0 :                         spprintf(&buf, 0, "%s/%s", PG(safe_mode_exec_dir), command);
    1010             :                 }
    1011             : 
    1012           0 :                 tmp = php_escape_shell_cmd(buf);
    1013           0 :                 fp = VCWD_POPEN(tmp, posix_mode);
    1014           0 :                 efree(tmp);
    1015             : 
    1016           0 :                 if (!fp) {
    1017           0 :                         php_error_docref2(NULL TSRMLS_CC, buf, posix_mode, E_WARNING, "%s", strerror(errno));
    1018           0 :                         efree(posix_mode);
    1019           0 :                         efree(buf);
    1020           0 :                         RETURN_FALSE;
    1021             :                 }
    1022             : 
    1023           0 :                 efree(buf);
    1024             : 
    1025             :         } else {
    1026           6 :                 fp = VCWD_POPEN(command, posix_mode);
    1027           6 :                 if (!fp) {
    1028           1 :                         php_error_docref2(NULL TSRMLS_CC, command, posix_mode, E_WARNING, "%s", strerror(errno));
    1029           1 :                         efree(posix_mode);
    1030           1 :                         RETURN_FALSE;
    1031             :                 }
    1032             :         }
    1033           5 :         stream = php_stream_fopen_from_pipe(fp, mode);
    1034             : 
    1035           5 :         if (stream == NULL)     {
    1036           0 :                 php_error_docref2(NULL TSRMLS_CC, command, mode, E_WARNING, "%s", strerror(errno));
    1037           0 :                 RETVAL_FALSE;
    1038             :         } else {
    1039           5 :                 php_stream_to_zval(stream, return_value);
    1040             :         }
    1041             : 
    1042           5 :         efree(posix_mode);
    1043             : }
    1044             : /* }}} */
    1045             : 
    1046             : /* {{{ proto int pclose(resource fp)
    1047             :    Close a file pointer opened by popen() */
    1048          37 : PHP_FUNCTION(pclose)
    1049             : {
    1050             :         zval *arg1;
    1051             :         php_stream *stream;
    1052             : 
    1053          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1054          32 :                 RETURN_FALSE;
    1055             :         }
    1056             : 
    1057           5 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1058             : 
    1059           5 :         zend_list_delete(stream->rsrc_id);
    1060           5 :         RETURN_LONG(FG(pclose_ret));
    1061             : }
    1062             : /* }}} */
    1063             : 
    1064             : /* {{{ proto bool feof(resource fp)
    1065             :    Test for end-of-file on a file pointer */
    1066     1110331 : PHPAPI PHP_FUNCTION(feof)
    1067             : {
    1068             :         zval *arg1;
    1069             :         php_stream *stream;
    1070             : 
    1071     1110331 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1072          13 :                 RETURN_FALSE;
    1073             :         }
    1074             : 
    1075     1110318 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1076             : 
    1077     1110291 :         if (php_stream_eof(stream)) {
    1078        3237 :                 RETURN_TRUE;
    1079             :         } else {
    1080     1107054 :                 RETURN_FALSE;
    1081             :         }
    1082             : }
    1083             : /* }}} */
    1084             : 
    1085             : /* {{{ proto string fgets(resource fp[, int length])
    1086             :    Get a line from file pointer */
    1087     1095488 : PHPAPI PHP_FUNCTION(fgets)
    1088             : {
    1089             :         zval *arg1;
    1090     1095488 :         long len = 1024;
    1091     1095488 :         char *buf = NULL;
    1092     1095488 :         int argc = ZEND_NUM_ARGS();
    1093     1095488 :         size_t line_len = 0;
    1094             :         php_stream *stream;
    1095             : 
    1096     1095488 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &len) == FAILURE) {
    1097          13 :                 RETURN_FALSE;
    1098             :         }
    1099             : 
    1100     1095475 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1101             : 
    1102     1095472 :         if (argc == 1) {
    1103             :                 /* ask streams to give us a buffer of an appropriate size */
    1104     1091014 :                 buf = php_stream_get_line(stream, NULL, 0, &line_len);
    1105     1091014 :                 if (buf == NULL) {
    1106       10517 :                         goto exit_failed;
    1107             :                 }
    1108        4458 :         } else if (argc > 1) {
    1109        4458 :                 if (len <= 0) {
    1110           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
    1111           2 :                         RETURN_FALSE;
    1112             :                 }
    1113             : 
    1114        4456 :                 buf = ecalloc(len + 1, sizeof(char));
    1115        4456 :                 if (php_stream_get_line(stream, buf, len, &line_len) == NULL) {
    1116           2 :                         goto exit_failed;
    1117             :                 }
    1118             :         }
    1119             : 
    1120     1084951 :         if (PG(magic_quotes_runtime)) {
    1121           0 :                 Z_STRVAL_P(return_value) = php_addslashes(buf, line_len, &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
    1122           0 :                 Z_TYPE_P(return_value) = IS_STRING;
    1123             :         } else {
    1124     1084951 :                 ZVAL_STRINGL(return_value, buf, line_len, 0);
    1125             :                 /* resize buffer if it's much larger than the result.
    1126             :                  * Only needed if the user requested a buffer size. */
    1127     1084951 :                 if (argc > 1 && Z_STRLEN_P(return_value) < len / 2) {
    1128        3919 :                         Z_STRVAL_P(return_value) = erealloc(buf, line_len + 1);
    1129             :                 }
    1130             :         }
    1131     1084951 :         return;
    1132             : 
    1133             : exit_failed:
    1134       10519 :         RETVAL_FALSE;
    1135       10519 :         if (buf) {
    1136           2 :                 efree(buf);
    1137             :         }
    1138             : }
    1139             : /* }}} */
    1140             : 
    1141             : /* {{{ proto string fgetc(resource fp)
    1142             :    Get a character from file pointer */
    1143         377 : PHPAPI PHP_FUNCTION(fgetc)
    1144             : {
    1145             :         zval *arg1;
    1146             :         char buf[2];
    1147             :         int result;
    1148             :         php_stream *stream;
    1149             : 
    1150         377 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1151          11 :                 RETURN_FALSE;
    1152             :         }
    1153             : 
    1154         366 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1155             : 
    1156         365 :         result = php_stream_getc(stream);
    1157             : 
    1158         365 :         if (result == EOF) {
    1159          15 :                 RETVAL_FALSE;
    1160             :         } else {
    1161         350 :                 buf[0] = result;
    1162         350 :                 buf[1] = '\0';
    1163             : 
    1164         350 :                 RETURN_STRINGL(buf, 1, 1);
    1165             :         }
    1166             : }
    1167             : /* }}} */
    1168             : 
    1169             : /* {{{ proto string fgetss(resource fp [, int length [, string allowable_tags]])
    1170             :    Get a line from file pointer and strip HTML tags */
    1171         299 : PHPAPI PHP_FUNCTION(fgetss)
    1172             : {
    1173             :         zval *fd;
    1174         299 :         long bytes = 0;
    1175         299 :         size_t len = 0;
    1176             :         size_t actual_len, retval_len;
    1177         299 :         char *buf = NULL, *retval;
    1178             :         php_stream *stream;
    1179         299 :         char *allowed_tags=NULL;
    1180         299 :         int allowed_tags_len=0;
    1181             : 
    1182         299 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ls", &fd, &bytes, &allowed_tags, &allowed_tags_len) == FAILURE) {
    1183           9 :                 RETURN_FALSE;
    1184             :         }
    1185             : 
    1186         290 :         PHP_STREAM_TO_ZVAL(stream, &fd);
    1187             : 
    1188         288 :         if (ZEND_NUM_ARGS() >= 2) {
    1189         227 :                 if (bytes <= 0) {
    1190           4 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
    1191           4 :                         RETURN_FALSE;
    1192             :                 }
    1193             : 
    1194         223 :                 len = (size_t) bytes;
    1195         223 :                 buf = safe_emalloc(sizeof(char), (len + 1), 0);
    1196             :                 /*needed because recv doesnt set null char at end*/
    1197         223 :                 memset(buf, 0, len + 1);
    1198             :         }
    1199             : 
    1200         284 :         if ((retval = php_stream_get_line(stream, buf, len, &actual_len)) == NULL)  {
    1201          52 :                 if (buf != NULL) {
    1202          21 :                         efree(buf);
    1203             :                 }
    1204          52 :                 RETURN_FALSE;
    1205             :         }
    1206             : 
    1207         232 :         retval_len = php_strip_tags(retval, actual_len, &stream->fgetss_state, allowed_tags, allowed_tags_len);
    1208             : 
    1209         232 :         RETURN_STRINGL(retval, retval_len, 0);
    1210             : }
    1211             : /* }}} */
    1212             : 
    1213             : /* {{{ proto mixed fscanf(resource stream, string format [, string ...])
    1214             :    Implements a mostly ANSI compatible fscanf() */
    1215        7048 : PHP_FUNCTION(fscanf)
    1216             : {
    1217        7048 :         int result, format_len, type, argc = 0;
    1218        7048 :         zval ***args = NULL;
    1219             :         zval *file_handle;
    1220             :         char *buf, *format;
    1221             :         size_t len;
    1222             :         void *what;
    1223             : 
    1224        7048 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs*", &file_handle, &format, &format_len, &args, &argc) == FAILURE) {
    1225           5 :                 return;
    1226             :         }
    1227             : 
    1228        7043 :         what = zend_fetch_resource(&file_handle TSRMLS_CC, -1, "File-Handle", &type, 2, php_file_le_stream(), php_file_le_pstream());
    1229             : 
    1230             :         /* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up
    1231             :          * with a leak if we have an invalid filehandle. This needs changing
    1232             :          * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */
    1233        7043 :         if (!what) {
    1234           1 :                 if (args) {
    1235           0 :                         efree(args);
    1236             :                 }
    1237           1 :                 RETURN_FALSE;
    1238             :         }
    1239             : 
    1240        7042 :         buf = php_stream_get_line((php_stream *) what, NULL, 0, &len);
    1241        7042 :         if (buf == NULL) {
    1242         791 :                 if (args) {
    1243           1 :                         efree(args);
    1244             :                 }
    1245         791 :                 RETURN_FALSE;
    1246             :         }
    1247             : 
    1248        6251 :         result = php_sscanf_internal(buf, format, argc, args, 0, &return_value TSRMLS_CC);
    1249             : 
    1250        6251 :         if (args) {
    1251           8 :                 efree(args);
    1252             :         }
    1253        6251 :         efree(buf);
    1254             : 
    1255        6251 :         if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
    1256           0 :                 WRONG_PARAM_COUNT;
    1257             :         }
    1258             : }
    1259             : /* }}} */
    1260             : 
    1261             : /* {{{ proto int fwrite(resource fp, string str [, int length])
    1262             :    Binary-safe file write */
    1263       87557 : PHPAPI PHP_FUNCTION(fwrite)
    1264             : {
    1265             :         zval *arg1;
    1266             :         char *arg2;
    1267             :         int arg2len;
    1268             :         int ret;
    1269             :         int num_bytes;
    1270       87557 :         long arg3 = 0;
    1271       87557 :         char *buffer = NULL;
    1272             :         php_stream *stream;
    1273             : 
    1274       87557 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &arg2, &arg2len, &arg3) == FAILURE) {
    1275          35 :                 RETURN_FALSE;
    1276             :         }
    1277             : 
    1278       87522 :         if (ZEND_NUM_ARGS() == 2) {
    1279       31043 :                 num_bytes = arg2len;
    1280             :         } else {
    1281       56479 :                 num_bytes = MAX(0, MIN((int)arg3, arg2len));
    1282             :         }
    1283             : 
    1284       87522 :         if (!num_bytes) {
    1285          31 :                 RETURN_LONG(0);
    1286             :         }
    1287             : 
    1288       87491 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1289             : 
    1290       87489 :         if (PG(magic_quotes_runtime)) {
    1291           0 :                 buffer = estrndup(arg2, num_bytes);
    1292           0 :                 php_stripslashes(buffer, &num_bytes TSRMLS_CC);
    1293             :         }
    1294             : 
    1295       87489 :         ret = php_stream_write(stream, buffer ? buffer : arg2, num_bytes);
    1296       87489 :         if (buffer) {
    1297           0 :                 efree(buffer);
    1298             :         }
    1299             : 
    1300       87489 :         RETURN_LONG(ret);
    1301             : }
    1302             : /* }}} */
    1303             : 
    1304             : /* {{{ proto bool fflush(resource fp)
    1305             :    Flushes output */
    1306         229 : PHPAPI PHP_FUNCTION(fflush)
    1307             : {
    1308             :         zval *arg1;
    1309             :         int ret;
    1310             :         php_stream *stream;
    1311             : 
    1312         229 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1313           8 :                 RETURN_FALSE;
    1314             :         }
    1315             : 
    1316         221 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1317             : 
    1318         221 :         ret = php_stream_flush(stream);
    1319         221 :         if (ret) {
    1320           0 :                 RETURN_FALSE;
    1321             :         }
    1322         221 :         RETURN_TRUE;
    1323             : }
    1324             : /* }}} */
    1325             : 
    1326             : /* {{{ proto bool rewind(resource fp)
    1327             :    Rewind the position of a file pointer */
    1328        3293 : PHPAPI PHP_FUNCTION(rewind)
    1329             : {
    1330             :         zval *arg1;
    1331             :         php_stream *stream;
    1332             : 
    1333        3293 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1334          13 :                 RETURN_FALSE;
    1335             :         }
    1336             : 
    1337        3280 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1338             : 
    1339        3279 :         if (-1 == php_stream_rewind(stream)) {
    1340           2 :                 RETURN_FALSE;
    1341             :         }
    1342        3277 :         RETURN_TRUE;
    1343             : }
    1344             : /* }}} */
    1345             : 
    1346             : /* {{{ proto int ftell(resource fp)
    1347             :    Get file pointer's read/write position */
    1348       14679 : PHPAPI PHP_FUNCTION(ftell)
    1349             : {
    1350             :         zval *arg1;
    1351             :         long ret;
    1352             :         php_stream *stream;
    1353             : 
    1354       14679 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1355          11 :                 RETURN_FALSE;
    1356             :         }
    1357             : 
    1358       14668 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1359             : 
    1360       14643 :         ret = php_stream_tell(stream);
    1361       14643 :         if (ret == -1)  {
    1362          10 :                 RETURN_FALSE;
    1363             :         }
    1364       14633 :         RETURN_LONG(ret);
    1365             : }
    1366             : /* }}} */
    1367             : 
    1368             : /* {{{ proto int fseek(resource fp, int offset [, int whence])
    1369             :    Seek on a file pointer */
    1370        7671 : PHPAPI PHP_FUNCTION(fseek)
    1371             : {
    1372             :         zval *arg1;
    1373        7671 :         long arg2, whence = SEEK_SET;
    1374             :         php_stream *stream;
    1375             : 
    1376        7671 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &arg2, &whence) == FAILURE) {
    1377          27 :                 RETURN_FALSE;
    1378             :         }
    1379             : 
    1380        7644 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1381             : 
    1382        7643 :         RETURN_LONG(php_stream_seek(stream, arg2, whence));
    1383             : }
    1384             : /* }}} */
    1385             : 
    1386             : /* {{{ php_mkdir
    1387             : */
    1388             : 
    1389             : /* DEPRECATED APIs: Use php_stream_mkdir() instead */
    1390        1670 : PHPAPI int php_mkdir_ex(char *dir, long mode, int options TSRMLS_DC)
    1391             : {
    1392             :         int ret;
    1393             : 
    1394        1670 :         if (PG(safe_mode) && (!php_checkuid(dir, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
    1395           0 :                 return -1;
    1396             :         }
    1397             : 
    1398        1670 :         if (php_check_open_basedir(dir TSRMLS_CC)) {
    1399           0 :                 return -1;
    1400             :         }
    1401             : 
    1402        1670 :         if ((ret = VCWD_MKDIR(dir, (mode_t)mode)) < 0 && (options & REPORT_ERRORS)) {
    1403          49 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
    1404             :         }
    1405             : 
    1406        1670 :         return ret;
    1407             : }
    1408             : 
    1409        1670 : PHPAPI int php_mkdir(char *dir, long mode TSRMLS_DC)
    1410             : {
    1411        1670 :         return php_mkdir_ex(dir, mode, REPORT_ERRORS TSRMLS_CC);
    1412             : }
    1413             : /* }}} */
    1414             : 
    1415             : /* {{{ proto bool mkdir(string pathname [, int mode [, bool recursive [, resource context]]])
    1416             :    Create a directory */
    1417        1724 : PHP_FUNCTION(mkdir)
    1418             : {
    1419             :         char *dir;
    1420             :         int dir_len;
    1421        1724 :         zval *zcontext = NULL;
    1422        1724 :         long mode = 0777;
    1423        1724 :         zend_bool recursive = 0;
    1424             :         php_stream_context *context;
    1425             : 
    1426        1724 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbr", &dir, &dir_len, &mode, &recursive, &zcontext) == FAILURE) {
    1427          50 :                 RETURN_FALSE;
    1428             :         }
    1429             : 
    1430        1674 :         if (strlen(dir) != dir_len) {
    1431           1 :                 RETURN_FALSE;
    1432             :         }
    1433             : 
    1434        1673 :         context = php_stream_context_from_zval(zcontext, 0);
    1435             : 
    1436        1673 :         RETURN_BOOL(php_stream_mkdir(dir, mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context));
    1437             : }
    1438             : /* }}} */
    1439             : 
    1440             : /* {{{ proto bool rmdir(string dirname[, resource context])
    1441             :    Remove a directory */
    1442        1739 : PHP_FUNCTION(rmdir)
    1443             : {
    1444             :         char *dir;
    1445             :         int dir_len;
    1446        1739 :         zval *zcontext = NULL;
    1447             :         php_stream_context *context;
    1448             : 
    1449        1739 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dir, &dir_len, &zcontext) == FAILURE) {
    1450          32 :                 RETURN_FALSE;
    1451             :         }
    1452             : 
    1453        1707 :         if (strlen(dir) != dir_len) {
    1454           1 :                 RETURN_FALSE;
    1455             :         }
    1456             : 
    1457        1706 :         context = php_stream_context_from_zval(zcontext, 0);
    1458             : 
    1459        1706 :         RETURN_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context));
    1460             : }
    1461             : /* }}} */
    1462             : 
    1463             : /* {{{ proto int readfile(string filename [, bool use_include_path[, resource context]])
    1464             :    Output a file or a URL */
    1465         461 : PHP_FUNCTION(readfile)
    1466             : {
    1467             :         char *filename;
    1468             :         int filename_len;
    1469         461 :         int size = 0;
    1470         461 :         zend_bool use_include_path = 0;
    1471         461 :         zval *zcontext = NULL;
    1472             :         php_stream *stream;
    1473         461 :         php_stream_context *context = NULL;
    1474             : 
    1475         461 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|br!", &filename, &filename_len, &use_include_path, &zcontext) == FAILURE) {
    1476          37 :                 RETURN_FALSE;
    1477             :         }
    1478             : 
    1479         424 :         if (strlen(filename) != filename_len) {
    1480           1 :                 RETURN_FALSE;
    1481             :         }
    1482             : 
    1483         423 :         context = php_stream_context_from_zval(zcontext, 0);
    1484             : 
    1485         423 :         stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
    1486         423 :         if (stream) {
    1487         407 :                 size = php_stream_passthru(stream);
    1488         407 :                 php_stream_close(stream);
    1489         407 :                 RETURN_LONG(size);
    1490             :         }
    1491             : 
    1492          16 :         RETURN_FALSE;
    1493             : }
    1494             : /* }}} */
    1495             : 
    1496             : /* {{{ proto int umask([int mask])
    1497             :    Return or change the umask */
    1498        2128 : PHP_FUNCTION(umask)
    1499             : {
    1500        2128 :         long arg1 = 0;
    1501             :         int oldumask;
    1502             :         
    1503        2128 :         oldumask = umask(077);
    1504             : 
    1505        2128 :         if (BG(umask) == -1) {
    1506           6 :                 BG(umask) = oldumask;
    1507             :         }
    1508             :         
    1509        2128 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &arg1) == FAILURE) {
    1510          13 :                 RETURN_FALSE;
    1511             :         }
    1512             : 
    1513        2115 :         if (ZEND_NUM_ARGS() == 0) {
    1514        1050 :                 umask(oldumask);
    1515             :         } else {
    1516        1065 :                 umask(arg1);
    1517             :         }
    1518             : 
    1519        2115 :         RETURN_LONG(oldumask);
    1520             : }
    1521             : /* }}} */
    1522             : 
    1523             : /* {{{ proto int fpassthru(resource fp)
    1524             :    Output all remaining data from a file pointer */
    1525         140 : PHPAPI PHP_FUNCTION(fpassthru)
    1526             : {
    1527             :         zval *arg1;
    1528             :         int size;
    1529             :         php_stream *stream;
    1530             : 
    1531         140 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
    1532          37 :                 RETURN_FALSE;
    1533             :         }
    1534             : 
    1535         103 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1536             : 
    1537         103 :         size = php_stream_passthru(stream);
    1538         103 :         RETURN_LONG(size);
    1539             : }
    1540             : /* }}} */
    1541             : 
    1542             : /* {{{ proto bool rename(string old_name, string new_name[, resource context])
    1543             :    Rename a file */
    1544         133 : PHP_FUNCTION(rename)
    1545             : {
    1546             :         char *old_name, *new_name;
    1547             :         int old_name_len, new_name_len;
    1548         133 :         zval *zcontext = NULL;
    1549             :         php_stream_wrapper *wrapper;
    1550             :         php_stream_context *context;
    1551             : 
    1552         133 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &old_name, &old_name_len, &new_name, &new_name_len, &zcontext) == FAILURE) {
    1553          34 :                 RETURN_FALSE;
    1554             :         }
    1555             : 
    1556          99 :         if (strlen(old_name) != old_name_len) {
    1557           1 :                 RETURN_FALSE;
    1558             :         }
    1559             : 
    1560          98 :         if (strlen(new_name) != new_name_len) {
    1561           1 :                 RETURN_FALSE;
    1562             :         }
    1563             : 
    1564          97 :         wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0 TSRMLS_CC);
    1565             : 
    1566          97 :         if (!wrapper || !wrapper->wops) {
    1567           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper");
    1568           0 :                 RETURN_FALSE;
    1569             :         }
    1570             : 
    1571          97 :         if (!wrapper->wops->rename) {
    1572           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s wrapper does not support renaming", wrapper->wops->label ? wrapper->wops->label : "Source");
    1573           1 :                 RETURN_FALSE;
    1574             :         }
    1575             : 
    1576          96 :         if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0 TSRMLS_CC)) {
    1577           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rename a file across wrapper types");
    1578           1 :                 RETURN_FALSE;
    1579             :         }
    1580             : 
    1581          95 :         context = php_stream_context_from_zval(zcontext, 0);
    1582             : 
    1583          95 :         RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context TSRMLS_CC));
    1584             : }
    1585             : /* }}} */
    1586             : 
    1587             : /* {{{ proto bool unlink(string filename[, context context])
    1588             :    Delete a file */
    1589      195185 : PHP_FUNCTION(unlink)
    1590             : {
    1591             :         char *filename;
    1592             :         int filename_len;
    1593             :         php_stream_wrapper *wrapper;
    1594      195185 :         zval *zcontext = NULL;
    1595      195185 :         php_stream_context *context = NULL;
    1596             : 
    1597      195185 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &filename, &filename_len, &zcontext) == FAILURE) {
    1598          36 :                 RETURN_FALSE;
    1599             :         }
    1600             : 
    1601      195149 :         if (strlen(filename) != filename_len) {
    1602           1 :                 RETURN_FALSE;
    1603             :         }
    1604             : 
    1605      195148 :         context = php_stream_context_from_zval(zcontext, 0);
    1606             : 
    1607      195148 :         wrapper = php_stream_locate_url_wrapper(filename, NULL, 0 TSRMLS_CC);
    1608             : 
    1609      195148 :         if (!wrapper || !wrapper->wops) {
    1610           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper");
    1611           0 :                 RETURN_FALSE;
    1612             :         }
    1613             : 
    1614      195148 :         if (!wrapper->wops->unlink) {
    1615           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s does not allow unlinking", wrapper->wops->label ? wrapper->wops->label : "Wrapper");
    1616           1 :                 RETURN_FALSE;
    1617             :         }
    1618      195147 :         RETURN_BOOL(wrapper->wops->unlink(wrapper, filename, ENFORCE_SAFE_MODE | REPORT_ERRORS, context TSRMLS_CC));
    1619             : }
    1620             : /* }}} */
    1621             : 
    1622             : /* {{{ proto bool ftruncate(resource fp, int size)
    1623             :    Truncate file to 'size' length */
    1624         388 : PHP_NAMED_FUNCTION(php_if_ftruncate)
    1625             : {
    1626             :         zval *fp;
    1627             :         long size;
    1628             :         php_stream *stream;
    1629             : 
    1630         388 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &fp, &size) == FAILURE) {
    1631          12 :                 RETURN_FALSE;
    1632             :         }
    1633             : 
    1634         376 :         PHP_STREAM_TO_ZVAL(stream, &fp);
    1635             : 
    1636         375 :         if (!php_stream_truncate_supported(stream)) {
    1637           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't truncate this stream!");
    1638           2 :                 RETURN_FALSE;
    1639             :         }
    1640             : 
    1641         373 :         RETURN_BOOL(0 == php_stream_truncate_set_size(stream, size));
    1642             : }
    1643             : /* }}} */
    1644             : 
    1645             : /* {{{ proto array fstat(resource fp)
    1646             :    Stat() on a filehandle */
    1647          42 : PHP_NAMED_FUNCTION(php_if_fstat)
    1648             : {
    1649             :         zval *fp;
    1650             :         zval *stat_dev, *stat_ino, *stat_mode, *stat_nlink, *stat_uid, *stat_gid, *stat_rdev,
    1651             :                  *stat_size, *stat_atime, *stat_mtime, *stat_ctime, *stat_blksize, *stat_blocks;
    1652             :         php_stream *stream;
    1653             :         php_stream_statbuf stat_ssb;
    1654             :         char *stat_sb_names[13] = {
    1655             :                 "dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
    1656             :                 "size", "atime", "mtime", "ctime", "blksize", "blocks"
    1657          42 :         };
    1658             : 
    1659          42 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &fp) == FAILURE) {
    1660          31 :                 RETURN_FALSE;
    1661             :         }
    1662             : 
    1663          11 :         PHP_STREAM_TO_ZVAL(stream, &fp);
    1664             : 
    1665          10 :         if (php_stream_stat(stream, &stat_ssb)) {
    1666           3 :                 RETURN_FALSE;
    1667             :         }
    1668             : 
    1669           7 :         array_init(return_value);
    1670             : 
    1671           7 :         MAKE_LONG_ZVAL_INCREF(stat_dev, stat_ssb.sb.st_dev);
    1672           7 :         MAKE_LONG_ZVAL_INCREF(stat_ino, stat_ssb.sb.st_ino);
    1673           7 :         MAKE_LONG_ZVAL_INCREF(stat_mode, stat_ssb.sb.st_mode);
    1674           7 :         MAKE_LONG_ZVAL_INCREF(stat_nlink, stat_ssb.sb.st_nlink);
    1675           7 :         MAKE_LONG_ZVAL_INCREF(stat_uid, stat_ssb.sb.st_uid);
    1676           7 :         MAKE_LONG_ZVAL_INCREF(stat_gid, stat_ssb.sb.st_gid);
    1677             : #ifdef HAVE_ST_RDEV
    1678           7 :         MAKE_LONG_ZVAL_INCREF(stat_rdev, stat_ssb.sb.st_rdev);
    1679             : #else
    1680             :         MAKE_LONG_ZVAL_INCREF(stat_rdev, -1);
    1681             : #endif
    1682           7 :         MAKE_LONG_ZVAL_INCREF(stat_size, stat_ssb.sb.st_size);
    1683           7 :         MAKE_LONG_ZVAL_INCREF(stat_atime, stat_ssb.sb.st_atime);
    1684           7 :         MAKE_LONG_ZVAL_INCREF(stat_mtime, stat_ssb.sb.st_mtime);
    1685           7 :         MAKE_LONG_ZVAL_INCREF(stat_ctime, stat_ssb.sb.st_ctime);
    1686             : #ifdef HAVE_ST_BLKSIZE
    1687           7 :         MAKE_LONG_ZVAL_INCREF(stat_blksize, stat_ssb.sb.st_blksize);
    1688             : #else
    1689             :         MAKE_LONG_ZVAL_INCREF(stat_blksize,-1);
    1690             : #endif
    1691             : #ifdef HAVE_ST_BLOCKS
    1692           7 :         MAKE_LONG_ZVAL_INCREF(stat_blocks, stat_ssb.sb.st_blocks);
    1693             : #else
    1694             :         MAKE_LONG_ZVAL_INCREF(stat_blocks,-1);
    1695             : #endif
    1696             :         /* Store numeric indexes in propper order */
    1697           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_dev, sizeof(zval *), NULL);
    1698           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ino, sizeof(zval *), NULL);
    1699           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mode, sizeof(zval *), NULL);
    1700           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_nlink, sizeof(zval *), NULL);
    1701           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_uid, sizeof(zval *), NULL);
    1702           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_gid, sizeof(zval *), NULL);
    1703           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_rdev, sizeof(zval *), NULL);
    1704           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_size, sizeof(zval *), NULL);
    1705           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_atime, sizeof(zval *), NULL);
    1706           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mtime, sizeof(zval *), NULL);
    1707           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ctime, sizeof(zval *), NULL);
    1708           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blksize, sizeof(zval *), NULL);
    1709           7 :         zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blocks, sizeof(zval *), NULL);
    1710             : 
    1711             :         /* Store string indexes referencing the same zval*/
    1712           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[0], strlen(stat_sb_names[0])+1, (void *)&stat_dev, sizeof(zval *), NULL);
    1713           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[1], strlen(stat_sb_names[1])+1, (void *)&stat_ino, sizeof(zval *), NULL);
    1714           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[2], strlen(stat_sb_names[2])+1, (void *)&stat_mode, sizeof(zval *), NULL);
    1715           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[3], strlen(stat_sb_names[3])+1, (void *)&stat_nlink, sizeof(zval *), NULL);
    1716           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[4], strlen(stat_sb_names[4])+1, (void *)&stat_uid, sizeof(zval *), NULL);
    1717           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[5], strlen(stat_sb_names[5])+1, (void *)&stat_gid, sizeof(zval *), NULL);
    1718           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[6], strlen(stat_sb_names[6])+1, (void *)&stat_rdev, sizeof(zval *), NULL);
    1719           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[7], strlen(stat_sb_names[7])+1, (void *)&stat_size, sizeof(zval *), NULL);
    1720           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[8], strlen(stat_sb_names[8])+1, (void *)&stat_atime, sizeof(zval *), NULL);
    1721           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[9], strlen(stat_sb_names[9])+1, (void *)&stat_mtime, sizeof(zval *), NULL);
    1722           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[10], strlen(stat_sb_names[10])+1, (void *)&stat_ctime, sizeof(zval *), NULL);
    1723           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[11], strlen(stat_sb_names[11])+1, (void *)&stat_blksize, sizeof(zval *), NULL);
    1724           7 :         zend_hash_update(HASH_OF(return_value), stat_sb_names[12], strlen(stat_sb_names[12])+1, (void *)&stat_blocks, sizeof(zval *), NULL);
    1725             : }
    1726             : /* }}} */
    1727             : 
    1728             : /* {{{ proto bool copy(string source_file, string destination_file [, resource context])
    1729             :    Copy a file */
    1730         185 : PHP_FUNCTION(copy)
    1731             : {
    1732             :         char *source, *target;
    1733             :         int source_len, target_len;
    1734         185 :         zval *zcontext = NULL;
    1735             :         php_stream_context *context;
    1736             : 
    1737         185 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &source, &source_len, &target, &target_len, &zcontext) == FAILURE) {
    1738           3 :                 return;
    1739             :         }
    1740             : 
    1741         182 :         if (strlen(source) != source_len) {
    1742           0 :                 RETURN_FALSE;
    1743             :         }
    1744             : 
    1745         182 :         if (strlen(target) != target_len) {
    1746           1 :                 RETURN_FALSE;
    1747             :         }
    1748             : 
    1749         181 :         if (PG(safe_mode) &&(!php_checkuid(source, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
    1750           0 :                 RETURN_FALSE;
    1751             :         }
    1752             : 
    1753         181 :         if (php_check_open_basedir(source TSRMLS_CC)) {
    1754           1 :                 RETURN_FALSE;
    1755             :         }
    1756             : 
    1757         180 :         context = php_stream_context_from_zval(zcontext, 0);
    1758             : 
    1759         180 :         if (php_copy_file_ctx(source, target, 0, context TSRMLS_CC) == SUCCESS) {
    1760         152 :                 RETURN_TRUE;
    1761             :         } else {
    1762          28 :                 RETURN_FALSE;
    1763             :         }
    1764             : }
    1765             : /* }}} */
    1766             : 
    1767             : /* {{{ php_copy_file
    1768             :  */
    1769           0 : PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC)
    1770             : {
    1771           0 :         return php_copy_file_ctx(src, dest, ENFORCE_SAFE_MODE, NULL TSRMLS_CC);
    1772             : }
    1773             : /* }}} */
    1774             : 
    1775             : /* {{{ php_copy_file_ex
    1776             :  */
    1777           0 : PHPAPI int php_copy_file_ex(char *src, char *dest, int src_chk TSRMLS_DC)
    1778             : {
    1779           0 :         return php_copy_file_ctx(src, dest, ENFORCE_SAFE_MODE, NULL TSRMLS_CC);
    1780             : }
    1781             : /* }}} */
    1782             : 
    1783             : /* {{{ php_copy_file_ctx
    1784             :  */
    1785         180 : PHPAPI int php_copy_file_ctx(char *src, char *dest, int src_chk, php_stream_context *context TSRMLS_DC)
    1786             : {
    1787         180 :         php_stream *srcstream = NULL, *deststream = NULL;
    1788         180 :         int ret = FAILURE;
    1789             :         php_stream_statbuf src_s, dest_s;
    1790             : 
    1791         180 :         switch (php_stream_stat_path_ex(src, 0, &src_s, context)) {
    1792             :                 case -1:
    1793             :                         /* non-statable stream */
    1794           8 :                         goto safe_to_copy;
    1795             :                         break;
    1796             :                 case 0:
    1797         172 :                         break;
    1798             :                 default: /* failed to stat file, does not exist? */
    1799           0 :                         return ret;
    1800             :         }
    1801         172 :         if (S_ISDIR(src_s.sb.st_mode)) {
    1802           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The first argument to copy() function cannot be a directory");
    1803           2 :                 return FAILURE;
    1804             :         }
    1805             : 
    1806         170 :         switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, context)) {
    1807             :                 case -1:
    1808             :                         /* non-statable stream */
    1809         160 :                         goto safe_to_copy;
    1810             :                         break;
    1811             :                 case 0:
    1812          10 :                         break;
    1813             :                 default: /* failed to stat file, does not exist? */
    1814           0 :                         return ret;
    1815             :         }
    1816          10 :         if (S_ISDIR(dest_s.sb.st_mode)) {
    1817           5 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The second argument to copy() function cannot be a directory");
    1818           5 :                 return FAILURE;
    1819             :         }
    1820           5 :         if (!src_s.sb.st_ino || !dest_s.sb.st_ino) {
    1821             :                 goto no_stat;
    1822             :         }
    1823           5 :         if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) {
    1824           4 :                 return ret;
    1825             :         } else {
    1826             :                 goto safe_to_copy;
    1827             :         }
    1828             : no_stat:
    1829             :         {
    1830             :                 char *sp, *dp;
    1831             :                 int res;
    1832             : 
    1833           0 :                 if ((sp = expand_filepath(src, NULL TSRMLS_CC)) == NULL) {
    1834           0 :                         return ret;
    1835             :                 }
    1836           0 :                 if ((dp = expand_filepath(dest, NULL TSRMLS_CC)) == NULL) {
    1837           0 :                         efree(sp);
    1838           0 :                         goto safe_to_copy;
    1839             :                 }
    1840             : 
    1841           0 :                 res =
    1842             : #ifndef PHP_WIN32
    1843           0 :                         !strcmp(sp, dp);
    1844             : #else
    1845             :                         !strcasecmp(sp, dp);
    1846             : #endif
    1847             : 
    1848           0 :                 efree(sp);
    1849           0 :                 efree(dp);
    1850           0 :                 if (res) {
    1851           0 :                         return ret;
    1852             :                 }
    1853             :         }
    1854             : safe_to_copy:
    1855             : 
    1856         169 :         srcstream = php_stream_open_wrapper_ex(src, "rb", src_chk | REPORT_ERRORS, NULL, context);
    1857             : 
    1858         169 :         if (!srcstream) {
    1859           6 :                 return ret;
    1860             :         }
    1861             : 
    1862         163 :         deststream = php_stream_open_wrapper_ex(dest, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
    1863             : 
    1864         163 :         if (srcstream && deststream) {
    1865         152 :                 ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL);
    1866             :         }
    1867         163 :         if (srcstream) {
    1868         163 :                 php_stream_close(srcstream);
    1869             :         }
    1870         163 :         if (deststream) {
    1871         152 :                 php_stream_close(deststream);
    1872             :         }
    1873         163 :         return ret;
    1874             : }
    1875             : /* }}} */
    1876             : 
    1877             : /* {{{ proto string fread(resource fp, int length)
    1878             :    Binary-safe file read */
    1879      701898 : PHPAPI PHP_FUNCTION(fread)
    1880             : {
    1881             :         zval *arg1;
    1882             :         long len;
    1883             :         php_stream *stream;
    1884             : 
    1885      701898 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &arg1, &len) == FAILURE) {
    1886          13 :                 RETURN_FALSE;
    1887             :         }
    1888             : 
    1889      701885 :         PHP_STREAM_TO_ZVAL(stream, &arg1);
    1890             : 
    1891      701882 :         if (len <= 0) {
    1892           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
    1893           4 :                 RETURN_FALSE;
    1894             :         }
    1895             : 
    1896      701878 :         Z_STRVAL_P(return_value) = emalloc(len + 1);
    1897      701878 :         Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len);
    1898             : 
    1899             :         /* needed because recv/read/gzread doesnt put a null at the end*/
    1900      701878 :         Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
    1901             : 
    1902      701878 :         if (PG(magic_quotes_runtime)) {
    1903           0 :                 Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_P(return_value),
    1904             :                                 Z_STRLEN_P(return_value), &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
    1905             :         }
    1906      701878 :         Z_TYPE_P(return_value) = IS_STRING;
    1907             : }
    1908             : /* }}} */
    1909             : 
    1910        3986 : static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t len, const char delimiter TSRMLS_DC) /* {{{ */
    1911             : {
    1912             :         int inc_len;
    1913        3986 :         unsigned char last_chars[2] = { 0, 0 };
    1914             : 
    1915       55400 :         while (len > 0) {
    1916       47428 :                 inc_len = (*ptr == '\0' ? 1: php_mblen(ptr, len));
    1917       47428 :                 switch (inc_len) {
    1918             :                         case -2:
    1919             :                         case -1:
    1920           0 :                                 inc_len = 1;
    1921           0 :                                 php_mblen(NULL, 0);
    1922           0 :                                 break;
    1923             :                         case 0:
    1924           0 :                                 goto quit_loop;
    1925             :                         case 1:
    1926             :                         default:
    1927       47428 :                                 last_chars[0] = last_chars[1];
    1928       47428 :                                 last_chars[1] = *ptr;
    1929             :                                 break;
    1930             :                 }
    1931       47428 :                 ptr += inc_len;
    1932       47428 :                 len -= inc_len;
    1933             :         }
    1934             : quit_loop:
    1935        3986 :         switch (last_chars[1]) {
    1936             :                 case '\n':
    1937        1674 :                         if (last_chars[0] == '\r') {
    1938           2 :                                 return ptr - 2;
    1939             :                         }
    1940             :                         /* break is omitted intentionally */
    1941             :                 case '\r':
    1942        1672 :                         return ptr - 1;
    1943             :         }
    1944        2312 :         return ptr;
    1945             : }
    1946             : /* }}} */
    1947             : 
    1948             : #define FPUTCSV_FLD_CHK(c) memchr(Z_STRVAL(field), c, Z_STRLEN(field))
    1949             : 
    1950             : /* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [, string enclosure]])
    1951             :    Format line as CSV and write to file pointer */
    1952        1399 : PHP_FUNCTION(fputcsv)
    1953             : {
    1954        1399 :         char delimiter = ',';   /* allow this to be set as parameter */
    1955        1399 :         char enclosure = '"';      /* allow this to be set as parameter */
    1956        1399 :         const char escape_char = '\\';
    1957             :         php_stream *stream;
    1958             :         int ret;
    1959        1399 :         zval *fp = NULL, *fields = NULL, **field_tmp = NULL, field;
    1960        1399 :         char *delimiter_str = NULL, *enclosure_str = NULL;
    1961        1399 :         int delimiter_str_len = 0, enclosure_str_len = 0;
    1962             :         HashPosition pos;
    1963        1399 :         int count, i = 0;
    1964        1399 :         smart_str csvline = {0};
    1965             : 
    1966        1399 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra|ss",
    1967             :                         &fp, &fields, &delimiter_str, &delimiter_str_len,
    1968             :                         &enclosure_str, &enclosure_str_len) == FAILURE) {
    1969          14 :                 return;
    1970             :         }
    1971             : 
    1972        1385 :         if (delimiter_str != NULL) {
    1973             :                 /* Make sure that there is at least one character in string */
    1974        1219 :                 if (delimiter_str_len < 1) {
    1975         216 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
    1976         216 :                         RETURN_FALSE;
    1977        1003 :                 } else if (delimiter_str_len > 1) {
    1978         243 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "delimiter must be a single character");
    1979             :                 }
    1980             : 
    1981             :                 /* use first character from string */
    1982        1003 :                 delimiter = *delimiter_str;
    1983             :         }
    1984             : 
    1985        1169 :         if (enclosure_str != NULL) {
    1986         677 :                 if (enclosure_str_len < 1) {
    1987         108 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
    1988         108 :                         RETURN_FALSE;
    1989         569 :                 } else if (enclosure_str_len > 1) {
    1990         135 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
    1991             :                 }
    1992             :                 /* use first character from string */
    1993         569 :                 enclosure = *enclosure_str;
    1994             :         }
    1995             : 
    1996        1061 :         PHP_STREAM_TO_ZVAL(stream, &fp);
    1997             : 
    1998        1061 :         count = zend_hash_num_elements(Z_ARRVAL_P(fields));
    1999        1061 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(fields), &pos);
    2000        3261 :         while (zend_hash_get_current_data_ex(Z_ARRVAL_P(fields), (void **) &field_tmp, &pos) == SUCCESS) {
    2001        1139 :                 field = **field_tmp;
    2002             : 
    2003        1139 :                 if (Z_TYPE_PP(field_tmp) != IS_STRING) {
    2004          10 :                         zval_copy_ctor(&field);
    2005          10 :                         convert_to_string(&field);
    2006             :                 }
    2007             : 
    2008             :                 /* enclose a field that contains a delimiter, an enclosure character, or a newline */
    2009        5481 :                 if (FPUTCSV_FLD_CHK(delimiter) ||
    2010         791 :                         FPUTCSV_FLD_CHK(enclosure) ||
    2011         602 :                         FPUTCSV_FLD_CHK(escape_char) ||
    2012         590 :                         FPUTCSV_FLD_CHK('\n') ||
    2013         578 :                         FPUTCSV_FLD_CHK('\r') ||
    2014         578 :                         FPUTCSV_FLD_CHK('\t') ||
    2015        1156 :                         FPUTCSV_FLD_CHK(' ')
    2016        1156 :                 ) {
    2017         625 :                         char *ch = Z_STRVAL(field);
    2018         625 :                         char *end = ch + Z_STRLEN(field);
    2019         625 :                         int escaped = 0;
    2020             : 
    2021         625 :                         smart_str_appendc(&csvline, enclosure);
    2022       11237 :                         while (ch < end) {
    2023        9987 :                                 if (*ch == escape_char) {
    2024          16 :                                         escaped = 1;
    2025       11558 :                                 } else if (!escaped && *ch == enclosure) {
    2026        1587 :                                         smart_str_appendc(&csvline, enclosure);
    2027             :                                 } else {
    2028        8384 :                                         escaped = 0;
    2029             :                                 }
    2030        9987 :                                 smart_str_appendc(&csvline, *ch);
    2031        9987 :                                 ch++;
    2032             :                         }
    2033         625 :                         smart_str_appendc(&csvline, enclosure);
    2034             :                 } else {
    2035         514 :                         smart_str_appendl(&csvline, Z_STRVAL(field), Z_STRLEN(field));
    2036             :                 }
    2037             : 
    2038        1139 :                 if (++i != count) {
    2039          78 :                         smart_str_appendl(&csvline, &delimiter, 1);
    2040             :                 }
    2041        1139 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(fields), &pos);
    2042             : 
    2043        1139 :                 if (Z_TYPE_PP(field_tmp) != IS_STRING) {
    2044          10 :                         zval_dtor(&field);
    2045             :                 }
    2046             :         }
    2047             : 
    2048        1061 :         smart_str_appendc(&csvline, '\n');
    2049        1061 :         smart_str_0(&csvline);
    2050             : 
    2051        1061 :         if (!PG(magic_quotes_runtime)) {
    2052        1061 :                 ret = php_stream_write(stream, csvline.c, csvline.len);
    2053             :         } else {
    2054           0 :                 char *buffer = estrndup(csvline.c, csvline.len);
    2055           0 :                 int len = csvline.len;
    2056           0 :                 php_stripslashes(buffer, &len TSRMLS_CC);
    2057           0 :                 ret = php_stream_write(stream, buffer, len);
    2058           0 :                 efree(buffer);
    2059             :         }
    2060             : 
    2061        1061 :         smart_str_free(&csvline);
    2062             : 
    2063        1061 :         RETURN_LONG(ret);
    2064             : }
    2065             : /* }}} */
    2066             : 
    2067             : /* {{{ proto array fgetcsv(resource fp [,int length [, string delimiter [, string enclosure [, string escape]]]])
    2068             :    Get line from file pointer and parse for CSV fields */
    2069        3014 : PHP_FUNCTION(fgetcsv)
    2070             : {
    2071        3014 :         char delimiter = ',';   /* allow this to be set as parameter */
    2072        3014 :         char enclosure = '"';      /* allow this to be set as parameter */
    2073        3014 :         char escape = '\\';
    2074             : 
    2075             :         /* first section exactly as php_fgetss */
    2076             : 
    2077        3014 :         long len = 0;
    2078             :         size_t buf_len;
    2079             :         char *buf;
    2080             :         php_stream *stream;
    2081             : 
    2082             :         {
    2083        3014 :                 zval *fd, **len_zv = NULL;
    2084        3014 :                 char *delimiter_str = NULL;
    2085        3014 :                 int delimiter_str_len = 0;
    2086        3014 :                 char *enclosure_str = NULL;
    2087        3014 :                 int enclosure_str_len = 0;
    2088        3014 :                 char *escape_str = NULL;
    2089        3014 :                 int escape_str_len = 0;
    2090             : 
    2091        3014 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|Zsss",
    2092             :                         &fd, &len_zv, &delimiter_str, &delimiter_str_len,
    2093             :                         &enclosure_str, &enclosure_str_len,
    2094             :                         &escape_str, &escape_str_len) == FAILURE
    2095             :                 ) {
    2096          14 :                         return;
    2097             :                 }
    2098             : 
    2099        3000 :                 if (delimiter_str != NULL) {
    2100             :                         /* Make sure that there is at least one character in string */
    2101        2375 :                         if (delimiter_str_len < 1) {
    2102         240 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
    2103         240 :                                 RETURN_FALSE;
    2104        2135 :                         } else if (delimiter_str_len > 1) {
    2105         195 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "delimiter must be a single character");
    2106             :                         }
    2107             : 
    2108             :                         /* use first character from string */
    2109        2135 :                         delimiter = delimiter_str[0];
    2110             :                 }
    2111             : 
    2112        2760 :                 if (enclosure_str != NULL) {
    2113        1469 :                         if (enclosure_str_len < 1) {
    2114         120 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
    2115         120 :                                 RETURN_FALSE;
    2116        1349 :                         } else if (enclosure_str_len > 1) {
    2117         120 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
    2118             :                         }
    2119             : 
    2120             :                         /* use first character from string */
    2121        1349 :                         enclosure = enclosure_str[0];
    2122             :                 }
    2123             : 
    2124        2640 :                 if (escape_str != NULL) {
    2125           1 :                         if (escape_str_len < 1) {
    2126           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "escape must be character");
    2127           0 :                                 RETURN_FALSE;
    2128           1 :                         } else if (escape_str_len > 1) {
    2129           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "escape must be a single character");
    2130             :                         }
    2131             : 
    2132           1 :                         escape = escape_str[0];
    2133             :                 }
    2134             : 
    2135        4564 :                 if (len_zv != NULL && Z_TYPE_PP(len_zv) != IS_NULL) {
    2136        2284 :                         convert_to_long_ex(len_zv);
    2137        2284 :                         len = Z_LVAL_PP(len_zv);
    2138        2284 :                         if (len < 0) {
    2139         360 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter may not be negative");
    2140         360 :                                 RETURN_FALSE;
    2141        1924 :                         } else if (len == 0) {
    2142         195 :                                 len = -1;
    2143             :                         }
    2144             :                 } else {
    2145         356 :                         len = -1;
    2146             :                 }
    2147             : 
    2148        2280 :                 PHP_STREAM_TO_ZVAL(stream, &fd);
    2149             :         }
    2150             : 
    2151        2280 :         if (len < 0) {
    2152         551 :                 if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
    2153         198 :                         RETURN_FALSE;
    2154             :                 }
    2155             :         } else {
    2156        1729 :                 buf = emalloc(len + 1);
    2157        1729 :                 if (php_stream_get_line(stream, buf, len + 1, &buf_len) == NULL) {
    2158         453 :                         efree(buf);
    2159         453 :                         RETURN_FALSE;
    2160             :                 }
    2161             :         }
    2162             : 
    2163        1629 :         php_fgetcsv(stream, delimiter, enclosure, escape, buf_len, buf, return_value TSRMLS_CC);
    2164             : }
    2165             : /* }}} */
    2166             : 
    2167        1668 : PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, char escape_char, size_t buf_len, char *buf, zval *return_value TSRMLS_DC) /* {{{ */
    2168             : {
    2169             :         char *temp, *tptr, *bptr, *line_end, *limit;
    2170             :         size_t temp_len, line_end_len;
    2171             :         int inc_len;
    2172        1668 :         zend_bool first_field = 1;
    2173             : 
    2174             :         /* initialize internal state */
    2175        1668 :         php_mblen(NULL, 0);
    2176             : 
    2177             :         /* Now into new section that parses buf for delimiter/enclosure fields */
    2178             : 
    2179             :         /* Strip trailing space from buf, saving end of line in case required for enclosure field */
    2180             : 
    2181        1668 :         bptr = buf;
    2182        1668 :         tptr = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter TSRMLS_CC);
    2183        1668 :         line_end_len = buf_len - (size_t)(tptr - buf);
    2184        1668 :         line_end = limit = tptr;
    2185             : 
    2186             :         /* reserve workspace for building each individual field */
    2187        1668 :         temp_len = buf_len;
    2188        1668 :         temp = emalloc(temp_len + line_end_len + 1);
    2189             : 
    2190             :         /* Initialize return array */
    2191        1668 :         array_init(return_value);
    2192             : 
    2193             :         /* Main loop to read CSV fields */
    2194             :         /* NB this routine will return a single null entry for a blank line */
    2195             : 
    2196             :         do {
    2197             :                 char *comp_end, *hunk_begin;
    2198             : 
    2199        3379 :                 tptr = temp;
    2200        3379 :                 inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, limit - bptr)): 0);
    2201        3379 :                 if (inc_len == 1) {
    2202        3205 :                         char *tmp = bptr;
    2203        6430 :                         while ((*tmp != delimiter) && isspace((int)*(unsigned char *)tmp)) {
    2204          20 :                                 tmp++;
    2205             :                         }
    2206        3205 :                         if (*tmp == enclosure) {
    2207        1261 :                                 bptr = tmp;
    2208             :                         }
    2209             :                 }
    2210             : 
    2211        3379 :                 if (first_field && bptr == line_end) {
    2212          32 :                         add_next_index_null(return_value);
    2213          32 :                         break;
    2214             :                 }
    2215        3347 :                 first_field = 0;
    2216             :                 /* 2. Read field, leaving bptr pointing at start of next field */
    2217        4608 :                 if (inc_len != 0 && *bptr == enclosure) {
    2218        1261 :                         int state = 0;
    2219             : 
    2220        1261 :                         bptr++; /* move on to first character in field */
    2221        1261 :                         hunk_begin = bptr;
    2222             : 
    2223             :                         /* 2A. handle enclosure delimited field */
    2224             :                         for (;;) {
    2225       10919 :                                 switch (inc_len) {
    2226             :                                         case 0:
    2227         590 :                                                 switch (state) {
    2228             :                                                         case 2:
    2229         309 :                                                                 memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
    2230         309 :                                                                 tptr += (bptr - hunk_begin - 1);
    2231         309 :                                                                 hunk_begin = bptr;
    2232         309 :                                                                 goto quit_loop_2;
    2233             : 
    2234             :                                                         case 1:
    2235           0 :                                                                 memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2236           0 :                                                                 tptr += (bptr - hunk_begin);
    2237           0 :                                                                 hunk_begin = bptr;
    2238             :                                                                 /* break is omitted intentionally */
    2239             : 
    2240             :                                                         case 0: {
    2241             :                                                                 char *new_buf;
    2242             :                                                                 size_t new_len;
    2243             :                                                                 char *new_temp;
    2244             : 
    2245         281 :                                                                 if (hunk_begin != line_end) {
    2246         113 :                                                                         memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2247         113 :                                                                         tptr += (bptr - hunk_begin);
    2248         113 :                                                                         hunk_begin = bptr;
    2249             :                                                                 }
    2250             : 
    2251             :                                                                 /* add the embedded line end to the field */
    2252         281 :                                                                 memcpy(tptr, line_end, line_end_len);
    2253         281 :                                                                 tptr += line_end_len;
    2254             : 
    2255         281 :                                                                 if (stream == NULL) {
    2256           0 :                                                                         goto quit_loop_2;
    2257         281 :                                                                 } else if ((new_buf = php_stream_get_line(stream, NULL, 0, &new_len)) == NULL) {
    2258             :                                                                         /* we've got an unterminated enclosure,
    2259             :                                                                          * assign all the data from the start of
    2260             :                                                                          * the enclosure to end of data to the
    2261             :                                                                          * last element */
    2262          49 :                                                                         if ((size_t)temp_len > (size_t)(limit - buf)) {
    2263          49 :                                                                                 goto quit_loop_2;
    2264             :                                                                         }
    2265           0 :                                                                         zval_dtor(return_value);
    2266           0 :                                                                         RETVAL_FALSE;
    2267           0 :                                                                         goto out;
    2268             :                                                                 }
    2269         232 :                                                                 temp_len += new_len;
    2270         232 :                                                                 new_temp = erealloc(temp, temp_len);
    2271         232 :                                                                 tptr = new_temp + (size_t)(tptr - temp);
    2272         232 :                                                                 temp = new_temp;
    2273             : 
    2274         232 :                                                                 efree(buf);
    2275         232 :                                                                 buf_len = new_len;
    2276         232 :                                                                 bptr = buf = new_buf;
    2277         232 :                                                                 hunk_begin = buf;
    2278             : 
    2279         232 :                                                                 line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len, delimiter TSRMLS_CC);
    2280         232 :                                                                 line_end_len = buf_len - (size_t)(limit - buf);
    2281             : 
    2282         232 :                                                                 state = 0;
    2283             :                                                         } break;
    2284             :                                                 }
    2285         232 :                                                 break;
    2286             : 
    2287             :                                         case -2:
    2288             :                                         case -1:
    2289           0 :                                                 php_mblen(NULL, 0);
    2290             :                                                 /* break is omitted intentionally */
    2291             :                                         case 1:
    2292             :                                                 /* we need to determine if the enclosure is
    2293             :                                                  * 'real' or is it escaped */
    2294       10329 :                                                 switch (state) {
    2295             :                                                         case 1: /* escaped */
    2296          29 :                                                                 bptr++;
    2297          29 :                                                                 state = 0;
    2298          29 :                                                                 break;
    2299             :                                                         case 2: /* embedded enclosure ? let's check it */
    2300        1188 :                                                                 if (*bptr != enclosure) {
    2301             :                                                                         /* real enclosure */
    2302         903 :                                                                         memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
    2303         903 :                                                                         tptr += (bptr - hunk_begin - 1);
    2304         903 :                                                                         hunk_begin = bptr;
    2305         903 :                                                                         goto quit_loop_2;
    2306             :                                                                 }
    2307         285 :                                                                 memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2308         285 :                                                                 tptr += (bptr - hunk_begin);
    2309         285 :                                                                 bptr++;
    2310         285 :                                                                 hunk_begin = bptr;
    2311         285 :                                                                 state = 0;
    2312         285 :                                                                 break;
    2313             :                                                         default:
    2314        9112 :                                                                 if (*bptr == enclosure) {
    2315        1497 :                                                                         state = 2;
    2316        7615 :                                                                 } else if (*bptr == escape_char) {
    2317          29 :                                                                         state = 1;
    2318             :                                                                 }
    2319        9112 :                                                                 bptr++;
    2320             :                                                                 break;
    2321             :                                                 }
    2322        9426 :                                                 break;
    2323             : 
    2324             :                                         default:
    2325           0 :                                                 switch (state) {
    2326             :                                                         case 2:
    2327             :                                                                 /* real enclosure */
    2328           0 :                                                                 memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
    2329           0 :                                                                 tptr += (bptr - hunk_begin - 1);
    2330           0 :                                                                 hunk_begin = bptr;
    2331           0 :                                                                 goto quit_loop_2;
    2332             :                                                         case 1:
    2333           0 :                                                                 bptr += inc_len;
    2334           0 :                                                                 memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2335           0 :                                                                 tptr += (bptr - hunk_begin);
    2336           0 :                                                                 hunk_begin = bptr;
    2337           0 :                                                                 break;
    2338             :                                                         default:
    2339           0 :                                                                 bptr += inc_len;
    2340             :                                                                 break;
    2341             :                                                 }
    2342             :                                                 break;
    2343             :                                 }
    2344        9658 :                                 inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, limit - bptr)): 0);
    2345        9658 :                         }
    2346             : 
    2347             :                 quit_loop_2:
    2348             :                         /* look up for a delimiter */
    2349             :                         for (;;) {
    2350        3454 :                                 switch (inc_len) {
    2351             :                                         case 0:
    2352         584 :                                                 goto quit_loop_3;
    2353             : 
    2354             :                                         case -2:
    2355             :                                         case -1:
    2356           0 :                                                 inc_len = 1;
    2357           0 :                                                 php_mblen(NULL, 0);
    2358             :                                                 /* break is omitted intentionally */
    2359             :                                         case 1:
    2360        2870 :                                                 if (*bptr == delimiter) {
    2361         677 :                                                         goto quit_loop_3;
    2362             :                                                 }
    2363             :                                                 break;
    2364             :                                         default:
    2365             :                                                 break;
    2366             :                                 }
    2367        2193 :                                 bptr += inc_len;
    2368        2193 :                                 inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, limit - bptr)): 0);
    2369        2193 :                         }
    2370             : 
    2371             :                 quit_loop_3:
    2372        1261 :                         memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2373        1261 :                         tptr += (bptr - hunk_begin);
    2374        1261 :                         bptr += inc_len;
    2375        1261 :                         comp_end = tptr;
    2376             :                 } else {
    2377             :                         /* 2B. Handle non-enclosure field */
    2378             : 
    2379        2086 :                         hunk_begin = bptr;
    2380             : 
    2381             :                         for (;;) {
    2382       17700 :                                 switch (inc_len) {
    2383             :                                         case 0:
    2384        1052 :                                                 goto quit_loop_4;
    2385             :                                         case -2:
    2386             :                                         case -1:
    2387           0 :                                                 inc_len = 1;
    2388           0 :                                                 php_mblen(NULL, 0);
    2389             :                                                 /* break is omitted intentionally */
    2390             :                                         case 1:
    2391       16648 :                                                 if (*bptr == delimiter) {
    2392        1034 :                                                         goto quit_loop_4;
    2393             :                                                 }
    2394             :                                                 break;
    2395             :                                         default:
    2396             :                                                 break;
    2397             :                                 }
    2398       15614 :                                 bptr += inc_len;
    2399       15614 :                                 inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, limit - bptr)): 0);
    2400       15614 :                         }
    2401             :                 quit_loop_4:
    2402        2086 :                         memcpy(tptr, hunk_begin, bptr - hunk_begin);
    2403        2086 :                         tptr += (bptr - hunk_begin);
    2404             : 
    2405        2086 :                         comp_end = (char *)php_fgetcsv_lookup_trailing_spaces(temp, tptr - temp, delimiter TSRMLS_CC);
    2406        2086 :                         if (*bptr == delimiter) {
    2407        1034 :                                 bptr++;
    2408             :                         }
    2409             :                 }
    2410             : 
    2411             :                 /* 3. Now pass our field back to php */
    2412        3347 :                 *comp_end = '\0';
    2413        3347 :                 add_next_index_stringl(return_value, temp, comp_end - temp, 1);
    2414        3347 :         } while (inc_len > 0);
    2415             : 
    2416             : out:
    2417        1668 :         efree(temp);
    2418        1668 :         if (stream) {
    2419        1652 :                 efree(buf);
    2420             :         }
    2421        1668 : }
    2422             : /* }}} */
    2423             : 
    2424             : #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
    2425             : /* {{{ proto string realpath(string path)
    2426             :    Return the resolved path */
    2427       25349 : PHP_FUNCTION(realpath)
    2428             : {
    2429             :         char *filename;
    2430             :         int filename_len;
    2431             :         char resolved_path_buff[MAXPATHLEN];
    2432             : 
    2433       25349 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
    2434           2 :                 return;
    2435             :         }
    2436             : 
    2437       25347 :         if (strlen(filename) != filename_len) {
    2438           0 :                 RETURN_FALSE;
    2439             :         }
    2440             : 
    2441       25347 :         if (VCWD_REALPATH(filename, resolved_path_buff)) {
    2442       25322 :                 if (PG(safe_mode) && (!php_checkuid(resolved_path_buff, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
    2443           0 :                         RETURN_FALSE;
    2444             :                 }
    2445             : 
    2446       25322 :                 if (php_check_open_basedir(resolved_path_buff TSRMLS_CC)) {
    2447           0 :                         RETURN_FALSE;
    2448             :                 }
    2449             : 
    2450             : #ifdef ZTS
    2451             :                 if (VCWD_ACCESS(resolved_path_buff, F_OK)) {
    2452             :                         RETURN_FALSE;
    2453             :                 }
    2454             : #endif
    2455       25322 :                 RETURN_STRING(resolved_path_buff, 1);
    2456             :         } else {
    2457          25 :                 RETURN_FALSE;
    2458             :         }
    2459             : }
    2460             : /* }}} */
    2461             : #endif
    2462             : 
    2463             : /* See http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 */
    2464             : #define PHP_META_HTML401_CHARS "-_.:"
    2465             : 
    2466             : /* {{{ php_next_meta_token
    2467             :    Tokenizes an HTML file for get_meta_tags */
    2468         210 : php_meta_tags_token php_next_meta_token(php_meta_tags_data *md TSRMLS_DC)
    2469             : {
    2470         210 :         int ch = 0, compliment;
    2471             :         char buff[META_DEF_BUFSIZE + 1];
    2472             : 
    2473         210 :         memset((void *)buff, 0, META_DEF_BUFSIZE + 1);
    2474             : 
    2475         432 :         while (md->ulc || (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)))) {
    2476         222 :                 if (php_stream_eof(md->stream)) {
    2477           6 :                         break;
    2478             :                 }
    2479             : 
    2480         216 :                 if (md->ulc) {
    2481          43 :                         ch = md->lc;
    2482          43 :                         md->ulc = 0;
    2483             :                 }
    2484             : 
    2485         216 :                 switch (ch) {
    2486             :                         case '<':
    2487          25 :                                 return TOK_OPENTAG;
    2488             :                                 break;
    2489             : 
    2490             :                         case '>':
    2491          13 :                                 return TOK_CLOSETAG;
    2492             :                                 break;
    2493             : 
    2494             :                         case '=':
    2495          26 :                                 return TOK_EQUAL;
    2496             :                                 break;
    2497             :                         case '/':
    2498           2 :                                 return TOK_SLASH;
    2499             :                                 break;
    2500             : 
    2501             :                         case '\'':
    2502             :                         case '"':
    2503          26 :                                 compliment = ch;
    2504          26 :                                 md->token_len = 0;
    2505         311 :                                 while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && ch != compliment && ch != '<' && ch != '>') {
    2506         259 :                                         buff[(md->token_len)++] = ch;
    2507             : 
    2508         259 :                                         if (md->token_len == META_DEF_BUFSIZE) {
    2509           0 :                                                 break;
    2510             :                                         }
    2511             :                                 }
    2512             : 
    2513          26 :                                 if (ch == '<' || ch == '>') {
    2514             :                                         /* Was just an apostrohpe */
    2515           0 :                                         md->ulc = 1;
    2516           0 :                                         md->lc = ch;
    2517             :                                 }
    2518             : 
    2519             :                                 /* We don't need to alloc unless we are in a meta tag */
    2520          26 :                                 if (md->in_meta) {
    2521          26 :                                         md->token_data = (char *) emalloc(md->token_len + 1);
    2522          26 :                                         memcpy(md->token_data, buff, md->token_len+1);
    2523             :                                 }
    2524             : 
    2525          26 :                                 return TOK_STRING;
    2526             :                                 break;
    2527             : 
    2528             :                         case '\n':
    2529             :                         case '\r':
    2530             :                         case '\t':
    2531          12 :                                 break;
    2532             : 
    2533             :                         case ' ':
    2534          67 :                                 return TOK_SPACE;
    2535             :                                 break;
    2536             : 
    2537             :                         default:
    2538          45 :                                 if (isalnum(ch)) {
    2539          45 :                                         md->token_len = 0;
    2540          45 :                                         buff[(md->token_len)++] = ch;
    2541         264 :                                         while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && (isalnum(ch) || strchr(PHP_META_HTML401_CHARS, ch))) {
    2542         174 :                                                 buff[(md->token_len)++] = ch;
    2543             : 
    2544         174 :                                                 if (md->token_len == META_DEF_BUFSIZE) {
    2545           0 :                                                         break;
    2546             :                                                 }
    2547             :                                         }
    2548             : 
    2549             :                                         /* This is ugly, but we have to replace ungetc */
    2550          45 :                                         if (!isalpha(ch) && ch != '-') {
    2551          45 :                                                 md->ulc = 1;
    2552          45 :                                                 md->lc = ch;
    2553             :                                         }
    2554             : 
    2555          45 :                                         md->token_data = (char *) emalloc(md->token_len + 1);
    2556          45 :                                         memcpy(md->token_data, buff, md->token_len+1);
    2557             : 
    2558          45 :                                         return TOK_ID;
    2559             :                                 } else {
    2560           0 :                                         return TOK_OTHER;
    2561             :                                 }
    2562             :                                 break;
    2563             :                 }
    2564             :         }
    2565             : 
    2566           6 :         return TOK_EOF;
    2567             : }
    2568             : /* }}} */
    2569             : 
    2570             : #ifdef HAVE_FNMATCH
    2571             : /* {{{ proto bool fnmatch(string pattern, string filename [, int flags])
    2572             :    Match filename against pattern */
    2573         254 : PHP_FUNCTION(fnmatch)
    2574             : {
    2575             :         char *pattern, *filename;
    2576             :         int pattern_len, filename_len;
    2577         254 :         long flags = 0;
    2578             : 
    2579         254 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &pattern, &pattern_len, &filename, &filename_len, &flags) == FAILURE) {
    2580           6 :                 return;
    2581             :         }
    2582             : 
    2583         248 :         if (strlen(pattern) != pattern_len) {
    2584          20 :                 RETURN_FALSE;
    2585             :         }
    2586             : 
    2587         228 :         if (strlen(filename) != filename_len) {
    2588          13 :                 RETURN_FALSE;
    2589             :         }
    2590             :         
    2591         215 :         if (filename_len >= MAXPATHLEN) {
    2592           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
    2593           0 :                 RETURN_FALSE;
    2594             :         }
    2595         215 :         if (pattern_len >= MAXPATHLEN) {
    2596           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
    2597           0 :                 RETURN_FALSE;
    2598             :         }
    2599             : 
    2600         215 :         RETURN_BOOL( ! fnmatch( pattern, filename, flags ));
    2601             : }
    2602             : /* }}} */
    2603             : #endif
    2604             : 
    2605             : /* {{{ proto string sys_get_temp_dir()
    2606             :    Returns directory path used for temporary files */
    2607         585 : PHP_FUNCTION(sys_get_temp_dir)
    2608             : {
    2609         585 :         if (zend_parse_parameters_none() == FAILURE) {
    2610           0 :                 return;
    2611             :         }
    2612         585 :         RETURN_STRING((char *)php_get_temporary_directory(), 1);
    2613             : }
    2614             : /* }}} */
    2615             : 
    2616             : /*
    2617             :  * Local variables:
    2618             :  * tab-width: 4
    2619             :  * c-basic-offset: 4
    2620             :  * End:
    2621             :  * vim600: noet sw=4 ts=4 fdm=marker
    2622             :  * vim<600: noet sw=4 ts=4
    2623             :  */

Generated by: LCOV version 1.10

Generated at Sun, 13 Jul 2014 23:57:58 +0000 (9 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.