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: 958 1081 88.6 %
Date: 2014-11-25 Functions: 50 52 96.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 25 Nov 2014 09:11:06 +0000 (3 days ago)

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