1999-04-17 00:37:12 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
1999-07-16 13:13:16 +00:00
|
|
|
| PHP version 4.0 |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
1999-07-16 13:13:16 +00:00
|
|
|
| Copyright (c) 1997, 1998, 1999 The PHP Group |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
1999-07-16 13:13:16 +00:00
|
|
|
| This source file is subject to version 2.0 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_0.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: |
|
1999-10-05 14:36:00 +00:00
|
|
|
| PHP 4.0 patches by Thies C. Arntzen (thies@digicol.de) |
|
1999-04-17 00:37:12 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
/* {{{ includes/startup/misc */
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
#include "php.h"
|
|
|
|
#include "fopen-wrappers.h"
|
|
|
|
|
1999-12-04 19:19:57 +00:00
|
|
|
#include "php_dir.h"
|
1999-04-17 00:37:12 +00:00
|
|
|
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
# include <dirent.h>
|
1999-04-17 00:37:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
1999-09-05 19:04:40 +00:00
|
|
|
#if WIN32||WINNT
|
1999-04-17 00:37:12 +00:00
|
|
|
#define NEEDRDH 1
|
|
|
|
#include "win32/readdir.h"
|
|
|
|
#endif
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
typedef struct {
|
|
|
|
int default_dir;
|
|
|
|
} php_dir_globals;
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
#define DIR(v) (dir_globals->v)
|
|
|
|
#define DIRLS_FETCH() php_dir_globals *dir_globals = ts_resource(dir_globals_id)
|
|
|
|
int dir_globals_id;
|
|
|
|
#else
|
1999-10-05 16:18:40 +00:00
|
|
|
#define DIR(v) (dir_globals.v)
|
1999-10-05 14:36:00 +00:00
|
|
|
#define DIRLS_FETCH()
|
|
|
|
php_dir_globals dir_globals;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int id;
|
|
|
|
DIR *dir;
|
|
|
|
} php_dir;
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
static int le_dirp;
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-05-28 11:09:39 +00:00
|
|
|
static zend_class_entry *dir_class_entry_ptr;
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
#define FETCH_DIRP() \
|
|
|
|
if (ARG_COUNT(ht) == 0) { \
|
|
|
|
myself = getThis(); \
|
|
|
|
if (myself) { \
|
|
|
|
if (zend_hash_find(myself->value.obj.properties, "handle", sizeof("handle"), (void **)&tmp) == FAILURE) { \
|
|
|
|
php_error(E_WARNING, "unable to find my handle property"); \
|
|
|
|
RETURN_FALSE; \
|
|
|
|
} \
|
|
|
|
ZEND_FETCH_RESOURCE(dirp,php_dir *,tmp,-1, "Directory", le_dirp); \
|
|
|
|
} else { \
|
|
|
|
ZEND_FETCH_RESOURCE(dirp,php_dir *,0,DIR(default_dir), "Directory", le_dirp); \
|
|
|
|
} \
|
|
|
|
} else if ((ARG_COUNT(ht) != 1) || getParametersEx(1, &id) == FAILURE) { \
|
|
|
|
WRONG_PARAM_COUNT; \
|
|
|
|
} else { \
|
|
|
|
ZEND_FETCH_RESOURCE(dirp,php_dir *,id,-1, "Directory", le_dirp); \
|
|
|
|
}
|
|
|
|
|
1999-05-28 11:09:39 +00:00
|
|
|
static zend_function_entry php_dir_functions[] = {
|
1999-07-24 22:16:54 +00:00
|
|
|
PHP_FE(opendir, NULL)
|
|
|
|
PHP_FE(closedir, NULL)
|
|
|
|
PHP_FE(chdir, NULL)
|
1999-10-20 16:17:30 +00:00
|
|
|
PHP_FE(getcwd, NULL)
|
1999-07-24 22:16:54 +00:00
|
|
|
PHP_FE(rewinddir, NULL)
|
|
|
|
PHP_FE(readdir, NULL)
|
1999-05-28 11:09:39 +00:00
|
|
|
PHP_FALIAS(dir, getdir, NULL)
|
1999-04-17 00:37:12 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
1999-05-28 11:09:39 +00:00
|
|
|
|
|
|
|
static zend_function_entry php_dir_class_functions[] = {
|
|
|
|
PHP_FALIAS(close, closedir, NULL)
|
|
|
|
PHP_FALIAS(rewind, rewinddir, NULL)
|
|
|
|
PHP_FALIAS(read, readdir, NULL)
|
1999-08-19 16:52:53 +00:00
|
|
|
{NULL, NULL, NULL}
|
1999-05-28 11:09:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-12-17 20:55:31 +00:00
|
|
|
zend_module_entry php3_dir_module_entry = {
|
1999-07-26 20:09:08 +00:00
|
|
|
"PHP_dir", php_dir_functions, PHP_MINIT(dir), NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
|
1999-04-17 00:37:12 +00:00
|
|
|
};
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
static void _dir_dtor(php_dir *dirp)
|
|
|
|
{
|
|
|
|
closedir(dirp->dir);
|
|
|
|
efree(dirp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
static void php_dir_init_globals(php_dir_globals *dir_globals)
|
|
|
|
{
|
|
|
|
DIR(default_dir) = 0;
|
|
|
|
}
|
|
|
|
#endif
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-07-26 20:09:08 +00:00
|
|
|
PHP_MINIT_FUNCTION(dir)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-05-28 11:09:39 +00:00
|
|
|
zend_class_entry dir_class_entry;
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
le_dirp = register_list_destructors(_dir_dtor,NULL);
|
1999-05-28 11:09:39 +00:00
|
|
|
|
|
|
|
INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
|
|
|
|
dir_class_entry_ptr = register_internal_class(&dir_class_entry);
|
1999-10-05 14:36:00 +00:00
|
|
|
|
|
|
|
#ifdef ZTS
|
1999-12-05 16:25:32 +00:00
|
|
|
dir_globals_id = ts_allocate_id(sizeof(php_dir_globals), (ts_allocate_ctor) php_dir_init_globals, NULL);
|
1999-10-05 14:36:00 +00:00
|
|
|
#else
|
|
|
|
DIR(default_dir) = 0;
|
|
|
|
#endif
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
/* }}} */
|
|
|
|
/* {{{ internal functions */
|
|
|
|
|
|
|
|
static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-10-05 14:36:00 +00:00
|
|
|
pval **arg;
|
|
|
|
php_dir *dirp;
|
|
|
|
DIRLS_FETCH();
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
if (ARG_COUNT(ht) != 1 || getParametersEx(1, &arg) == FAILURE) {
|
1999-04-17 00:37:12 +00:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
1999-10-05 14:36:00 +00:00
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-12-17 19:16:50 +00:00
|
|
|
if (php_check_open_basedir((*arg)->value.str.val)) {
|
1999-10-05 14:36:00 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
dirp = emalloc(sizeof(php_dir));
|
|
|
|
|
|
|
|
dirp->dir = opendir((*arg)->value.str.val);
|
|
|
|
|
|
|
|
if (! dirp->dir) {
|
|
|
|
efree(dirp);
|
|
|
|
php_error(E_WARNING, "OpenDir: %s (errno %d)", strerror(errno), errno);
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-10-05 14:36:00 +00:00
|
|
|
|
|
|
|
dirp->id = zend_list_insert(dirp,le_dirp);
|
|
|
|
|
|
|
|
DIR(default_dir) = dirp->id;
|
|
|
|
|
|
|
|
if (createobject) {
|
|
|
|
object_init_ex(return_value, dir_class_entry_ptr);
|
|
|
|
add_property_stringl(return_value, "path", (*arg)->value.str.val, (*arg)->value.str.len, 1);
|
|
|
|
add_property_resource(return_value, "handle", dirp->id);
|
|
|
|
zend_list_addref(dirp->id);
|
|
|
|
} else {
|
|
|
|
RETURN_RESOURCE(dirp->id);
|
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
/* }}} */
|
1999-10-05 14:36:00 +00:00
|
|
|
/* {{{ proto int opendir(string path)
|
|
|
|
Open a directory and return a dir_handle */
|
|
|
|
|
|
|
|
PHP_FUNCTION(opendir)
|
|
|
|
{
|
|
|
|
_php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ proto class dir(string directory)
|
|
|
|
Directory class with properties, handle and class and methods read, rewind and close */
|
|
|
|
|
|
|
|
PHP_FUNCTION(getdir)
|
|
|
|
{
|
|
|
|
_php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
|
|
|
|
}
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
/* }}} */
|
1999-04-17 00:37:12 +00:00
|
|
|
/* {{{ proto void closedir([int dir_handle])
|
|
|
|
Close directory connection identified by the dir_handle */
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(closedir)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-10-05 14:36:00 +00:00
|
|
|
pval **id, **tmp, *myself;
|
|
|
|
php_dir *dirp;
|
|
|
|
DIRLS_FETCH();
|
|
|
|
|
|
|
|
FETCH_DIRP();
|
|
|
|
|
1999-12-17 19:51:39 +00:00
|
|
|
zend_list_delete(dirp->id);
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
1999-10-20 16:17:30 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
/* }}} */
|
|
|
|
/* {{{ proto int chdir(string directory)
|
1999-10-20 16:17:30 +00:00
|
|
|
Change the current directory */
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(chdir)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-10-05 14:36:00 +00:00
|
|
|
pval **arg;
|
1999-04-17 00:37:12 +00:00
|
|
|
int ret;
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
if (ARG_COUNT(ht) != 1 || getParametersEx(1, &arg) == FAILURE) {
|
1999-04-17 00:37:12 +00:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
1999-10-05 14:36:00 +00:00
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
ret = chdir((*arg)->value.str.val);
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
if (ret < 0) {
|
1999-08-02 19:17:14 +00:00
|
|
|
php_error(E_WARNING, "ChDir: %s (errno %d)", strerror(errno), errno);
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
1999-10-20 16:17:30 +00:00
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ proto string getcwd()
|
|
|
|
Gets the current directory */
|
|
|
|
|
|
|
|
PHP_FUNCTION(getcwd)
|
|
|
|
{
|
|
|
|
char path[MAXPATHLEN];
|
1999-10-20 18:09:44 +00:00
|
|
|
char *ret=NULL;
|
1999-10-20 16:17:30 +00:00
|
|
|
|
|
|
|
if (ARG_COUNT(ht) != 0) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_GETCWD
|
|
|
|
ret = getcwd(path,MAXPATHLEN-1);
|
|
|
|
#elif HAVE_GETWD
|
|
|
|
ret = getwd(path);
|
1999-10-20 18:09:44 +00:00
|
|
|
/*
|
|
|
|
* #warning is not ANSI C
|
|
|
|
* #else
|
|
|
|
* #warning no proper getcwd support for your site
|
|
|
|
*/
|
1999-10-20 16:17:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
RETURN_STRING(path,1);
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
/* }}} */
|
|
|
|
/* {{{ proto void rewinddir([int dir_handle])
|
|
|
|
Rewind dir_handle back to the start */
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(rewinddir)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-10-05 14:36:00 +00:00
|
|
|
pval **id, **tmp, *myself;
|
|
|
|
php_dir *dirp;
|
|
|
|
DIRLS_FETCH();
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
FETCH_DIRP();
|
|
|
|
|
|
|
|
rewinddir(dirp->dir);
|
1999-04-17 00:37:12 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ proto string readdir([int dir_handle])
|
|
|
|
Read directory entry from dir_handle */
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-05-16 11:19:26 +00:00
|
|
|
PHP_FUNCTION(readdir)
|
1999-04-17 00:37:12 +00:00
|
|
|
{
|
1999-10-05 14:36:00 +00:00
|
|
|
pval **id, **tmp, *myself;
|
|
|
|
php_dir *dirp;
|
1999-04-17 00:37:12 +00:00
|
|
|
struct dirent *direntp;
|
1999-10-05 14:36:00 +00:00
|
|
|
DIRLS_FETCH();
|
|
|
|
|
|
|
|
FETCH_DIRP();
|
1999-04-17 00:37:12 +00:00
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
direntp = readdir(dirp->dir);
|
1999-04-17 00:37:12 +00:00
|
|
|
if (direntp) {
|
|
|
|
RETURN_STRINGL(direntp->d_name, strlen(direntp->d_name), 1);
|
|
|
|
}
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
1999-10-05 14:36:00 +00:00
|
|
|
|
1999-04-17 00:37:12 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|