~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/standard/dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
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
   +----------------------------------------------------------------------+
 
17
 */
 
18
 
 
19
/* $Id: dir.c,v 1.141.2.2 2005/02/23 18:53:19 iliaa Exp $ */
 
20
 
 
21
/* {{{ includes/startup/misc */
 
22
 
 
23
#include "php.h"
 
24
#include "fopen_wrappers.h"
 
25
#include "file.h"
 
26
#include "php_dir.h"
 
27
#include "php_scandir.h"
 
28
 
 
29
#ifdef HAVE_DIRENT_H
 
30
#include <dirent.h>
 
31
#endif
 
32
 
 
33
#if HAVE_UNISTD_H
 
34
#include <unistd.h>
 
35
#endif
 
36
 
 
37
#include <errno.h>
 
38
 
 
39
#ifdef PHP_WIN32
 
40
#include "win32/readdir.h"
 
41
#endif
 
42
 
 
43
 
 
44
#ifdef HAVE_GLOB
 
45
#ifndef PHP_WIN32
 
46
#include <glob.h>
 
47
#else
 
48
#include "win32/glob.h"
 
49
#endif
 
50
#endif
 
51
 
 
52
typedef struct {
 
53
        int default_dir;
 
54
} php_dir_globals;
 
55
 
 
56
#ifdef ZTS
 
57
#define DIRG(v) TSRMG(dir_globals_id, php_dir_globals *, v)
 
58
int dir_globals_id;
 
59
#else
 
60
#define DIRG(v) (dir_globals.v)
 
61
php_dir_globals dir_globals;
 
62
#endif
 
63
 
 
64
#if 0
 
65
typedef struct {
 
66
        int id;
 
67
        DIR *dir;
 
68
} php_dir;
 
69
 
 
70
static int le_dirp;
 
71
#endif
 
72
 
 
73
static zend_class_entry *dir_class_entry_ptr;
 
74
 
 
75
#define FETCH_DIRP() \
 
76
        if (ZEND_NUM_ARGS() == 0) { \
 
77
                myself = getThis(); \
 
78
                if (myself) { \
 
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"); \
 
81
                                RETURN_FALSE; \
 
82
                        } \
 
83
                        ZEND_FETCH_RESOURCE(dirp, php_stream *, tmp, -1, "Directory", php_file_le_stream()); \
 
84
                } else { \
 
85
                        ZEND_FETCH_RESOURCE(dirp, php_stream *, 0, DIRG(default_dir), "Directory", php_file_le_stream()); \
 
86
                } \
 
87
        } else if ((ZEND_NUM_ARGS() != 1) || zend_get_parameters_ex(1, &id) == FAILURE) { \
 
88
                WRONG_PARAM_COUNT; \
 
89
        } else { \
 
90
                dirp = (php_stream *) zend_fetch_resource(id TSRMLS_CC, -1, "Directory", NULL, 1, php_file_le_stream()); \
 
91
                if (!dirp) \
 
92
                        RETURN_FALSE; \
 
93
        } 
 
94
 
 
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)
 
99
        {NULL, NULL, NULL}
 
100
};
 
101
 
 
102
 
 
103
static void php_set_default_dir(int id TSRMLS_DC)
 
104
{
 
105
        if (DIRG(default_dir)!=-1) {
 
106
                zend_list_delete(DIRG(default_dir));
 
107
        }
 
108
 
 
109
        if (id != -1) {
 
110
                zend_list_addref(id);
 
111
        }
 
112
        
 
113
        DIRG(default_dir) = id;
 
114
}
 
115
 
 
116
PHP_RINIT_FUNCTION(dir)
 
117
{
 
118
        DIRG(default_dir) = -1;
 
119
        return SUCCESS;
 
120
}
 
121
 
 
122
PHP_MINIT_FUNCTION(dir)
 
123
{
 
124
        static char dirsep_str[2], pathsep_str[2];
 
125
        zend_class_entry dir_class_entry;
 
126
 
 
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);
 
129
 
 
130
#ifdef ZTS
 
