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: 960 1084 88.6 %
Date: 2014-10-30 Functions: 50 52 96.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:42 +0000 (6 hours ago)

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