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