Coverage Report

Created: 2026-06-02 06:36

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