Back to index

php5  5.3.10
Classes | Defines | Functions | Variables
dir.c File Reference
#include "php.h"
#include "fopen_wrappers.h"
#include "file.h"
#include "php_dir.h"
#include "php_string.h"
#include "php_scandir.h"
#include "basic_functions.h"
#include <errno.h>

Go to the source code of this file.

Classes

struct  php_dir_globals

Defines

#define DIRG(v)   (dir_globals.v)
#define FETCH_DIRP()

Functions

static void php_set_default_dir (int id TSRMLS_DC)
 PHP_RINIT_FUNCTION (dir)
 PHP_MINIT_FUNCTION (dir)
static void _php_do_opendir (INTERNAL_FUNCTION_PARAMETERS, int createobject)
 PHP_FUNCTION (opendir)
 PHP_FUNCTION (getdir)
 PHP_FUNCTION (closedir)
 PHP_FUNCTION (chdir)
 PHP_FUNCTION (getcwd)
 PHP_FUNCTION (rewinddir)
 PHP_NAMED_FUNCTION (php_if_readdir)
 PHP_FUNCTION (scandir)

Variables

php_dir_globals dir_globals
static zend_class_entry * dir_class_entry_ptr
static const zend_function_entry php_dir_class_functions []

Class Documentation

struct php_dir_globals

Definition at line 54 of file dir.c.

Class Members
int default_dir

Define Documentation

#define DIRG (   v)    (dir_globals.v)

Definition at line 62 of file dir.c.

#define FETCH_DIRP ( )
Value:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &id) == FAILURE) { \
              return; \
       } \
       if (ZEND_NUM_ARGS() == 0) { \
              myself = getThis(); \
              if (myself) { \
                     if (zend_hash_find(Z_OBJPROP_P(myself), "handle", sizeof("handle"), (void **)&tmp) == FAILURE) { \
                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find my handle property"); \
                            RETURN_FALSE; \
                     } \
                     ZEND_FETCH_RESOURCE(dirp, php_stream *, tmp, -1, "Directory", php_file_le_stream()); \
              } else { \
                     ZEND_FETCH_RESOURCE(dirp, php_stream *, 0, DIRG(default_dir), "Directory", php_file_le_stream()); \
              } \
       } else { \
              dirp = (php_stream *) zend_fetch_resource(&id TSRMLS_CC, -1, "Directory", NULL, 1, php_file_le_stream()); \
              if (!dirp) \
                     RETURN_FALSE; \
       }

Definition at line 77 of file dir.c.


Function Documentation

static void _php_do_opendir ( INTERNAL_FUNCTION_PARAMETERS  ,
int  createobject 
) [static]

Definition at line 210 of file dir.c.

{
       char *dirname;
       int dir_len;
       zval *zcontext = NULL;
       php_stream_context *context = NULL;
       php_stream *dirp;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dirname, &dir_len, &zcontext) == FAILURE) {
              RETURN_NULL();
       }

       context = php_stream_context_from_zval(zcontext, 0);
       
       dirp = php_stream_opendir(dirname, ENFORCE_SAFE_MODE|REPORT_ERRORS, context);

       if (dirp == NULL) {
              RETURN_FALSE;
       }

       dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
              
       php_set_default_dir(dirp->rsrc_id TSRMLS_CC);

       if (createobject) {
              object_init_ex(return_value, dir_class_entry_ptr);
              add_property_stringl(return_value, "path", dirname, dir_len, 1);
              add_property_resource(return_value, "handle", dirp->rsrc_id);
              php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
       } else {
              php_stream_to_zval(dirp, return_value);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 247 of file dir.c.

Here is the call graph for this function:

PHP_FUNCTION ( getdir  )

Definition at line 255 of file dir.c.

Here is the call graph for this function:

Definition at line 263 of file dir.c.

{
       zval *id = NULL, **tmp, *myself;
       php_stream *dirp;
       int rsrc_id;

       FETCH_DIRP();

       if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
              RETURN_FALSE;
       }

       rsrc_id = dirp->rsrc_id;
       zend_list_delete(dirp->rsrc_id);

       if (rsrc_id == DIRG(default_dir)) {
              php_set_default_dir(-1 TSRMLS_CC);
       }
}

Here is the call graph for this function:

Definition at line 319 of file dir.c.

{
       char *str;
       int ret, str_len;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
              RETURN_FALSE;
       }

       if (strlen(str) != str_len) {
              RETURN_FALSE;
       }

       if ((PG(safe_mode) && !php_checkuid(str, NULL, CHECKUID_CHECK_FILE_AND_DIR)) || php_check_open_basedir(str TSRMLS_CC)) {
              RETURN_FALSE;
       }
       ret = VCWD_CHDIR(str);
       
       if (ret != 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
              RETURN_FALSE;
       }

       if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) {
              efree(BG(CurrentStatFile));
              BG(CurrentStatFile) = NULL;
       }
       if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) {
              efree(BG(CurrentLStatFile));
              BG(CurrentLStatFile) = NULL;
       }

       RETURN_TRUE;
}

Here is the call graph for this function:

Definition at line 357 of file dir.c.

