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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:18 +0000 (6 days ago)

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