1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Authors: |
| PHP 4.0 patches by Thies C. Arntzen (thies@digicol.de) |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* {{{ includes/startup/misc */
#include "php.h"
#include "fopen-wrappers.h"
#include "php_dir.h"
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#if WIN32||WINNT
#define NEEDRDH 1
#include "win32/readdir.h"
#endif
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
#define DIR(v) (dir_globals.v)
#define DIRLS_FETCH()
php_dir_globals dir_globals;
#endif
typedef struct {
int id;
DIR *dir;
} php_dir;
static int le_dirp;
static zend_class_entry *dir_class_entry_ptr;
#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); \
}
static zend_function_entry php_dir_functions[] = {
PHP_FE(opendir, NULL)
PHP_FE(closedir, NULL)
PHP_FE(chdir, NULL)
PHP_FE(getcwd, NULL)
PHP_FE(rewinddir, NULL)
PHP_FE(readdir, NULL)
PHP_FALIAS(dir, getdir, NULL)
{NULL, NULL, NULL}
};
static zend_function_entry php_dir_class_functions[] = {
PHP_FALIAS(close, closedir, NULL)
PHP_FALIAS(rewind, rewinddir, NULL)
PHP_FALIAS(read, readdir, NULL)
{NULL, NULL, NULL}
};
zend_module_entry php3_dir_module_entry = {
"PHP_dir", php_dir_functions, PHP_MINIT(dir), NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
};
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
PHP_MINIT_FUNCTION(dir)
{
zend_class_entry dir_class_entry;
le_dirp = register_list_destructors(_dir_dtor,NULL);
INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
dir_class_entry_ptr = register_internal_class(&dir_class_entry);
#ifdef ZTS
dir_globals_id = ts_allocate_id(sizeof(php_dir_globals), (ts_allocate_ctor) php_dir_init_globals, NULL);
#else
DIR(default_dir) = 0;
#endif
return SUCCESS;
}
/* }}} */
/* {{{ internal functions */
static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
{
pval **arg;
php_dir *dirp;
DIRLS_FETCH();
if (ARG_COUNT(ht) != 1 || getParametersEx(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
if (php_check_open_basedir((*arg)->value.str.val)) {
RETURN_FALSE;
}
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);
RETURN_FALSE;
}
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);
}
}
/* }}} */
/* {{{ 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);
}
/* }}} */
/* {{{ proto void closedir([int dir_handle])
Close directory connection identified by the dir_handle */
PHP_FUNCTION(closedir)
{
pval **id, **tmp, *myself;
php_dir *dirp;
DIRLS_FETCH();
FETCH_DIRP();
zend_list_delete(dirp->id);
}
/* }}} */
/* {{{ proto int chdir(string directory)
Change the current directory */
PHP_FUNCTION(chdir)
{
pval **arg;
int ret;
if (ARG_COUNT(ht) != 1 || getParametersEx(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
ret = chdir((*arg)->value.str.val);
if (ret < 0) {
php_error(E_WARNING, "ChDir: %s (errno %d)", strerror(errno), errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string getcwd()
Gets the current directory */
PHP_FUNCTION(getcwd)
{
char path[MAXPATHLEN];
char *ret=NULL;
if (ARG_COUNT(ht) != 0) {
WRONG_PARAM_COUNT;
}
#if HAVE_GETCWD
ret = getcwd(path,MAXPATHLEN-1);
#elif HAVE_GETWD
ret = getwd(path);
/*
* #warning is not ANSI C
* #else
* #warning no proper getcwd support for your site
*/
#endif
if (ret) {
RETURN_STRING(path,1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto void rewinddir([int dir_handle])
Rewind dir_handle back to the start */
PHP_FUNCTION(rewinddir)
{
pval **id, **tmp, *myself;
php_dir *dirp;
DIRLS_FETCH();
FETCH_DIRP();
rewinddir(dirp->dir);
}
/* }}} */
/* {{{ proto string readdir([int dir_handle])
Read directory entry from dir_handle */
PHP_FUNCTION(readdir)
{
pval **id, **tmp, *myself;
php_dir *dirp;
struct dirent *direntp;
DIRLS_FETCH();
FETCH_DIRP();
direntp = readdir(dirp->dir);
if (direntp) {
RETURN_STRINGL(direntp->d_name, strlen(direntp->d_name), 1);
}
RETURN_FALSE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|