{
       char path[MAXPATHLEN];
       char *ret=NULL;
       
       if (zend_parse_parameters_none() == FAILURE) {
              return;
       }

#if HAVE_GETCWD
       ret = VCWD_GETCWD(path, MAXPATHLEN);
#elif HAVE_GETWD
       ret = VCWD_GETWD(path);
#endif

       if (ret) {
              RETURN_STRING(path, 1);
       } else {
              RETURN_FALSE;
       }
}

Definition at line 382 of file dir.c.

{
       zval *id = NULL, **tmp, *myself;
       php_stream *dirp;
       
       FETCH_DIRP();

       if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
              RETURN_FALSE;
       }

       php_stream_rewinddir(dirp);
}
PHP_FUNCTION ( scandir  )

Definition at line 554 of file dir.c.

{
       char *dirn;
       int dirn_len;
       long flags = 0;
       char **namelist;
       int n, i;
       zval *zcontext = NULL;
       php_stream_context *context = NULL;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &dirn, &dirn_len, &flags, &zcontext) == FAILURE) {
              return;
       }

       if (strlen(dirn) != dirn_len) {
              RETURN_FALSE;
       }

       if (dirn_len < 1) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Directory name cannot be empty");
              RETURN_FALSE;
       }

       if (zcontext) {
              context = php_stream_context_from_zval(zcontext, 0);
       }

       if (!flags) {
              n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
       } else {
              n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
       }
       if (n < 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno));
              RETURN_FALSE;
       }
       
       array_init(return_value);

       for (i = 0; i < n; i++) {
              add_next_index_string(return_value, namelist[i], 0);
       }

       if (n) {
              efree(namelist);
       }
}

Here is the call graph for this function:

PHP_MINIT_FUNCTION ( dir  )

Definition at line 131 of file dir.c.

{
       static char dirsep_str[2], pathsep_str[2];
       zend_class_entry dir_class_entry;

       INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
       dir_class_entry_ptr = zend_register_internal_class(&dir_class_entry TSRMLS_CC);

#ifdef ZTS
       ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
#endif

       dirsep_str[0] = DEFAULT_SLASH;
       dirsep_str[1] = '\0';
       REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);

       pathsep_str[0] = ZEND_PATHS_SEPARATOR;
       pathsep_str[1] = '\0';
       REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);

#ifdef HAVE_GLOB

#ifdef GLOB_BRACE
       REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
#else
# define GLOB_BRACE 0
#endif

#ifdef GLOB_MARK
       REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
#else
# define GLOB_MARK 0
#endif

#ifdef GLOB_NOSORT
       REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
#else 
# define GLOB_NOSORT 0
#endif

#ifdef GLOB_NOCHECK
       REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
#else 
# define GLOB_NOCHECK 0
#endif

#ifdef GLOB_NOESCAPE
       REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
#else 
# define GLOB_NOESCAPE 0
#endif

#ifdef GLOB_ERR
       REGISTER_LONG_CONSTANT("GLOB_ERR", GLOB_ERR, CONST_CS | CONST_PERSISTENT);
#else 
# define GLOB_ERR 0
#endif

#ifndef GLOB_ONLYDIR
# define GLOB_ONLYDIR (1<<30)
# define GLOB_EMULATE_ONLYDIR
# define GLOB_FLAGMASK (~GLOB_ONLYDIR)
#else
# define GLOB_FLAGMASK (~0)
#endif

/* This is used for checking validity of passed flags (passing invalid flags causes segfault in glob()!! */
#define GLOB_AVAILABLE_FLAGS (0 | GLOB_BRACE | GLOB_MARK | GLOB_NOSORT | GLOB_NOCHECK | GLOB_NOESCAPE | GLOB_ERR | GLOB_ONLYDIR)

       REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
       REGISTER_LONG_CONSTANT("GLOB_AVAILABLE_FLAGS", GLOB_AVAILABLE_FLAGS, CONST_CS | CONST_PERSISTENT);

#endif /* HAVE_GLOB */

       return SUCCESS;
}

Here is the call graph for this function:

PHP_NAMED_FUNCTION ( php_if_readdir  )

Definition at line 400 of file dir.c.

{
       zval *id = NULL, **tmp, *myself;
       php_stream *dirp;
       php_stream_dirent entry;

       FETCH_DIRP();

       if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
              RETURN_FALSE;
       }

       if (php_stream_readdir(dirp, &entry)) {
              RETURN_STRINGL(entry.d_name, strlen(entry.d_name), 1);
       }
       RETURN_FALSE;
}
PHP_RINIT_FUNCTION ( dir  )

Definition at line 125 of file dir.c.

{
       DIRG(default_dir) = -1;
       return SUCCESS;
}
static void php_set_default_dir ( int id  TSRMLS_DC) [static]

Definition at line 112 of file dir.c.

{
       if (DIRG(default_dir)!=-1) {
              zend_list_delete(DIRG(default_dir));
       }

       if (id != -1) {
              zend_list_addref(id);
       }
       
       DIRG(default_dir) = id;
}

Here is the caller graph for this function:


Variable Documentation

zend_class_entry* dir_class_entry_ptr [static]

Definition at line 75 of file dir.c.

Definition at line 63 of file dir.c.

Initial value:
 {


       PHP_NAMED_FE(read,  php_if_readdir, arginfo_dir)
       {NULL, NULL, NULL}
}

Definition at line 104 of file dir.c.