]> granicus.if.org Git - php/commitdiff
Added scandir() function, which allows quick retrieval of all files &
authorIlia Alshanetsky <iliaa@php.net>
Tue, 28 Jan 2003 01:48:57 +0000 (01:48 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Tue, 28 Jan 2003 01:48:57 +0000 (01:48 +0000)
directories within the specified path and sort the output in alphabetical
or reverse alphabetical order.

ext/standard/basic_functions.c
ext/standard/dir.c
ext/standard/php_dir.h

index 0f4c4f65b14de713bb44aa828ec47f314b3c706b..99aee756f936cd5bc3bc6dc2cf2b7bdf24c88421 100644 (file)
@@ -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
index 778c5047171c91f4251406fee8b1689b6cf8c4ef..b274fed410681042cacb6531945087e477710a0c 100644 (file)
 #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
index 87015042bdc8dfb8999f7ce7bd2b70630f7d98eb..40c29ffd55f8f8752436606972a86044705ddfc2 100644 (file)
@@ -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 */