131
        ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
 
132
#endif
 
133
 
 
134
        dirsep_str[0] = DEFAULT_SLASH;
 
135
        dirsep_str[1] = '\0';
 
136
        REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);
 
137
 
 
138
        pathsep_str[0] = ZEND_PATHS_SEPARATOR;
 
139
        pathsep_str[1] = '\0';
 
140
        REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);
 
141
 
 
142
#ifdef HAVE_GLOB
 
143
#ifdef GLOB_BRACE
 
144
        REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
 
145
#endif
 
146
#ifdef GLOB_MARK
 
147
        REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
 
148
#endif
 
149
#ifdef GLOB_NOSORT
 
150
        REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
 
151
#endif
 
152
#ifdef GLOB_NOCHECK
 
153
        REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
 
154
#endif
 
155
#ifdef GLOB_NOESCAPE
 
156
        REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
 
157
#endif
 
158
 
 
159
#ifndef GLOB_ONLYDIR
 
160
#define GLOB_ONLYDIR (1<<30)
 
161
#define GLOB_EMULATE_ONLYDIR
 
162
#define GLOB_FLAGMASK (~GLOB_ONLYDIR)
 
163
#else
 
164
#define GLOB_FLAGMASK (~0)
 
165
#endif
 
166
 
 
167
        REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
 
168
 
 
169
#endif /* HAVE_GLOB */
 
170
 
 
171
        return SUCCESS;
 
172
}
 
173
/* }}} */
 
174
 
 
175
/* {{{ internal functions */
 
176
static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
 
177
{
 
178
        char *dirname;
 
179
        int dir_len;
 
180
        zval *zcontext = NULL;
 
181
        php_stream_context *context = NULL;
 
182
        php_stream *dirp;
 
183
 
 
184
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dirname, &dir_len, &zcontext) == FAILURE) {
 
185
                RETURN_NULL();
 
186
        }
 
187
 
 
188
        if (zcontext) {
 
189
                context = php_stream_context_from_zval(zcontext, 0);
 
190
        }
 
191
        
 
192
        dirp = php_stream_opendir(dirname, ENFORCE_SAFE_MODE|REPORT_ERRORS, context);
 
193
 
 
194
        if (dirp == NULL) {
 
195
                RETURN_FALSE;
 
196
        }
 
197
                
 
198
        php_set_default_dir(dirp->rsrc_id TSRMLS_CC);
 
199
 
 
200
        if (createobject) {
 
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 */
 
205
        } else {
 
206
                php_stream_to_zval(dirp, return_value);
 
207
        }
 
208
}
 
209
/* }}} */
 
210
 
 
211
/* {{{ proto mixed opendir(string path[, resource context])
 
212
   Open a directory and return a dir_handle */
 
213
PHP_FUNCTION(opendir)
 
214
{
 
215
        _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 
216
}
 
217
/* }}} */
 
218
 
 
219
/* {{{ proto object dir(string directory[, resource context])
 
220
   Directory class with properties, handle and class and methods read, rewind and close */
 
221
PHP_FUNCTION(getdir)
 
222
{
 
223
        _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 
224
}
 
225
/* }}} */
 
226
 
 
227
/* {{{ proto void closedir([resource dir_handle])
 
228
   Close directory connection identified by the dir_handle */
 
229
PHP_FUNCTION(closedir)
 
230
{
 
231
        pval **id, **tmp, *myself;
 
232
        php_stream *dirp;
 
233
 
 
234
        FETCH_DIRP();
 
235
 
 
236
        if (dirp->rsrc_id == DIRG(default_dir)) {
 
237
                php_set_default_dir(-1 TSRMLS_CC);
 
238
        }
 
239
 
 
240
        zend_list_delete(dirp->rsrc_id);
 
241
}
 
242
/* }}} */
 
243
 
 
244
#if defined(HAVE_CHROOT) && !defined(ZTS) && ENABLE_CHROOT_FUNC
 
