PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/5.2.1.x/ext/standard/dir.c

https://github.com/cuiplay/dezend
C | 511 lines | 347 code | 85 blank | 79 comment | 73 complexity | 8891796cf5805c7bb9040c20a16552ee MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2007 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 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_01.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. /* $Id: dir.c,v 1.147.2.3.2.3 2007/01/22 09:31:46 dmitry Exp $ */
  19. /* {{{ includes/startup/misc */
  20. #include "php.h"
  21. #include "fopen_wrappers.h"
  22. #include "file.h"
  23. #include "php_dir.h"
  24. #include "php_scandir.h"
  25. #ifdef HAVE_DIRENT_H
  26. #include <dirent.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <errno.h>
  32. #ifdef PHP_WIN32
  33. #include "win32/readdir.h"
  34. #endif
  35. #ifdef HAVE_GLOB
  36. #ifndef PHP_WIN32
  37. #include <glob.h>
  38. #else
  39. #include "win32/glob.h"
  40. #endif
  41. #endif
  42. typedef struct {
  43. int default_dir;
  44. } php_dir_globals;
  45. #ifdef ZTS
  46. #define DIRG(v) TSRMG(dir_globals_id, php_dir_globals *, v)
  47. int dir_globals_id;
  48. #else
  49. #define DIRG(v) (dir_globals.v)
  50. php_dir_globals dir_globals;
  51. #endif
  52. #if 0
  53. typedef struct {
  54. int id;
  55. DIR *dir;
  56. } php_dir;
  57. static int le_dirp;
  58. #endif
  59. static zend_class_entry *dir_class_entry_ptr;
  60. #define FETCH_DIRP() \
  61. if (ZEND_NUM_ARGS() == 0) { \
  62. myself = getThis(); \
  63. if (myself) { \
  64. if (zend_hash_find(Z_OBJPROP_P(myself), "handle", sizeof("handle"), (void **)&tmp) == FAILURE) { \
  65. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find my handle property"); \
  66. RETURN_FALSE; \
  67. } \
  68. ZEND_FETCH_RESOURCE(dirp, php_stream *, tmp, -1, "Directory", php_file_le_stream()); \
  69. } else { \
  70. ZEND_FETCH_RESOURCE(dirp, php_stream *, 0, DIRG(default_dir), "Directory", php_file_le_stream()); \
  71. } \
  72. } else if ((ZEND_NUM_ARGS() != 1) || zend_get_parameters_ex(1, &id) == FAILURE) { \
  73. WRONG_PARAM_COUNT; \
  74. } else { \
  75. dirp = (php_stream *) zend_fetch_resource(id TSRMLS_CC, -1, "Directory", NULL, 1, php_file_le_stream()); \
  76. if (!dirp) \
  77. RETURN_FALSE; \
  78. }
  79. static zend_function_entry php_dir_class_functions[] = {
  80. PHP_FALIAS(close, closedir, NULL)
  81. PHP_FALIAS(rewind, rewinddir, NULL)
  82. PHP_NAMED_FE(read, php_if_readdir, NULL)
  83. {NULL, NULL, NULL}
  84. };
  85. static void php_set_default_dir(int id TSRMLS_DC)
  86. {
  87. if (DIRG(default_dir)!=-1) {
  88. zend_list_delete(DIRG(default_dir));
  89. }
  90. if (id != -1) {
  91. zend_list_addref(id);
  92. }
  93. DIRG(default_dir) = id;
  94. }
  95. PHP_RINIT_FUNCTION(dir)
  96. {
  97. DIRG(default_dir) = -1;
  98. return SUCCESS;
  99. }
  100. PHP_MINIT_FUNCTION(dir)
  101. {
  102. static char dirsep_str[2], pathsep_str[2];
  103. zend_class_entry dir_class_entry;
  104. INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
  105. dir_class_entry_ptr = zend_register_internal_class(&dir_class_entry TSRMLS_CC);
  106. #ifdef ZTS
  107. ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
  108. #endif
  109. dirsep_str[0] = DEFAULT_SLASH;
  110. dirsep_str[1] = '\0';
  111. REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);
  112. pathsep_str[0] = ZEND_PATHS_SEPARATOR;
  113. pathsep_str[1] = '\0';
  114. REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);
  115. #ifdef HAVE_GLOB
  116. #ifdef GLOB_BRACE
  117. REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
  118. #endif
  119. #ifdef GLOB_MARK
  120. REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
  121. #endif
  122. #ifdef GLOB_NOSORT
  123. REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
  124. #endif
  125. #ifdef GLOB_NOCHECK
  126. REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
  127. #endif
  128. #ifdef GLOB_NOESCAPE
  129. REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
  130. #endif
  131. #ifdef GLOB_ERR
  132. REGISTER_LONG_CONSTANT("GLOB_ERR", GLOB_ERR, CONST_CS | CONST_PERSISTENT);
  133. #endif
  134. #ifndef GLOB_ONLYDIR
  135. #define GLOB_ONLYDIR (1<<30)
  136. #define GLOB_EMULATE_ONLYDIR
  137. #define GLOB_FLAGMASK (~GLOB_ONLYDIR)
  138. #else
  139. #define GLOB_FLAGMASK (~0)
  140. #endif
  141. REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
  142. #endif /* HAVE_GLOB */
  143. return SUCCESS;
  144. }
  145. /* }}} */
  146. /* {{{ internal functions */
  147. static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
  148. {
  149. char *dirname;
  150. int dir_len;
  151. zval *zcontext = NULL;
  152. php_stream_context *context = NULL;
  153. php_stream *dirp;
  154. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dirname, &dir_len, &zcontext) == FAILURE) {
  155. RETURN_NULL();
  156. }
  157. if (zcontext) {
  158. context = php_stream_context_from_zval(zcontext, 0);
  159. }
  160. dirp = php_stream_opendir(dirname, ENFORCE_SAFE_MODE|REPORT_ERRORS, context);
  161. if (dirp == NULL) {
  162. RETURN_FALSE;
  163. }
  164. php_set_default_dir(dirp->rsrc_id TSRMLS_CC);
  165. if (createobject) {
  166. object_init_ex(return_value, dir_class_entry_ptr);
  167. add_property_stringl(return_value, "path", dirname, dir_len, 1);
  168. add_property_resource(return_value, "handle", dirp->rsrc_id);
  169. php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
  170. } else {
  171. php_stream_to_zval(dirp, return_value);
  172. }
  173. }
  174. /* }}} */
  175. /* {{{ proto mixed opendir(string path[, resource context])
  176. Open a directory and return a dir_handle */
  177. PHP_FUNCTION(opendir)
  178. {
  179. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  180. }
  181. /* }}} */
  182. /* {{{ proto object dir(string directory[, resource context])
  183. Directory class with properties, handle and class and methods read, rewind and close */
  184. PHP_FUNCTION(getdir)
  185. {
  186. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  187. }
  188. /* }}} */
  189. /* {{{ proto void closedir([resource dir_handle])
  190. Close directory connection identified by the dir_handle */
  191. PHP_FUNCTION(closedir)
  192. {
  193. zval **id, **tmp, *myself;
  194. php_stream *dirp;
  195. FETCH_DIRP();
  196. if (dirp->rsrc_id == DIRG(default_dir)) {
  197. php_set_default_dir(-1 TSRMLS_CC);
  198. }
  199. zend_list_delete(dirp->rsrc_id);
  200. }
  201. /* }}} */
  202. #if defined(HAVE_CHROOT) && !defined(ZTS) && ENABLE_CHROOT_FUNC
  203. /* {{{ proto bool chroot(string directory)
  204. Change root directory */
  205. PHP_FUNCTION(chroot)
  206. {
  207. char *str;
  208. int ret, str_len;
  209. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  210. RETURN_FALSE;
  211. }
  212. ret = chroot(str);
  213. if (ret != 0) {
  214. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  215. RETURN_FALSE;
  216. }
  217. realpath_cache_clean(TSRMLS_C);
  218. ret = chdir("/");
  219. if (ret != 0) {
  220. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  221. RETURN_FALSE;
  222. }
  223. RETURN_TRUE;
  224. }
  225. /* }}} */
  226. #endif
  227. /* {{{ proto bool chdir(string directory)
  228. Change the current directory */
  229. PHP_FUNCTION(chdir)
  230. {
  231. char *str;
  232. int ret, str_len;
  233. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  234. RETURN_FALSE;
  235. }
  236. if ((PG(safe_mode) && !php_checkuid(str, NULL, CHECKUID_CHECK_FILE_AND_DIR)) || php_check_open_basedir(str TSRMLS_CC)) {
  237. RETURN_FALSE;
  238. }
  239. ret = VCWD_CHDIR(str);
  240. if (ret != 0) {
  241. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  242. RETURN_FALSE;
  243. }
  244. RETURN_TRUE;
  245. }
  246. /* }}} */
  247. /* {{{ proto mixed getcwd(void)
  248. Gets the current directory */
  249. PHP_FUNCTION(getcwd)
  250. {
  251. char path[MAXPATHLEN];
  252. char *ret=NULL;
  253. if (ZEND_NUM_ARGS() != 0) {
  254. WRONG_PARAM_COUNT;
  255. }
  256. #if HAVE_GETCWD
  257. ret = VCWD_GETCWD(path, MAXPATHLEN);
  258. #elif HAVE_GETWD
  259. ret = VCWD_GETWD(path);
  260. #endif
  261. if (ret) {
  262. RETURN_STRING(path, 1);
  263. } else {
  264. RETURN_FALSE;
  265. }
  266. }
  267. /* }}} */
  268. /* {{{ proto void rewinddir([resource dir_handle])
  269. Rewind dir_handle back to the start */
  270. PHP_FUNCTION(rewinddir)
  271. {
  272. zval **id, **tmp, *myself;
  273. php_stream *dirp;
  274. FETCH_DIRP();
  275. php_stream_rewinddir(dirp);
  276. }
  277. /* }}} */
  278. /* {{{ proto string readdir([resource dir_handle])
  279. Read directory entry from dir_handle */
  280. PHP_NAMED_FUNCTION(php_if_readdir)
  281. {
  282. zval **id, **tmp, *myself;
  283. php_stream *dirp;
  284. php_stream_dirent entry;
  285. FETCH_DIRP();
  286. if (php_stream_readdir(dirp, &entry)) {
  287. RETURN_STRINGL(entry.d_name, strlen(entry.d_name), 1);
  288. }
  289. RETURN_FALSE;
  290. }
  291. /* }}} */
  292. #ifdef HAVE_GLOB
  293. /* {{{ proto array glob(string pattern [, int flags])
  294. Find pathnames matching a pattern */
  295. PHP_FUNCTION(glob)
  296. {
  297. char cwd[MAXPATHLEN];
  298. int cwd_skip = 0;
  299. #ifdef ZTS
  300. char work_pattern[MAXPATHLEN];
  301. char *result;
  302. #endif
  303. char *pattern = NULL;
  304. int pattern_len;
  305. long flags = 0;
  306. glob_t globbuf;
  307. int n;
  308. int ret;
  309. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &pattern, &pattern_len, &flags) == FAILURE)
  310. return;
  311. #ifdef ZTS
  312. if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
  313. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  314. if (!result) {
  315. cwd[0] = '\0';
  316. }
  317. #ifdef PHP_WIN32
  318. if (IS_SLASH(*pattern)) {
  319. cwd[2] = '\0';
  320. }
  321. #endif
  322. cwd_skip = strlen(cwd)+1;
  323. snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
  324. pattern = work_pattern;
  325. }
  326. #endif
  327. globbuf.gl_offs = 0;
  328. if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
  329. #ifdef GLOB_NOMATCH
  330. if (GLOB_NOMATCH == ret) {
  331. /* Some glob implementation simply return no data if no matches
  332. were found, others return the GLOB_NOMATCH error code.
  333. We don't want to treat GLOB_NOMATCH as an error condition
  334. so that PHP glob() behaves the same on both types of
  335. implementations and so that 'foreach (glob() as ...'
  336. can be used for simple glob() calls without further error
  337. checking.
  338. */
  339. array_init(return_value);
  340. return;
  341. }
  342. #endif
  343. RETURN_FALSE;
  344. }
  345. /* now catch the FreeBSD style of "no matches" */
  346. if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
  347. array_init(return_value);
  348. return;
  349. }
  350. /* we assume that any glob pattern will match files from one directory only
  351. so checking the dirname of the first match should be sufficient */
  352. strncpy(cwd, globbuf.gl_pathv[0], MAXPATHLEN);
  353. if (PG(safe_mode) && (!php_checkuid(cwd, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  354. RETURN_FALSE;
  355. }
  356. if (php_check_open_basedir(cwd TSRMLS_CC)) {
  357. RETURN_FALSE;
  358. }
  359. array_init(return_value);
  360. for (n = 0; n < globbuf.gl_pathc; n++) {
  361. /* we need to do this everytime since GLOB_ONLYDIR does not guarantee that
  362. * all directories will be filtered. GNU libc documentation states the
  363. * following:
  364. * If the information about the type of the file is easily available
  365. * non-directories will be rejected but no extra work will be done to
  366. * determine the information for each file. I.e., the caller must still be
  367. * able to filter directories out.
  368. */
  369. if (flags & GLOB_ONLYDIR) {
  370. struct stat s;
  371. if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
  372. continue;
  373. }
  374. if (S_IFDIR != (s.st_mode & S_IFMT)) {
  375. continue;
  376. }
  377. }
  378. add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip, 1);
  379. }
  380. globfree(&globbuf);
  381. }
  382. /* }}} */
  383. #endif
  384. /* {{{ proto array scandir(string dir [, int sorting_order [, resource context]])
  385. List files & directories inside the specified path */
  386. PHP_FUNCTION(scandir)
  387. {
  388. char *dirn;
  389. int dirn_len;
  390. long flags = 0;
  391. char **namelist;
  392. int n, i;
  393. zval *zcontext = NULL;
  394. php_stream_context *context = NULL;
  395. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &dirn, &dirn_len, &flags, &zcontext) == FAILURE) {
  396. return;
  397. }
  398. if (zcontext) {
  399. context = php_stream_context_from_zval(zcontext, 0);
  400. }
  401. if (!flags) {
  402. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
  403. } else {
  404. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
  405. }
  406. if (n < 0) {
  407. php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno));
  408. RETURN_FALSE;
  409. }
  410. array_init(return_value);
  411. for (i = 0; i < n; i++) {
  412. add_next_index_string(return_value, namelist[i], 0);
  413. }
  414. if (n) {
  415. efree(namelist);
  416. }
  417. }
  418. /* }}} */
  419. /*
  420. * Local variables:
  421. * tab-width: 4
  422. * c-basic-offset: 4
  423. * End:
  424. * vim600: sw=4 ts=4 fdm=marker
  425. * vim<600: sw=4 ts=4
  426. */