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

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:21 +0000 (6 days ago)

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