245
/* {{{ proto bool chroot(string directory)
 
246
   Change root directory */
 
247
PHP_FUNCTION(chroot)
 
248
{
 
249
        char *str;
 
250
        int ret, str_len;
 
251
        
 
252
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
 
253
                RETURN_FALSE;
 
254
        }
 
255
        
 
256
        ret = chroot(str);
 
257
        
 
258
        if (ret != 0) {
 
259
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
 
260
                RETURN_FALSE;
 
261
        }
 
262
 
 
263
        ret = chdir("/");
 
264
        
 
265
        if (ret != 0) {
 
266
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
 
267
                RETURN_FALSE;
 
268
        }
 
269
 
 
270
        RETURN_TRUE;
 
271
}
 
272
/* }}} */
 
273
#endif
 
274
 
 
275
/* {{{ proto bool chdir(string directory)
 
276
   Change the current directory */
 
277
PHP_FUNCTION(chdir)
 
278
{
 
279
        char *str;
 
280
        int ret, str_len;
 
281
        
 
282
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
 
283
                RETURN_FALSE;
 
284
        }
 
285
 
 
286
        if (PG(safe_mode) && !php_checkuid(str, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
 
287
                RETURN_FALSE;
 
288
        }
 
289
        ret = VCWD_CHDIR(str);
 
290
        
 
291
        if (ret != 0) {
 
292
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
 
293
                RETURN_FALSE;
 
294
        }
 
295
 
 
296
        RETURN_TRUE;
 
297
}
 
298
/* }}} */
 
299
 
 
300
/* {{{ proto mixed getcwd(void)
 
301
   Gets the current directory */
 
302
PHP_FUNCTION(getcwd)
 
303
{
 
304
        char path[MAXPATHLEN];
 
305
        char *ret=NULL;
 
306
        
 
307
        if (ZEND_NUM_ARGS() != 0) {
 
308
                WRONG_PARAM_COUNT;
 
309
        }
 
310
 
 
311
#if HAVE_GETCWD
 
312
        ret = VCWD_GETCWD(path, MAXPATHLEN);
 
313
#elif HAVE_GETWD
 
314
        ret = VCWD_GETWD(path);
 
315
#endif
 
316
 
 
317
        if (ret) {
 
318
                RETURN_STRING(path, 1);
 
319
        } else {
 
320
                RETURN_FALSE;
 
321
        }
 
322
}
 
323
/* }}} */
 
324
 
 
325
/* {{{ proto void rewinddir([resource dir_handle])
 
326
   Rewind dir_handle back to the start */
 
327
PHP_FUNCTION(rewinddir)
 
328
{
 
329
        pval **id, **tmp, *myself;
 
330
        php_stream *dirp;
 
331
        
 
332
        FETCH_DIRP();
 
333
 
 
334
        php_stream_rewinddir(dirp);
 
335
}
 
336
/* }}} */
 
337
 
 
338
/* {{{ proto string readdir([resource dir_handle])
 
339
   Read directory entry from dir_handle */
 
340
PHP_NAMED_FUNCTION(php_if_readdir)
 
341
{
 
342
        pval **id, **tmp, *myself;
 
343
        php_stream *dirp;
 
344
        php_stream_dirent entry;
 
345
 
 
346
        FETCH_DIRP();
 
347
 
 
348
        if (php_stream_readdir(dirp, &entry)) {
 
349
                RETURN_STRINGL(entry.d_name, strlen(entry.d_name), 1);
 
350
        }
 
351
        RETURN_FALSE;
 
352
}
 
353
/* }}} */
 
354
 
 
355
#ifdef HAVE_GLOB
 
356
/* {{{ proto array glob(string pattern [, int flags])
 
357
   Find pathnames matching a pattern */
 
358
PHP_FUNCTION(glob)
 
