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