xref: /PHP-7.2/ext/spl/spl_directory.c (revision a5a15965)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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    | Author: Marcus Boerger <helly@php.net>                               |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "ext/standard/file.h"
29 #include "ext/standard/php_string.h"
30 #include "zend_compile.h"
31 #include "zend_exceptions.h"
32 #include "zend_interfaces.h"
33 
34 #include "php_spl.h"
35 #include "spl_functions.h"
36 #include "spl_engine.h"
37 #include "spl_iterators.h"
38 #include "spl_directory.h"
39 #include "spl_exceptions.h"
40 
41 #include "php.h"
42 #include "fopen_wrappers.h"
43 
44 #include "ext/standard/basic_functions.h"
45 #include "ext/standard/php_filestat.h"
46 
47 #define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
48 
49 /* declare the class handlers */
50 static zend_object_handlers spl_filesystem_object_handlers;
51 /* includes handler to validate object state when retrieving methods */
52 static zend_object_handlers spl_filesystem_object_check_handlers;
53 
54 /* decalre the class entry */
55 PHPAPI zend_class_entry *spl_ce_SplFileInfo;
56 PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
57 PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
58 PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
59 PHPAPI zend_class_entry *spl_ce_GlobIterator;
60 PHPAPI zend_class_entry *spl_ce_SplFileObject;
61 PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
62 
spl_filesystem_file_free_line(spl_filesystem_object * intern)63 static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
64 {
65 	if (intern->u.file.current_line) {
66 		efree(intern->u.file.current_line);
67 		intern->u.file.current_line = NULL;
68 	}
69 	if (!Z_ISUNDEF(intern->u.file.current_zval)) {
70 		zval_ptr_dtor(&intern->u.file.current_zval);
71 		ZVAL_UNDEF(&intern->u.file.current_zval);
72 	}
73 } /* }}} */
74 
spl_filesystem_object_destroy_object(zend_object * object)75 static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
76 {
77 	spl_filesystem_object *intern = spl_filesystem_from_obj(object);
78 
79 	zend_objects_destroy_object(object);
80 
81 	switch(intern->type) {
82 	case SPL_FS_DIR:
83 		if (intern->u.dir.dirp) {
84 			php_stream_close(intern->u.dir.dirp);
85 			intern->u.dir.dirp = NULL;
86 		}
87 		break;
88 	case SPL_FS_FILE:
89 		if (intern->u.file.stream) {
90 			/*
91 			if (intern->u.file.zcontext) {
92 			   zend_list_delref(Z_RESVAL_P(intern->zcontext));
93 			}
94 			*/
95 			if (!intern->u.file.stream->is_persistent) {
96 				php_stream_close(intern->u.file.stream);
97 			} else {
98 				php_stream_pclose(intern->u.file.stream);
99 			}
100 			intern->u.file.stream = NULL;
101 		}
102 		break;
103 	}
104 } /* }}} */
105 
spl_filesystem_object_free_storage(zend_object * object)106 static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
107 {
108 	spl_filesystem_object *intern = spl_filesystem_from_obj(object);
109 
110 	if (intern->oth_handler && intern->oth_handler->dtor) {
111 		intern->oth_handler->dtor(intern);
112 	}
113 
114 	zend_object_std_dtor(&intern->std);
115 
116 	if (intern->_path) {
117 		efree(intern->_path);
118 	}
119 	if (intern->file_name) {
120 		efree(intern->file_name);
121 	}
122 	switch(intern->type) {
123 	case SPL_FS_INFO:
124 		break;
125 	case SPL_FS_DIR:
126 		if (intern->u.dir.sub_path) {
127 			efree(intern->u.dir.sub_path);
128 		}
129 		break;
130 	case SPL_FS_FILE:
131 		if (intern->u.file.open_mode) {
132 			efree(intern->u.file.open_mode);
133 		}
134 		if (intern->orig_path) {
135 			efree(intern->orig_path);
136 		}
137 		spl_filesystem_file_free_line(intern);
138 		break;
139 	}
140 } /* }}} */
141 
142 /* {{{ spl_ce_dir_object_new */
143 /* creates the object by
144    - allocating memory
145    - initializing the object members
146    - storing the object
147    - setting it's handlers
148 
149    called from
150    - clone
151    - new
152  */
spl_filesystem_object_new_ex(zend_class_entry * class_type)153 static zend_object *spl_filesystem_object_new_ex(zend_class_entry *class_type)
154 {
155 	spl_filesystem_object *intern;
156 
157 	intern = ecalloc(1, sizeof(spl_filesystem_object) + zend_object_properties_size(class_type));
158 	/* intern->type = SPL_FS_INFO; done by set 0 */
159 	intern->file_class = spl_ce_SplFileObject;
160 	intern->info_class = spl_ce_SplFileInfo;
161 
162 	zend_object_std_init(&intern->std, class_type);
163 	object_properties_init(&intern->std, class_type);
164 	intern->std.handlers = &spl_filesystem_object_handlers;
165 
166 	return &intern->std;
167 }
168 /* }}} */
169 
170 /* {{{ spl_filesystem_object_new */
171 /* See spl_filesystem_object_new_ex */
spl_filesystem_object_new(zend_class_entry * class_type)172 static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
173 {
174 	return spl_filesystem_object_new_ex(class_type);
175 }
176 /* }}} */
177 
178 /* {{{ spl_filesystem_object_new_check */
spl_filesystem_object_new_check(zend_class_entry * class_type)179 static zend_object *spl_filesystem_object_new_check(zend_class_entry *class_type)
180 {
181 	spl_filesystem_object *ret = spl_filesystem_from_obj(spl_filesystem_object_new_ex(class_type));
182 	ret->std.handlers = &spl_filesystem_object_check_handlers;
183 	return &ret->std;
184 }
185 /* }}} */
186 
spl_filesystem_object_get_path(spl_filesystem_object * intern,size_t * len)187 PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_t *len) /* {{{ */
188 {
189 #ifdef HAVE_GLOB
190 	if (intern->type == SPL_FS_DIR) {
191 		if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
192 			return php_glob_stream_get_path(intern->u.dir.dirp, 0, len);
193 		}
194 	}
195 #endif
196 	if (len) {
197 		*len = intern->_path_len;
198 	}
199 	return intern->_path;
200 } /* }}} */
201 
spl_filesystem_object_get_file_name(spl_filesystem_object * intern)202 static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
203 {
204 	char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
205 
206 	switch (intern->type) {
207 		case SPL_FS_INFO:
208 		case SPL_FS_FILE:
209 			if (!intern->file_name) {
210 				php_error_docref(NULL, E_ERROR, "Object not initialized");
211 			}
212 			break;
213 		case SPL_FS_DIR:
214 			{
215 				size_t path_len = 0;
216 				char *path = spl_filesystem_object_get_path(intern, &path_len);
217 				if (intern->file_name) {
218 					efree(intern->file_name);
219 				}
220 				/* if there is parent path, ammend it, otherwise just use the given path as is */
221 				if (path_len == 0) {
222 					intern->file_name_len = spprintf(
223 						&intern->file_name, 0, "%s", intern->u.dir.entry.d_name);
224 				} else {
225 					intern->file_name_len = spprintf(
226 						&intern->file_name, 0, "%s%c%s", path, slash, intern->u.dir.entry.d_name);
227 				}
228 			}
229 			break;
230 	}
231 } /* }}} */
232 
spl_filesystem_dir_read(spl_filesystem_object * intern)233 static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
234 {
235 	if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
236 		intern->u.dir.entry.d_name[0] = '\0';
237 		return 0;
238 	} else {
239 		return 1;
240 	}
241 }
242 /* }}} */
243 
244 #define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
245 
spl_filesystem_is_dot(const char * d_name)246 static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
247 {
248 	return !strcmp(d_name, ".") || !strcmp(d_name, "..");
249 }
250 /* }}} */
251 
252 /* {{{ spl_filesystem_dir_open */
253 /* open a directory resource */
spl_filesystem_dir_open(spl_filesystem_object * intern,char * path)254 static void spl_filesystem_dir_open(spl_filesystem_object* intern, char *path)
255 {
256 	int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
257 
258 	intern->type = SPL_FS_DIR;
259 	intern->_path_len = strlen(path);
260 	intern->u.dir.dirp = php_stream_opendir(path, REPORT_ERRORS, FG(default_context));
261 
262 	if (intern->_path_len > 1 && IS_SLASH_AT(path, intern->_path_len-1)) {
263 		intern->_path = estrndup(path, --intern->_path_len);
264 	} else {
265 		intern->_path = estrndup(path, intern->_path_len);
266 	}
267 	intern->u.dir.index = 0;
268 
269 	if (EG(exception) || intern->u.dir.dirp == NULL) {
270 		intern->u.dir.entry.d_name[0] = '\0';
271 		if (!EG(exception)) {
272 			/* open failed w/out notice (turned to exception due to EH_THROW) */
273 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
274 				"Failed to open directory \"%s\"", path);
275 		}
276 	} else {
277 		do {
278 			spl_filesystem_dir_read(intern);
279 		} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
280 	}
281 }
282 /* }}} */
283 
spl_filesystem_file_open(spl_filesystem_object * intern,int use_include_path,int silent)284 static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_include_path, int silent) /* {{{ */
285 {
286 	zval tmp;
287 
288 	intern->type = SPL_FS_FILE;
289 
290 	php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp);
291 	if (Z_TYPE(tmp) == IS_TRUE) {
292 		intern->u.file.open_mode = NULL;
293 		intern->file_name = NULL;
294 		zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
295 		return FAILURE;
296 	}
297 
298 	intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
299 	intern->u.file.stream = php_stream_open_wrapper_ex(intern->file_name, intern->u.file.open_mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
300 
301 	if (!intern->file_name_len || !intern->u.file.stream) {
302 		if (!EG(exception)) {
303 			zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", intern->file_name_len ? intern->file_name : "");
304 		}
305 		intern->file_name = NULL; /* until here it is not a copy */
306 		intern->u.file.open_mode = NULL;
307 		return FAILURE;
308 	}
309 
310 	/*
311 	if (intern->u.file.zcontext) {
312 		//zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
313 		Z_ADDREF_P(intern->u.file.zcontext);
314 	}
315 	*/
316 
317 	if (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
318 		intern->file_name_len--;
319 	}
320 
321 	intern->orig_path = estrndup(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path));
322 
323 	intern->file_name = estrndup(intern->file_name, intern->file_name_len);
324 	intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
325 
326 	/* avoid reference counting in debug mode, thus do it manually */
327 	ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
328 	/*!!! TODO: maybe bug?
329 	Z_SET_REFCOUNT(intern->u.file.zresource, 1);
330 	*/
331 
332 	intern->u.file.delimiter = ',';
333 	intern->u.file.enclosure = '"';
334 	intern->u.file.escape = '\\';
335 
336 	intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
337 
338 	return SUCCESS;
339 } /* }}} */
340 
341 /* {{{ spl_filesystem_object_clone */
342 /* Local zend_object creation (on stack)
343    Load the 'other' object
344    Create a new empty object (See spl_filesystem_object_new_ex)
345    Open the directory
346    Clone other members (properties)
347  */
spl_filesystem_object_clone(zval * zobject)348 static zend_object *spl_filesystem_object_clone(zval *zobject)
349 {
350 	zend_object *old_object;
351 	zend_object *new_object;
352 	spl_filesystem_object *intern;
353 	spl_filesystem_object *source;
354 	int index, skip_dots;
355 
356 	old_object = Z_OBJ_P(zobject);
357 	source = spl_filesystem_from_obj(old_object);
358 	new_object = spl_filesystem_object_new_ex(old_object->ce);
359 	intern = spl_filesystem_from_obj(new_object);
360 
361 	intern->flags = source->flags;
362 
363 	switch (source->type) {
364 		case SPL_FS_INFO:
365 			intern->_path_len = source->_path_len;
366 			intern->_path = estrndup(source->_path, source->_path_len);
367 			intern->file_name_len = source->file_name_len;
368 			intern->file_name = estrndup(source->file_name, intern->file_name_len);
369 			break;
370 		case SPL_FS_DIR:
371 			spl_filesystem_dir_open(intern, source->_path);
372 			/* read until we hit the position in which we were before */
373 			skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
374 			for(index = 0; index < source->u.dir.index; ++index) {
375 				do {
376 					spl_filesystem_dir_read(intern);
377 				} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
378 			}
379 			intern->u.dir.index = index;
380 			break;
381 		case SPL_FS_FILE:
382 			ZEND_ASSERT(0);
383 	}
384 
385 	intern->file_class = source->file_class;
386 	intern->info_class = source->info_class;
387 	intern->oth = source->oth;
388 	intern->oth_handler = source->oth_handler;
389 
390 	zend_objects_clone_members(new_object, old_object);
391 
392 	if (intern->oth_handler && intern->oth_handler->clone) {
393 		intern->oth_handler->clone(source, intern);
394 	}
395 
396 	return new_object;
397 }
398 /* }}} */
399 
spl_filesystem_info_set_filename(spl_filesystem_object * intern,char * path,size_t len,size_t use_copy)400 void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path, size_t len, size_t use_copy) /* {{{ */
401 {
402 	char *p1, *p2;
403 
404 	if (intern->file_name) {
405 		efree(intern->file_name);
406 	}
407 
408 	intern->file_name = use_copy ? estrndup(path, len) : path;
409 	intern->file_name_len = len;
410 
411 	while (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
412 		intern->file_name[intern->file_name_len-1] = 0;
413 		intern->file_name_len--;
414 	}
415 
416 	p1 = strrchr(intern->file_name, '/');
417 #if defined(PHP_WIN32)
418 	p2 = strrchr(intern->file_name, '\\');
419 #else
420 	p2 = 0;
421 #endif
422 	if (p1 || p2) {
423 		intern->_path_len = ((p1 > p2 ? p1 : p2) - intern->file_name);
424 	} else {
425 		intern->_path_len = 0;
426 	}
427 
428 	if (intern->_path) {
429 		efree(intern->_path);
430 	}
431 	intern->_path = estrndup(path, intern->_path_len);
432 } /* }}} */
433 
spl_filesystem_object_create_info(spl_filesystem_object * source,char * file_path,size_t file_path_len,int use_copy,zend_class_entry * ce,zval * return_value)434 static spl_filesystem_object *spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, size_t file_path_len, int use_copy, zend_class_entry *ce, zval *return_value) /* {{{ */
435 {
436 	spl_filesystem_object *intern;
437 	zval arg1;
438 	zend_error_handling error_handling;
439 
440 	if (!file_path || !file_path_len) {
441 #if defined(PHP_WIN32)
442 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot create SplFileInfo for empty path");
443 		if (file_path && !use_copy) {
444 			efree(file_path);
445 		}
446 #else
447 		if (file_path && !use_copy) {
448 			efree(file_path);
449 		}
450 		file_path_len = 1;
451 		file_path = "/";
452 #endif
453 		return NULL;
454 	}
455 
456 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
457 
458 	ce = ce ? ce : source->info_class;
459 
460 	zend_update_class_constants(ce);
461 
462 	intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
463 	ZVAL_OBJ(return_value, &intern->std);
464 
465 	if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
466 		ZVAL_STRINGL(&arg1, file_path, file_path_len);
467 		zend_call_method_with_1_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1);
468 		zval_ptr_dtor(&arg1);
469 	} else {
470 		spl_filesystem_info_set_filename(intern, file_path, file_path_len, use_copy);
471 	}
472 
473 	zend_restore_error_handling(&error_handling);
474 	return intern;
475 } /* }}} */
476 
spl_filesystem_object_create_type(int ht,spl_filesystem_object * source,int type,zend_class_entry * ce,zval * return_value)477 static spl_filesystem_object *spl_filesystem_object_create_type(int ht, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value) /* {{{ */
478 {
479 	spl_filesystem_object *intern;
480 	zend_bool use_include_path = 0;
481 	zval arg1, arg2;
482 	zend_error_handling error_handling;
483 
484 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
485 
486 	switch (source->type) {
487 		case SPL_FS_INFO:
488 		case SPL_FS_FILE:
489 			break;
490 		case SPL_FS_DIR:
491 			if (!source->u.dir.entry.d_name[0]) {
492 				zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
493 				zend_restore_error_handling(&error_handling);
494 				return NULL;
495 			}
496 	}
497 
498 	switch (type) {
499 		case SPL_FS_INFO:
500 			ce = ce ? ce : source->info_class;
501 
502 			if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
503 				break;
504 			}
505 
506 			intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
507 			ZVAL_OBJ(return_value, &intern->std);
508 
509 			spl_filesystem_object_get_file_name(source);
510 			if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
511 				ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
512 				zend_call_method_with_1_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1);
513 				zval_ptr_dtor(&arg1);
514 			} else {
515 				intern->file_name = estrndup(source->file_name, source->file_name_len);
516 				intern->file_name_len = source->file_name_len;
517 				intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
518 				intern->_path = estrndup(intern->_path, intern->_path_len);
519 			}
520 			break;
521 		case SPL_FS_FILE:
522 			ce = ce ? ce : source->file_class;
523 
524 			if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
525 				break;
526 			}
527 
528 			intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
529 
530 			ZVAL_OBJ(return_value, &intern->std);
531 
532 			spl_filesystem_object_get_file_name(source);
533 
534 			if (ce->constructor->common.scope != spl_ce_SplFileObject) {
535 				ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
536 				ZVAL_STRINGL(&arg2, "r", 1);
537 				zend_call_method_with_2_params(return_value, ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
538 				zval_ptr_dtor(&arg1);
539 				zval_ptr_dtor(&arg2);
540 			} else {
541 				intern->file_name = source->file_name;
542 				intern->file_name_len = source->file_name_len;
543 				intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
544 				intern->_path = estrndup(intern->_path, intern->_path_len);
545 
546 				intern->u.file.open_mode = "r";
547 				intern->u.file.open_mode_len = 1;
548 
549 				if (ht && zend_parse_parameters(ht, "|sbr",
550 							&intern->u.file.open_mode, &intern->u.file.open_mode_len,
551 							&use_include_path, &intern->u.file.zcontext) == FAILURE) {
552 					zend_restore_error_handling(&error_handling);
553 					intern->u.file.open_mode = NULL;
554 					intern->file_name = NULL;
555 					zval_ptr_dtor(return_value);
556 					ZVAL_NULL(return_value);
557 					return NULL;
558 				}
559 
560 				if (spl_filesystem_file_open(intern, use_include_path, 0) == FAILURE) {
561 					zend_restore_error_handling(&error_handling);
562 					zval_ptr_dtor(return_value);
563 					ZVAL_NULL(return_value);
564 					return NULL;
565 				}
566 			}
567 			break;
568 		case SPL_FS_DIR:
569 			zend_restore_error_handling(&error_handling);
570 			zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
571 			return NULL;
572 	}
573 	zend_restore_error_handling(&error_handling);
574 	return NULL;
575 } /* }}} */
576 
spl_filesystem_is_invalid_or_dot(const char * d_name)577 static int spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
578 {
579 	return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
580 }
581 /* }}} */
582 
spl_filesystem_object_get_pathname(spl_filesystem_object * intern,size_t * len)583 static char *spl_filesystem_object_get_pathname(spl_filesystem_object *intern, size_t *len) { /* {{{ */
584 	switch (intern->type) {
585 		case SPL_FS_INFO:
586 		case SPL_FS_FILE:
587 			*len = intern->file_name_len;
588 			return intern->file_name;
589 		case SPL_FS_DIR:
590 			if (intern->u.dir.entry.d_name[0]) {
591 				spl_filesystem_object_get_file_name(intern);
592 				*len = intern->file_name_len;
593 				return intern->file_name;
594 			}
595 	}
596 	*len = 0;
597 	return NULL;
598 }
599 /* }}} */
600 
spl_filesystem_object_get_debug_info(zval * object,int * is_temp)601 static HashTable *spl_filesystem_object_get_debug_info(zval *object, int *is_temp) /* {{{ */
602 {
603 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(object);
604 	zval tmp;
605 	HashTable *rv;
606 	zend_string *pnstr;
607 	char *path;
608 	size_t  path_len;
609 	char stmp[2];
610 
611 	*is_temp = 1;
612 
613 	if (!intern->std.properties) {
614 		rebuild_object_properties(&intern->std);
615 	}
616 
617 	rv = zend_array_dup(intern->std.properties);
618 
619 	pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "pathName", sizeof("pathName")-1);
620 	path = spl_filesystem_object_get_pathname(intern, &path_len);
621 	ZVAL_STRINGL(&tmp, path, path_len);
622 	zend_symtable_update(rv, pnstr, &tmp);
623 	zend_string_release(pnstr);
624 
625 	if (intern->file_name) {
626 		pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "fileName", sizeof("fileName")-1);
627 		spl_filesystem_object_get_path(intern, &path_len);
628 
629 		if (path_len && path_len < intern->file_name_len) {
630 			ZVAL_STRINGL(&tmp, intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
631 		} else {
632 			ZVAL_STRINGL(&tmp, intern->file_name, intern->file_name_len);
633 		}
634 		zend_symtable_update(rv, pnstr, &tmp);
635 		zend_string_release(pnstr);
636 	}
637 	if (intern->type == SPL_FS_DIR) {
638 #ifdef HAVE_GLOB
639 		pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
640 		if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
641 			ZVAL_STRINGL(&tmp, intern->_path, intern->_path_len);
642 		} else {
643 			ZVAL_FALSE(&tmp);
644 		}
645 		zend_symtable_update(rv, pnstr, &tmp);
646 		zend_string_release(pnstr);
647 #endif
648 		pnstr = spl_gen_private_prop_name(spl_ce_RecursiveDirectoryIterator, "subPathName", sizeof("subPathName")-1);
649 		if (intern->u.dir.sub_path) {
650 			ZVAL_STRINGL(&tmp, intern->u.dir.sub_path, intern->u.dir.sub_path_len);
651 		} else {
652 			ZVAL_EMPTY_STRING(&tmp);
653 		}
654 		zend_symtable_update(rv, pnstr, &tmp);
655 		zend_string_release(pnstr);
656 	}
657 	if (intern->type == SPL_FS_FILE) {
658 		pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1);
659 		ZVAL_STRINGL(&tmp, intern->u.file.open_mode, intern->u.file.open_mode_len);
660 		zend_symtable_update(rv, pnstr, &tmp);
661 		zend_string_release(pnstr);
662 		stmp[1] = '\0';
663 		stmp[0] = intern->u.file.delimiter;
664 		pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "delimiter", sizeof("delimiter")-1);
665 		ZVAL_STRINGL(&tmp, stmp, 1);
666 		zend_symtable_update(rv, pnstr, &tmp);
667 		zend_string_release(pnstr);
668 		stmp[0] = intern->u.file.enclosure;
669 		pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "enclosure", sizeof("enclosure")-1);
670 		ZVAL_STRINGL(&tmp, stmp, 1);
671 		zend_symtable_update(rv, pnstr, &tmp);
672 		zend_string_release(pnstr);
673 	}
674 
675 	return rv;
676 }
677 /* }}} */
678 
spl_filesystem_object_get_method_check(zend_object ** object,zend_string * method,const zval * key)679 zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
680 {
681 	spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
682 
683 	if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
684 		zend_function *func;
685 		zend_string *tmp = zend_string_init("_bad_state_ex", sizeof("_bad_state_ex") - 1, 0);
686 		func = zend_get_std_object_handlers()->get_method(object, tmp, NULL);
687 		zend_string_release(tmp);
688 		return func;
689 	}
690 
691 	return zend_get_std_object_handlers()->get_method(object, method, key);
692 }
693 /* }}} */
694 
695 #define DIT_CTOR_FLAGS  0x00000001
696 #define DIT_CTOR_GLOB   0x00000002
697 
spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS,zend_long ctor_flags)698 void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
699 {
700 	spl_filesystem_object *intern;
701 	char *path;
702 	int parsed;
703 	size_t len;
704 	zend_long flags;
705 	zend_error_handling error_handling;
706 
707 	zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
708 
709 	if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
710 		flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
711 		parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &path, &len, &flags);
712 	} else {
713 		flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
714 		parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &len);
715 	}
716 	if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
717 		flags |= SPL_FILE_DIR_SKIPDOTS;
718 	}
719 	if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_UNIXPATHS)) {
720 		flags |= SPL_FILE_DIR_UNIXPATHS;
721 	}
722 	if (parsed == FAILURE) {
723 		zend_restore_error_handling(&error_handling);
724 		return;
725 	}
726 	if (!len) {
727 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Directory name must not be empty.");
728 		zend_restore_error_handling(&error_handling);
729 		return;
730 	}
731 
732 	intern = Z_SPLFILESYSTEM_P(getThis());
733 	if (intern->_path) {
734 		/* object is alreay initialized */
735 		zend_restore_error_handling(&error_handling);
736 		php_error_docref(NULL, E_WARNING, "Directory object is already initialized");
737 		return;
738 	}
739 	intern->flags = flags;
740 #ifdef HAVE_GLOB
741 	if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && strstr(path, "glob://") != path) {
742 		spprintf(&path, 0, "glob://%s", path);
743 		spl_filesystem_dir_open(intern, path);
744 		efree(path);
745 	} else
746 #endif
747 	{
748 		spl_filesystem_dir_open(intern, path);
749 
750 	}
751 
752 	intern->u.dir.is_recursive = instanceof_function(intern->std.ce, spl_ce_RecursiveDirectoryIterator) ? 1 : 0;
753 
754 	zend_restore_error_handling(&error_handling);
755 }
756 /* }}} */
757 
758 /* {{{ proto void DirectoryIterator::__construct(string path)
759  Cronstructs a new dir iterator from a path. */
SPL_METHOD(DirectoryIterator,__construct)760 SPL_METHOD(DirectoryIterator, __construct)
761 {
762 	spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
763 }
764 /* }}} */
765 
766 /* {{{ proto void DirectoryIterator::rewind()
767    Rewind dir back to the start */
SPL_METHOD(DirectoryIterator,rewind)768 SPL_METHOD(DirectoryIterator, rewind)
769 {
770 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
771 
772 	if (zend_parse_parameters_none() == FAILURE) {
773 		return;
774 	}
775 
776 	intern->u.dir.index = 0;
777 	if (intern->u.dir.dirp) {
778 		php_stream_rewinddir(intern->u.dir.dirp);
779 	}
780 	spl_filesystem_dir_read(intern);
781 }
782 /* }}} */
783 
784 /* {{{ proto string DirectoryIterator::key()
785    Return current dir entry */
SPL_METHOD(DirectoryIterator,key)786 SPL_METHOD(DirectoryIterator, key)
787 {
788 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
789 
790 	if (zend_parse_parameters_none() == FAILURE) {
791 		return;
792 	}
793 
794 	if (intern->u.dir.dirp) {
795 		RETURN_LONG(intern->u.dir.index);
796 	} else {
797 		RETURN_FALSE;
798 	}
799 }
800 /* }}} */
801 
802 /* {{{ proto DirectoryIterator DirectoryIterator::current()
803    Return this (needed for Iterator interface) */
SPL_METHOD(DirectoryIterator,current)804 SPL_METHOD(DirectoryIterator, current)
805 {
806 	if (zend_parse_parameters_none() == FAILURE) {
807 		return;
808 	}
809 	ZVAL_OBJ(return_value, Z_OBJ_P(getThis()));
810 	Z_ADDREF_P(return_value);
811 }
812 /* }}} */
813 
814 /* {{{ proto void DirectoryIterator::next()
815    Move to next entry */
SPL_METHOD(DirectoryIterator,next)816 SPL_METHOD(DirectoryIterator, next)
817 {
818 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
819 	int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
820 
821 	if (zend_parse_parameters_none() == FAILURE) {
822 		return;
823 	}
824 
825 	intern->u.dir.index++;
826 	do {
827 		spl_filesystem_dir_read(intern);
828 	} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
829 	if (intern->file_name) {
830 		efree(intern->file_name);
831 		intern->file_name = NULL;
832 	}
833 }
834 /* }}} */
835 
836 /* {{{ proto void DirectoryIterator::seek(int position)
837    Seek to the given position */
SPL_METHOD(DirectoryIterator,seek)838 SPL_METHOD(DirectoryIterator, seek)
839 {
840 	spl_filesystem_object *intern    = Z_SPLFILESYSTEM_P(getThis());
841 	zval retval;
842 	zend_long pos;
843 
844 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
845 		return;
846 	}
847 
848 	if (intern->u.dir.index > pos) {
849 		/* we first rewind */
850 		zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_rewind, "rewind", NULL);
851 	}
852 
853 	while (intern->u.dir.index < pos) {
854 		int valid = 0;
855 		zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_valid, "valid", &retval);
856 		if (!Z_ISUNDEF(retval)) {
857 			valid = zend_is_true(&retval);
858 			zval_ptr_dtor(&retval);
859 		}
860 		if (!valid) {
861 			zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
862 			return;
863 		}
864 		zend_call_method_with_0_params(&EX(This), Z_OBJCE(EX(This)), &intern->u.dir.func_next, "next", NULL);
865 	}
866 } /* }}} */
867 
868 /* {{{ proto string DirectoryIterator::valid()
869    Check whether dir contains more entries */
SPL_METHOD(DirectoryIterator,valid)870 SPL_METHOD(DirectoryIterator, valid)
871 {
872 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
873 
874 	if (zend_parse_parameters_none() == FAILURE) {
875 		return;
876 	}
877 
878 	RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
879 }
880 /* }}} */
881 
882 /* {{{ proto string SplFileInfo::getPath()
883    Return the path */
SPL_METHOD(SplFileInfo,getPath)884 SPL_METHOD(SplFileInfo, getPath)
885 {
886 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
887 	char *path;
888 	size_t path_len;
889 
890 	if (zend_parse_parameters_none() == FAILURE) {
891 		return;
892 	}
893 
894   	path = spl_filesystem_object_get_path(intern, &path_len);
895 	RETURN_STRINGL(path, path_len);
896 }
897 /* }}} */
898 
899 /* {{{ proto string SplFileInfo::getFilename()
900    Return filename only */
SPL_METHOD(SplFileInfo,getFilename)901 SPL_METHOD(SplFileInfo, getFilename)
902 {
903 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
904 	size_t path_len;
905 
906 	if (zend_parse_parameters_none() == FAILURE) {
907 		return;
908 	}
909 
910 	spl_filesystem_object_get_path(intern, &path_len);
911 
912 	if (path_len && path_len < intern->file_name_len) {
913 		RETURN_STRINGL(intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
914 	} else {
915 		RETURN_STRINGL(intern->file_name, intern->file_name_len);
916 	}
917 }
918 /* }}} */
919 
920 /* {{{ proto string DirectoryIterator::getFilename()
921    Return filename of current dir entry */
SPL_METHOD(DirectoryIterator,getFilename)922 SPL_METHOD(DirectoryIterator, getFilename)
923 {
924 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
925 
926 	if (zend_parse_parameters_none() == FAILURE) {
927 		return;
928 	}
929 
930 	RETURN_STRING(intern->u.dir.entry.d_name);
931 }
932 /* }}} */
933 
934 /* {{{ proto string SplFileInfo::getExtension()
935    Returns file extension component of path */
SPL_METHOD(SplFileInfo,getExtension)936 SPL_METHOD(SplFileInfo, getExtension)
937 {
938 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
939 	char *fname = NULL;
940 	const char *p;
941 	size_t flen;
942 	size_t path_len;
943 	size_t idx;
944 	zend_string *ret;
945 
946 	if (zend_parse_parameters_none() == FAILURE) {
947 		return;
948 	}
949 
950 	spl_filesystem_object_get_path(intern, &path_len);
951 
952 	if (path_len && path_len < intern->file_name_len) {
953 		fname = intern->file_name + path_len + 1;
954 		flen = intern->file_name_len - (path_len + 1);
955 	} else {
956 		fname = intern->file_name;
957 		flen = intern->file_name_len;
958 	}
959 
960 	ret = php_basename(fname, flen, NULL, 0);
961 
962 	p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
963 	if (p) {
964 		idx = p - ZSTR_VAL(ret);
965 		RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
966 		zend_string_release(ret);
967 		return;
968 	} else {
969 		zend_string_release(ret);
970 		RETURN_EMPTY_STRING();
971 	}
972 }
973 /* }}}*/
974 
975 /* {{{ proto string DirectoryIterator::getExtension()
976    Returns the file extension component of path */
SPL_METHOD(DirectoryIterator,getExtension)977 SPL_METHOD(DirectoryIterator, getExtension)
978 {
979 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
980 	const char *p;
981 	size_t idx;
982 	zend_string *fname;
983 
984 	if (zend_parse_parameters_none() == FAILURE) {
985 		return;
986 	}
987 
988 	fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
989 
990 	p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
991 	if (p) {
992 		idx = p - ZSTR_VAL(fname);
993 		RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
994 		zend_string_release(fname);
995 	} else {
996 		zend_string_release(fname);
997 		RETURN_EMPTY_STRING();
998 	}
999 }
1000 /* }}} */
1001 
1002 /* {{{ proto string SplFileInfo::getBasename([string $suffix])
1003    Returns filename component of path */
SPL_METHOD(SplFileInfo,getBasename)1004 SPL_METHOD(SplFileInfo, getBasename)
1005 {
1006 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1007 	char *fname, *suffix = 0;
1008 	size_t flen;
1009 	size_t slen = 0, path_len;
1010 
1011 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
1012 		return;
1013 	}
1014 
1015 	spl_filesystem_object_get_path(intern, &path_len);
1016 
1017 	if (path_len && path_len < intern->file_name_len) {
1018 		fname = intern->file_name + path_len + 1;
1019 		flen = intern->file_name_len - (path_len + 1);
1020 	} else {
1021 		fname = intern->file_name;
1022 		flen = intern->file_name_len;
1023 	}
1024 
1025 	RETURN_STR(php_basename(fname, flen, suffix, slen));
1026 }
1027 /* }}}*/
1028 
1029 /* {{{ proto string DirectoryIterator::getBasename([string $suffix])
1030    Returns filename component of current dir entry */
SPL_METHOD(DirectoryIterator,getBasename)1031 SPL_METHOD(DirectoryIterator, getBasename)
1032 {
1033 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1034 	char *suffix = 0;
1035 	size_t slen = 0;
1036 	zend_string *fname;
1037 
1038 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
1039 		return;
1040 	}
1041 
1042 	fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
1043 
1044 	RETVAL_STR(fname);
1045 }
1046 /* }}} */
1047 
1048 /* {{{ proto string SplFileInfo::getPathname()
1049    Return path and filename */
SPL_METHOD(SplFileInfo,getPathname)1050 SPL_METHOD(SplFileInfo, getPathname)
1051 {
1052 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1053 	char *path;
1054 	size_t path_len;
1055 
1056 	if (zend_parse_parameters_none() == FAILURE) {
1057 		return;
1058 	}
1059 	path = spl_filesystem_object_get_pathname(intern, &path_len);
1060 	if (path != NULL) {
1061 		RETURN_STRINGL(path, path_len);
1062 	} else {
1063 		RETURN_FALSE;
1064 	}
1065 }
1066 /* }}} */
1067 
1068 /* {{{ proto string FilesystemIterator::key()
1069    Return getPathname() or getFilename() depending on flags */
SPL_METHOD(FilesystemIterator,key)1070 SPL_METHOD(FilesystemIterator, key)
1071 {
1072 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1073 
1074 	if (zend_parse_parameters_none() == FAILURE) {
1075 		return;
1076 	}
1077 
1078 	if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1079 		RETURN_STRING(intern->u.dir.entry.d_name);
1080 	} else {
1081 		spl_filesystem_object_get_file_name(intern);
1082 		RETURN_STRINGL(intern->file_name, intern->file_name_len);
1083 	}
1084 }
1085 /* }}} */
1086 
1087 /* {{{ proto string FilesystemIterator::current()
1088    Return getFilename(), getFileInfo() or $this depending on flags */
SPL_METHOD(FilesystemIterator,current)1089 SPL_METHOD(FilesystemIterator, current)
1090 {
1091 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1092 
1093 	if (zend_parse_parameters_none() == FAILURE) {
1094 		return;
1095 	}
1096 
1097 	if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1098 		spl_filesystem_object_get_file_name(intern);
1099 		RETURN_STRINGL(intern->file_name, intern->file_name_len);
1100 	} else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1101 		spl_filesystem_object_get_file_name(intern);
1102 		spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
1103 	} else {
1104 		ZVAL_OBJ(return_value, Z_OBJ_P(getThis()));
1105 		Z_ADDREF_P(return_value);
1106 		/*RETURN_STRING(intern->u.dir.entry.d_name, 1);*/
1107 	}
1108 }
1109 /* }}} */
1110 
1111 /* {{{ proto bool DirectoryIterator::isDot()
1112    Returns true if current entry is '.' or  '..' */
SPL_METHOD(DirectoryIterator,isDot)1113 SPL_METHOD(DirectoryIterator, isDot)
1114 {
1115 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1116 
1117 	if (zend_parse_parameters_none() == FAILURE) {
1118 		return;
1119 	}
1120 
1121 	RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1122 }
1123 /* }}} */
1124 
1125 /* {{{ proto void SplFileInfo::__construct(string file_name)
1126  Cronstructs a new SplFileInfo from a path. */
1127 /* When the constructor gets called the object is already created
1128    by the engine, so we must only call 'additional' initializations.
1129  */
SPL_METHOD(SplFileInfo,__construct)1130 SPL_METHOD(SplFileInfo, __construct)
1131 {
1132 	spl_filesystem_object *intern;
1133 	char *path;
1134 	size_t len;
1135 
1136 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &path, &len) == FAILURE) {
1137 		return;
1138 	}
1139 
1140 	intern = Z_SPLFILESYSTEM_P(getThis());
1141 
1142 	spl_filesystem_info_set_filename(intern, path, len, 1);
1143 
1144 	/* intern->type = SPL_FS_INFO; already set */
1145 }
1146 /* }}} */
1147 
1148 /* {{{ FileInfoFunction */
1149 #define FileInfoFunction(func_name, func_num) \
1150 SPL_METHOD(SplFileInfo, func_name) \
1151 { \
1152 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis()); \
1153 	zend_error_handling error_handling; \
1154 	if (zend_parse_parameters_none() == FAILURE) { \
1155 		return; \
1156 	} \
1157  \
1158 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
1159 	spl_filesystem_object_get_file_name(intern); \
1160 	php_stat(intern->file_name, intern->file_name_len, func_num, return_value); \
1161 	zend_restore_error_handling(&error_handling); \
1162 }
1163 /* }}} */
1164 
1165 /* {{{ proto int SplFileInfo::getPerms()
1166    Get file permissions */
FileInfoFunction(getPerms,FS_PERMS)1167 FileInfoFunction(getPerms, FS_PERMS)
1168 /* }}} */
1169 
1170 /* {{{ proto int SplFileInfo::getInode()
1171    Get file inode */
1172 FileInfoFunction(getInode, FS_INODE)
1173 /* }}} */
1174 
1175 /* {{{ proto int SplFileInfo::getSize()
1176    Get file size */
1177 FileInfoFunction(getSize, FS_SIZE)
1178 /* }}} */
1179 
1180 /* {{{ proto int SplFileInfo::getOwner()
1181    Get file owner */
1182 FileInfoFunction(getOwner, FS_OWNER)
1183 /* }}} */
1184 
1185 /* {{{ proto int SplFileInfo::getGroup()
1186    Get file group */
1187 FileInfoFunction(getGroup, FS_GROUP)
1188 /* }}} */
1189 
1190 /* {{{ proto int SplFileInfo::getATime()
1191    Get last access time of file */
1192 FileInfoFunction(getATime, FS_ATIME)
1193 /* }}} */
1194 
1195 /* {{{ proto int SplFileInfo::getMTime()
1196    Get last modification time of file */
1197 FileInfoFunction(getMTime, FS_MTIME)
1198 /* }}} */
1199 
1200 /* {{{ proto int SplFileInfo::getCTime()
1201    Get inode modification time of file */
1202 FileInfoFunction(getCTime, FS_CTIME)
1203 /* }}} */
1204 
1205 /* {{{ proto string SplFileInfo::getType()
1206    Get file type */
1207 FileInfoFunction(getType, FS_TYPE)
1208 /* }}} */
1209 
1210 /* {{{ proto bool SplFileInfo::isWritable()
1211    Returns true if file can be written */
1212 FileInfoFunction(isWritable, FS_IS_W)
1213 /* }}} */
1214 
1215 /* {{{ proto bool SplFileInfo::isReadable()
1216    Returns true if file can be read */
1217 FileInfoFunction(isReadable, FS_IS_R)
1218 /* }}} */
1219 
1220 /* {{{ proto bool SplFileInfo::isExecutable()
1221    Returns true if file is executable */
1222 FileInfoFunction(isExecutable, FS_IS_X)
1223 /* }}} */
1224 
1225 /* {{{ proto bool SplFileInfo::isFile()
1226    Returns true if file is a regular file */
1227 FileInfoFunction(isFile, FS_IS_FILE)
1228 /* }}} */
1229 
1230 /* {{{ proto bool SplFileInfo::isDir()
1231    Returns true if file is directory */
1232 FileInfoFunction(isDir, FS_IS_DIR)
1233 /* }}} */
1234 
1235 /* {{{ proto bool SplFileInfo::isLink()
1236    Returns true if file is symbolic link */
1237 FileInfoFunction(isLink, FS_IS_LINK)
1238 /* }}} */
1239 
1240 /* {{{ proto string SplFileInfo::getLinkTarget()
1241    Return the target of a symbolic link */
1242 SPL_METHOD(SplFileInfo, getLinkTarget)
1243 {
1244 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1245 	int ret;
1246 	char buff[MAXPATHLEN];
1247 	zend_error_handling error_handling;
1248 
1249 	if (zend_parse_parameters_none() == FAILURE) {
1250 		return;
1251 	}
1252 
1253 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
1254 
1255 	if (intern->file_name == NULL) {
1256 		spl_filesystem_object_get_file_name(intern);
1257 	}
1258 #if defined(PHP_WIN32) || HAVE_SYMLINK
1259 	if (intern->file_name == NULL) {
1260 		php_error_docref(NULL, E_WARNING, "Empty filename");
1261 		RETURN_FALSE;
1262 	} else if (!IS_ABSOLUTE_PATH(intern->file_name, intern->file_name_len)) {
1263 		char expanded_path[MAXPATHLEN];
1264 		if (!expand_filepath_with_mode(intern->file_name, expanded_path, NULL, 0, CWD_EXPAND )) {
1265 			php_error_docref(NULL, E_WARNING, "No such file or directory");
1266 			RETURN_FALSE;
1267 		}
1268 		ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
1269 	} else {
1270 		ret = php_sys_readlink(intern->file_name, buff,  MAXPATHLEN-1);
1271 	}
1272 #else
1273 	ret = -1; /* always fail if not implemented */
1274 #endif
1275 
1276 	if (ret == -1) {
1277 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to read link %s, error: %s", intern->file_name, strerror(errno));
1278 		RETVAL_FALSE;
1279 	} else {
1280 		/* Append NULL to the end of the string */
1281 		buff[ret] = '\0';
1282 
1283 		RETVAL_STRINGL(buff, ret);
1284 	}
1285 
1286 	zend_restore_error_handling(&error_handling);
1287 }
1288 /* }}} */
1289 
1290 #if (!defined(__BEOS__) && HAVE_REALPATH) || defined(ZTS)
1291 /* {{{ proto string SplFileInfo::getRealPath()
1292    Return the resolved path */
SPL_METHOD(SplFileInfo,getRealPath)1293 SPL_METHOD(SplFileInfo, getRealPath)
1294 {
1295 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1296 	char buff[MAXPATHLEN];
1297 	char *filename;
1298 	zend_error_handling error_handling;
1299 
1300 	if (zend_parse_parameters_none() == FAILURE) {
1301 		return;
1302 	}
1303 
1304 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
1305 
1306 	if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
1307 		spl_filesystem_object_get_file_name(intern);
1308 	}
1309 
1310 	if (intern->orig_path) {
1311 		filename = intern->orig_path;
1312 	} else {
1313 		filename = intern->file_name;
1314 	}
1315 
1316 
1317 	if (filename && VCWD_REALPATH(filename, buff)) {
1318 #ifdef ZTS
1319 		if (VCWD_ACCESS(buff, F_OK)) {
1320 			RETVAL_FALSE;
1321 		} else
1322 #endif
1323 		RETVAL_STRING(buff);
1324 	} else {
1325 		RETVAL_FALSE;
1326 	}
1327 
1328 	zend_restore_error_handling(&error_handling);
1329 }
1330 /* }}} */
1331 #endif
1332 
1333 /* {{{ proto SplFileObject SplFileInfo::openFile([string mode = 'r' [, bool use_include_path  [, resource context]]])
1334    Open the current file */
SPL_METHOD(SplFileInfo,openFile)1335 SPL_METHOD(SplFileInfo, openFile)
1336 {
1337 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1338 
1339 	spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_FILE, NULL, return_value);
1340 }
1341 /* }}} */
1342 
1343 /* {{{ proto void SplFileInfo::setFileClass([string class_name])
1344    Class to use in openFile() */
SPL_METHOD(SplFileInfo,setFileClass)1345 SPL_METHOD(SplFileInfo, setFileClass)
1346 {
1347 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1348 	zend_class_entry *ce = spl_ce_SplFileObject;
1349 	zend_error_handling error_handling;
1350 
1351 	zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
1352 
1353 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
1354 		intern->file_class = ce;
1355 	}
1356 
1357 	zend_restore_error_handling(&error_handling);
1358 }
1359 /* }}} */
1360 
1361 /* {{{ proto void SplFileInfo::setInfoClass([string class_name])
1362    Class to use in getFileInfo(), getPathInfo() */
SPL_METHOD(SplFileInfo,setInfoClass)1363 SPL_METHOD(SplFileInfo, setInfoClass)
1364 {
1365 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1366 	zend_class_entry *ce = spl_ce_SplFileInfo;
1367 	zend_error_handling error_handling;
1368 
1369 	zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling );
1370 
1371 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
1372 		intern->info_class = ce;
1373 	}
1374 
1375 	zend_restore_error_handling(&error_handling);
1376 }
1377 /* }}} */
1378 
1379 /* {{{ proto SplFileInfo SplFileInfo::getFileInfo([string $class_name])
1380    Get/copy file info */
SPL_METHOD(SplFileInfo,getFileInfo)1381 SPL_METHOD(SplFileInfo, getFileInfo)
1382 {
1383 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1384 	zend_class_entry *ce = intern->info_class;
1385 	zend_error_handling error_handling;
1386 
1387 	zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
1388 
1389 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
1390 		spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_INFO, ce, return_value);
1391 	}
1392 
1393 	zend_restore_error_handling(&error_handling);
1394 }
1395 /* }}} */
1396 
1397 /* {{{ proto SplFileInfo SplFileInfo::getPathInfo([string $class_name])
1398    Get/copy file info */
SPL_METHOD(SplFileInfo,getPathInfo)1399 SPL_METHOD(SplFileInfo, getPathInfo)
1400 {
1401 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1402 	zend_class_entry *ce = intern->info_class;
1403 	zend_error_handling error_handling;
1404 
1405 	zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
1406 
1407 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == SUCCESS) {
1408 		size_t path_len;
1409 		char *path = spl_filesystem_object_get_pathname(intern, &path_len);
1410 		if (path) {
1411 			char *dpath = estrndup(path, path_len);
1412 			path_len = php_dirname(dpath, path_len);
1413 			spl_filesystem_object_create_info(intern, dpath, path_len, 1, ce, return_value);
1414 			efree(dpath);
1415 		}
1416 	}
1417 
1418 	zend_restore_error_handling(&error_handling);
1419 }
1420 /* }}} */
1421 
1422 /* {{{  proto SplFileInfo::_bad_state_ex(void) */
SPL_METHOD(SplFileInfo,_bad_state_ex)1423 SPL_METHOD(SplFileInfo, _bad_state_ex)
1424 {
1425 	zend_throw_exception_ex(spl_ce_LogicException, 0,
1426 		"The parent constructor was not called: the object is in an "
1427 		"invalid state ");
1428 }
1429 /* }}} */
1430 
1431 /* {{{ proto void FilesystemIterator::__construct(string path [, int flags])
1432  Cronstructs a new dir iterator from a path. */
SPL_METHOD(FilesystemIterator,__construct)1433 SPL_METHOD(FilesystemIterator, __construct)
1434 {
1435 	spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
1436 }
1437 /* }}} */
1438 
1439 /* {{{ proto void FilesystemIterator::rewind()
1440    Rewind dir back to the start */
SPL_METHOD(FilesystemIterator,rewind)1441 SPL_METHOD(FilesystemIterator, rewind)
1442 {
1443 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1444 	int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
1445 
1446 	if (zend_parse_parameters_none() == FAILURE) {
1447 		return;
1448 	}
1449 
1450 	intern->u.dir.index = 0;
1451 	if (intern->u.dir.dirp) {
1452 		php_stream_rewinddir(intern->u.dir.dirp);
1453 	}
1454 	do {
1455 		spl_filesystem_dir_read(intern);
1456 	} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1457 }
1458 /* }}} */
1459 
1460 /* {{{ proto int FilesystemIterator::getFlags()
1461    Get handling flags */
SPL_METHOD(FilesystemIterator,getFlags)1462 SPL_METHOD(FilesystemIterator, getFlags)
1463 {
1464 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1465 
1466 	if (zend_parse_parameters_none() == FAILURE) {
1467 		return;
1468 	}
1469 
1470 	RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
1471 } /* }}} */
1472 
1473 /* {{{ proto void FilesystemIterator::setFlags(long $flags)
1474    Set handling flags */
SPL_METHOD(FilesystemIterator,setFlags)1475 SPL_METHOD(FilesystemIterator, setFlags)
1476 {
1477 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1478 	zend_long flags;
1479 
1480 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &flags) == FAILURE) {
1481 		return;
1482 	}
1483 
1484 	intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
1485 	intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
1486 } /* }}} */
1487 
1488 /* {{{ proto bool RecursiveDirectoryIterator::hasChildren([bool $allow_links = false])
1489    Returns whether current entry is a directory and not '.' or '..' */
SPL_METHOD(RecursiveDirectoryIterator,hasChildren)1490 SPL_METHOD(RecursiveDirectoryIterator, hasChildren)
1491 {
1492 	zend_bool allow_links = 0;
1493 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1494 
1495 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &allow_links) == FAILURE) {
1496 		return;
1497 	}
1498 	if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
1499 		RETURN_FALSE;
1500 	} else {
1501 		spl_filesystem_object_get_file_name(intern);
1502 		if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
1503 			php_stat(intern->file_name, intern->file_name_len, FS_IS_LINK, return_value);
1504 			if (zend_is_true(return_value)) {
1505 				RETURN_FALSE;
1506 			}
1507 		}
1508 		php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, return_value);
1509     }
1510 }
1511 /* }}} */
1512 
1513 /* {{{ proto RecursiveDirectoryIterator DirectoryIterator::getChildren()
1514    Returns an iterator for the current entry if it is a directory */
SPL_METHOD(RecursiveDirectoryIterator,getChildren)1515 SPL_METHOD(RecursiveDirectoryIterator, getChildren)
1516 {
1517 	zval zpath, zflags;
1518 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1519 	spl_filesystem_object *subdir;
1520 	char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1521 
1522 	if (zend_parse_parameters_none() == FAILURE) {
1523 		return;
1524 	}
1525 
1526 	spl_filesystem_object_get_file_name(intern);
1527 
1528 	ZVAL_LONG(&zflags, intern->flags);
1529 	ZVAL_STRINGL(&zpath, intern->file_name, intern->file_name_len);
1530 	spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, &zpath, &zflags);
1531 	zval_ptr_dtor(&zpath);
1532 	zval_ptr_dtor(&zflags);
1533 
1534 	subdir = Z_SPLFILESYSTEM_P(return_value);
1535 	if (subdir) {
1536 		if (intern->u.dir.sub_path && intern->u.dir.sub_path[0]) {
1537 			subdir->u.dir.sub_path_len = spprintf(&subdir->u.dir.sub_path, 0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name);
1538 		} else {
1539 			subdir->u.dir.sub_path_len = strlen(intern->u.dir.entry.d_name);
1540 			subdir->u.dir.sub_path = estrndup(intern->u.dir.entry.d_name, subdir->u.dir.sub_path_len);
1541 		}
1542 		subdir->info_class = intern->info_class;
1543 		subdir->file_class = intern->file_class;
1544 		subdir->oth = intern->oth;
1545 	}
1546 }
1547 /* }}} */
1548 
1549 /* {{{ proto void RecursiveDirectoryIterator::getSubPath()
1550    Get sub path */
SPL_METHOD(RecursiveDirectoryIterator,getSubPath)1551 SPL_METHOD(RecursiveDirectoryIterator, getSubPath)
1552 {
1553 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1554 
1555 	if (zend_parse_parameters_none() == FAILURE) {
1556 		return;
1557 	}
1558 
1559 	if (intern->u.dir.sub_path) {
1560 		RETURN_STRINGL(intern->u.dir.sub_path, intern->u.dir.sub_path_len);
1561 	} else {
1562 		RETURN_EMPTY_STRING();
1563 	}
1564 }
1565 /* }}} */
1566 
1567 /* {{{ proto void RecursiveDirectoryIterator::getSubPathname()
1568    Get sub path and file name */
SPL_METHOD(RecursiveDirectoryIterator,getSubPathname)1569 SPL_METHOD(RecursiveDirectoryIterator, getSubPathname)
1570 {
1571 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1572 	char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1573 
1574 	if (zend_parse_parameters_none() == FAILURE) {
1575 		return;
1576 	}
1577 
1578 	if (intern->u.dir.sub_path) {
1579 		RETURN_NEW_STR(strpprintf(0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name));
1580 	} else {
1581 		RETURN_STRING(intern->u.dir.entry.d_name);
1582 	}
1583 }
1584 /* }}} */
1585 
1586 /* {{{ proto int RecursiveDirectoryIterator::__construct(string path [, int flags])
1587  Cronstructs a new dir iterator from a path. */
SPL_METHOD(RecursiveDirectoryIterator,__construct)1588 SPL_METHOD(RecursiveDirectoryIterator, __construct)
1589 {
1590 	spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
1591 }
1592 /* }}} */
1593 
1594 #ifdef HAVE_GLOB
1595 /* {{{ proto int GlobIterator::__construct(string path [, int flags])
1596  Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
SPL_METHOD(GlobIterator,__construct)1597 SPL_METHOD(GlobIterator, __construct)
1598 {
1599 	spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
1600 }
1601 /* }}} */
1602 
1603 /* {{{ proto int GlobIterator::cont()
1604    Return the number of directories and files found by globbing */
SPL_METHOD(GlobIterator,count)1605 SPL_METHOD(GlobIterator, count)
1606 {
1607 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
1608 
1609 	if (zend_parse_parameters_none() == FAILURE) {
1610 		return;
1611 	}
1612 
1613 	if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
1614 		RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
1615 	} else {
1616 		/* should not happen */
1617 		php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
1618 	}
1619 }
1620 /* }}} */
1621 #endif /* HAVE_GLOB */
1622 
1623 /* {{{ forward declarations to the iterator handlers */
1624 static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
1625 static int spl_filesystem_dir_it_valid(zend_object_iterator *iter);
1626 static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter);
1627 static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key);
1628 static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter);
1629 static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter);
1630 
1631 /* iterator handler table */
1632 zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
1633 	spl_filesystem_dir_it_dtor,
1634 	spl_filesystem_dir_it_valid,
1635 	spl_filesystem_dir_it_current_data,
1636 	spl_filesystem_dir_it_current_key,
1637 	spl_filesystem_dir_it_move_forward,
1638 	spl_filesystem_dir_it_rewind,
1639 	NULL
1640 };
1641 /* }}} */
1642 
1643 /* {{{ spl_ce_dir_get_iterator */
spl_filesystem_dir_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1644 zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1645 {
1646 	spl_filesystem_iterator *iterator;
1647 	spl_filesystem_object *dir_object;
1648 
1649 	if (by_ref) {
1650 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1651 	}
1652 	dir_object = Z_SPLFILESYSTEM_P(object);
1653 	iterator = spl_filesystem_object_to_iterator(dir_object);
1654 	ZVAL_COPY(&iterator->intern.data, object);
1655 	iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
1656 	/* ->current must be initialized; rewind doesn't set it and valid
1657 	 * doesn't check whether it's set */
1658 	iterator->current = *object;
1659 
1660 	return &iterator->intern;
1661 }
1662 /* }}} */
1663 
1664 /* {{{ spl_filesystem_dir_it_dtor */
spl_filesystem_dir_it_dtor(zend_object_iterator * iter)1665 static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter)
1666 {
1667 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1668 
1669 	if (!Z_ISUNDEF(iterator->intern.data)) {
1670 		zval *object = &iterator->intern.data;
1671 		zval_ptr_dtor(object);
1672 	}
1673 	/* Otherwise we were called from the owning object free storage handler as
1674 	 * it sets iterator->intern.data to IS_UNDEF.
1675 	 * We don't even need to destroy iterator->current as we didn't add a
1676 	 * reference to it in move_forward or get_iterator */
1677 }
1678 /* }}} */
1679 
1680 /* {{{ spl_filesystem_dir_it_valid */
spl_filesystem_dir_it_valid(zend_object_iterator * iter)1681 static int spl_filesystem_dir_it_valid(zend_object_iterator *iter)
1682 {
1683 	spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1684 
1685 	return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
1686 }
1687 /* }}} */
1688 
1689 /* {{{ spl_filesystem_dir_it_current_data */
spl_filesystem_dir_it_current_data(zend_object_iterator * iter)1690 static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter)
1691 {
1692 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1693 
1694 	return &iterator->current;
1695 }
1696 /* }}} */
1697 
1698 /* {{{ spl_filesystem_dir_it_current_key */
spl_filesystem_dir_it_current_key(zend_object_iterator * iter,zval * key)1699 static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key)
1700 {
1701 	spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1702 
1703 	ZVAL_LONG(key, object->u.dir.index);
1704 }
1705 /* }}} */
1706 
1707 /* {{{ spl_filesystem_dir_it_move_forward */
spl_filesystem_dir_it_move_forward(zend_object_iterator * iter)1708 static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter)
1709 {
1710 	spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1711 
1712 	object->u.dir.index++;
1713 	spl_filesystem_dir_read(object);
1714 	if (object->file_name) {
1715 		efree(object->file_name);
1716 		object->file_name = NULL;
1717 	}
1718 }
1719 /* }}} */
1720 
1721 /* {{{ spl_filesystem_dir_it_rewind */
spl_filesystem_dir_it_rewind(zend_object_iterator * iter)1722 static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter)
1723 {
1724 	spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1725 
1726 	object->u.dir.index = 0;
1727 	if (object->u.dir.dirp) {
1728 		php_stream_rewinddir(object->u.dir.dirp);
1729 	}
1730 	spl_filesystem_dir_read(object);
1731 }
1732 /* }}} */
1733 
1734 /* {{{ spl_filesystem_tree_it_dtor */
spl_filesystem_tree_it_dtor(zend_object_iterator * iter)1735 static void spl_filesystem_tree_it_dtor(zend_object_iterator *iter)
1736 {
1737 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1738 
1739 	if (!Z_ISUNDEF(iterator->intern.data)) {
1740 		zval *object = &iterator->intern.data;
1741 		zval_ptr_dtor(object);
1742 	} else {
1743 		if (!Z_ISUNDEF(iterator->current)) {
1744 			zval_ptr_dtor(&iterator->current);
1745 			ZVAL_UNDEF(&iterator->current);
1746 		}
1747 	}
1748 }
1749 /* }}} */
1750 
1751 /* {{{ spl_filesystem_tree_it_current_data */
spl_filesystem_tree_it_current_data(zend_object_iterator * iter)1752 static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
1753 {
1754 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1755 	spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1756 
1757 	if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1758 		if (Z_ISUNDEF(iterator->current)) {
1759 			spl_filesystem_object_get_file_name(object);
1760 			ZVAL_STRINGL(&iterator->current, object->file_name, object->file_name_len);
1761 		}
1762 		return &iterator->current;
1763 	} else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1764 		if (Z_ISUNDEF(iterator->current)) {
1765 			spl_filesystem_object_get_file_name(object);
1766 			spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
1767 		}
1768 		return &iterator->current;
1769 	} else {
1770 		return &iterator->intern.data;
1771 	}
1772 }
1773 /* }}} */
1774 
1775 /* {{{ spl_filesystem_tree_it_current_key */
spl_filesystem_tree_it_current_key(zend_object_iterator * iter,zval * key)1776 static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval *key)
1777 {
1778 	spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1779 
1780 	if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1781 		ZVAL_STRING(key, object->u.dir.entry.d_name);
1782 	} else {
1783 		spl_filesystem_object_get_file_name(object);
1784 		ZVAL_STRINGL(key, object->file_name, object->file_name_len);
1785 	}
1786 }
1787 /* }}} */
1788 
1789 /* {{{ spl_filesystem_tree_it_move_forward */
spl_filesystem_tree_it_move_forward(zend_object_iterator * iter)1790 static void spl_filesystem_tree_it_move_forward(zend_object_iterator *iter)
1791 {
1792 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1793 	spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1794 
1795 	object->u.dir.index++;
1796 	do {
1797 		spl_filesystem_dir_read(object);
1798 	} while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
1799 	if (object->file_name) {
1800 		efree(object->file_name);
1801 		object->file_name = NULL;
1802 	}
1803 	if (!Z_ISUNDEF(iterator->current)) {
1804 		zval_ptr_dtor(&iterator->current);
1805 		ZVAL_UNDEF(&iterator->current);
1806 	}
1807 }
1808 /* }}} */
1809 
1810 /* {{{ spl_filesystem_tree_it_rewind */
spl_filesystem_tree_it_rewind(zend_object_iterator * iter)1811 static void spl_filesystem_tree_it_rewind(zend_object_iterator *iter)
1812 {
1813 	spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1814 	spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1815 
1816 	object->u.dir.index = 0;
1817 	if (object->u.dir.dirp) {
1818 		php_stream_rewinddir(object->u.dir.dirp);
1819 	}
1820 	do {
1821 		spl_filesystem_dir_read(object);
1822 	} while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
1823 	if (!Z_ISUNDEF(iterator->current)) {
1824 		zval_ptr_dtor(&iterator->current);
1825 		ZVAL_UNDEF(&iterator->current);
1826 	}
1827 }
1828 /* }}} */
1829 
1830 /* {{{ iterator handler table */
1831 zend_object_iterator_funcs spl_filesystem_tree_it_funcs = {
1832 	spl_filesystem_tree_it_dtor,
1833 	spl_filesystem_dir_it_valid,
1834 	spl_filesystem_tree_it_current_data,
1835 	spl_filesystem_tree_it_current_key,
1836 	spl_filesystem_tree_it_move_forward,
1837 	spl_filesystem_tree_it_rewind,
1838 	NULL
1839 };
1840 /* }}} */
1841 
1842 /* {{{ spl_ce_dir_get_iterator */
spl_filesystem_tree_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1843 zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1844 {
1845 	spl_filesystem_iterator *iterator;
1846 	spl_filesystem_object *dir_object;
1847 
1848 	if (by_ref) {
1849 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1850 	}
1851 	dir_object = Z_SPLFILESYSTEM_P(object);
1852 	iterator = spl_filesystem_object_to_iterator(dir_object);
1853 
1854 	ZVAL_COPY(&iterator->intern.data, object);
1855 	iterator->intern.funcs = &spl_filesystem_tree_it_funcs;
1856 
1857 	return &iterator->intern;
1858 }
1859 /* }}} */
1860 
1861 /* {{{ spl_filesystem_object_cast */
spl_filesystem_object_cast(zval * readobj,zval * writeobj,int type)1862 static int spl_filesystem_object_cast(zval *readobj, zval *writeobj, int type)
1863 {
1864 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(readobj);
1865 
1866 	if (type == IS_STRING) {
1867 		if (Z_OBJCE_P(readobj)->__tostring) {
1868 			return std_object_handlers.cast_object(readobj, writeobj, type);
1869 		}
1870 
1871 		switch (intern->type) {
1872 		case SPL_FS_INFO:
1873 		case SPL_FS_FILE:
1874 			if (readobj == writeobj) {
1875 				zval retval;
1876 				zval *retval_ptr = &retval;
1877 
1878 				ZVAL_STRINGL(retval_ptr, intern->file_name, intern->file_name_len);
1879 				zval_ptr_dtor(readobj);
1880 				ZVAL_NEW_STR(writeobj, Z_STR_P(retval_ptr));
1881 			} else {
1882 				ZVAL_STRINGL(writeobj, intern->file_name, intern->file_name_len);
1883 			}
1884 			return SUCCESS;
1885 		case SPL_FS_DIR:
1886 			if (readobj == writeobj) {
1887 				zval retval;
1888 				zval *retval_ptr = &retval;
1889 
1890 				ZVAL_STRING(retval_ptr, intern->u.dir.entry.d_name);
1891 				zval_ptr_dtor(readobj);
1892 				ZVAL_NEW_STR(writeobj, Z_STR_P(retval_ptr));
1893 			} else {
1894 				ZVAL_STRING(writeobj, intern->u.dir.entry.d_name);
1895 			}
1896 			return SUCCESS;
1897 		}
1898 	} else if (type == _IS_BOOL) {
1899 		ZVAL_TRUE(writeobj);
1900 		return SUCCESS;
1901 	}
1902 	if (readobj == writeobj) {
1903 		zval_ptr_dtor(readobj);
1904 	}
1905 	ZVAL_NULL(writeobj);
1906 	return FAILURE;
1907 }
1908 /* }}} */
1909 
1910 /* {{{ declare method parameters */
1911 /* supply a name and default to call by parameter */
1912 ZEND_BEGIN_ARG_INFO(arginfo_info___construct, 0)
1913 	ZEND_ARG_INFO(0, file_name)
1914 ZEND_END_ARG_INFO()
1915 
1916 ZEND_BEGIN_ARG_INFO_EX(arginfo_info_openFile, 0, 0, 0)
1917 	ZEND_ARG_INFO(0, open_mode)
1918 	ZEND_ARG_INFO(0, use_include_path)
1919 	ZEND_ARG_INFO(0, context)
1920 ZEND_END_ARG_INFO()
1921 
1922 ZEND_BEGIN_ARG_INFO_EX(arginfo_info_optinalFileClass, 0, 0, 0)
1923 	ZEND_ARG_INFO(0, class_name)
1924 ZEND_END_ARG_INFO()
1925 
1926 ZEND_BEGIN_ARG_INFO_EX(arginfo_optinalSuffix, 0, 0, 0)
1927 	ZEND_ARG_INFO(0, suffix)
1928 ZEND_END_ARG_INFO()
1929 
1930 ZEND_BEGIN_ARG_INFO(arginfo_splfileinfo_void, 0)
1931 ZEND_END_ARG_INFO()
1932 
1933 /* the method table */
1934 /* each method can have its own parameters and visibility */
1935 static const zend_function_entry spl_SplFileInfo_functions[] = {
1936 	SPL_ME(SplFileInfo,       __construct,   arginfo_info___construct, ZEND_ACC_PUBLIC)
1937 	SPL_ME(SplFileInfo,       getPath,       arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1938 	SPL_ME(SplFileInfo,       getFilename,   arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1939 	SPL_ME(SplFileInfo,       getExtension,  arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1940 	SPL_ME(SplFileInfo,       getBasename,   arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
1941 	SPL_ME(SplFileInfo,       getPathname,   arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1942 	SPL_ME(SplFileInfo,       getPerms,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1943 	SPL_ME(SplFileInfo,       getInode,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1944 	SPL_ME(SplFileInfo,       getSize,       arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1945 	SPL_ME(SplFileInfo,       getOwner,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1946 	SPL_ME(SplFileInfo,       getGroup,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1947 	SPL_ME(SplFileInfo,       getATime,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1948 	SPL_ME(SplFileInfo,       getMTime,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1949 	SPL_ME(SplFileInfo,       getCTime,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1950 	SPL_ME(SplFileInfo,       getType,       arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1951 	SPL_ME(SplFileInfo,       isWritable,    arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1952 	SPL_ME(SplFileInfo,       isReadable,    arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1953 	SPL_ME(SplFileInfo,       isExecutable,  arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1954 	SPL_ME(SplFileInfo,       isFile,        arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1955 	SPL_ME(SplFileInfo,       isDir,         arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1956 	SPL_ME(SplFileInfo,       isLink,        arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1957 	SPL_ME(SplFileInfo,       getLinkTarget, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1958 #if (!defined(__BEOS__) && HAVE_REALPATH) || defined(ZTS)
1959 	SPL_ME(SplFileInfo,       getRealPath,   arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1960 #endif
1961 	SPL_ME(SplFileInfo,       getFileInfo,   arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
1962 	SPL_ME(SplFileInfo,       getPathInfo,   arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
1963 	SPL_ME(SplFileInfo,       openFile,      arginfo_info_openFile,         ZEND_ACC_PUBLIC)
1964 	SPL_ME(SplFileInfo,       setFileClass,  arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
1965 	SPL_ME(SplFileInfo,       setInfoClass,  arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
1966 	SPL_ME(SplFileInfo,       _bad_state_ex, NULL,							ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
1967 	SPL_MA(SplFileInfo,       __toString, SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1968 	PHP_FE_END
1969 };
1970 
1971 ZEND_BEGIN_ARG_INFO(arginfo_dir___construct, 0)
1972 	ZEND_ARG_INFO(0, path)
1973 ZEND_END_ARG_INFO()
1974 
1975 ZEND_BEGIN_ARG_INFO(arginfo_dir_it_seek, 0)
1976 	ZEND_ARG_INFO(0, position)
1977 ZEND_END_ARG_INFO();
1978 
1979 /* the method table */
1980 /* each method can have its own parameters and visibility */
1981 static const zend_function_entry spl_DirectoryIterator_functions[] = {
1982 	SPL_ME(DirectoryIterator, __construct,   arginfo_dir___construct, ZEND_ACC_PUBLIC)
1983 	SPL_ME(DirectoryIterator, getFilename,   arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1984 	SPL_ME(DirectoryIterator, getExtension,  arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1985 	SPL_ME(DirectoryIterator, getBasename,   arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
1986 	SPL_ME(DirectoryIterator, isDot,         arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1987 	SPL_ME(DirectoryIterator, rewind,        arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1988 	SPL_ME(DirectoryIterator, valid,         arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1989 	SPL_ME(DirectoryIterator, key,           arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1990 	SPL_ME(DirectoryIterator, current,       arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1991 	SPL_ME(DirectoryIterator, next,          arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1992 	SPL_ME(DirectoryIterator, seek,          arginfo_dir_it_seek, ZEND_ACC_PUBLIC)
1993 	SPL_MA(DirectoryIterator, __toString, DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
1994 	PHP_FE_END
1995 };
1996 
1997 ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir___construct, 0, 0, 1)
1998 	ZEND_ARG_INFO(0, path)
1999 	ZEND_ARG_INFO(0, flags)
2000 ZEND_END_ARG_INFO()
2001 
2002 ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_hasChildren, 0, 0, 0)
2003 	ZEND_ARG_INFO(0, allow_links)
2004 ZEND_END_ARG_INFO()
2005 
2006 ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_setFlags, 0, 0, 0)
2007 	ZEND_ARG_INFO(0, flags)
2008 ZEND_END_ARG_INFO()
2009 
2010 static const zend_function_entry spl_FilesystemIterator_functions[] = {
2011 	SPL_ME(FilesystemIterator, __construct,   arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
2012 	SPL_ME(FilesystemIterator, rewind,        arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2013 	SPL_ME(DirectoryIterator,  next,          arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2014 	SPL_ME(FilesystemIterator, key,           arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2015 	SPL_ME(FilesystemIterator, current,       arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2016 	SPL_ME(FilesystemIterator, getFlags,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2017 	SPL_ME(FilesystemIterator, setFlags,      arginfo_r_dir_setFlags, ZEND_ACC_PUBLIC)
2018 	PHP_FE_END
2019 };
2020 
2021 static const zend_function_entry spl_RecursiveDirectoryIterator_functions[] = {
2022 	SPL_ME(RecursiveDirectoryIterator, __construct,   arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
2023 	SPL_ME(RecursiveDirectoryIterator, hasChildren,   arginfo_r_dir_hasChildren, ZEND_ACC_PUBLIC)
2024 	SPL_ME(RecursiveDirectoryIterator, getChildren,   arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2025 	SPL_ME(RecursiveDirectoryIterator, getSubPath,    arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2026 	SPL_ME(RecursiveDirectoryIterator, getSubPathname,arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
2027 	PHP_FE_END
2028 };
2029 
2030 #ifdef HAVE_GLOB
2031 static const zend_function_entry spl_GlobIterator_functions[] = {
2032 	SPL_ME(GlobIterator, __construct,   arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
2033 	SPL_ME(GlobIterator, count,         arginfo_splfileinfo_void,  ZEND_ACC_PUBLIC)
2034 	PHP_FE_END
2035 };
2036 #endif
2037 /* }}} */
2038 
spl_filesystem_file_read(spl_filesystem_object * intern,int silent)2039 static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /* {{{ */
2040 {
2041 	char *buf;
2042 	size_t line_len = 0;
2043 	zend_long line_add = (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) ? 1 : 0;
2044 
2045 	spl_filesystem_file_free_line(intern);
2046 
2047 	if (php_stream_eof(intern->u.file.stream)) {
2048 		if (!silent) {
2049 			zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", intern->file_name);
2050 		}
2051 		return FAILURE;
2052 	}
2053 
2054 	if (intern->u.file.max_line_len > 0) {
2055 		buf = safe_emalloc((intern->u.file.max_line_len + 1), sizeof(char), 0);
2056 		if (php_stream_get_line(intern->u.file.stream, buf, intern->u.file.max_line_len + 1, &line_len) == NULL) {
2057 			efree(buf);
2058 			buf = NULL;
2059 		} else {
2060 			buf[line_len] = '\0';
2061 		}
2062 	} else {
2063 		buf = php_stream_get_line(intern->u.file.stream, NULL, 0, &line_len);
2064 	}
2065 
2066 	if (!buf) {
2067 		intern->u.file.current_line = estrdup("");
2068 		intern->u.file.current_line_len = 0;
2069 	} else {
2070 		if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
2071 			line_len = strcspn(buf, "\r\n");
2072 			buf[line_len] = '\0';
2073 		}
2074 
2075 		intern->u.file.current_line = buf;
2076 		intern->u.file.current_line_len = line_len;
2077 	}
2078 	intern->u.file.current_line_num += line_add;
2079 
2080 	return SUCCESS;
2081 } /* }}} */
2082 
spl_filesystem_file_call(spl_filesystem_object * intern,zend_function * func_ptr,int pass_num_args,zval * return_value,zval * arg2)2083 static int spl_filesystem_file_call(spl_filesystem_object *intern, zend_function *func_ptr, int pass_num_args, zval *return_value, zval *arg2) /* {{{ */
2084 {
2085 	zend_fcall_info fci;
2086 	zend_fcall_info_cache fcic;
2087 	zval *zresource_ptr = &intern->u.file.zresource, retval;
2088 	int result;
2089 	int num_args = pass_num_args + (arg2 ? 2 : 1);
2090 
2091 	zval *params = (zval*)safe_emalloc(num_args, sizeof(zval), 0);
2092 
2093 	params[0] = *zresource_ptr;
2094 
2095 	if (arg2) {
2096 		params[1] = *arg2;
2097 	}
2098 
2099 	if (zend_get_parameters_array_ex(pass_num_args, params + (arg2 ? 2 : 1)) != SUCCESS) {
2100 		efree(params);
2101 		WRONG_PARAM_COUNT_WITH_RETVAL(FAILURE);
2102 	}
2103 
2104 	ZVAL_UNDEF(&retval);
2105 
2106 	fci.size = sizeof(fci);
2107 	fci.object = NULL;
2108 	fci.retval = &retval;
2109 	fci.param_count = num_args;
2110 	fci.params = params;
2111 	fci.no_separation = 1;
2112 	ZVAL_STR(&fci.function_name, func_ptr->common.function_name);
2113 
2114 	fcic.initialized = 1;
2115 	fcic.function_handler = func_ptr;
2116 	fcic.calling_scope = NULL;
2117 	fcic.called_scope = NULL;
2118 	fcic.object = NULL;
2119 
2120 	result = zend_call_function(&fci, &fcic);
2121 
2122 	if (result == FAILURE || Z_ISUNDEF(retval)) {
2123 		RETVAL_FALSE;
2124 	} else {
2125 		ZVAL_ZVAL(return_value, &retval, 0, 0);
2126 	}
2127 
2128 	efree(params);
2129 	return result;
2130 } /* }}} */
2131 
2132 #define FileFunctionCall(func_name, pass_num_args, arg2) /* {{{ */ \
2133 { \
2134 	zend_function *func_ptr; \
2135 	func_ptr = (zend_function *)zend_hash_str_find_ptr(EG(function_table), #func_name, sizeof(#func_name) - 1); \
2136 	if (func_ptr == NULL) { \
2137 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Internal error, function '%s' not found. Please report", #func_name); \
2138 		return; \
2139 	} \
2140 	spl_filesystem_file_call(intern, func_ptr, pass_num_args, return_value, arg2); \
2141 } /* }}} */
2142 
spl_filesystem_file_read_csv(spl_filesystem_object * intern,char delimiter,char enclosure,char escape,zval * return_value)2143 static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, char escape, zval *return_value) /* {{{ */
2144 {
2145 	int ret = SUCCESS;
2146 	zval *value;
2147 
2148 	do {
2149 		ret = spl_filesystem_file_read(intern, 1);
2150 	} while (ret == SUCCESS && !intern->u.file.current_line_len && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
2151 
2152 	if (ret == SUCCESS) {
2153 		size_t buf_len = intern->u.file.current_line_len;
2154 		char *buf = estrndup(intern->u.file.current_line, buf_len);
2155 
2156 		if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2157 			zval_ptr_dtor(&intern->u.file.current_zval);
2158 			ZVAL_UNDEF(&intern->u.file.current_zval);
2159 		}
2160 
2161 		php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, &intern->u.file.current_zval);
2162 		if (return_value) {
2163 			zval_ptr_dtor(return_value);
2164 			value = &intern->u.file.current_zval;
2165 			ZVAL_DEREF(value);
2166 			ZVAL_COPY(return_value, value);
2167 		}
2168 	}
2169 	return ret;
2170 }
2171 /* }}} */
2172 
spl_filesystem_file_read_line_ex(zval * this_ptr,spl_filesystem_object * intern,int silent)2173 static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
2174 {
2175 	zval retval;
2176 
2177 	/* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
2178 	if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) {
2179 		if (php_stream_eof(intern->u.file.stream)) {
2180 			if (!silent) {
2181 				zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", intern->file_name);
2182 			}
2183 			return FAILURE;
2184 		}
2185 		if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
2186 			return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL);
2187 		} else {
2188 			zend_execute_data *execute_data = EG(current_execute_data);
2189 			zend_call_method_with_0_params(this_ptr, Z_OBJCE(EX(This)), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
2190 		}
2191 		if (!Z_ISUNDEF(retval)) {
2192 			if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
2193 				intern->u.file.current_line_num++;
2194 			}
2195 			spl_filesystem_file_free_line(intern);
2196 			if (Z_TYPE(retval) == IS_STRING) {
2197 				intern->u.file.current_line = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
2198 				intern->u.file.current_line_len = Z_STRLEN(retval);
2199 			} else {
2200 				zval *value = &retval;
2201 
2202 				ZVAL_DEREF(value);
2203 				ZVAL_COPY(&intern->u.file.current_zval, value);
2204 			}
2205 			zval_ptr_dtor(&retval);
2206 			return SUCCESS;
2207 		} else {
2208 			return FAILURE;
2209 		}
2210 	} else {
2211 		return spl_filesystem_file_read(intern, silent);
2212 	}
2213 } /* }}} */
2214 
spl_filesystem_file_is_empty_line(spl_filesystem_object * intern)2215 static int spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /* {{{ */
2216 {
2217 	if (intern->u.file.current_line) {
2218 		return intern->u.file.current_line_len == 0;
2219 	} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2220 		switch(Z_TYPE(intern->u.file.current_zval)) {
2221 			case IS_STRING:
2222 				return Z_STRLEN(intern->u.file.current_zval) == 0;
2223 			case IS_ARRAY:
2224 				if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
2225 						&& zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 1) {
2226 					uint32_t idx = 0;
2227 					zval *first;
2228 
2229 					while (Z_ISUNDEF(Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val)) {
2230 						idx++;
2231 					}
2232 					first = &Z_ARRVAL(intern->u.file.current_zval)->arData[idx].val;
2233 					return Z_TYPE_P(first) == IS_STRING && Z_STRLEN_P(first) == 0;
2234 				}
2235 				return zend_hash_num_elements(Z_ARRVAL(intern->u.file.current_zval)) == 0;
2236 			case IS_NULL:
2237 				return 1;
2238 			default:
2239 				return 0;
2240 		}
2241 	} else {
2242 		return 1;
2243 	}
2244 }
2245 /* }}} */
2246 
spl_filesystem_file_read_line(zval * this_ptr,spl_filesystem_object * intern,int silent)2247 static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
2248 {
2249 	int ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
2250 
2251 	while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && spl_filesystem_file_is_empty_line(intern)) {
2252 		spl_filesystem_file_free_line(intern);
2253 		ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
2254 	}
2255 
2256 	return ret;
2257 }
2258 /* }}} */
2259 
spl_filesystem_file_rewind(zval * this_ptr,spl_filesystem_object * intern)2260 static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
2261 {
2262 	if(!intern->u.file.stream) {
2263 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2264 		return;
2265 	}
2266 	if (-1 == php_stream_rewind(intern->u.file.stream)) {
2267 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot rewind file %s", intern->file_name);
2268 	} else {
2269 		spl_filesystem_file_free_line(intern);
2270 		intern->u.file.current_line_num = 0;
2271 	}
2272 	if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2273 		spl_filesystem_file_read_line(this_ptr, intern, 1);
2274 	}
2275 } /* }}} */
2276 
2277 /* {{{ proto void SplFileObject::__construct(string filename [, string mode = 'r' [, bool use_include_path  [, resource context]]]])
2278    Construct a new file object */
SPL_METHOD(SplFileObject,__construct)2279 SPL_METHOD(SplFileObject, __construct)
2280 {
2281 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2282 	zend_bool use_include_path = 0;
2283 	char *p1, *p2;
2284 	char *tmp_path;
2285 	size_t   tmp_path_len;
2286 	zend_error_handling error_handling;
2287 
2288 	intern->u.file.open_mode = NULL;
2289 	intern->u.file.open_mode_len = 0;
2290 
2291 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|sbr!",
2292 			&intern->file_name, &intern->file_name_len,
2293 			&intern->u.file.open_mode, &intern->u.file.open_mode_len,
2294 			&use_include_path, &intern->u.file.zcontext) == FAILURE) {
2295 		intern->u.file.open_mode = NULL;
2296 		intern->file_name = NULL;
2297 		return;
2298 	}
2299 
2300 	if (intern->u.file.open_mode == NULL) {
2301 		intern->u.file.open_mode = "r";
2302 		intern->u.file.open_mode_len = 1;
2303 	}
2304 
2305 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
2306 
2307 	if (spl_filesystem_file_open(intern, use_include_path, 0) == SUCCESS) {
2308 		tmp_path_len = strlen(intern->u.file.stream->orig_path);
2309 
2310 		if (tmp_path_len > 1 && IS_SLASH_AT(intern->u.file.stream->orig_path, tmp_path_len-1)) {
2311 			tmp_path_len--;
2312 		}
2313 
2314 		tmp_path = estrndup(intern->u.file.stream->orig_path, tmp_path_len);
2315 
2316 		p1 = strrchr(tmp_path, '/');
2317 #if defined(PHP_WIN32)
2318 		p2 = strrchr(tmp_path, '\\');
2319 #else
2320 		p2 = 0;
2321 #endif
2322 		if (p1 || p2) {
2323 			intern->_path_len = ((p1 > p2 ? p1 : p2) - tmp_path);
2324 		} else {
2325 			intern->_path_len = 0;
2326 		}
2327 
2328 		efree(tmp_path);
2329 
2330 		intern->_path = estrndup(intern->u.file.stream->orig_path, intern->_path_len);
2331 	}
2332 
2333 	zend_restore_error_handling(&error_handling);
2334 
2335 } /* }}} */
2336 
2337 /* {{{ proto void SplTempFileObject::__construct([int max_memory])
2338    Construct a new temp file object */
SPL_METHOD(SplTempFileObject,__construct)2339 SPL_METHOD(SplTempFileObject, __construct)
2340 {
2341 	zend_long max_memory = PHP_STREAM_MAX_MEM;
2342 	char tmp_fname[48];
2343 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2344 	zend_error_handling error_handling;
2345 
2346 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|l", &max_memory) == FAILURE) {
2347 		return;
2348 	}
2349 
2350 	if (max_memory < 0) {
2351 		intern->file_name = "php://memory";
2352 		intern->file_name_len = 12;
2353 	} else if (ZEND_NUM_ARGS()) {
2354 		intern->file_name_len = slprintf(tmp_fname, sizeof(tmp_fname), "php://temp/maxmemory:" ZEND_LONG_FMT, max_memory);
2355 		intern->file_name = tmp_fname;
2356 	} else {
2357 		intern->file_name = "php://temp";
2358 		intern->file_name_len = 10;
2359 	}
2360 	intern->u.file.open_mode = "wb";
2361 	intern->u.file.open_mode_len = 1;
2362 
2363 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
2364 	if (spl_filesystem_file_open(intern, 0, 0) == SUCCESS) {
2365 		intern->_path_len = 0;
2366 		intern->_path = estrndup("", 0);
2367 	}
2368 	zend_restore_error_handling(&error_handling);
2369 } /* }}} */
2370 
2371 /* {{{ proto void SplFileObject::rewind()
2372    Rewind the file and read the first line */
SPL_METHOD(SplFileObject,rewind)2373 SPL_METHOD(SplFileObject, rewind)
2374 {
2375 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2376 
2377 	if (zend_parse_parameters_none() == FAILURE) {
2378 		return;
2379 	}
2380 
2381 	spl_filesystem_file_rewind(getThis(), intern);
2382 } /* }}} */
2383 
2384 /* {{{ proto void SplFileObject::eof()
2385    Return whether end of file is reached */
SPL_METHOD(SplFileObject,eof)2386 SPL_METHOD(SplFileObject, eof)
2387 {
2388 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2389 
2390 	if (zend_parse_parameters_none() == FAILURE) {
2391 		return;
2392 	}
2393 
2394 	if(!intern->u.file.stream) {
2395 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2396 		return;
2397 	}
2398 
2399 	RETURN_BOOL(php_stream_eof(intern->u.file.stream));
2400 } /* }}} */
2401 
2402 /* {{{ proto void SplFileObject::valid()
2403    Return !eof() */
SPL_METHOD(SplFileObject,valid)2404 SPL_METHOD(SplFileObject, valid)
2405 {
2406 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2407 
2408 	if (zend_parse_parameters_none() == FAILURE) {
2409 		return;
2410 	}
2411 
2412 	if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2413 		RETURN_BOOL(intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval));
2414 	} else {
2415 		if(!intern->u.file.stream) {
2416 			RETURN_FALSE;
2417 		}
2418 		RETVAL_BOOL(!php_stream_eof(intern->u.file.stream));
2419 	}
2420 } /* }}} */
2421 
2422 /* {{{ proto string SplFileObject::fgets()
2423    Rturn next line from file */
SPL_METHOD(SplFileObject,fgets)2424 SPL_METHOD(SplFileObject, fgets)
2425 {
2426 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2427 
2428 	if (zend_parse_parameters_none() == FAILURE) {
2429 		return;
2430 	}
2431 
2432 	if(!intern->u.file.stream) {
2433 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2434 		return;
2435 	}
2436 
2437 	if (spl_filesystem_file_read(intern, 0) == FAILURE) {
2438 		RETURN_FALSE;
2439 	}
2440 	RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
2441 } /* }}} */
2442 
2443 /* {{{ proto string SplFileObject::current()
2444    Return current line from file */
SPL_METHOD(SplFileObject,current)2445 SPL_METHOD(SplFileObject, current)
2446 {
2447 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2448 
2449 	if (zend_parse_parameters_none() == FAILURE) {
2450 		return;
2451 	}
2452 
2453 	if(!intern->u.file.stream) {
2454 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2455 		return;
2456 	}
2457 
2458 	if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2459 		spl_filesystem_file_read_line(getThis(), intern, 1);
2460 	}
2461 	if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || Z_ISUNDEF(intern->u.file.current_zval))) {
2462 		RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
2463 	} else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2464 		zval *value = &intern->u.file.current_zval;
2465 
2466 		ZVAL_DEREF(value);
2467 		ZVAL_COPY(return_value, value);
2468 		return;
2469 	}
2470 	RETURN_FALSE;
2471 } /* }}} */
2472 
2473 /* {{{ proto int SplFileObject::key()
2474    Return line number */
SPL_METHOD(SplFileObject,key)2475 SPL_METHOD(SplFileObject, key)
2476 {
2477 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2478 
2479 	if (zend_parse_parameters_none() == FAILURE) {
2480 		return;
2481 	}
2482 
2483 /*	Do not read the next line to support correct counting with fgetc()
2484 	if (!intern->current_line) {
2485 		spl_filesystem_file_read_line(getThis(), intern, 1);
2486 	} */
2487 	RETURN_LONG(intern->u.file.current_line_num);
2488 } /* }}} */
2489 
2490 /* {{{ proto void SplFileObject::next()
2491    Read next line */
SPL_METHOD(SplFileObject,next)2492 SPL_METHOD(SplFileObject, next)
2493 {
2494 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2495 
2496 	if (zend_parse_parameters_none() == FAILURE) {
2497 		return;
2498 	}
2499 
2500 	spl_filesystem_file_free_line(intern);
2501 	if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2502 		spl_filesystem_file_read_line(getThis(), intern, 1);
2503 	}
2504 	intern->u.file.current_line_num++;
2505 } /* }}} */
2506 
2507 /* {{{ proto void SplFileObject::setFlags(int flags)
2508    Set file handling flags */
SPL_METHOD(SplFileObject,setFlags)2509 SPL_METHOD(SplFileObject, setFlags)
2510 {
2511 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2512 
2513 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
2514 		return;
2515 	}
2516 } /* }}} */
2517 
2518 /* {{{ proto int SplFileObject::getFlags()
2519    Get file handling flags */
SPL_METHOD(SplFileObject,getFlags)2520 SPL_METHOD(SplFileObject, getFlags)
2521 {
2522 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2523 
2524 	if (zend_parse_parameters_none() == FAILURE) {
2525 		return;
2526 	}
2527 
2528 	RETURN_LONG(intern->flags & SPL_FILE_OBJECT_MASK);
2529 } /* }}} */
2530 
2531 /* {{{ proto void SplFileObject::setMaxLineLen(int max_len)
2532    Set maximum line length */
SPL_METHOD(SplFileObject,setMaxLineLen)2533 SPL_METHOD(SplFileObject, setMaxLineLen)
2534 {
2535 	zend_long max_len;
2536 
2537 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2538 
2539 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max_len) == FAILURE) {
2540 		return;
2541 	}
2542 
2543 	if (max_len < 0) {
2544 		zend_throw_exception_ex(spl_ce_DomainException, 0, "Maximum line length must be greater than or equal zero");
2545 		return;
2546 	}
2547 
2548 	intern->u.file.max_line_len = max_len;
2549 } /* }}} */
2550 
2551 /* {{{ proto int SplFileObject::getMaxLineLen()
2552    Get maximum line length */
SPL_METHOD(SplFileObject,getMaxLineLen)2553 SPL_METHOD(SplFileObject, getMaxLineLen)
2554 {
2555 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2556 
2557 	if (zend_parse_parameters_none() == FAILURE) {
2558 		return;
2559 	}
2560 
2561 	RETURN_LONG((zend_long)intern->u.file.max_line_len);
2562 } /* }}} */
2563 
2564 /* {{{ proto bool SplFileObject::hasChildren()
2565    Return false */
SPL_METHOD(SplFileObject,hasChildren)2566 SPL_METHOD(SplFileObject, hasChildren)
2567 {
2568 	if (zend_parse_parameters_none() == FAILURE) {
2569 		return;
2570 	}
2571 
2572 	RETURN_FALSE;
2573 } /* }}} */
2574 
2575 /* {{{ proto bool SplFileObject::getChildren()
2576    Read NULL */
SPL_METHOD(SplFileObject,getChildren)2577 SPL_METHOD(SplFileObject, getChildren)
2578 {
2579 	if (zend_parse_parameters_none() == FAILURE) {
2580 		return;
2581 	}
2582 	/* return NULL */
2583 } /* }}} */
2584 
2585 /* {{{ FileFunction */
2586 #define FileFunction(func_name) \
2587 SPL_METHOD(SplFileObject, func_name) \
2588 { \
2589 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis()); \
2590 	FileFunctionCall(func_name, ZEND_NUM_ARGS(), NULL); \
2591 }
2592 /* }}} */
2593 
2594 /* {{{ proto array SplFileObject::fgetcsv([string delimiter [, string enclosure [, escape = '\\']]])
2595    Return current line as csv */
SPL_METHOD(SplFileObject,fgetcsv)2596 SPL_METHOD(SplFileObject, fgetcsv)
2597 {
2598 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2599 	char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
2600 	char *delim = NULL, *enclo = NULL, *esc = NULL;
2601 	size_t d_len = 0, e_len = 0, esc_len = 0;
2602 
2603 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
2604 
2605 		if(!intern->u.file.stream) {
2606 			zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2607 			return;
2608 		}
2609 
2610 		switch(ZEND_NUM_ARGS())
2611 		{
2612 		case 3:
2613 			if (esc_len != 1) {
2614 				php_error_docref(NULL, E_WARNING, "escape must be a character");
2615 				RETURN_FALSE;
2616 			}
2617 			escape = esc[0];
2618 			/* no break */
2619 		case 2:
2620 			if (e_len != 1) {
2621 				php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2622 				RETURN_FALSE;
2623 			}
2624 			enclosure = enclo[0];
2625 			/* no break */
2626 		case 1:
2627 			if (d_len != 1) {
2628 				php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2629 				RETURN_FALSE;
2630 			}
2631 			delimiter = delim[0];
2632 			/* no break */
2633 		case 0:
2634 			break;
2635 		}
2636 		spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value);
2637 	}
2638 }
2639 /* }}} */
2640 
2641 /* {{{ proto int SplFileObject::fputcsv(array fields, [string delimiter [, string enclosure [, string escape]]])
2642    Output a field array as a CSV line */
SPL_METHOD(SplFileObject,fputcsv)2643 SPL_METHOD(SplFileObject, fputcsv)
2644 {
2645 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2646 	char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
2647 	char *delim = NULL, *enclo = NULL, *esc = NULL;
2648 	size_t d_len = 0, e_len = 0, esc_len = 0;
2649 	zend_long ret;
2650 	zval *fields = NULL;
2651 
2652 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sss", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
2653 		switch(ZEND_NUM_ARGS())
2654 		{
2655 		case 4:
2656 			if (esc_len != 1) {
2657 				php_error_docref(NULL, E_WARNING, "escape must be a character");
2658 				RETURN_FALSE;
2659 			}
2660 			escape = esc[0];
2661 			/* no break */
2662 		case 3:
2663 			if (e_len != 1) {
2664 				php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2665 				RETURN_FALSE;
2666 			}
2667 			enclosure = enclo[0];
2668 			/* no break */
2669 		case 2:
2670 			if (d_len != 1) {
2671 				php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2672 				RETURN_FALSE;
2673 			}
2674 			delimiter = delim[0];
2675 			/* no break */
2676 		case 1:
2677 		case 0:
2678 			break;
2679 		}
2680 		ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape);
2681 		RETURN_LONG(ret);
2682 	}
2683 }
2684 /* }}} */
2685 
2686 /* {{{ proto void SplFileObject::setCsvControl([string delimiter [, string enclosure [, string escape ]]])
2687    Set the delimiter, enclosure and escape character used in fgetcsv */
SPL_METHOD(SplFileObject,setCsvControl)2688 SPL_METHOD(SplFileObject, setCsvControl)
2689 {
2690 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2691 	char delimiter = ',', enclosure = '"', escape='\\';
2692 	char *delim = NULL, *enclo = NULL, *esc = NULL;
2693 	size_t d_len = 0, e_len = 0, esc_len = 0;
2694 
2695 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
2696 		switch(ZEND_NUM_ARGS())
2697 		{
2698 		case 3:
2699 			if (esc_len != 1) {
2700 				php_error_docref(NULL, E_WARNING, "escape must be a character");
2701 				RETURN_FALSE;
2702 			}
2703 			escape = esc[0];
2704 			/* no break */
2705 		case 2:
2706 			if (e_len != 1) {
2707 				php_error_docref(NULL, E_WARNING, "enclosure must be a character");
2708 				RETURN_FALSE;
2709 			}
2710 			enclosure = enclo[0];
2711 			/* no break */
2712 		case 1:
2713 			if (d_len != 1) {
2714 				php_error_docref(NULL, E_WARNING, "delimiter must be a character");
2715 				RETURN_FALSE;
2716 			}
2717 			delimiter = delim[0];
2718 			/* no break */
2719 		case 0:
2720 			break;
2721 		}
2722 		intern->u.file.delimiter = delimiter;
2723 		intern->u.file.enclosure = enclosure;
2724 		intern->u.file.escape    = escape;
2725 	}
2726 }
2727 /* }}} */
2728 
2729 /* {{{ proto array SplFileObject::getCsvControl()
2730    Get the delimiter, enclosure and escape character used in fgetcsv */
SPL_METHOD(SplFileObject,getCsvControl)2731 SPL_METHOD(SplFileObject, getCsvControl)
2732 {
2733 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2734 	char delimiter[2], enclosure[2], escape[2];
2735 
2736 	array_init(return_value);
2737 
2738 	delimiter[0] = intern->u.file.delimiter;
2739 	delimiter[1] = '\0';
2740 	enclosure[0] = intern->u.file.enclosure;
2741 	enclosure[1] = '\0';
2742 	escape[0] = intern->u.file.escape;
2743 	escape[1] = '\0';
2744 
2745 	add_next_index_string(return_value, delimiter);
2746 	add_next_index_string(return_value, enclosure);
2747 	add_next_index_string(return_value, escape);
2748 }
2749 /* }}} */
2750 
2751 /* {{{ proto bool SplFileObject::flock(int operation [, int &wouldblock])
2752    Portable file locking */
2753 FileFunction(flock)
2754 /* }}} */
2755 
2756 /* {{{ proto bool SplFileObject::fflush()
2757    Flush the file */
SPL_METHOD(SplFileObject,fflush)2758 SPL_METHOD(SplFileObject, fflush)
2759 {
2760 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2761 
2762 	if(!intern->u.file.stream) {
2763 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2764 		return;
2765 	}
2766 
2767 	RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
2768 } /* }}} */
2769 
2770 /* {{{ proto int SplFileObject::ftell()
2771    Return current file position */
SPL_METHOD(SplFileObject,ftell)2772 SPL_METHOD(SplFileObject, ftell)
2773 {
2774 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2775 	zend_long ret;
2776 
2777 	if(!intern->u.file.stream) {
2778 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2779 		return;
2780 	}
2781 
2782 	ret = php_stream_tell(intern->u.file.stream);
2783 
2784 	if (ret == -1) {
2785 		RETURN_FALSE;
2786 	} else {
2787 		RETURN_LONG(ret);
2788 	}
2789 } /* }}} */
2790 
2791 /* {{{ proto int SplFileObject::fseek(int pos [, int whence = SEEK_SET])
2792    Return current file position */
SPL_METHOD(SplFileObject,fseek)2793 SPL_METHOD(SplFileObject, fseek)
2794 {
2795 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2796 	zend_long pos, whence = SEEK_SET;
2797 
2798 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &pos, &whence) == FAILURE) {
2799 		return;
2800 	}
2801 
2802 	if(!intern->u.file.stream) {
2803 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2804 		return;
2805 	}
2806 
2807 	spl_filesystem_file_free_line(intern);
2808 	RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, (int)whence));
2809 } /* }}} */
2810 
2811 /* {{{ proto int SplFileObject::fgetc()
2812    Get a character form the file */
SPL_METHOD(SplFileObject,fgetc)2813 SPL_METHOD(SplFileObject, fgetc)
2814 {
2815 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2816 	char buf[2];
2817 	int result;
2818 
2819 	if(!intern->u.file.stream) {
2820 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2821 		return;
2822 	}
2823 
2824 	spl_filesystem_file_free_line(intern);
2825 
2826 	result = php_stream_getc(intern->u.file.stream);
2827 
2828 	if (result == EOF) {
2829 		RETVAL_FALSE;
2830 	} else {
2831 		if (result == '\n') {
2832 			intern->u.file.current_line_num++;
2833 		}
2834 		buf[0] = result;
2835 		buf[1] = '\0';
2836 
2837 		RETURN_STRINGL(buf, 1);
2838 	}
2839 } /* }}} */
2840 
2841 /* {{{ proto string SplFileObject::fgetss([string allowable_tags])
2842    Get a line from file pointer and strip HTML tags */
SPL_METHOD(SplFileObject,fgetss)2843 SPL_METHOD(SplFileObject, fgetss)
2844 {
2845 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2846 	zval arg2;
2847 
2848 	if(!intern->u.file.stream) {
2849 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2850 		return;
2851 	}
2852 
2853 	if (intern->u.file.max_line_len > 0) {
2854 		ZVAL_LONG(&arg2, intern->u.file.max_line_len);
2855 	} else {
2856 		ZVAL_LONG(&arg2, 1024);
2857 	}
2858 
2859 	spl_filesystem_file_free_line(intern);
2860 	intern->u.file.current_line_num++;
2861 
2862 	FileFunctionCall(fgetss, ZEND_NUM_ARGS(), &arg2);
2863 } /* }}} */
2864 
2865 /* {{{ proto int SplFileObject::fpassthru()
2866    Output all remaining data from a file pointer */
SPL_METHOD(SplFileObject,fpassthru)2867 SPL_METHOD(SplFileObject, fpassthru)
2868 {
2869 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2870 
2871 	if(!intern->u.file.stream) {
2872 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2873 		return;
2874 	}
2875 
2876 	RETURN_LONG(php_stream_passthru(intern->u.file.stream));
2877 } /* }}} */
2878 
2879 /* {{{ proto bool SplFileObject::fscanf(string format [, string ...])
2880    Implements a mostly ANSI compatible fscanf() */
SPL_METHOD(SplFileObject,fscanf)2881 SPL_METHOD(SplFileObject, fscanf)
2882 {
2883 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2884 
2885 	if(!intern->u.file.stream) {
2886 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2887 		return;
2888 	}
2889 
2890 	spl_filesystem_file_free_line(intern);
2891 	intern->u.file.current_line_num++;
2892 
2893 	FileFunctionCall(fscanf, ZEND_NUM_ARGS(), NULL);
2894 }
2895 /* }}} */
2896 
2897 /* {{{ proto mixed SplFileObject::fwrite(string str [, int length])
2898    Binary-safe file write */
SPL_METHOD(SplFileObject,fwrite)2899 SPL_METHOD(SplFileObject, fwrite)
2900 {
2901 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2902 	char *str;
2903 	size_t str_len;
2904 	zend_long length = 0;
2905 
2906 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str, &str_len, &length) == FAILURE) {
2907 		return;
2908 	}
2909 
2910 	if(!intern->u.file.stream) {
2911 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2912 		return;
2913 	}
2914 
2915 	if (ZEND_NUM_ARGS() > 1) {
2916 		if (length >= 0) {
2917 			str_len = MIN((size_t)length, str_len);
2918 		} else {
2919 			/* Negative length given, nothing to write */
2920 			str_len = 0;
2921 		}
2922 	}
2923 	if (!str_len) {
2924 		RETURN_LONG(0);
2925 	}
2926 
2927 	RETURN_LONG(php_stream_write(intern->u.file.stream, str, str_len));
2928 } /* }}} */
2929 
SPL_METHOD(SplFileObject,fread)2930 SPL_METHOD(SplFileObject, fread)
2931 {
2932 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2933 	zend_long length = 0;
2934 
2935 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &length) == FAILURE) {
2936 		return;
2937 	}
2938 
2939 	if(!intern->u.file.stream) {
2940 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2941 		return;
2942 	}
2943 
2944 	if (length <= 0) {
2945 		php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
2946 		RETURN_FALSE;
2947 	}
2948 
2949 	ZVAL_NEW_STR(return_value, zend_string_alloc(length, 0));
2950 	Z_STRLEN_P(return_value) = php_stream_read(intern->u.file.stream, Z_STRVAL_P(return_value), length);
2951 
2952 	/* needed because recv/read/gzread doesnt put a null at the end*/
2953 	Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
2954 }
2955 
2956 /* {{{ proto bool SplFileObject::fstat()
2957    Stat() on a filehandle */
2958 FileFunction(fstat)
2959 /* }}} */
2960 
2961 /* {{{ proto bool SplFileObject::ftruncate(int size)
2962    Truncate file to 'size' length */
SPL_METHOD(SplFileObject,ftruncate)2963 SPL_METHOD(SplFileObject, ftruncate)
2964 {
2965 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2966 	zend_long size;
2967 
2968 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
2969 		return;
2970 	}
2971 
2972 	if(!intern->u.file.stream) {
2973 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2974 		return;
2975 	}
2976 
2977 	if (!php_stream_truncate_supported(intern->u.file.stream)) {
2978 		zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't truncate file %s", intern->file_name);
2979 		RETURN_FALSE;
2980 	}
2981 
2982 	RETURN_BOOL(0 == php_stream_truncate_set_size(intern->u.file.stream, size));
2983 } /* }}} */
2984 
2985 /* {{{ proto void SplFileObject::seek(int line_pos)
2986    Seek to specified line */
SPL_METHOD(SplFileObject,seek)2987 SPL_METHOD(SplFileObject, seek)
2988 {
2989 	spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(getThis());
2990 	zend_long line_pos;
2991 
2992 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
2993 		return;
2994 	}
2995 	if(!intern->u.file.stream) {
2996 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2997 		return;
2998 	}
2999 
3000 	if (line_pos < 0) {
3001 		zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't seek file %s to negative line " ZEND_LONG_FMT, intern->file_name, line_pos);
3002 		RETURN_FALSE;
3003 	}
3004 
3005 	spl_filesystem_file_rewind(getThis(), intern);
3006 
3007 	while(intern->u.file.current_line_num < line_pos) {
3008 		if (spl_filesystem_file_read_line(getThis(), intern, 1) == FAILURE) {
3009 			break;
3010 		}
3011 	}
3012 } /* }}} */
3013 
3014 /* {{{ Function/Class/Method definitions */
3015 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object___construct, 0, 0, 1)
3016 	ZEND_ARG_INFO(0, file_name)
3017 	ZEND_ARG_INFO(0, open_mode)
3018 	ZEND_ARG_INFO(0, use_include_path)
3019 	ZEND_ARG_INFO(0, context)
3020 ZEND_END_ARG_INFO()
3021 
3022 ZEND_BEGIN_ARG_INFO(arginfo_file_object_setFlags, 0)
3023 	ZEND_ARG_INFO(0, flags)
3024 ZEND_END_ARG_INFO()
3025 
3026 ZEND_BEGIN_ARG_INFO(arginfo_file_object_setMaxLineLen, 0)
3027 	ZEND_ARG_INFO(0, max_len)
3028 ZEND_END_ARG_INFO()
3029 
3030 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetcsv, 0, 0, 0)
3031 	ZEND_ARG_INFO(0, delimiter)
3032 	ZEND_ARG_INFO(0, enclosure)
3033 	ZEND_ARG_INFO(0, escape)
3034 ZEND_END_ARG_INFO()
3035 
3036 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fputcsv, 0, 0, 1)
3037 	ZEND_ARG_INFO(0, fields)
3038 	ZEND_ARG_INFO(0, delimiter)
3039 	ZEND_ARG_INFO(0, enclosure)
3040 	ZEND_ARG_INFO(0, escape)
3041 ZEND_END_ARG_INFO()
3042 
3043 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_flock, 0, 0, 1)
3044 	ZEND_ARG_INFO(0, operation)
3045 	ZEND_ARG_INFO(1, wouldblock)
3046 ZEND_END_ARG_INFO()
3047 
3048 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fseek, 0, 0, 1)
3049 	ZEND_ARG_INFO(0, pos)
3050 	ZEND_ARG_INFO(0, whence)
3051 ZEND_END_ARG_INFO()
3052 
3053 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetss, 0, 0, 0)
3054 	ZEND_ARG_INFO(0, allowable_tags)
3055 ZEND_END_ARG_INFO()
3056 
3057 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fscanf, 0, 0, 1)
3058 	ZEND_ARG_INFO(0, format)
3059 	ZEND_ARG_VARIADIC_INFO(1, vars)
3060 ZEND_END_ARG_INFO()
3061 
3062 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fwrite, 0, 0, 1)
3063 	ZEND_ARG_INFO(0, str)
3064 	ZEND_ARG_INFO(0, length)
3065 ZEND_END_ARG_INFO()
3066 
3067 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fread, 0, 0, 1)
3068 	ZEND_ARG_INFO(0, length)
3069 ZEND_END_ARG_INFO()
3070 
3071 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_ftruncate, 0, 0, 1)
3072 	ZEND_ARG_INFO(0, size)
3073 ZEND_END_ARG_INFO()
3074 
3075 ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_seek, 0, 0, 1)
3076 	ZEND_ARG_INFO(0, line_pos)
3077 ZEND_END_ARG_INFO()
3078 
3079 static const zend_function_entry spl_SplFileObject_functions[] = {
3080 	SPL_ME(SplFileObject, __construct,    arginfo_file_object___construct,   ZEND_ACC_PUBLIC)
3081 	SPL_ME(SplFileObject, rewind,         arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3082 	SPL_ME(SplFileObject, eof,            arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3083 	SPL_ME(SplFileObject, valid,          arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3084 	SPL_ME(SplFileObject, fgets,          arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3085 	SPL_ME(SplFileObject, fgetcsv,        arginfo_file_object_fgetcsv,       ZEND_ACC_PUBLIC)
3086 	SPL_ME(SplFileObject, fputcsv,        arginfo_file_object_fputcsv,       ZEND_ACC_PUBLIC)
3087 	SPL_ME(SplFileObject, setCsvControl,  arginfo_file_object_fgetcsv,       ZEND_ACC_PUBLIC)
3088 	SPL_ME(SplFileObject, getCsvControl,  arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3089 	SPL_ME(SplFileObject, flock,          arginfo_file_object_flock,         ZEND_ACC_PUBLIC)
3090 	SPL_ME(SplFileObject, fflush,         arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3091 	SPL_ME(SplFileObject, ftell,          arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3092 	SPL_ME(SplFileObject, fseek,          arginfo_file_object_fseek,         ZEND_ACC_PUBLIC)
3093 	SPL_ME(SplFileObject, fgetc,          arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3094 	SPL_ME(SplFileObject, fpassthru,      arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3095 	SPL_ME(SplFileObject, fgetss,         arginfo_file_object_fgetss,        ZEND_ACC_PUBLIC)
3096 	SPL_ME(SplFileObject, fscanf,         arginfo_file_object_fscanf,        ZEND_ACC_PUBLIC)
3097 	SPL_ME(SplFileObject, fwrite,         arginfo_file_object_fwrite,        ZEND_ACC_PUBLIC)
3098 	SPL_ME(SplFileObject, fread,          arginfo_file_object_fread,         ZEND_ACC_PUBLIC)
3099 	SPL_ME(SplFileObject, fstat,          arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3100 	SPL_ME(SplFileObject, ftruncate,      arginfo_file_object_ftruncate,     ZEND_ACC_PUBLIC)
3101 	SPL_ME(SplFileObject, current,        arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3102 	SPL_ME(SplFileObject, key,            arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3103 	SPL_ME(SplFileObject, next,           arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3104 	SPL_ME(SplFileObject, setFlags,       arginfo_file_object_setFlags,      ZEND_ACC_PUBLIC)
3105 	SPL_ME(SplFileObject, getFlags,       arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3106 	SPL_ME(SplFileObject, setMaxLineLen,  arginfo_file_object_setMaxLineLen, ZEND_ACC_PUBLIC)
3107 	SPL_ME(SplFileObject, getMaxLineLen,  arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3108 	SPL_ME(SplFileObject, hasChildren,    arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3109 	SPL_ME(SplFileObject, getChildren,    arginfo_splfileinfo_void,          ZEND_ACC_PUBLIC)
3110 	SPL_ME(SplFileObject, seek,           arginfo_file_object_seek,          ZEND_ACC_PUBLIC)
3111 	/* mappings */
3112 	SPL_MA(SplFileObject, getCurrentLine, SplFileObject, fgets,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
3113 	SPL_MA(SplFileObject, __toString,     SplFileObject, fgets,      arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
3114 	PHP_FE_END
3115 };
3116 
3117 ZEND_BEGIN_ARG_INFO_EX(arginfo_temp_file_object___construct, 0, 0, 0)
3118 	ZEND_ARG_INFO(0, max_memory)
3119 ZEND_END_ARG_INFO()
3120 
3121 static const zend_function_entry spl_SplTempFileObject_functions[] = {
3122 	SPL_ME(SplTempFileObject, __construct, arginfo_temp_file_object___construct,  ZEND_ACC_PUBLIC)
3123 	PHP_FE_END
3124 };
3125 /* }}} */
3126 
3127 /* {{{ PHP_MINIT_FUNCTION(spl_directory)
3128  */
PHP_MINIT_FUNCTION(spl_directory)3129 PHP_MINIT_FUNCTION(spl_directory)
3130 {
3131 	REGISTER_SPL_STD_CLASS_EX(SplFileInfo, spl_filesystem_object_new, spl_SplFileInfo_functions);
3132 	memcpy(&spl_filesystem_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
3133 	spl_filesystem_object_handlers.offset = XtOffsetOf(spl_filesystem_object, std);
3134 	spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
3135 	spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
3136 	spl_filesystem_object_handlers.get_debug_info  = spl_filesystem_object_get_debug_info;
3137 	spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
3138 	spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
3139 	spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
3140 	spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;
3141 
3142 
3143 	REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, spl_filesystem_object_new, spl_DirectoryIterator_functions);
3144 	zend_class_implements(spl_ce_DirectoryIterator, 1, zend_ce_iterator);
3145 	REGISTER_SPL_IMPLEMENTS(DirectoryIterator, SeekableIterator);
3146 
3147 	spl_ce_DirectoryIterator->get_iterator = spl_filesystem_dir_get_iterator;
3148 
3149 	REGISTER_SPL_SUB_CLASS_EX(FilesystemIterator, DirectoryIterator, spl_filesystem_object_new, spl_FilesystemIterator_functions);
3150 
3151 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_MODE_MASK",   SPL_FILE_DIR_CURRENT_MODE_MASK);
3152 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_PATHNAME", SPL_FILE_DIR_CURRENT_AS_PATHNAME);
3153 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_FILEINFO", SPL_FILE_DIR_CURRENT_AS_FILEINFO);
3154 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_SELF",     SPL_FILE_DIR_CURRENT_AS_SELF);
3155 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_MODE_MASK",       SPL_FILE_DIR_KEY_MODE_MASK);
3156 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_PATHNAME",     SPL_FILE_DIR_KEY_AS_PATHNAME);
3157 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "FOLLOW_SYMLINKS",     SPL_FILE_DIR_FOLLOW_SYMLINKS);
3158 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_FILENAME",     SPL_FILE_DIR_KEY_AS_FILENAME);
3159 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "NEW_CURRENT_AND_KEY", SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO);
3160 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "OTHER_MODE_MASK",     SPL_FILE_DIR_OTHERS_MASK);
3161 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "SKIP_DOTS",           SPL_FILE_DIR_SKIPDOTS);
3162 	REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "UNIX_PATHS",          SPL_FILE_DIR_UNIXPATHS);
3163 
3164 	spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;
3165 
3166 	REGISTER_SPL_SUB_CLASS_EX(RecursiveDirectoryIterator, FilesystemIterator, spl_filesystem_object_new, spl_RecursiveDirectoryIterator_functions);
3167 	REGISTER_SPL_IMPLEMENTS(RecursiveDirectoryIterator, RecursiveIterator);
3168 
3169 	memcpy(&spl_filesystem_object_check_handlers, &spl_filesystem_object_handlers, sizeof(zend_object_handlers));
3170 	spl_filesystem_object_check_handlers.clone_obj = NULL;
3171 	spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
3172 
3173 #ifdef HAVE_GLOB
3174 	REGISTER_SPL_SUB_CLASS_EX(GlobIterator, FilesystemIterator, spl_filesystem_object_new_check, spl_GlobIterator_functions);
3175 	REGISTER_SPL_IMPLEMENTS(GlobIterator, Countable);
3176 #endif
3177 
3178 	REGISTER_SPL_SUB_CLASS_EX(SplFileObject, SplFileInfo, spl_filesystem_object_new_check, spl_SplFileObject_functions);
3179 	REGISTER_SPL_IMPLEMENTS(SplFileObject, RecursiveIterator);
3180 	REGISTER_SPL_IMPLEMENTS(SplFileObject, SeekableIterator);
3181 
3182 	REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "DROP_NEW_LINE", SPL_FILE_OBJECT_DROP_NEW_LINE);
3183 	REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_AHEAD",    SPL_FILE_OBJECT_READ_AHEAD);
3184 	REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "SKIP_EMPTY",    SPL_FILE_OBJECT_SKIP_EMPTY);
3185 	REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_CSV",      SPL_FILE_OBJECT_READ_CSV);
3186 
3187 	REGISTER_SPL_SUB_CLASS_EX(SplTempFileObject, SplFileObject, spl_filesystem_object_new_check, spl_SplTempFileObject_functions);
3188 	return SUCCESS;
3189 }
3190 /* }}} */
3191 
3192 /*
3193  * Local variables:
3194  * tab-width: 4
3195  * c-basic-offset: 4
3196  * End:
3197  * vim600: noet sw=4 ts=4 fdm=marker
3198  * vim<600: noet sw=4 ts=4
3199  */
3200