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: 973 1099 88.5 %
Date: 2014-07-21 Functions: 50 52 96.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:21 +0000 (3 days ago)

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