File: dir.c

package info (click to toggle)
php3 3%3A3.0.18-0potato1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,736 kB
  • ctags: 11,198
  • sloc: ansic: 108,120; sh: 2,512; php: 2,024; yacc: 1,887; makefile: 1,038; perl: 537; pascal: 238; awk: 90; cpp: 28; sql: 11
file content (287 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download | duplicates (3)
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
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2000 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors:                                                             |
   +----------------------------------------------------------------------+
 */

/* $Id: dir.c,v 1.62 2000/02/20 20:16:31 eschmid Exp $ */
#include "php.h"
#include "internal_functions.h"
#include "fopen-wrappers.h"
#include "php3_list.h"

#include "php3_dir.h"

#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <errno.h>

#if MSVC5
#if !(APACHE)
#define NEEDRDH 1
#endif
#include "win32/readdir.h"
#endif

static int dirp_id = 0;
static int le_dirp;

function_entry php3_dir_functions[] = {
	{"opendir",		php3_opendir,	NULL},
	{"closedir",	php3_closedir,	NULL},
	{"chdir",		php3_chdir,		NULL},
	{"rewinddir",	php3_rewinddir,	NULL},
	{"readdir",		php3_readdir,	NULL},
	{"dir",			php3_getdir,	NULL},
	{NULL, NULL, NULL}
};

php3_module_entry php3_dir_module_entry = {
	"PHP_dir", php3_dir_functions, php3_minit_dir, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
};


static void php3i_destructor_closedir(DIR *dir) {
	(void)closedir(dir);
}

int php3_minit_dir(INIT_FUNC_ARGS)
{
	TLS_VARS;
	
	GLOBAL(le_dirp) = register_list_destructors(php3i_destructor_closedir,NULL);
	return SUCCESS;
}

/* {{{ proto int opendir(string path)
   Open a directory and return a dir_handle */
void php3_opendir(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *arg;
	DIR *dirp;
	int ret;
	TLS_VARS;
	
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	convert_to_string(arg);

	/* Check open_basedir */
	if (_php3_check_open_basedir(arg->value.str.val)) RETURN_FALSE;
	
	dirp = opendir(arg->value.str.val);
	if (!dirp) {
		php3_error(E_WARNING, "OpenDir: %s (errno %d)", strerror(errno),errno);
		RETURN_FALSE;
	}
	ret = php3_list_insert(dirp, GLOBAL(le_dirp));
	GLOBAL(dirp_id) = ret;
	RETURN_LONG(ret);
}
/* }}} */

/* {{{ proto void closedir([int dir_handle])
   Close directory connection identified by the dir_handle */
void php3_closedir(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *id, *tmp;
	int id_to_find;
	DIR *dirp;
	int dirp_type;
	TLS_VARS;
	
	if (ARG_COUNT(ht) == 0) {
		if (getThis(&id) == SUCCESS) {
			if (_php3_hash_find(id->value.ht, "handle", sizeof("handle"), (void **)&tmp) == FAILURE) {
				php3_error(E_WARNING, "unable to find my handle property");
				RETURN_FALSE;
			}
			id_to_find = tmp->value.lval;
		} else {
			id_to_find = GLOBAL(dirp_id);
		}
	} else if ((ARG_COUNT(ht) != 1) || getParameters(ht, 1, &id) == FAILURE) {
		WRONG_PARAM_COUNT;
	} else {
		convert_to_long(id);
		id_to_find = id->value.lval;
	}
		
	dirp = (DIR *)php3_list_find(id_to_find, &dirp_type);
	if (!dirp || dirp_type != GLOBAL(le_dirp)) {
		php3_error(E_WARNING, "unable to find identifier (%d)", id_to_find);
		RETURN_FALSE;
	}
	php3_list_delete(id_to_find);
}
/* }}} */

/* {{{ proto int chdir(string directory)
   Change the current directory */
void php3_chdir(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *arg;
	int ret;
	TLS_VARS;
	
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	convert_to_string(arg);
	ret = chdir(arg->value.str.val);

	if (ret < 0) {
		php3_error(E_WARNING, "ChDir: %s (errno %d)", strerror(errno), errno);
		RETURN_FALSE;
	}
	RETURN_TRUE;
}
/* }}} */

/* {{{ proto void rewinddir([int dir_handle])
Rewind dir_handle back to the start */
void php3_rewinddir(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *id, *tmp;
	int id_to_find;
	DIR *dirp;
	int dirp_type;
	TLS_VARS;
	
	if (ARG_COUNT(ht) == 0) {
		if (getThis(&id) == SUCCESS) {
			if (_php3_hash_find(id->value.ht, "handle", sizeof("handle"), (void **)&tmp) == FAILURE) {
				php3_error(E_WARNING, "unable to find my handle property");
				RETURN_FALSE;
			}
			id_to_find = tmp->value.lval;
		} else {
			id_to_find = GLOBAL(dirp_id);
		}
	} else if ((ARG_COUNT(ht) != 1) || getParameters(ht, 1, &id) == FAILURE) {
		WRONG_PARAM_COUNT;
	} else {
		convert_to_long(id);
		id_to_find = id->value.lval;
	}
		
	dirp = (DIR *)php3_list_find(id_to_find, &dirp_type);
	if (!dirp || dirp_type != GLOBAL(le_dirp)) {
		php3_error(E_WARNING, "unable to find identifier (%d)", id_to_find);
		RETURN_FALSE;
	}
	rewinddir(dirp);
}
/* }}} */

/* {{{ proto string readdir([int dir_handle])
   Read directory entry from dir_handle */
void php3_readdir(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *id, *tmp;
	int id_to_find;
	DIR *dirp;
	int dirp_type;
	struct dirent *direntp;
	TLS_VARS;
	
	if (ARG_COUNT(ht) == 0) {
		if (getThis(&id) == SUCCESS) {
			if (_php3_hash_find(id->value.ht, "handle", sizeof("handle"), (void **)&tmp) == FAILURE) {
				php3_error(E_WARNING, "unable to find my handle property");
				RETURN_FALSE;
			}
			id_to_find = tmp->value.lval;
		} else {
			id_to_find = GLOBAL(dirp_id);
		}
	} else if ((ARG_COUNT(ht) != 1) || getParameters(ht, 1, &id) == FAILURE) {
		WRONG_PARAM_COUNT;
	} else {
		convert_to_long(id);
		id_to_find = id->value.lval;
	}
		
	dirp = (DIR *)php3_list_find(id_to_find, &dirp_type);
	if (!dirp || dirp_type != GLOBAL(le_dirp)) {
		php3_error(E_WARNING, "unable to find identifier (%d)", id_to_find);
		RETURN_FALSE;
	}
	direntp = readdir(dirp);
	if (direntp) {
		RETURN_STRINGL(direntp->d_name, strlen(direntp->d_name), 1);
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto class dir(string directory)
   Directory class with properties, handle and class and methods read, rewind and close */
void php3_getdir(INTERNAL_FUNCTION_PARAMETERS) {
	pval *arg;
	DIR *dirp;
	int ret;
	TLS_VARS;
	
	if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	convert_to_string(arg);

	/* Check open_basedir */
	if (_php3_check_open_basedir(arg->value.str.val)) RETURN_FALSE;
	
	dirp = opendir(arg->value.str.val);
	if (!dirp) {
		php3_error(E_WARNING, "OpenDir: %s (errno %d)", strerror(errno), errno);
		RETURN_FALSE;
	}
	ret = php3_list_insert(dirp, GLOBAL(le_dirp));
	GLOBAL(dirp_id) = ret;

	/* construct an object with some methods */
	object_init(return_value);
	add_property_long(return_value, "handle", ret);
	add_property_stringl(return_value, "path", arg->value.str.val, arg->value.str.len, 1);
	add_method(return_value, "read", php3_readdir);
	add_method(return_value, "rewind", php3_rewinddir);
	add_method(return_value, "close", php3_closedir);
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */