2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Author: Thies C. Arntzen <thies@thieso.net> |
16
+----------------------------------------------------------------------+
19
/* $Id: dir.c,v 1.141.2.2 2005/02/23 18:53:19 iliaa Exp $ */
21
/* {{{ includes/startup/misc */
24
#include "fopen_wrappers.h"
27
#include "php_scandir.h"
40
#include "win32/readdir.h"
48
#include "win32/glob.h"
57
#define DIRG(v) TSRMG(dir_globals_id, php_dir_globals *, v)
60
#define DIRG(v) (dir_globals.v)
61
php_dir_globals dir_globals;
73
static zend_class_entry *dir_class_entry_ptr;
75
#define FETCH_DIRP() \
76
if (ZEND_NUM_ARGS() == 0) { \
79
if (zend_hash_find(Z_OBJPROP_P(myself), "handle", sizeof("handle"), (void **)&tmp) == FAILURE) { \
80
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find my handle property"); \
83
ZEND_FETCH_RESOURCE(dirp, php_stream *, tmp, -1, "Directory", php_file_le_stream()); \
85
ZEND_FETCH_RESOURCE(dirp, php_stream *, 0, DIRG(default_dir), "Directory", php_file_le_stream()); \
87
} else if ((ZEND_NUM_ARGS() != 1) || zend_get_parameters_ex(1, &id) == FAILURE) { \
90
dirp = (php_stream *) zend_fetch_resource(id TSRMLS_CC, -1, "Directory", NULL, 1, php_file_le_stream()); \
95
static zend_function_entry php_dir_class_functions[] = {
96
PHP_FALIAS(close, closedir, NULL)
97
PHP_FALIAS(rewind, rewinddir, NULL)
98
PHP_NAMED_FE(read, php_if_readdir, NULL)
103
static void php_set_default_dir(int id TSRMLS_DC)
105
if (DIRG(default_dir)!=-1) {
106
zend_list_delete(DIRG(default_dir));
110
zend_list_addref(id);
113
DIRG(default_dir) = id;
116
PHP_RINIT_FUNCTION(dir)
118
DIRG(default_dir) = -1;
122
PHP_MINIT_FUNCTION(dir)
124
static char dirsep_str[2], pathsep_str[2];
125
zend_class_entry dir_class_entry;
127
INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
128
dir_class_entry_ptr = zend_register_internal_class(&dir_class_entry TSRMLS_CC);
131
ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
134
dirsep_str[0] = DEFAULT_SLASH;
135
dirsep_str[1] = '\0';
136
REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);
138
pathsep_str[0] = ZEND_PATHS_SEPARATOR;
139
pathsep_str[1] = '\0';
140
REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);
144
REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
147
REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
150
REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
153
REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
156
REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
160
#define GLOB_ONLYDIR (1<<30)
161
#define GLOB_EMULATE_ONLYDIR
162
#define GLOB_FLAGMASK (~GLOB_ONLYDIR)
164
#define GLOB_FLAGMASK (~0)
167
REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
169
#endif /* HAVE_GLOB */
175
/* {{{ internal functions */
176
static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
180
zval *zcontext = NULL;
181
php_stream_context *context = NULL;
184
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dirname, &dir_len, &zcontext) == FAILURE) {
189
context = php_stream_context_from_zval(zcontext, 0);
192
dirp = php_stream_opendir(dirname, ENFORCE_SAFE_MODE|REPORT_ERRORS, context);
198
php_set_default_dir(dirp->rsrc_id TSRMLS_CC);
201
object_init_ex(return_value, dir_class_entry_ptr);
202
add_property_stringl(return_value, "path", dirname, dir_len, 1);
203
add_property_resource(return_value, "handle", dirp->rsrc_id);
204
php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
206
php_stream_to_zval(dirp, return_value);
211
/* {{{ proto mixed opendir(string path[, resource context])
212
Open a directory and return a dir_handle */
213
PHP_FUNCTION(opendir)
215
_php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
219
/* {{{ proto object dir(string directory[, resource context])
220
Directory class with properties, handle and class and methods read, rewind and close */
223
_php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
227
/* {{{ proto void closedir([resource dir_handle])
228
Close directory connection identified by the dir_handle */
229
PHP_FUNCTION(closedir)
231
pval **id, **tmp, *myself;
236
if (dirp->rsrc_id == DIRG(default_dir)) {
237
php_set_default_dir(-1 TSRMLS_CC);
240
zend_list_delete(dirp->rsrc_id);
244
#if defined(HAVE_CHROOT) && !defined(ZTS) && ENABLE_CHROOT_FUNC
245
/* {{{ proto bool chroot(string directory)
246
Change root directory */
252
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
259
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
266
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
275
/* {{{ proto bool chdir(string directory)
276
Change the current directory */
282
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
286
if (PG(safe_mode) && !php_checkuid(str, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
289
ret = VCWD_CHDIR(str);
292
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
300
/* {{{ proto mixed getcwd(void)
301
Gets the current directory */
304
char path[MAXPATHLEN];
307
if (ZEND_NUM_ARGS() != 0) {
312
ret = VCWD_GETCWD(path, MAXPATHLEN);
314
ret = VCWD_GETWD(path);
318
RETURN_STRING(path, 1);
325
/* {{{ proto void rewinddir([resource dir_handle])
326
Rewind dir_handle back to the start */
327
PHP_FUNCTION(rewinddir)
329
pval **id, **tmp, *myself;
334
php_stream_rewinddir(dirp);
338
/* {{{ proto string readdir([resource dir_handle])
339
Read directory entry from dir_handle */
340
PHP_NAMED_FUNCTION(php_if_readdir)
342
pval **id, **tmp, *myself;
344
php_stream_dirent entry;
348
if (php_stream_readdir(dirp, &entry)) {
349
RETURN_STRINGL(entry.d_name, strlen(entry.d_name), 1);
356
/* {{{ proto array glob(string pattern [, int flags])
357
Find pathnames matching a pattern */
360
char cwd[MAXPATHLEN];
363
char work_pattern[MAXPATHLEN];
366
char *pattern = NULL;
373
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &pattern, &pattern_len, &flags) == FAILURE)
377
if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
378
result = VCWD_GETCWD(cwd, MAXPATHLEN);
383
if (IS_SLASH(*pattern)) {
387
cwd_skip = strlen(cwd)+1;
389
snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
390
pattern = work_pattern;
395
if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
397
if (GLOB_NOMATCH == ret) {
398
/* Linux handles no matches as an error condition, but FreeBSD
399
* doesn't. This ensure that if no match is found, an empty array
400
* is always returned so it can be used without worrying in e.g.
405
array_init(return_value);
413
/* now catch the FreeBSD style of "no matches" */
414
if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
415
array_init(return_value);
419
/* we assume that any glob pattern will match files from one directory only
420
so checking the dirname of the first match should be sufficient */
421
strncpy(cwd, globbuf.gl_pathv[0], MAXPATHLEN);
422
if (PG(safe_mode) && (!php_checkuid(cwd, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
425
if (php_check_open_basedir(cwd TSRMLS_CC)) {
429
array_init(return_value);
430
for (n = 0; n < globbuf.gl_pathc; n++) {
431
/* we need to this everytime since GLOB_ONLYDIR does not guarantee that
432
* all directories will be filtered. GNU libc documentation states the
434
* If the information about the type of the file is easily available
435
* non-directories will be rejected but no extra work will be done to
436
* determine the information for each file. I.e., the caller must still be
437
* able to filter directories out.
439
if (flags & GLOB_ONLYDIR) {
442
if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
446
if (S_IFDIR != (s.st_mode & S_IFMT)) {
450
add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip, 1);
458
/* {{{ proto array scandir(string dir [, int sorting_order [, resource context]])
459
List files & directories inside the specified path */
460
PHP_FUNCTION(scandir)
467
zval *zcontext = NULL;
468
php_stream_context *context = NULL;
470
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &dirn, &dirn_len, &flags, &zcontext) == FAILURE) {
475
context = php_stream_context_from_zval(zcontext, 0);
479
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
481
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
484
php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno));
488
array_init(return_value);
490
for (i = 0; i < n; i++) {
491
add_next_index_string(return_value, namelist[i], 0);
505
* vim600: sw=4 ts=4 fdm=marker