summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlia Alshanetsky <iliaa@php.net>2003-01-28 01:48:57 +0000
committerIlia Alshanetsky <iliaa@php.net>2003-01-28 01:48:57 +0000
commit2d0f43249010cd5f02140ed615194bb1a50bc79e (patch)
treefa0af9733698dbbb7a3e3bf9170d061f6dad6baa
parent38a3f33625813dcf3f66d50b36014a483eba12aa (diff)
downloadphp-git-2d0f43249010cd5f02140ed615194bb1a50bc79e.tar.gz
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.
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/dir.c75
-rw-r--r--ext/standard/php_dir.h1
3 files changed, 77 insertions, 0 deletions
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 <glob.h>
@@ -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 */