359
{
 
360
        char cwd[MAXPATHLEN];
 
361
        int cwd_skip = 0;
 
362
#ifdef ZTS
 
363
        char work_pattern[MAXPATHLEN];
 
364
        char *result;
 
365
#endif
 
366
        char *pattern = NULL;
 
367
        int pattern_len;
 
368
        long flags = 0;
 
369
        glob_t globbuf;
 
370
        unsigned int n;
 
371
        int ret;
 
372
 
 
373
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &pattern, &pattern_len, &flags) == FAILURE) 
 
374
                return;
 
375
 
 
376
#ifdef ZTS 
 
377
        if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
 
378
                result = VCWD_GETCWD(cwd, MAXPATHLEN);  
 
379
                if (!result) {
 
380
                        cwd[0] = '\0';
 
381
                }
 
382
#ifdef PHP_WIN32
 
383
                if (IS_SLASH(*pattern)) {
 
384
                        cwd[2] = '\0';
 
385
                }
 
386
#endif
 
387
                cwd_skip = strlen(cwd)+1;
 
388
 
 
389
                snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
 
390
                pattern = work_pattern;
 
391
        } 
 
392
#endif
 
393
 
 
394
        globbuf.gl_offs = 0;
 
395
        if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
 
396
#ifdef GLOB_NOMATCH
 
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.
 
401
                         * foreach() */
 
402
#ifndef __linux__
 
403
                        RETURN_FALSE;
 
404
#else
 
405
                        array_init(return_value);
 
406
                        return;
 
407
#endif
 
408
                }
 
409
#endif
 
410
                RETURN_FALSE;
 
411
        }
 
412
 
 
413
        /* now catch the FreeBSD style of "no matches" */
 
414
        if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
 
415
                array_init(return_value);
 
416
                return;
 
417
        }
 
418
 
 
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))) {
 
423
                RETURN_FALSE;
 
424
        }
 
425
        if (php_check_open_basedir(cwd TSRMLS_CC)) {
 
426
                RETURN_FALSE;
 
427
        }
 
428
 
 
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
 
433
                 * following: 
 
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. 
 
438
                 */
 
439
                if (flags & GLOB_ONLYDIR) {
 
440
                        struct stat s;
 
441
 
 
442
                        if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
 
443
                                continue;
 
444
                        }
 
445
 
 
446
                        if (S_IFDIR != (s.st_mode & S_IFMT)) {
 
447
                                continue;
 
448
                        }
 
449
                }
 
450
                add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip, 1);
 
451
        }
 
452
 
 
453
        globfree(&globbuf);
 
454
}
 
455
/* }}} */
 
456
#endif 
 
457
 
 
458
/* {{{ proto array scandir(string dir [, int sorting_order [, resource context]])
 
459
   List files & directories inside the specified path */
 
460
PHP_FUNCTION(scandir)
 
461
{
 
462
        char *dirn;
 
463
        int dirn_len;
 
464
        long flags = 0;
 
465
        char **namelist;
 
466
        int n, i;
 
467
        zval *zcontext = NULL;
 
468
        php_stream_context *context = NULL;
 
469
 
 
470
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &dirn, &dirn_len, &flags, &zcontext) == FAILURE) {
 
471
                return;
 
472
        }
 
473
 
 
474
        if (zcontext) {
 
475
                context = php_stream_context_from_zval(zcontext, 0);
 
476
        }
 
477
 
 
478
        if (!flags) {
 
479
                n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
 
480
        } else {
 
481
                n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
 
482
        }
 
483
        if (n < 0) {
 
484
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno));
 
485
                RETURN_FALSE;
 
486
        }
 
487
        
 
488
        array_init(return_value);
 
489
 
 
490
        for (i = 0; i < n; i++) {
 
491
                add_next_index_string(return_value, namelist[i], 0);
 
492
        }
 
493
 
 
494
        if (n) {
 
495
                efree(namelist);
 
496
        }
 
497
}
 
498
/* }}} */
 
499
 
 
500
/*
 
501
 * Local variables:
 
502
 * tab-width: 4
 
503
 * c-basic-offset: 4
 
504
 * End:
 
505
 * vim600: sw=4 ts=4 fdm=marker
 
506
 * vim<600: sw=4 ts=4
 
507
 */