From: Ilia Alshanetsky Date: Tue, 28 Jan 2003 01:48:57 +0000 (+0000) Subject: Added scandir() function, which allows quick retrieval of all files & X-Git-Tag: PHP_5_0_dev_before_13561_fix~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d0f43249010cd5f02140ed615194bb1a50bc79e;p=php Added scandir() function, which allows quick retrieval of all files & directories within the specified path and sort the output in alphabetical or reverse alphabetical order. --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 0f4c4f65b1..99aee756f9 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -724,6 +724,7 @@ function_entry basic_functions[] = { PHP_FE(rewinddir, NULL) PHP_STATIC_FE("readdir", php_if_readdir, NULL) PHP_FALIAS(dir, getdir, NULL) + PHP_FE(scandir, NULL) #ifdef HAVE_GLOB PHP_FE(glob, NULL) #endif diff --git a/ext/standard/dir.c b/ext/standard/dir.c index 778c504717..b274fed410 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -39,6 +39,10 @@ #include "win32/readdir.h" #endif +#if !HAVE_ALPHASORT || !HAVE_SCANDIR +#include "php_scandir.h" +#endif + #ifdef HAVE_GLOB #ifndef PHP_WIN32 #include @@ -422,6 +426,77 @@ PHP_FUNCTION(glob) /* }}} */ #endif +/* {{{ php_alphasortr +*/ +static int php_alphasortr(const struct dirent **a, const struct dirent **b) +{ + return strcoll((*b)->d_name, (*a)->d_name); +} +/* }}} */ + +/* {{{ proto array scandir(string dir [, int sorting_order]) + List files & directories inside the specified path */ +PHP_FUNCTION(scandir) +{ + char *dirn; + int dirn_len; + int flags = 0; + char *path; + struct dirent **namelist; + int n, i; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &dirn, &dirn_len, &flags) == FAILURE) { + return; + } + +#ifdef ZTS + if(!IS_ABSOLUTE_PATH(dirn, dirn_len)) { + path = expand_filepath(dirn, NULL TSRMLS_CC); + } else +#endif + path = dirn; + + if (PG(safe_mode) && (!php_checkuid(path, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + RETVAL_FALSE; + goto err; + } + if(php_check_open_basedir(path TSRMLS_CC)) { + RETVAL_FALSE; + goto err; + } + + if (!flags) { + n = scandir(path, &namelist, 0, alphasort); + } else { + n = scandir(path, &namelist, 0, php_alphasortr); + } + + if (n < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno)); + RETVAL_FALSE; + goto err; + } + + array_init(return_value); + + for (i = 0; i < n; i++) { + add_next_index_string(return_value, namelist[i]->d_name, 1); + free(namelist[i]); + } + + if (n) { + free(namelist); + } + +err: + if (path && path != dirn) { + efree(path); + } + + return; +} +/* }}} */ + /* * Local variables: * tab-width: 4 diff --git a/ext/standard/php_dir.h b/ext/standard/php_dir.h index 87015042bd..40c29ffd55 100644 --- a/ext/standard/php_dir.h +++ b/ext/standard/php_dir.h @@ -35,5 +35,6 @@ PHP_FUNCTION(rewinddir); PHP_NAMED_FUNCTION(php_if_readdir); PHP_FUNCTION(getdir); PHP_FUNCTION(glob); +PHP_FUNCTION(scandir); #endif /* PHP_DIR_H */