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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:59 +0000 (7 days ago)

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