iliaa           Mon Jan 27 20:48:57 2003 EDT

  Modified files:              
    /php4/ext/standard  dir.c php_dir.h basic_functions.c 
  Log:
  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.
  
  
Index: php4/ext/standard/dir.c
diff -u php4/ext/standard/dir.c:1.114 php4/ext/standard/dir.c:1.115
--- php4/ext/standard/dir.c:1.114       Mon Jan 27 11:29:47 2003
+++ php4/ext/standard/dir.c     Mon Jan 27 20:48:57 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: dir.c,v 1.114 2003/01/27 16:29:47 edink Exp $ */
+/* $Id: dir.c,v 1.115 2003/01/28 01:48:57 iliaa Exp $ */
 
 /* {{{ includes/startup/misc */
 
@@ -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>
@@ -421,6 +425,77 @@
 }
 /* }}} */
 #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:
Index: php4/ext/standard/php_dir.h
diff -u php4/ext/standard/php_dir.h:1.20 php4/ext/standard/php_dir.h:1.21
--- php4/ext/standard/php_dir.h:1.20    Tue Dec 31 11:07:50 2002
+++ php4/ext/standard/php_dir.h Mon Jan 27 20:48:57 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_dir.h,v 1.20 2002/12/31 16:07:50 sebastian Exp $ */
+/* $Id: php_dir.h,v 1.21 2003/01/28 01:48:57 iliaa Exp $ */
 
 #ifndef PHP_DIR_H
 #define PHP_DIR_H
@@ -35,5 +35,6 @@
 PHP_NAMED_FUNCTION(php_if_readdir);
 PHP_FUNCTION(getdir);
 PHP_FUNCTION(glob);
+PHP_FUNCTION(scandir);
 
 #endif /* PHP_DIR_H */
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.565 
php4/ext/standard/basic_functions.c:1.566
--- php4/ext/standard/basic_functions.c:1.565   Tue Jan 21 09:53:16 2003
+++ php4/ext/standard/basic_functions.c Mon Jan 27 20:48:57 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.565 2003/01/21 14:53:16 iliaa Exp $ */
+/* $Id: basic_functions.c,v 1.566 2003/01/28 01:48:57 iliaa Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -724,6 +724,7 @@
        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



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to