Annotation of embedaddon/php/ext/spl/spl_directory.c, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>