PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/file.c

https://github.com/php/php-src
C | 2476 lines | 1965 code | 351 blank | 160 comment | 351 complexity | 21a37653722ba804c836a7befde83e01 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  14. | Stig Bakken <ssb@php.net> |
  15. | Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | PHP 4.0 patches by Thies C. Arntzen (thies@thieso.net) |
  18. | PHP streams by Wez Furlong (wez@thebrainroom.com) |
  19. +----------------------------------------------------------------------+
  20. */
  21. /* {{{ includes */
  22. #include "php.h"
  23. #include "php_globals.h"
  24. #include "ext/standard/flock_compat.h"
  25. #include "ext/standard/exec.h"
  26. #include "ext/standard/php_filestat.h"
  27. #include "php_open_temporary_file.h"
  28. #include "ext/standard/basic_functions.h"
  29. #include "php_ini.h"
  30. #include "zend_smart_str.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <wchar.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <fcntl.h>
  38. #ifdef PHP_WIN32
  39. # include <io.h>
  40. # define O_RDONLY _O_RDONLY
  41. # include "win32/param.h"
  42. # include "win32/winutil.h"
  43. # include "win32/fnmatch.h"
  44. # include "win32/ioutil.h"
  45. #else
  46. # if HAVE_SYS_PARAM_H
  47. # include <sys/param.h>
  48. # endif
  49. # if HAVE_SYS_SELECT_H
  50. # include <sys/select.h>
  51. # endif
  52. # include <sys/socket.h>
  53. # include <netinet/in.h>
  54. # include <netdb.h>
  55. # if HAVE_ARPA_INET_H
  56. # include <arpa/inet.h>
  57. # endif
  58. #endif
  59. #include "ext/standard/head.h"
  60. #include "php_string.h"
  61. #include "file.h"
  62. #if HAVE_PWD_H
  63. # ifdef PHP_WIN32
  64. # include "win32/pwd.h"
  65. # else
  66. # include <pwd.h>
  67. # endif
  68. #endif
  69. #ifdef HAVE_SYS_TIME_H
  70. # include <sys/time.h>
  71. #endif
  72. #include "fsock.h"
  73. #include "fopen_wrappers.h"
  74. #include "streamsfuncs.h"
  75. #include "php_globals.h"
  76. #ifdef HAVE_SYS_FILE_H
  77. # include <sys/file.h>
  78. #endif
  79. #if MISSING_FCLOSE_DECL
  80. extern int fclose(FILE *);
  81. #endif
  82. #ifdef HAVE_SYS_MMAN_H
  83. # include <sys/mman.h>
  84. #endif
  85. #include "scanf.h"
  86. #include "zend_API.h"
  87. #ifdef ZTS
  88. int file_globals_id;
  89. #else
  90. php_file_globals file_globals;
  91. #endif
  92. #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32)
  93. # ifndef _GNU_SOURCE
  94. # define _GNU_SOURCE
  95. # endif
  96. # include <fnmatch.h>
  97. #endif
  98. /* }}} */
  99. #define PHP_STREAM_TO_ZVAL(stream, arg) \
  100. ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); \
  101. php_stream_from_res(stream, Z_RES_P(arg));
  102. /* {{{ ZTS-stuff / Globals / Prototypes */
  103. /* sharing globals is *evil* */
  104. static int le_stream_context = FAILURE;
  105. PHPAPI int php_le_stream_context(void)
  106. {
  107. return le_stream_context;
  108. }
  109. /* }}} */
  110. /* {{{ Module-Stuff */
  111. static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
  112. {
  113. php_stream_context *context = (php_stream_context*)res->ptr;
  114. if (Z_TYPE(context->options) != IS_UNDEF) {
  115. zval_ptr_dtor(&context->options);
  116. ZVAL_UNDEF(&context->options);
  117. }
  118. php_stream_context_free(context);
  119. }
  120. static void file_globals_ctor(php_file_globals *file_globals_p)
  121. {
  122. memset(file_globals_p, 0, sizeof(php_file_globals));
  123. file_globals_p->def_chunk_size = PHP_SOCK_CHUNK_SIZE;
  124. }
  125. static void file_globals_dtor(php_file_globals *file_globals_p)
  126. {
  127. #if defined(HAVE_GETHOSTBYNAME_R)
  128. if (file_globals_p->tmp_host_buf) {
  129. free(file_globals_p->tmp_host_buf);
  130. }
  131. #endif
  132. }
  133. static PHP_INI_MH(OnUpdateAutoDetectLineEndings)
  134. {
  135. if (zend_ini_parse_bool(new_value)) {
  136. zend_error(E_DEPRECATED, "auto_detect_line_endings is deprecated");
  137. }
  138. return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  139. }
  140. PHP_INI_BEGIN()
  141. STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateString, user_agent, php_file_globals, file_globals)
  142. STD_PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, OnUpdateString, from_address, php_file_globals, file_globals)
  143. STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals)
  144. STD_PHP_INI_BOOLEAN("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateAutoDetectLineEndings, auto_detect_line_endings, php_file_globals, file_globals)
  145. PHP_INI_END()
  146. PHP_MINIT_FUNCTION(file)
  147. {
  148. le_stream_context = zend_register_list_destructors_ex(file_context_dtor, NULL, "stream-context", module_number);
  149. #ifdef ZTS
  150. ts_allocate_id(&file_globals_id, sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);
  151. #else
  152. file_globals_ctor(&file_globals);
  153. #endif
  154. REGISTER_INI_ENTRIES();
  155. REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
  156. REGISTER_LONG_CONSTANT("SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
  157. REGISTER_LONG_CONSTANT("SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
  158. REGISTER_LONG_CONSTANT("LOCK_SH", PHP_LOCK_SH, CONST_CS | CONST_PERSISTENT);
  159. REGISTER_LONG_CONSTANT("LOCK_EX", PHP_LOCK_EX, CONST_CS | CONST_PERSISTENT);
  160. REGISTER_LONG_CONSTANT("LOCK_UN", PHP_LOCK_UN, CONST_CS | CONST_PERSISTENT);
  161. REGISTER_LONG_CONSTANT("LOCK_NB", PHP_LOCK_NB, CONST_CS | CONST_PERSISTENT);
  162. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_CONNECT", PHP_STREAM_NOTIFY_CONNECT, CONST_CS | CONST_PERSISTENT);
  163. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_REQUIRED", PHP_STREAM_NOTIFY_AUTH_REQUIRED, CONST_CS | CONST_PERSISTENT);
  164. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_AUTH_RESULT", PHP_STREAM_NOTIFY_AUTH_RESULT, CONST_CS | CONST_PERSISTENT);
  165. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_MIME_TYPE_IS", PHP_STREAM_NOTIFY_MIME_TYPE_IS, CONST_CS | CONST_PERSISTENT);
  166. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FILE_SIZE_IS", PHP_STREAM_NOTIFY_FILE_SIZE_IS, CONST_CS | CONST_PERSISTENT);
  167. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_REDIRECTED", PHP_STREAM_NOTIFY_REDIRECTED, CONST_CS | CONST_PERSISTENT);
  168. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_PROGRESS", PHP_STREAM_NOTIFY_PROGRESS, CONST_CS | CONST_PERSISTENT);
  169. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_FAILURE", PHP_STREAM_NOTIFY_FAILURE, CONST_CS | CONST_PERSISTENT);
  170. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_COMPLETED", PHP_STREAM_NOTIFY_COMPLETED, CONST_CS | CONST_PERSISTENT);
  171. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_RESOLVE", PHP_STREAM_NOTIFY_RESOLVE, CONST_CS | CONST_PERSISTENT);
  172. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_INFO", PHP_STREAM_NOTIFY_SEVERITY_INFO, CONST_CS | CONST_PERSISTENT);
  173. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN", PHP_STREAM_NOTIFY_SEVERITY_WARN, CONST_CS | CONST_PERSISTENT);
  174. REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR", PHP_STREAM_NOTIFY_SEVERITY_ERR, CONST_CS | CONST_PERSISTENT);
  175. REGISTER_LONG_CONSTANT("STREAM_FILTER_READ", PHP_STREAM_FILTER_READ, CONST_CS | CONST_PERSISTENT);
  176. REGISTER_LONG_CONSTANT("STREAM_FILTER_WRITE", PHP_STREAM_FILTER_WRITE, CONST_CS | CONST_PERSISTENT);
  177. REGISTER_LONG_CONSTANT("STREAM_FILTER_ALL", PHP_STREAM_FILTER_ALL, CONST_CS | CONST_PERSISTENT);
  178. REGISTER_LONG_CONSTANT("STREAM_CLIENT_PERSISTENT", PHP_STREAM_CLIENT_PERSISTENT, CONST_CS | CONST_PERSISTENT);
  179. REGISTER_LONG_CONSTANT("STREAM_CLIENT_ASYNC_CONNECT", PHP_STREAM_CLIENT_ASYNC_CONNECT, CONST_CS | CONST_PERSISTENT);
  180. REGISTER_LONG_CONSTANT("STREAM_CLIENT_CONNECT", PHP_STREAM_CLIENT_CONNECT, CONST_CS | CONST_PERSISTENT);
  181. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_CLIENT", STREAM_CRYPTO_METHOD_ANY_CLIENT, CONST_CS|CONST_PERSISTENT);
  182. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_CLIENT", STREAM_CRYPTO_METHOD_SSLv2_CLIENT, CONST_CS|CONST_PERSISTENT);
  183. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_CLIENT", STREAM_CRYPTO_METHOD_SSLv3_CLIENT, CONST_CS|CONST_PERSISTENT);
  184. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_CLIENT", STREAM_CRYPTO_METHOD_SSLv23_CLIENT, CONST_CS|CONST_PERSISTENT);
  185. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_CLIENT", STREAM_CRYPTO_METHOD_TLS_CLIENT, CONST_CS|CONST_PERSISTENT);
  186. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, CONST_CS|CONST_PERSISTENT);
  187. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, CONST_CS|CONST_PERSISTENT);
  188. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, CONST_CS|CONST_PERSISTENT);
  189. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT", STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT, CONST_CS|CONST_PERSISTENT);
  190. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_ANY_SERVER", STREAM_CRYPTO_METHOD_ANY_SERVER, CONST_CS|CONST_PERSISTENT);
  191. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv2_SERVER", STREAM_CRYPTO_METHOD_SSLv2_SERVER, CONST_CS|CONST_PERSISTENT);
  192. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv3_SERVER", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
  193. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_SERVER", STREAM_CRYPTO_METHOD_SSLv23_SERVER, CONST_CS|CONST_PERSISTENT);
  194. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_SERVER", STREAM_CRYPTO_METHOD_TLS_SERVER, CONST_CS|CONST_PERSISTENT);
  195. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_0_SERVER", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
  196. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_1_SERVER", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
  197. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_2_SERVER", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
  198. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLSv1_3_SERVER", STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, CONST_CS|CONST_PERSISTENT);
  199. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_SSLv3", STREAM_CRYPTO_METHOD_SSLv3_SERVER, CONST_CS|CONST_PERSISTENT);
  200. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_0", STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, CONST_CS|CONST_PERSISTENT);
  201. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_1", STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, CONST_CS|CONST_PERSISTENT);
  202. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_2", STREAM_CRYPTO_METHOD_TLSv1_2_SERVER, CONST_CS|CONST_PERSISTENT);
  203. REGISTER_LONG_CONSTANT("STREAM_CRYPTO_PROTO_TLSv1_3", STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, CONST_CS|CONST_PERSISTENT);
  204. REGISTER_LONG_CONSTANT("STREAM_SHUT_RD", STREAM_SHUT_RD, CONST_CS|CONST_PERSISTENT);
  205. REGISTER_LONG_CONSTANT("STREAM_SHUT_WR", STREAM_SHUT_WR, CONST_CS|CONST_PERSISTENT);
  206. REGISTER_LONG_CONSTANT("STREAM_SHUT_RDWR", STREAM_SHUT_RDWR, CONST_CS|CONST_PERSISTENT);
  207. #ifdef PF_INET
  208. REGISTER_LONG_CONSTANT("STREAM_PF_INET", PF_INET, CONST_CS|CONST_PERSISTENT);
  209. #elif defined(AF_INET)
  210. REGISTER_LONG_CONSTANT("STREAM_PF_INET", AF_INET, CONST_CS|CONST_PERSISTENT);
  211. #endif
  212. #if HAVE_IPV6
  213. # ifdef PF_INET6
  214. REGISTER_LONG_CONSTANT("STREAM_PF_INET6", PF_INET6, CONST_CS|CONST_PERSISTENT);
  215. # elif defined(AF_INET6)
  216. REGISTER_LONG_CONSTANT("STREAM_PF_INET6", AF_INET6, CONST_CS|CONST_PERSISTENT);
  217. # endif
  218. #endif
  219. #ifdef PF_UNIX
  220. REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", PF_UNIX, CONST_CS|CONST_PERSISTENT);
  221. #elif defined(AF_UNIX)
  222. REGISTER_LONG_CONSTANT("STREAM_PF_UNIX", AF_UNIX, CONST_CS|CONST_PERSISTENT);
  223. #endif
  224. #ifdef IPPROTO_IP
  225. /* most people will use this one when calling socket() or socketpair() */
  226. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_IP", IPPROTO_IP, CONST_CS|CONST_PERSISTENT);
  227. #endif
  228. #if defined(IPPROTO_TCP) || defined(PHP_WIN32)
  229. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_TCP", IPPROTO_TCP, CONST_CS|CONST_PERSISTENT);
  230. #endif
  231. #if defined(IPPROTO_UDP) || defined(PHP_WIN32)
  232. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_UDP", IPPROTO_UDP, CONST_CS|CONST_PERSISTENT);
  233. #endif
  234. #if defined(IPPROTO_ICMP) || defined(PHP_WIN32)
  235. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_ICMP", IPPROTO_ICMP, CONST_CS|CONST_PERSISTENT);
  236. #endif
  237. #if defined(IPPROTO_RAW) || defined(PHP_WIN32)
  238. REGISTER_LONG_CONSTANT("STREAM_IPPROTO_RAW", IPPROTO_RAW, CONST_CS|CONST_PERSISTENT);
  239. #endif
  240. REGISTER_LONG_CONSTANT("STREAM_SOCK_STREAM", SOCK_STREAM, CONST_CS|CONST_PERSISTENT);
  241. REGISTER_LONG_CONSTANT("STREAM_SOCK_DGRAM", SOCK_DGRAM, CONST_CS|CONST_PERSISTENT);
  242. #ifdef SOCK_RAW
  243. REGISTER_LONG_CONSTANT("STREAM_SOCK_RAW", SOCK_RAW, CONST_CS|CONST_PERSISTENT);
  244. #endif
  245. #ifdef SOCK_SEQPACKET
  246. REGISTER_LONG_CONSTANT("STREAM_SOCK_SEQPACKET", SOCK_SEQPACKET, CONST_CS|CONST_PERSISTENT);
  247. #endif
  248. #ifdef SOCK_RDM
  249. REGISTER_LONG_CONSTANT("STREAM_SOCK_RDM", SOCK_RDM, CONST_CS|CONST_PERSISTENT);
  250. #endif
  251. REGISTER_LONG_CONSTANT("STREAM_PEEK", STREAM_PEEK, CONST_CS | CONST_PERSISTENT);
  252. REGISTER_LONG_CONSTANT("STREAM_OOB", STREAM_OOB, CONST_CS | CONST_PERSISTENT);
  253. REGISTER_LONG_CONSTANT("STREAM_SERVER_BIND", STREAM_XPORT_BIND, CONST_CS | CONST_PERSISTENT);
  254. REGISTER_LONG_CONSTANT("STREAM_SERVER_LISTEN", STREAM_XPORT_LISTEN, CONST_CS | CONST_PERSISTENT);
  255. REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH, CONST_CS | CONST_PERSISTENT);
  256. REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES, CONST_CS | CONST_PERSISTENT);
  257. REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES, CONST_CS | CONST_PERSISTENT);
  258. REGISTER_LONG_CONSTANT("FILE_APPEND", PHP_FILE_APPEND, CONST_CS | CONST_PERSISTENT);
  259. REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT", PHP_FILE_NO_DEFAULT_CONTEXT, CONST_CS | CONST_PERSISTENT);
  260. REGISTER_LONG_CONSTANT("FILE_TEXT", 0, CONST_CS | CONST_PERSISTENT | CONST_DEPRECATED);
  261. REGISTER_LONG_CONSTANT("FILE_BINARY", 0, CONST_CS | CONST_PERSISTENT | CONST_DEPRECATED);
  262. #ifdef HAVE_FNMATCH
  263. REGISTER_LONG_CONSTANT("FNM_NOESCAPE", FNM_NOESCAPE, CONST_CS | CONST_PERSISTENT);
  264. REGISTER_LONG_CONSTANT("FNM_PATHNAME", FNM_PATHNAME, CONST_CS | CONST_PERSISTENT);
  265. REGISTER_LONG_CONSTANT("FNM_PERIOD", FNM_PERIOD, CONST_CS | CONST_PERSISTENT);
  266. # ifdef FNM_CASEFOLD /* a GNU extension */ /* TODO emulate if not available */
  267. REGISTER_LONG_CONSTANT("FNM_CASEFOLD", FNM_CASEFOLD, CONST_CS | CONST_PERSISTENT);
  268. # endif
  269. #endif
  270. return SUCCESS;
  271. }
  272. /* }}} */
  273. PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */
  274. {
  275. #ifndef ZTS
  276. file_globals_dtor(&file_globals);
  277. #endif
  278. return SUCCESS;
  279. }
  280. /* }}} */
  281. PHPAPI void php_flock_common(php_stream *stream, zend_long operation,
  282. uint32_t operation_arg_num, zval *wouldblock, zval *return_value)
  283. {
  284. int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
  285. int act;
  286. act = operation & PHP_LOCK_UN;
  287. if (act < 1 || act > 3) {
  288. zend_argument_value_error(operation_arg_num, "must be one of LOCK_SH, LOCK_EX, or LOCK_UN");
  289. RETURN_THROWS();
  290. }
  291. if (wouldblock) {
  292. ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 0);
  293. }
  294. /* flock_values contains all possible actions if (operation & PHP_LOCK_NB) we won't block on the lock */
  295. act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0);
  296. if (php_stream_lock(stream, act)) {
  297. if (operation && errno == EWOULDBLOCK && wouldblock) {
  298. ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 1);
  299. }
  300. RETURN_FALSE;
  301. }
  302. RETURN_TRUE;
  303. }
  304. /* {{{ Portable file locking */
  305. PHP_FUNCTION(flock)
  306. {
  307. zval *res, *wouldblock = NULL;
  308. php_stream *stream;
  309. zend_long operation = 0;
  310. ZEND_PARSE_PARAMETERS_START(2, 3)
  311. Z_PARAM_RESOURCE(res)
  312. Z_PARAM_LONG(operation)
  313. Z_PARAM_OPTIONAL
  314. Z_PARAM_ZVAL(wouldblock)
  315. ZEND_PARSE_PARAMETERS_END();
  316. PHP_STREAM_TO_ZVAL(stream, res);
  317. php_flock_common(stream, operation, 2, wouldblock, return_value);
  318. }
  319. /* }}} */
  320. #define PHP_META_UNSAFE ".\\+*?[^]$() "
  321. /* {{{ Extracts all meta tag content attributes from a file and returns an array */
  322. PHP_FUNCTION(get_meta_tags)
  323. {
  324. char *filename;
  325. size_t filename_len;
  326. bool use_include_path = 0;
  327. int in_tag = 0, done = 0;
  328. int looking_for_val = 0, have_name = 0, have_content = 0;
  329. int saw_name = 0, saw_content = 0;
  330. char *name = NULL, *value = NULL, *temp = NULL;
  331. php_meta_tags_token tok, tok_last;
  332. php_meta_tags_data md;
  333. /* Initialize our structure */
  334. memset(&md, 0, sizeof(md));
  335. /* Parse arguments */
  336. ZEND_PARSE_PARAMETERS_START(1, 2)
  337. Z_PARAM_PATH(filename, filename_len)
  338. Z_PARAM_OPTIONAL
  339. Z_PARAM_BOOL(use_include_path)
  340. ZEND_PARSE_PARAMETERS_END();
  341. md.stream = php_stream_open_wrapper(filename, "rb",
  342. (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
  343. NULL);
  344. if (!md.stream) {
  345. RETURN_FALSE;
  346. }
  347. array_init(return_value);
  348. tok_last = TOK_EOF;
  349. while (!done && (tok = php_next_meta_token(&md)) != TOK_EOF) {
  350. if (tok == TOK_ID) {
  351. if (tok_last == TOK_OPENTAG) {
  352. md.in_meta = !strcasecmp("meta", md.token_data);
  353. } else if (tok_last == TOK_SLASH && in_tag) {
  354. if (strcasecmp("head", md.token_data) == 0) {
  355. /* We are done here! */
  356. done = 1;
  357. }
  358. } else if (tok_last == TOK_EQUAL && looking_for_val) {
  359. if (saw_name) {
  360. if (name) efree(name);
  361. /* Get the NAME attr (Single word attr, non-quoted) */
  362. temp = name = estrndup(md.token_data, md.token_len);
  363. while (temp && *temp) {
  364. if (strchr(PHP_META_UNSAFE, *temp)) {
  365. *temp = '_';
  366. }
  367. temp++;
  368. }
  369. have_name = 1;
  370. } else if (saw_content) {
  371. if (value) efree(value);
  372. value = estrndup(md.token_data, md.token_len);
  373. have_content = 1;
  374. }
  375. looking_for_val = 0;
  376. } else {
  377. if (md.in_meta) {
  378. if (strcasecmp("name", md.token_data) == 0) {
  379. saw_name = 1;
  380. saw_content = 0;
  381. looking_for_val = 1;
  382. } else if (strcasecmp("content", md.token_data) == 0) {
  383. saw_name = 0;
  384. saw_content = 1;
  385. looking_for_val = 1;
  386. }
  387. }
  388. }
  389. } else if (tok == TOK_STRING && tok_last == TOK_EQUAL && looking_for_val) {
  390. if (saw_name) {
  391. if (name) efree(name);
  392. /* Get the NAME attr (Quoted single/double) */
  393. temp = name = estrndup(md.token_data, md.token_len);
  394. while (temp && *temp) {
  395. if (strchr(PHP_META_UNSAFE, *temp)) {
  396. *temp = '_';
  397. }
  398. temp++;
  399. }
  400. have_name = 1;
  401. } else if (saw_content) {
  402. if (value) efree(value);
  403. value = estrndup(md.token_data, md.token_len);
  404. have_content = 1;
  405. }
  406. looking_for_val = 0;
  407. } else if (tok == TOK_OPENTAG) {
  408. if (looking_for_val) {
  409. looking_for_val = 0;
  410. have_name = saw_name = 0;
  411. have_content = saw_content = 0;
  412. }
  413. in_tag = 1;
  414. } else if (tok == TOK_CLOSETAG) {
  415. if (have_name) {
  416. /* For BC */
  417. zend_str_tolower(name, strlen(name));
  418. if (have_content) {
  419. add_assoc_string(return_value, name, value);
  420. } else {
  421. add_assoc_string(return_value, name, "");
  422. }
  423. efree(name);
  424. if (value) efree(value);
  425. } else if (have_content) {
  426. efree(value);
  427. }
  428. name = value = NULL;
  429. /* Reset all of our flags */
  430. in_tag = looking_for_val = 0;
  431. have_name = saw_name = 0;
  432. have_content = saw_content = 0;
  433. md.in_meta = 0;
  434. }
  435. tok_last = tok;
  436. if (md.token_data)
  437. efree(md.token_data);
  438. md.token_data = NULL;
  439. }
  440. if (value) efree(value);
  441. if (name) efree(name);
  442. php_stream_close(md.stream);
  443. }
  444. /* }}} */
  445. /* {{{ Read the entire file into a string */
  446. PHP_FUNCTION(file_get_contents)
  447. {
  448. char *filename;
  449. size_t filename_len;
  450. bool use_include_path = 0;
  451. php_stream *stream;
  452. zend_long offset = 0;
  453. zend_long maxlen;
  454. bool maxlen_is_null = 1;
  455. zval *zcontext = NULL;
  456. php_stream_context *context = NULL;
  457. zend_string *contents;
  458. /* Parse arguments */
  459. ZEND_PARSE_PARAMETERS_START(1, 5)
  460. Z_PARAM_PATH(filename, filename_len)
  461. Z_PARAM_OPTIONAL
  462. Z_PARAM_BOOL(use_include_path)
  463. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  464. Z_PARAM_LONG(offset)
  465. Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
  466. ZEND_PARSE_PARAMETERS_END();
  467. if (maxlen_is_null) {
  468. maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
  469. } else if (maxlen < 0) {
  470. zend_argument_value_error(5, "must be greater than or equal to 0");
  471. RETURN_THROWS();
  472. }
  473. context = php_stream_context_from_zval(zcontext, 0);
  474. stream = php_stream_open_wrapper_ex(filename, "rb",
  475. (use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
  476. NULL, context);
  477. if (!stream) {
  478. RETURN_FALSE;
  479. }
  480. if (offset != 0 && php_stream_seek(stream, offset, ((offset > 0) ? SEEK_SET : SEEK_END)) < 0) {
  481. php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", offset);
  482. php_stream_close(stream);
  483. RETURN_FALSE;
  484. }
  485. if ((contents = php_stream_copy_to_mem(stream, maxlen, 0)) != NULL) {
  486. RETVAL_STR(contents);
  487. } else {
  488. RETVAL_EMPTY_STRING();
  489. }
  490. php_stream_close(stream);
  491. }
  492. /* }}} */
  493. /* {{{ Write/Create a file with contents data and return the number of bytes written */
  494. PHP_FUNCTION(file_put_contents)
  495. {
  496. php_stream *stream;
  497. char *filename;
  498. size_t filename_len;
  499. zval *data;
  500. ssize_t numbytes = 0;
  501. zend_long flags = 0;
  502. zval *zcontext = NULL;
  503. php_stream_context *context = NULL;
  504. php_stream *srcstream = NULL;
  505. char mode[3] = "wb";
  506. ZEND_PARSE_PARAMETERS_START(2, 4)
  507. Z_PARAM_PATH(filename, filename_len)
  508. Z_PARAM_ZVAL(data)
  509. Z_PARAM_OPTIONAL
  510. Z_PARAM_LONG(flags)
  511. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  512. ZEND_PARSE_PARAMETERS_END();
  513. if (Z_TYPE_P(data) == IS_RESOURCE) {
  514. php_stream_from_zval(srcstream, data);
  515. }
  516. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  517. if (flags & PHP_FILE_APPEND) {
  518. mode[0] = 'a';
  519. } else if (flags & LOCK_EX) {
  520. /* check to make sure we are dealing with a regular file */
  521. if (php_memnstr(filename, "://", sizeof("://") - 1, filename + filename_len)) {
  522. if (strncasecmp(filename, "file://", sizeof("file://") - 1)) {
  523. php_error_docref(NULL, E_WARNING, "Exclusive locks may only be set for regular files");
  524. RETURN_FALSE;
  525. }
  526. }
  527. mode[0] = 'c';
  528. }
  529. mode[2] = '\0';
  530. stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  531. if (stream == NULL) {
  532. RETURN_FALSE;
  533. }
  534. if ((flags & LOCK_EX) && (!php_stream_supports_lock(stream) || php_stream_lock(stream, LOCK_EX))) {
  535. php_stream_close(stream);
  536. php_error_docref(NULL, E_WARNING, "Exclusive locks are not supported for this stream");
  537. RETURN_FALSE;
  538. }
  539. if (mode[0] == 'c') {
  540. php_stream_truncate_set_size(stream, 0);
  541. }
  542. switch (Z_TYPE_P(data)) {
  543. case IS_RESOURCE: {
  544. size_t len;
  545. if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
  546. numbytes = -1;
  547. } else {
  548. if (len > ZEND_LONG_MAX) {
  549. php_error_docref(NULL, E_WARNING, "content truncated from %zu to " ZEND_LONG_FMT " bytes", len, ZEND_LONG_MAX);
  550. len = ZEND_LONG_MAX;
  551. }
  552. numbytes = len;
  553. }
  554. break;
  555. }
  556. case IS_NULL:
  557. case IS_LONG:
  558. case IS_DOUBLE:
  559. case IS_FALSE:
  560. case IS_TRUE:
  561. convert_to_string(data);
  562. ZEND_FALLTHROUGH;
  563. case IS_STRING:
  564. if (Z_STRLEN_P(data)) {
  565. numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));
  566. if (numbytes != -1 && numbytes != Z_STRLEN_P(data)) {
  567. php_error_docref(NULL, E_WARNING, "Only %zd of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN_P(data));
  568. numbytes = -1;
  569. }
  570. }
  571. break;
  572. case IS_ARRAY:
  573. if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
  574. ssize_t bytes_written;
  575. zval *tmp;
  576. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), tmp) {
  577. zend_string *t;
  578. zend_string *str = zval_get_tmp_string(tmp, &t);
  579. if (ZSTR_LEN(str)) {
  580. numbytes += ZSTR_LEN(str);
  581. bytes_written = php_stream_write(stream, ZSTR_VAL(str), ZSTR_LEN(str));
  582. if (bytes_written != ZSTR_LEN(str)) {
  583. php_error_docref(NULL, E_WARNING, "Failed to write %zd bytes to %s", ZSTR_LEN(str), filename);
  584. zend_tmp_string_release(t);
  585. numbytes = -1;
  586. break;
  587. }
  588. }
  589. zend_tmp_string_release(t);
  590. } ZEND_HASH_FOREACH_END();
  591. }
  592. break;
  593. case IS_OBJECT:
  594. if (Z_OBJ_HT_P(data) != NULL) {
  595. zval out;
  596. if (zend_std_cast_object_tostring(Z_OBJ_P(data), &out, IS_STRING) == SUCCESS) {
  597. numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRLEN(out));
  598. if (numbytes != -1 && numbytes != Z_STRLEN(out)) {
  599. php_error_docref(NULL, E_WARNING, "Only %zd of %zd bytes written, possibly out of free disk space", numbytes, Z_STRLEN(out));
  600. numbytes = -1;
  601. }
  602. zval_ptr_dtor_str(&out);
  603. break;
  604. }
  605. }
  606. ZEND_FALLTHROUGH;
  607. default:
  608. numbytes = -1;
  609. break;
  610. }
  611. php_stream_close(stream);
  612. if (numbytes < 0) {
  613. RETURN_FALSE;
  614. }
  615. RETURN_LONG(numbytes);
  616. }
  617. /* }}} */
  618. #define PHP_FILE_BUF_SIZE 80
  619. /* {{{ Read entire file into an array */
  620. PHP_FUNCTION(file)
  621. {
  622. char *filename;
  623. size_t filename_len;
  624. char *p, *s, *e;
  625. int i = 0;
  626. char eol_marker = '\n';
  627. zend_long flags = 0;
  628. bool use_include_path;
  629. bool include_new_line;
  630. bool skip_blank_lines;
  631. php_stream *stream;
  632. zval *zcontext = NULL;
  633. php_stream_context *context = NULL;
  634. zend_string *target_buf;
  635. /* Parse arguments */
  636. ZEND_PARSE_PARAMETERS_START(1, 3)
  637. Z_PARAM_PATH(filename, filename_len)
  638. Z_PARAM_OPTIONAL
  639. Z_PARAM_LONG(flags)
  640. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  641. ZEND_PARSE_PARAMETERS_END();
  642. if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
  643. zend_argument_value_error(2, "must be a valid flag value");
  644. RETURN_THROWS();
  645. }
  646. use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH;
  647. include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES);
  648. skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES;
  649. context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
  650. stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  651. if (!stream) {
  652. RETURN_FALSE;
  653. }
  654. /* Initialize return array */
  655. array_init(return_value);
  656. if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) {
  657. s = ZSTR_VAL(target_buf);
  658. e = ZSTR_VAL(target_buf) + ZSTR_LEN(target_buf);
  659. if (!(p = (char*)php_stream_locate_eol(stream, target_buf))) {
  660. p = e;
  661. goto parse_eol;
  662. }
  663. if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
  664. eol_marker = '\r';
  665. }
  666. /* for performance reasons the code is duplicated, so that the if (include_new_line)
  667. * will not need to be done for every single line in the file. */
  668. if (include_new_line) {
  669. do {
  670. p++;
  671. parse_eol:
  672. add_index_stringl(return_value, i++, s, p-s);
  673. s = p;
  674. } while ((p = memchr(p, eol_marker, (e-p))));
  675. } else {
  676. do {
  677. int windows_eol = 0;
  678. if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') {
  679. windows_eol++;
  680. }
  681. if (skip_blank_lines && !(p-s-windows_eol)) {
  682. s = ++p;
  683. continue;
  684. }
  685. add_index_stringl(return_value, i++, s, p-s-windows_eol);
  686. s = ++p;
  687. } while ((p = memchr(p, eol_marker, (e-p))));
  688. }
  689. /* handle any left overs of files without new lines */
  690. if (s != e) {
  691. p = e;
  692. goto parse_eol;
  693. }
  694. }
  695. if (target_buf) {
  696. zend_string_free(target_buf);
  697. }
  698. php_stream_close(stream);
  699. }
  700. /* }}} */
  701. /* {{{ Create a unique filename in a directory */
  702. PHP_FUNCTION(tempnam)
  703. {
  704. char *dir, *prefix;
  705. size_t dir_len, prefix_len;
  706. zend_string *opened_path;
  707. int fd;
  708. zend_string *p;
  709. ZEND_PARSE_PARAMETERS_START(2, 2)
  710. Z_PARAM_PATH(dir, dir_len)
  711. Z_PARAM_PATH(prefix, prefix_len)
  712. ZEND_PARSE_PARAMETERS_END();
  713. p = php_basename(prefix, prefix_len, NULL, 0);
  714. if (ZSTR_LEN(p) > 64) {
  715. ZSTR_VAL(p)[63] = '\0';
  716. }
  717. RETVAL_FALSE;
  718. if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ALWAYS)) >= 0) {
  719. close(fd);
  720. RETVAL_STR(opened_path);
  721. }
  722. zend_string_release_ex(p, 0);
  723. }
  724. /* }}} */
  725. /* {{{ Create a temporary file that will be deleted automatically after use */
  726. PHP_FUNCTION(tmpfile)
  727. {
  728. php_stream *stream;
  729. ZEND_PARSE_PARAMETERS_NONE();
  730. stream = php_stream_fopen_tmpfile();
  731. if (stream) {
  732. php_stream_to_zval(stream, return_value);
  733. } else {
  734. RETURN_FALSE;
  735. }
  736. }
  737. /* }}} */
  738. /* {{{ Open a file or a URL and return a file pointer */
  739. PHP_FUNCTION(fopen)
  740. {
  741. char *filename, *mode;
  742. size_t filename_len, mode_len;
  743. bool use_include_path = 0;
  744. zval *zcontext = NULL;
  745. php_stream *stream;
  746. php_stream_context *context = NULL;
  747. ZEND_PARSE_PARAMETERS_START(2, 4)
  748. Z_PARAM_PATH(filename, filename_len)
  749. Z_PARAM_STRING(mode, mode_len)
  750. Z_PARAM_OPTIONAL
  751. Z_PARAM_BOOL(use_include_path)
  752. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  753. ZEND_PARSE_PARAMETERS_END();
  754. context = php_stream_context_from_zval(zcontext, 0);
  755. stream = php_stream_open_wrapper_ex(filename, mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  756. if (stream == NULL) {
  757. RETURN_FALSE;
  758. }
  759. php_stream_to_zval(stream, return_value);
  760. }
  761. /* }}} */
  762. /* {{{ Close an open file pointer */
  763. PHPAPI PHP_FUNCTION(fclose)
  764. {
  765. zval *res;
  766. php_stream *stream;
  767. ZEND_PARSE_PARAMETERS_START(1, 1)
  768. Z_PARAM_RESOURCE(res)
  769. ZEND_PARSE_PARAMETERS_END();
  770. PHP_STREAM_TO_ZVAL(stream, res);
  771. if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
  772. php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " is not a valid stream resource", stream->res->handle);
  773. RETURN_FALSE;
  774. }
  775. php_stream_free(stream,
  776. PHP_STREAM_FREE_KEEP_RSRC |
  777. (stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE));
  778. RETURN_TRUE;
  779. }
  780. /* }}} */
  781. /* {{{ Execute a command and open either a read or a write pipe to it */
  782. PHP_FUNCTION(popen)
  783. {
  784. char *command, *mode;
  785. size_t command_len, mode_len;
  786. FILE *fp;
  787. php_stream *stream;
  788. char *posix_mode;
  789. ZEND_PARSE_PARAMETERS_START(2, 2)
  790. Z_PARAM_PATH(command, command_len)
  791. Z_PARAM_STRING(mode, mode_len)
  792. ZEND_PARSE_PARAMETERS_END();
  793. posix_mode = estrndup(mode, mode_len);
  794. #ifndef PHP_WIN32
  795. {
  796. char *z = memchr(posix_mode, 'b', mode_len);
  797. if (z) {
  798. memmove(z, z + 1, mode_len - (z - posix_mode));
  799. mode_len--;
  800. }
  801. }
  802. #endif
  803. /* Musl only partially validates the mode. Manually check it to ensure consistent behavior. */
  804. if (mode_len > 2 ||
  805. (mode_len == 1 && (*posix_mode != 'r' && *posix_mode != 'w')) ||
  806. (mode_len == 2 && (memcmp(posix_mode, "rb", 2) && memcmp(posix_mode, "wb", 2)))
  807. ) {
  808. zend_argument_value_error(2, "must be one of \"r\", \"rb\", \"w\", or \"wb\"");
  809. efree(posix_mode);
  810. RETURN_THROWS();
  811. }
  812. fp = VCWD_POPEN(command, posix_mode);
  813. if (!fp) {
  814. php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
  815. efree(posix_mode);
  816. RETURN_FALSE;
  817. }
  818. stream = php_stream_fopen_from_pipe(fp, mode);
  819. if (stream == NULL) {
  820. php_error_docref2(NULL, command, mode, E_WARNING, "%s", strerror(errno));
  821. RETVAL_FALSE;
  822. } else {
  823. php_stream_to_zval(stream, return_value);
  824. }
  825. efree(posix_mode);
  826. }
  827. /* }}} */
  828. /* {{{ Close a file pointer opened by popen() */
  829. PHP_FUNCTION(pclose)
  830. {
  831. zval *res;
  832. php_stream *stream;
  833. ZEND_PARSE_PARAMETERS_START(1, 1)
  834. Z_PARAM_RESOURCE(res)
  835. ZEND_PARSE_PARAMETERS_END();
  836. PHP_STREAM_TO_ZVAL(stream, res);
  837. FG(pclose_wait) = 1;
  838. zend_list_close(stream->res);
  839. FG(pclose_wait) = 0;
  840. RETURN_LONG(FG(pclose_ret));
  841. }
  842. /* }}} */
  843. /* {{{ Test for end-of-file on a file pointer */
  844. PHPAPI PHP_FUNCTION(feof)
  845. {
  846. zval *res;
  847. php_stream *stream;
  848. ZEND_PARSE_PARAMETERS_START(1, 1)
  849. Z_PARAM_RESOURCE(res)
  850. ZEND_PARSE_PARAMETERS_END();
  851. PHP_STREAM_TO_ZVAL(stream, res);
  852. if (php_stream_eof(stream)) {
  853. RETURN_TRUE;
  854. } else {
  855. RETURN_FALSE;
  856. }
  857. }
  858. /* }}} */
  859. /* {{{ Get a line from file pointer */
  860. PHPAPI PHP_FUNCTION(fgets)
  861. {
  862. zval *res;
  863. zend_long len = 1024;
  864. bool len_is_null = 1;
  865. char *buf = NULL;
  866. size_t line_len = 0;
  867. zend_string *str;
  868. php_stream *stream;
  869. ZEND_PARSE_PARAMETERS_START(1, 2)
  870. Z_PARAM_RESOURCE(res)
  871. Z_PARAM_OPTIONAL
  872. Z_PARAM_LONG_OR_NULL(len, len_is_null)
  873. ZEND_PARSE_PARAMETERS_END();
  874. PHP_STREAM_TO_ZVAL(stream, res);
  875. if (len_is_null) {
  876. /* ask streams to give us a buffer of an appropriate size */
  877. buf = php_stream_get_line(stream, NULL, 0, &line_len);
  878. if (buf == NULL) {
  879. RETURN_FALSE;
  880. }
  881. // TODO: avoid reallocation ???
  882. RETVAL_STRINGL(buf, line_len);
  883. efree(buf);
  884. } else {
  885. if (len <= 0) {
  886. zend_argument_value_error(2, "must be greater than 0");
  887. RETURN_THROWS();
  888. }
  889. str = zend_string_alloc(len, 0);
  890. if (php_stream_get_line(stream, ZSTR_VAL(str), len, &line_len) == NULL) {
  891. zend_string_efree(str);
  892. RETURN_FALSE;
  893. }
  894. /* resize buffer if it's much larger than the result.
  895. * Only needed if the user requested a buffer size. */
  896. if (line_len < (size_t)len / 2) {
  897. str = zend_string_truncate(str, line_len, 0);
  898. } else {
  899. ZSTR_LEN(str) = line_len;
  900. }
  901. RETURN_NEW_STR(str);
  902. }
  903. }
  904. /* }}} */
  905. /* {{{ Get a character from file pointer */
  906. PHPAPI PHP_FUNCTION(fgetc)
  907. {
  908. zval *res;
  909. php_stream *stream;
  910. ZEND_PARSE_PARAMETERS_START(1, 1)
  911. Z_PARAM_RESOURCE(res)
  912. ZEND_PARSE_PARAMETERS_END();
  913. PHP_STREAM_TO_ZVAL(stream, res);
  914. int result = php_stream_getc(stream);
  915. if (result == EOF) {
  916. RETVAL_FALSE;
  917. } else {
  918. RETURN_CHAR(result);
  919. }
  920. }
  921. /* }}} */
  922. /* {{{ Implements a mostly ANSI compatible fscanf() */
  923. PHP_FUNCTION(fscanf)
  924. {
  925. int result, argc = 0;
  926. size_t format_len;
  927. zval *args = NULL;
  928. zval *file_handle;
  929. char *buf, *format;
  930. size_t len;
  931. void *what;
  932. ZEND_PARSE_PARAMETERS_START(2, -1)
  933. Z_PARAM_RESOURCE(file_handle)
  934. Z_PARAM_STRING(format, format_len)
  935. Z_PARAM_VARIADIC('*', args, argc)
  936. ZEND_PARSE_PARAMETERS_END();
  937. what = zend_fetch_resource2(Z_RES_P(file_handle), "File-Handle", php_file_le_stream(), php_file_le_pstream());
  938. /* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up
  939. * with a leak if we have an invalid filehandle. This needs changing
  940. * if the code behind ZEND_VERIFY_RESOURCE changed. - cc */
  941. if (!what) {
  942. RETURN_THROWS();
  943. }
  944. buf = php_stream_get_line((php_stream *) what, NULL, 0, &len);
  945. if (buf == NULL) {
  946. RETURN_FALSE;
  947. }
  948. result = php_sscanf_internal(buf, format, argc, args, 0, return_value);
  949. efree(buf);
  950. if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
  951. WRONG_PARAM_COUNT;
  952. }
  953. }
  954. /* }}} */
  955. /* {{{ Binary-safe file write */
  956. PHPAPI PHP_FUNCTION(fwrite)
  957. {
  958. zval *res;
  959. char *input;
  960. size_t inputlen;
  961. ssize_t ret;
  962. size_t num_bytes;
  963. zend_long maxlen = 0;
  964. bool maxlen_is_null = 1;
  965. php_stream *stream;
  966. ZEND_PARSE_PARAMETERS_START(2, 3)
  967. Z_PARAM_RESOURCE(res)
  968. Z_PARAM_STRING(input, inputlen)
  969. Z_PARAM_OPTIONAL
  970. Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
  971. ZEND_PARSE_PARAMETERS_END();
  972. if (maxlen_is_null) {
  973. num_bytes = inputlen;
  974. } else if (maxlen <= 0) {
  975. num_bytes = 0;
  976. } else {
  977. num_bytes = MIN((size_t) maxlen, inputlen);
  978. }
  979. if (!num_bytes) {
  980. RETURN_LONG(0);
  981. }
  982. PHP_STREAM_TO_ZVAL(stream, res);
  983. ret = php_stream_write(stream, input, num_bytes);
  984. if (ret < 0) {
  985. RETURN_FALSE;
  986. }
  987. RETURN_LONG(ret);
  988. }
  989. /* }}} */
  990. /* {{{ Flushes output */
  991. PHPAPI PHP_FUNCTION(fflush)
  992. {
  993. zval *res;
  994. int ret;
  995. php_stream *stream;
  996. ZEND_PARSE_PARAMETERS_START(1, 1)
  997. Z_PARAM_RESOURCE(res)
  998. ZEND_PARSE_PARAMETERS_END();
  999. PHP_STREAM_TO_ZVAL(stream, res);
  1000. ret = php_stream_flush(stream);
  1001. if (ret) {
  1002. RETURN_FALSE;
  1003. }
  1004. RETURN_TRUE;
  1005. }
  1006. /* }}} */
  1007. /* {{{ Rewind the position of a file pointer */
  1008. PHPAPI PHP_FUNCTION(rewind)
  1009. {
  1010. zval *res;
  1011. php_stream *stream;
  1012. ZEND_PARSE_PARAMETERS_START(1, 1)
  1013. Z_PARAM_RESOURCE(res)
  1014. ZEND_PARSE_PARAMETERS_END();
  1015. PHP_STREAM_TO_ZVAL(stream, res);
  1016. if (-1 == php_stream_rewind(stream)) {
  1017. RETURN_FALSE;
  1018. }
  1019. RETURN_TRUE;
  1020. }
  1021. /* }}} */
  1022. /* {{{ Get file pointer's read/write position */
  1023. PHPAPI PHP_FUNCTION(ftell)
  1024. {
  1025. zval *res;
  1026. zend_long ret;
  1027. php_stream *stream;
  1028. ZEND_PARSE_PARAMETERS_START(1, 1)
  1029. Z_PARAM_RESOURCE(res)
  1030. ZEND_PARSE_PARAMETERS_END();
  1031. PHP_STREAM_TO_ZVAL(stream, res);
  1032. ret = php_stream_tell(stream);
  1033. if (ret == -1) {
  1034. RETURN_FALSE;
  1035. }
  1036. RETURN_LONG(ret);
  1037. }
  1038. /* }}} */
  1039. /* {{{ Seek on a file pointer */
  1040. PHPAPI PHP_FUNCTION(fseek)
  1041. {
  1042. zval *res;
  1043. zend_long offset, whence = SEEK_SET;
  1044. php_stream *stream;
  1045. ZEND_PARSE_PARAMETERS_START(2, 3)
  1046. Z_PARAM_RESOURCE(res)
  1047. Z_PARAM_LONG(offset)
  1048. Z_PARAM_OPTIONAL
  1049. Z_PARAM_LONG(whence)
  1050. ZEND_PARSE_PARAMETERS_END();
  1051. PHP_STREAM_TO_ZVAL(stream, res);
  1052. RETURN_LONG(php_stream_seek(stream, offset, (int) whence));
  1053. }
  1054. /* }}} */
  1055. /* {{{ php_mkdir */
  1056. /* DEPRECATED APIs: Use php_stream_mkdir() instead */
  1057. PHPAPI int php_mkdir_ex(const char *dir, zend_long mode, int options)
  1058. {
  1059. int ret;
  1060. if (php_check_open_basedir(dir)) {
  1061. return -1;
  1062. }
  1063. if ((ret = VCWD_MKDIR(dir, (mode_t)mode)) < 0 && (options & REPORT_ERRORS)) {
  1064. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  1065. }
  1066. return ret;
  1067. }
  1068. PHPAPI int php_mkdir(const char *dir, zend_long mode)
  1069. {
  1070. return php_mkdir_ex(dir, mode, REPORT_ERRORS);
  1071. }
  1072. /* }}} */
  1073. /* {{{ Create a directory */
  1074. PHP_FUNCTION(mkdir)
  1075. {
  1076. char *dir;
  1077. size_t dir_len;
  1078. zval *zcontext = NULL;
  1079. zend_long mode = 0777;
  1080. bool recursive = 0;
  1081. php_stream_context *context;
  1082. ZEND_PARSE_PARAMETERS_START(1, 4)
  1083. Z_PARAM_PATH(dir, dir_len)
  1084. Z_PARAM_OPTIONAL
  1085. Z_PARAM_LONG(mode)
  1086. Z_PARAM_BOOL(recursive)
  1087. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1088. ZEND_PARSE_PARAMETERS_END();
  1089. context = php_stream_context_from_zval(zcontext, 0);
  1090. RETURN_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context));
  1091. }
  1092. /* }}} */
  1093. /* {{{ Remove a directory */
  1094. PHP_FUNCTION(rmdir)
  1095. {
  1096. char *dir;
  1097. size_t dir_len;
  1098. zval *zcontext = NULL;
  1099. php_stream_context *context;
  1100. ZEND_PARSE_PARAMETERS_START(1, 2)
  1101. Z_PARAM_PATH(dir, dir_len)
  1102. Z_PARAM_OPTIONAL
  1103. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1104. ZEND_PARSE_PARAMETERS_END();
  1105. context = php_stream_context_from_zval(zcontext, 0);
  1106. RETURN_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context));
  1107. }
  1108. /* }}} */
  1109. /* {{{ Output a file or a URL */
  1110. PHP_FUNCTION(readfile)
  1111. {
  1112. char *filename;
  1113. size_t filename_len;
  1114. size_t size = 0;
  1115. bool use_include_path = 0;
  1116. zval *zcontext = NULL;
  1117. php_stream *stream;
  1118. php_stream_context *context = NULL;
  1119. ZEND_PARSE_PARAMETERS_START(1, 3)
  1120. Z_PARAM_PATH(filename, filename_len)
  1121. Z_PARAM_OPTIONAL
  1122. Z_PARAM_BOOL(use_include_path)
  1123. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1124. ZEND_PARSE_PARAMETERS_END();
  1125. context = php_stream_context_from_zval(zcontext, 0);
  1126. stream = php_stream_open_wrapper_ex(filename, "rb", (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, context);
  1127. if (stream) {
  1128. size = php_stream_passthru(stream);
  1129. php_stream_close(stream);
  1130. RETURN_LONG(size);
  1131. }
  1132. RETURN_FALSE;
  1133. }
  1134. /* }}} */
  1135. /* {{{ Return or change the umask */
  1136. PHP_FUNCTION(umask)
  1137. {
  1138. zend_long mask = 0;
  1139. bool mask_is_null = 1;
  1140. int oldumask;
  1141. ZEND_PARSE_PARAMETERS_START(0, 1)
  1142. Z_PARAM_OPTIONAL
  1143. Z_PARAM_LONG_OR_NULL(mask, mask_is_null)
  1144. ZEND_PARSE_PARAMETERS_END();
  1145. oldumask = umask(077);
  1146. if (BG(umask) == -1) {
  1147. BG(umask) = oldumask;
  1148. }
  1149. if (mask_is_null) {
  1150. umask(oldumask);
  1151. } else {
  1152. umask((int) mask);
  1153. }
  1154. RETURN_LONG(oldumask);
  1155. }
  1156. /* }}} */
  1157. /* {{{ Output all remaining data from a file pointer */
  1158. PHPAPI PHP_FUNCTION(fpassthru)
  1159. {
  1160. zval *res;
  1161. size_t size;
  1162. php_stream *stream;
  1163. ZEND_PARSE_PARAMETERS_START(1, 1)
  1164. Z_PARAM_RESOURCE(res)
  1165. ZEND_PARSE_PARAMETERS_END();
  1166. PHP_STREAM_TO_ZVAL(stream, res);
  1167. size = php_stream_passthru(stream);
  1168. RETURN_LONG(size);
  1169. }
  1170. /* }}} */
  1171. /* {{{ Rename a file */
  1172. PHP_FUNCTION(rename)
  1173. {
  1174. char *old_name, *new_name;
  1175. size_t old_name_len, new_name_len;
  1176. zval *zcontext = NULL;
  1177. php_stream_wrapper *wrapper;
  1178. php_stream_context *context;
  1179. ZEND_PARSE_PARAMETERS_START(2, 3)
  1180. Z_PARAM_PATH(old_name, old_name_len)
  1181. Z_PARAM_PATH(new_name, new_name_len)
  1182. Z_PARAM_OPTIONAL
  1183. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1184. ZEND_PARSE_PARAMETERS_END();
  1185. wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0);
  1186. if (!wrapper || !wrapper->wops) {
  1187. php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");
  1188. RETURN_FALSE;
  1189. }
  1190. if (!wrapper->wops->rename) {
  1191. php_error_docref(NULL, E_WARNING, "%s wrapper does not support renaming", wrapper->wops->label ? wrapper->wops->label : "Source");
  1192. RETURN_FALSE;
  1193. }
  1194. if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0)) {
  1195. php_error_docref(NULL, E_WARNING, "Cannot rename a file across wrapper types");
  1196. RETURN_FALSE;
  1197. }
  1198. context = php_stream_context_from_zval(zcontext, 0);
  1199. RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context));
  1200. }
  1201. /* }}} */
  1202. /* {{{ Delete a file */
  1203. PHP_FUNCTION(unlink)
  1204. {
  1205. char *filename;
  1206. size_t filename_len;
  1207. php_stream_wrapper *wrapper;
  1208. zval *zcontext = NULL;
  1209. php_stream_context *context = NULL;
  1210. ZEND_PARSE_PARAMETERS_START(1, 2)
  1211. Z_PARAM_PATH(filename, filename_len)
  1212. Z_PARAM_OPTIONAL
  1213. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1214. ZEND_PARSE_PARAMETERS_END();
  1215. context = php_stream_context_from_zval(zcontext, 0);
  1216. wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
  1217. if (!wrapper || !wrapper->wops) {
  1218. php_error_docref(NULL, E_WARNING, "Unable to locate stream wrapper");
  1219. RETURN_FALSE;
  1220. }
  1221. if (!wrapper->wops->unlink) {
  1222. php_error_docref(NULL, E_WARNING, "%s does not allow unlinking", wrapper->wops->label ? wrapper->wops->label : "Wrapper");
  1223. RETURN_FALSE;
  1224. }
  1225. RETURN_BOOL(wrapper->wops->unlink(wrapper, filename, REPORT_ERRORS, context));
  1226. }
  1227. /* }}} */
  1228. PHP_FUNCTION(fsync)
  1229. {
  1230. zval *res;
  1231. php_stream *stream;
  1232. ZEND_PARSE_PARAMETERS_START(1, 1)
  1233. Z_PARAM_RESOURCE(res)
  1234. ZEND_PARSE_PARAMETERS_END();
  1235. PHP_STREAM_TO_ZVAL(stream, res);
  1236. if (!php_stream_sync_supported(stream)) {
  1237. php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
  1238. RETURN_FALSE;
  1239. }
  1240. RETURN_BOOL(php_stream_sync(stream, /* data_only */ 0) == 0);
  1241. }
  1242. PHP_FUNCTION(fdatasync)
  1243. {
  1244. zval *res;
  1245. php_stream *stream;
  1246. ZEND_PARSE_PARAMETERS_START(1, 1)
  1247. Z_PARAM_RESOURCE(res)
  1248. ZEND_PARSE_PARAMETERS_END();
  1249. PHP_STREAM_TO_ZVAL(stream, res);
  1250. if (!php_stream_sync_supported(stream)) {
  1251. php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
  1252. RETURN_FALSE;
  1253. }
  1254. RETURN_BOOL(php_stream_sync(stream, /* data_only */ 1) == 0);
  1255. }
  1256. /* {{{ Truncate file to 'size' length */
  1257. PHP_FUNCTION(ftruncate)
  1258. {
  1259. zval *fp;
  1260. zend_long size;
  1261. php_stream *stream;
  1262. ZEND_PARSE_PARAMETERS_START(2, 2)
  1263. Z_PARAM_RESOURCE(fp)
  1264. Z_PARAM_LONG(size)
  1265. ZEND_PARSE_PARAMETERS_END();
  1266. if (size < 0) {
  1267. zend_argument_value_error(2, "must be greater than or equal to 0");
  1268. RETURN_THROWS();
  1269. }
  1270. PHP_STREAM_TO_ZVAL(stream, fp);
  1271. if (!php_stream_truncate_supported(stream)) {
  1272. php_error_docref(NULL, E_WARNING, "Can't truncate this stream!");
  1273. RETURN_FALSE;
  1274. }
  1275. RETURN_BOOL(0 == php_stream_truncate_set_size(stream, size));
  1276. }
  1277. /* }}} */
  1278. PHPAPI void php_fstat(php_stream *stream, zval *return_value)
  1279. {
  1280. php_stream_statbuf stat_ssb;
  1281. zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
  1282. stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
  1283. char *stat_sb_names[13] = {
  1284. "dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
  1285. "size", "atime", "mtime", "ctime", "blksize", "blocks"
  1286. };
  1287. if (php_stream_stat(stream, &stat_ssb)) {
  1288. RETURN_FALSE;
  1289. }
  1290. array_init(return_value);
  1291. ZVAL_LONG(&stat_dev, stat_ssb.sb.st_dev);
  1292. ZVAL_LONG(&stat_ino, stat_ssb.sb.st_ino);
  1293. ZVAL_LONG(&stat_mode, stat_ssb.sb.st_mode);
  1294. ZVAL_LONG(&stat_nlink, stat_ssb.sb.st_nlink);
  1295. ZVAL_LONG(&stat_uid, stat_ssb.sb.st_uid);
  1296. ZVAL_LONG(&stat_gid, stat_ssb.sb.st_gid);
  1297. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  1298. ZVAL_LONG(&stat_rdev, stat_ssb.sb.st_rdev);
  1299. #else
  1300. ZVAL_LONG(&stat_rdev, -1);
  1301. #endif
  1302. ZVAL_LONG(&stat_size, stat_ssb.sb.st_size);
  1303. ZVAL_LONG(&stat_atime, stat_ssb.sb.st_atime);
  1304. ZVAL_LONG(&stat_mtime, stat_ssb.sb.st_mtime);
  1305. ZVAL_LONG(&stat_ctime, stat_ssb.sb.st_ctime);
  1306. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  1307. ZVAL_LONG(&stat_blksize, stat_ssb.sb.st_blksize);
  1308. #else
  1309. ZVAL_LONG(&stat_blksize,-1);
  1310. #endif
  1311. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  1312. ZVAL_LONG(&stat_blocks, stat_ssb.sb.st_blocks);
  1313. #else
  1314. ZVAL_LONG(&stat_blocks,-1);
  1315. #endif
  1316. /* Store numeric indexes in proper order */
  1317. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
  1318. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
  1319. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
  1320. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
  1321. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
  1322. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
  1323. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
  1324. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
  1325. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
  1326. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
  1327. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
  1328. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
  1329. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
  1330. /* Store string indexes referencing the same zval*/
  1331. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
  1332. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
  1333. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
  1334. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
  1335. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
  1336. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
  1337. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
  1338. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
  1339. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
  1340. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
  1341. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
  1342. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
  1343. zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
  1344. }
  1345. /* {{{ Stat() on a filehandle */
  1346. PHP_FUNCTION(fstat)
  1347. {
  1348. zval *fp;
  1349. php_stream *stream;
  1350. ZEND_PARSE_PARAMETERS_START(1, 1)
  1351. Z_PARAM_RESOURCE(fp)
  1352. ZEND_PARSE_PARAMETERS_END();
  1353. PHP_STREAM_TO_ZVAL(stream, fp);
  1354. php_fstat(stream, return_value);
  1355. }
  1356. /* }}} */
  1357. /* {{{ Copy a file */
  1358. PHP_FUNCTION(copy)
  1359. {
  1360. char *source, *target;
  1361. size_t source_len, target_len;
  1362. zval *zcontext = NULL;
  1363. php_stream_context *context;
  1364. ZEND_PARSE_PARAMETERS_START(2, 3)
  1365. Z_PARAM_PATH(source, source_len)
  1366. Z_PARAM_PATH(target, target_len)
  1367. Z_PARAM_OPTIONAL
  1368. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  1369. ZEND_PARSE_PARAMETERS_END();
  1370. if (php_stream_locate_url_wrapper(source, NULL, 0) == &php_plain_files_wrapper && php_check_open_basedir(source)) {
  1371. RETURN_FALSE;
  1372. }
  1373. context = php_stream_context_from_zval(zcontext, 0);
  1374. if (php_copy_file_ctx(source, target, 0, context) == SUCCESS) {
  1375. RETURN_TRUE;
  1376. } else {
  1377. RETURN_FALSE;
  1378. }
  1379. }
  1380. /* }}} */
  1381. /* {{{ php_copy_file */
  1382. PHPAPI int php_copy_file(const char *src, const char *dest)
  1383. {
  1384. return php_copy_file_ctx(src, dest, 0, NULL);
  1385. }
  1386. /* }}} */
  1387. /* {{{ php_copy_file_ex */
  1388. PHPAPI int php_copy_file_ex(const char *src, const char *dest, int src_flg)
  1389. {
  1390. return php_copy_file_ctx(src, dest, src_flg, NULL);
  1391. }
  1392. /* }}} */
  1393. /* {{{ php_copy_file_ctx */
  1394. PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php_stream_context *ctx)
  1395. {
  1396. php_stream *srcstream = NULL, *deststream = NULL;
  1397. int ret = FAILURE;
  1398. php_stream_statbuf src_s, dest_s;
  1399. switch (php_stream_stat_path_ex(src, 0, &src_s, ctx)) {
  1400. case -1:
  1401. /* non-statable stream */
  1402. goto safe_to_copy;
  1403. break;
  1404. case 0:
  1405. break;
  1406. default: /* failed to stat file, does not exist? */
  1407. return ret;
  1408. }
  1409. if (S_ISDIR(src_s.sb.st_mode)) {
  1410. php_error_docref(NULL, E_WARNING, "The first argument to copy() function cannot be a directory");
  1411. return FAILURE;
  1412. }
  1413. switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, ctx)) {
  1414. case -1:
  1415. /* non-statable stream */
  1416. goto safe_to_copy;
  1417. break;
  1418. case 0:
  1419. break;
  1420. default: /* failed to stat file, does not exist? */
  1421. return ret;
  1422. }
  1423. if (S_ISDIR(dest_s.sb.st_mode)) {
  1424. php_error_docref(NULL, E_WARNING, "The second argument to copy() function cannot be a directory");
  1425. return FAILURE;
  1426. }
  1427. if (!src_s.sb.st_ino || !dest_s.sb.st_ino) {
  1428. goto no_stat;
  1429. }
  1430. if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) {
  1431. return ret;
  1432. } else {
  1433. goto safe_to_copy;
  1434. }
  1435. no_stat:
  1436. {
  1437. char *sp, *dp;
  1438. int res;
  1439. if ((sp = expand_filepath(src, NULL)) == NULL) {
  1440. return ret;
  1441. }
  1442. if ((dp = expand_filepath(dest, NULL)) == NULL) {
  1443. efree(sp);
  1444. goto safe_to_copy;
  1445. }
  1446. res =
  1447. #ifndef PHP_WIN32
  1448. !strcmp(sp, dp);
  1449. #else
  1450. !strcasecmp(sp, dp);
  1451. #endif
  1452. efree(sp);
  1453. efree(dp);
  1454. if (res) {
  1455. return ret;
  1456. }
  1457. }
  1458. safe_to_copy:
  1459. srcstream = php_stream_open_wrapper_ex(src, "rb", src_flg | REPORT_ERRORS, NULL, ctx);
  1460. if (!srcstream) {
  1461. return ret;
  1462. }
  1463. deststream = php_stream_open_wrapper_ex(dest, "wb", REPORT_ERRORS, NULL, ctx);
  1464. if (srcstream && deststream) {
  1465. ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL);
  1466. }
  1467. if (srcstream) {
  1468. php_stream_close(srcstream);
  1469. }
  1470. if (deststream) {
  1471. php_stream_close(deststream);
  1472. }
  1473. return ret;
  1474. }
  1475. /* }}} */
  1476. /* {{{ Binary-safe file read */
  1477. PHPAPI PHP_FUNCTION(fread)
  1478. {
  1479. zval *res;
  1480. zend_long len;
  1481. php_stream *stream;
  1482. zend_string *str;
  1483. ZEND_PARSE_PARAMETERS_START(2, 2)
  1484. Z_PARAM_RESOURCE(res)
  1485. Z_PARAM_LONG(len)
  1486. ZEND_PARSE_PARAMETERS_END();
  1487. PHP_STREAM_TO_ZVAL(stream, res);
  1488. if (len <= 0) {
  1489. zend_argument_value_error(2, "must be greater than 0");
  1490. RETURN_THROWS();
  1491. }
  1492. str = php_stream_read_to_str(stream, len);
  1493. if (!str) {
  1494. zval_ptr_dtor_str(return_value);
  1495. RETURN_FALSE;
  1496. }
  1497. RETURN_STR(str);
  1498. }
  1499. /* }}} */
  1500. static const char *php_fgetcsv_lookup_trailing_spaces(const char *ptr, size_t len) /* {{{ */
  1501. {
  1502. int inc_len;
  1503. unsigned char last_chars[2] = { 0, 0 };
  1504. while (len > 0) {
  1505. inc_len = (*ptr == '\0' ? 1 : php_mblen(ptr, len));
  1506. switch (inc_len) {
  1507. case -2:
  1508. case -1:
  1509. inc_len = 1;
  1510. php_mb_reset();
  1511. break;
  1512. case 0:
  1513. goto quit_loop;
  1514. case 1:
  1515. default:
  1516. last_chars[0] = last_chars[1];
  1517. last_chars[1] = *ptr;
  1518. break;
  1519. }
  1520. ptr += inc_len;
  1521. len -= inc_len;
  1522. }
  1523. quit_loop:
  1524. switch (last_chars[1]) {
  1525. case '\n':
  1526. if (last_chars[0] == '\r') {
  1527. return ptr - 2;
  1528. }
  1529. ZEND_FALLTHROUGH;
  1530. case '\r':
  1531. return ptr - 1;
  1532. }
  1533. return ptr;
  1534. }
  1535. /* }}} */
  1536. #define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str))
  1537. /* {{{ Format line as CSV and write to file pointer */
  1538. PHP_FUNCTION(fputcsv)
  1539. {
  1540. char delimiter = ','; /* allow this to be set as parameter */
  1541. char enclosure = '"'; /* allow this to be set as parameter */
  1542. int escape_char = (unsigned char) '\\'; /* allow this to be set as parameter */
  1543. php_stream *stream;
  1544. zval *fp = NULL, *fields = NULL;
  1545. ssize_t ret;
  1546. char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL;
  1547. size_t delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0;
  1548. zend_string *eol_str = NULL;
  1549. ZEND_PARSE_PARAMETERS_START(2, 6)
  1550. Z_PARAM_RESOURCE(fp)
  1551. Z_PARAM_ARRAY(fields)
  1552. Z_PARAM_OPTIONAL
  1553. Z_PARAM_STRING(delimiter_str, delimiter_str_len)
  1554. Z_PARAM_STRING(enclosure_str, enclosure_str_len)
  1555. Z_PARAM_STRING(escape_str, escape_str_len)
  1556. Z_PARAM_STR_OR_NULL(eol_str)
  1557. ZEND_PARSE_PARAMETERS_END();
  1558. if (delimiter_str != NULL) {
  1559. /* Make sure that there is at least one character in string */
  1560. if (delimiter_str_len != 1) {
  1561. zend_argument_value_error(3, "must be a single character");
  1562. RETURN_THROWS();
  1563. }
  1564. /* use first character from string */
  1565. delimiter = *delimiter_str;
  1566. }
  1567. if (enclosure_str != NULL) {
  1568. if (enclosure_str_len != 1) {
  1569. zend_argument_value_error(4, "must be a single character");
  1570. RETURN_THROWS();
  1571. }
  1572. /* use first character from string */
  1573. enclosure = *enclosure_str;
  1574. }
  1575. if (escape_str != NULL) {
  1576. if (escape_str_len > 1) {
  1577. zend_argument_value_error(5, "must be empty or a single character");
  1578. RETURN_THROWS();
  1579. }
  1580. if (escape_str_len < 1) {
  1581. escape_char = PHP_CSV_NO_ESCAPE;
  1582. } else {
  1583. /* use first character from string */
  1584. escape_char = (unsigned char) *escape_str;
  1585. }
  1586. }
  1587. PHP_STREAM_TO_ZVAL(stream, fp);
  1588. ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char, eol_str);
  1589. if (ret < 0) {
  1590. RETURN_FALSE;
  1591. }
  1592. RETURN_LONG(ret);
  1593. }
  1594. /* }}} */
  1595. /* {{{ PHPAPI size_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str) */
  1596. PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str)
  1597. {
  1598. uint32_t count, i = 0;
  1599. size_t ret;
  1600. zval *field_tmp;
  1601. smart_str csvline = {0};
  1602. ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
  1603. count = zend_hash_num_elements(Z_ARRVAL_P(fields));
  1604. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(fields), field_tmp) {
  1605. zend_string *tmp_field_str;
  1606. zend_string *field_str = zval_get_tmp_string(field_tmp, &tmp_field_str);
  1607. /* enclose a field that contains a delimiter, an enclosure character, or a newline */
  1608. if (FPUTCSV_FLD_CHK(delimiter) ||
  1609. FPUTCSV_FLD_CHK(enclosure) ||
  1610. (escape_char != PHP_CSV_NO_ESCAPE && FPUTCSV_FLD_CHK(escape_char)) ||
  1611. FPUTCSV_FLD_CHK('\n') ||
  1612. FPUTCSV_FLD_CHK('\r') ||
  1613. FPUTCSV_FLD_CHK('\t') ||
  1614. FPUTCSV_FLD_CHK(' ')
  1615. ) {
  1616. char *ch = ZSTR_VAL(field_str);
  1617. char *end = ch + ZSTR_LEN(field_str);
  1618. int escaped = 0;
  1619. smart_str_appendc(&csvline, enclosure);
  1620. while (ch < end) {
  1621. if (escape_char != PHP_CSV_NO_ESCAPE && *ch == escape_char) {
  1622. escaped = 1;
  1623. } else if (!escaped && *ch == enclosure) {
  1624. smart_str_appendc(&csvline, enclosure);
  1625. } else {
  1626. escaped = 0;
  1627. }
  1628. smart_str_appendc(&csvline, *ch);
  1629. ch++;
  1630. }
  1631. smart_str_appendc(&csvline, enclosure);
  1632. } else {
  1633. smart_str_append(&csvline, field_str);
  1634. }
  1635. if (++i != count) {
  1636. smart_str_appendl(&csvline, &delimiter, 1);
  1637. }
  1638. zend_tmp_string_release(tmp_field_str);
  1639. } ZEND_HASH_FOREACH_END();
  1640. if (eol_str) {
  1641. smart_str_append(&csvline, eol_str);
  1642. } else {
  1643. smart_str_appendc(&csvline, '\n');
  1644. }
  1645. smart_str_0(&csvline);
  1646. ret = php_stream_write(stream, ZSTR_VAL(csvline.s), ZSTR_LEN(csvline.s));
  1647. smart_str_free(&csvline);
  1648. return ret;
  1649. }
  1650. /* }}} */
  1651. /* {{{ Get line from file pointer and parse for CSV fields */
  1652. PHP_FUNCTION(fgetcsv)
  1653. {
  1654. char delimiter = ','; /* allow this to be set as parameter */
  1655. char enclosure = '"'; /* allow this to be set as parameter */
  1656. int escape = (unsigned char) '\\';
  1657. zend_long len = 0;
  1658. size_t buf_len;
  1659. char *buf;
  1660. php_stream *stream;
  1661. {
  1662. zval *fd;
  1663. bool len_is_null = 1;
  1664. char *delimiter_str = NULL;
  1665. size_t delimiter_str_len = 0;
  1666. char *enclosure_str = NULL;
  1667. size_t enclosure_str_len = 0;
  1668. char *escape_str = NULL;
  1669. size_t escape_str_len = 0;
  1670. ZEND_PARSE_PARAMETERS_START(1, 5)
  1671. Z_PARAM_RESOURCE(fd)
  1672. Z_PARAM_OPTIONAL
  1673. Z_PARAM_LONG_OR_NULL(len, len_is_null)
  1674. Z_PARAM_STRING(delimiter_str, delimiter_str_len)
  1675. Z_PARAM_STRING(enclosure_str, enclosure_str_len)
  1676. Z_PARAM_STRING(escape_str, escape_str_len)
  1677. ZEND_PARSE_PARAMETERS_END();
  1678. if (delimiter_str != NULL) {
  1679. /* Make sure that there is at least one character in string */
  1680. if (delimiter_str_len != 1) {
  1681. zend_argument_value_error(3, "must be a single character");
  1682. RETURN_THROWS();
  1683. }
  1684. /* use first character from string */
  1685. delimiter = delimiter_str[0];
  1686. }
  1687. if (enclosure_str != NULL) {
  1688. if (enclosure_str_len != 1) {
  1689. zend_argument_value_error(4, "must be a single character");
  1690. RETURN_THROWS();
  1691. }
  1692. /* use first character from string */
  1693. enclosure = enclosure_str[0];
  1694. }
  1695. if (escape_str != NULL) {
  1696. if (escape_str_len > 1) {
  1697. zend_argument_value_error(5, "must be empty or a single character");
  1698. RETURN_THROWS();
  1699. }
  1700. if (escape_str_len < 1) {
  1701. escape = PHP_CSV_NO_ESCAPE;
  1702. } else {
  1703. escape = (unsigned char) escape_str[0];
  1704. }
  1705. }
  1706. if (len_is_null || len == 0) {
  1707. len = -1;
  1708. } else if (len < 0) {
  1709. zend_argument_value_error(2, "must be a greater than or equal to 0");
  1710. RETURN_THROWS();
  1711. }
  1712. PHP_STREAM_TO_ZVAL(stream, fd);
  1713. }
  1714. if (len < 0) {
  1715. if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
  1716. RETURN_FALSE;
  1717. }
  1718. } else {
  1719. buf = emalloc(len + 1);
  1720. if (php_stream_get_line(stream, buf, len + 1, &buf_len) == NULL) {
  1721. efree(buf);
  1722. RETURN_FALSE;
  1723. }
  1724. }
  1725. php_fgetcsv(stream, delimiter, enclosure, escape, buf_len, buf, return_value);
  1726. }
  1727. /* }}} */
  1728. PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */
  1729. {
  1730. char *temp, *bptr, *line_end, *limit;
  1731. size_t temp_len, line_end_len;
  1732. int inc_len;
  1733. bool first_field = true;
  1734. ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
  1735. /* initialize internal state */
  1736. php_mb_reset();
  1737. /* Now into new section that parses buf for delimiter/enclosure fields */
  1738. /* Strip trailing space from buf, saving end of line in case required for enclosure field */
  1739. bptr = buf;
  1740. line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len);
  1741. line_end_len = buf_len - (size_t)(limit - buf);
  1742. /* reserve workspace for building each individual field */
  1743. temp_len = buf_len;
  1744. temp = emalloc(temp_len + line_end_len + 1);
  1745. /* Initialize return array */
  1746. array_init(return_value);
  1747. /* Main loop to read CSV fields */
  1748. /* NB this routine will return a single null entry for a blank line */
  1749. do {
  1750. char *comp_end, *hunk_begin;
  1751. char *tptr = temp;
  1752. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1753. if (inc_len == 1) {
  1754. char *tmp = bptr;
  1755. while ((*tmp != delimiter) && isspace((int)*(unsigned char *)tmp)) {
  1756. tmp++;
  1757. }
  1758. if (*tmp == enclosure) {
  1759. bptr = tmp;
  1760. }
  1761. }
  1762. if (first_field && bptr == line_end) {
  1763. add_next_index_null(return_value);
  1764. break;
  1765. }
  1766. first_field = false;
  1767. /* 2. Read field, leaving bptr pointing at start of next field */
  1768. if (inc_len != 0 && *bptr == enclosure) {
  1769. int state = 0;
  1770. bptr++; /* move on to first character in field */
  1771. hunk_begin = bptr;
  1772. /* 2A. handle enclosure delimited field */
  1773. for (;;) {
  1774. switch (inc_len) {
  1775. case 0:
  1776. switch (state) {
  1777. case 2:
  1778. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1779. tptr += (bptr - hunk_begin - 1);
  1780. hunk_begin = bptr;
  1781. goto quit_loop_2;
  1782. case 1:
  1783. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1784. tptr += (bptr - hunk_begin);
  1785. hunk_begin = bptr;
  1786. ZEND_FALLTHROUGH;
  1787. case 0: {
  1788. if (hunk_begin != line_end) {
  1789. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1790. tptr += (bptr - hunk_begin);
  1791. hunk_begin = bptr;
  1792. }
  1793. /* add the embedded line end to the field */
  1794. memcpy(tptr, line_end, line_end_len);
  1795. tptr += line_end_len;
  1796. if (stream == NULL) {
  1797. goto quit_loop_2;
  1798. }
  1799. size_t new_len;
  1800. char *new_buf = php_stream_get_line(stream, NULL, 0, &new_len);
  1801. if (!new_buf) {
  1802. /* we've got an unterminated enclosure,
  1803. * assign all the data from the start of
  1804. * the enclosure to end of data to the
  1805. * last element */
  1806. goto quit_loop_2;
  1807. }
  1808. temp_len += new_len;
  1809. char *new_temp = erealloc(temp, temp_len);
  1810. tptr = new_temp + (size_t)(tptr - temp);
  1811. temp = new_temp;
  1812. efree(buf);
  1813. buf_len = new_len;
  1814. bptr = buf = new_buf;
  1815. hunk_begin = buf;
  1816. line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len);
  1817. line_end_len = buf_len - (size_t)(limit - buf);
  1818. state = 0;
  1819. } break;
  1820. }
  1821. break;
  1822. case -2:
  1823. case -1:
  1824. php_mb_reset();
  1825. ZEND_FALLTHROUGH;
  1826. case 1:
  1827. /* we need to determine if the enclosure is
  1828. * 'real' or is it escaped */
  1829. switch (state) {
  1830. case 1: /* escaped */
  1831. bptr++;
  1832. state = 0;
  1833. break;
  1834. case 2: /* embedded enclosure ? let's check it */
  1835. if (*bptr != enclosure) {
  1836. /* real enclosure */
  1837. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1838. tptr += (bptr - hunk_begin - 1);
  1839. hunk_begin = bptr;
  1840. goto quit_loop_2;
  1841. }
  1842. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1843. tptr += (bptr - hunk_begin);
  1844. bptr++;
  1845. hunk_begin = bptr;
  1846. state = 0;
  1847. break;
  1848. default:
  1849. if (*bptr == enclosure) {
  1850. state = 2;
  1851. } else if (escape_char != PHP_CSV_NO_ESCAPE && *bptr == escape_char) {
  1852. state = 1;
  1853. }
  1854. bptr++;
  1855. break;
  1856. }
  1857. break;
  1858. default:
  1859. switch (state) {
  1860. case 2:
  1861. /* real enclosure */
  1862. memcpy(tptr, hunk_begin, bptr - hunk_begin - 1);
  1863. tptr += (bptr - hunk_begin - 1);
  1864. hunk_begin = bptr;
  1865. goto quit_loop_2;
  1866. case 1:
  1867. bptr += inc_len;
  1868. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1869. tptr += (bptr - hunk_begin);
  1870. hunk_begin = bptr;
  1871. state = 0;
  1872. break;
  1873. default:
  1874. bptr += inc_len;
  1875. break;
  1876. }
  1877. break;
  1878. }
  1879. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1880. }
  1881. quit_loop_2:
  1882. /* look up for a delimiter */
  1883. for (;;) {
  1884. switch (inc_len) {
  1885. case 0:
  1886. goto quit_loop_3;
  1887. case -2:
  1888. case -1:
  1889. inc_len = 1;
  1890. php_mb_reset();
  1891. ZEND_FALLTHROUGH;
  1892. case 1:
  1893. if (*bptr == delimiter) {
  1894. goto quit_loop_3;
  1895. }
  1896. break;
  1897. default:
  1898. break;
  1899. }
  1900. bptr += inc_len;
  1901. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1902. }
  1903. quit_loop_3:
  1904. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1905. tptr += (bptr - hunk_begin);
  1906. bptr += inc_len;
  1907. comp_end = tptr;
  1908. } else {
  1909. /* 2B. Handle non-enclosure field */
  1910. hunk_begin = bptr;
  1911. for (;;) {
  1912. switch (inc_len) {
  1913. case 0:
  1914. goto quit_loop_4;
  1915. case -2:
  1916. case -1:
  1917. inc_len = 1;
  1918. php_mb_reset();
  1919. ZEND_FALLTHROUGH;
  1920. case 1:
  1921. if (*bptr == delimiter) {
  1922. goto quit_loop_4;
  1923. }
  1924. break;
  1925. default:
  1926. break;
  1927. }
  1928. bptr += inc_len;
  1929. inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
  1930. }
  1931. quit_loop_4:
  1932. memcpy(tptr, hunk_begin, bptr - hunk_begin);
  1933. tptr += (bptr - hunk_begin);
  1934. comp_end = (char *)php_fgetcsv_lookup_trailing_spaces(temp, tptr - temp);
  1935. if (*bptr == delimiter) {
  1936. bptr++;
  1937. }
  1938. }
  1939. /* 3. Now pass our field back to php */
  1940. *comp_end = '\0';
  1941. add_next_index_stringl(return_value, temp, comp_end - temp);
  1942. } while (inc_len > 0);
  1943. efree(temp);
  1944. if (stream) {
  1945. efree(buf);
  1946. }
  1947. }
  1948. /* }}} */
  1949. /* {{{ Return the resolved path */
  1950. PHP_FUNCTION(realpath)
  1951. {
  1952. char *filename;
  1953. size_t filename_len;
  1954. char resolved_path_buff[MAXPATHLEN];
  1955. ZEND_PARSE_PARAMETERS_START(1, 1)
  1956. Z_PARAM_PATH(filename, filename_len)
  1957. ZEND_PARSE_PARAMETERS_END();
  1958. if (VCWD_REALPATH(filename, resolved_path_buff)) {
  1959. if (php_check_open_basedir(resolved_path_buff)) {
  1960. RETURN_FALSE;
  1961. }
  1962. #ifdef ZTS
  1963. if (VCWD_ACCESS(resolved_path_buff, F_OK)) {
  1964. RETURN_FALSE;
  1965. }
  1966. #endif
  1967. RETURN_STRING(resolved_path_buff);
  1968. } else {
  1969. RETURN_FALSE;
  1970. }
  1971. }
  1972. /* }}} */
  1973. /* See http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 */
  1974. #define PHP_META_HTML401_CHARS "-_.:"
  1975. /* {{{ php_next_meta_token
  1976. Tokenizes an HTML file for get_meta_tags */
  1977. php_meta_tags_token php_next_meta_token(php_meta_tags_data *md)
  1978. {
  1979. int ch = 0, compliment;
  1980. char buff[META_DEF_BUFSIZE + 1];
  1981. memset((void *)buff, 0, META_DEF_BUFSIZE + 1);
  1982. while (md->ulc || (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)))) {
  1983. if (php_stream_eof(md->stream)) {
  1984. break;
  1985. }
  1986. if (md->ulc) {
  1987. ch = md->lc;
  1988. md->ulc = 0;
  1989. }
  1990. switch (ch) {
  1991. case '<':
  1992. return TOK_OPENTAG;
  1993. break;
  1994. case '>':
  1995. return TOK_CLOSETAG;
  1996. break;
  1997. case '=':
  1998. return TOK_EQUAL;
  1999. break;
  2000. case '/':
  2001. return TOK_SLASH;
  2002. break;
  2003. case '\'':
  2004. case '"':
  2005. compliment = ch;
  2006. md->token_len = 0;
  2007. while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && ch != compliment && ch != '<' && ch != '>') {
  2008. buff[(md->token_len)++] = ch;
  2009. if (md->token_len == META_DEF_BUFSIZE) {
  2010. break;
  2011. }
  2012. }
  2013. if (ch == '<' || ch == '>') {
  2014. /* Was just an apostrophe */
  2015. md->ulc = 1;
  2016. md->lc = ch;
  2017. }
  2018. /* We don't need to alloc unless we are in a meta tag */
  2019. if (md->in_meta) {
  2020. md->token_data = (char *) emalloc(md->token_len + 1);
  2021. memcpy(md->token_data, buff, md->token_len+1);
  2022. }
  2023. return TOK_STRING;
  2024. break;
  2025. case '\n':
  2026. case '\r':
  2027. case '\t':
  2028. break;
  2029. case ' ':
  2030. return TOK_SPACE;
  2031. break;
  2032. default:
  2033. if (isalnum(ch)) {
  2034. md->token_len = 0;
  2035. buff[(md->token_len)++] = ch;
  2036. while (!php_stream_eof(md->stream) && (ch = php_stream_getc(md->stream)) && (isalnum(ch) || strchr(PHP_META_HTML401_CHARS, ch))) {
  2037. buff[(md->token_len)++] = ch;
  2038. if (md->token_len == META_DEF_BUFSIZE) {
  2039. break;
  2040. }
  2041. }
  2042. /* This is ugly, but we have to replace ungetc */
  2043. if (!isalpha(ch) && ch != '-') {
  2044. md->ulc = 1;
  2045. md->lc = ch;
  2046. }
  2047. md->token_data = (char *) emalloc(md->token_len + 1);
  2048. memcpy(md->token_data, buff, md->token_len+1);
  2049. return TOK_ID;
  2050. } else {
  2051. return TOK_OTHER;
  2052. }
  2053. break;
  2054. }
  2055. }
  2056. return TOK_EOF;
  2057. }
  2058. /* }}} */
  2059. #ifdef HAVE_FNMATCH
  2060. /* {{{ Match filename against pattern */
  2061. PHP_FUNCTION(fnmatch)
  2062. {
  2063. char *pattern, *filename;
  2064. size_t pattern_len, filename_len;
  2065. zend_long flags = 0;
  2066. ZEND_PARSE_PARAMETERS_START(2, 3)
  2067. Z_PARAM_PATH(pattern, pattern_len)
  2068. Z_PARAM_PATH(filename, filename_len)
  2069. Z_PARAM_OPTIONAL
  2070. Z_PARAM_LONG(flags)
  2071. ZEND_PARSE_PARAMETERS_END();
  2072. if (filename_len >= MAXPATHLEN) {
  2073. php_error_docref(NULL, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  2074. RETURN_FALSE;
  2075. }
  2076. if (pattern_len >= MAXPATHLEN) {
  2077. php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  2078. RETURN_FALSE;
  2079. }
  2080. RETURN_BOOL( ! fnmatch( pattern, filename, (int)flags ));
  2081. }
  2082. /* }}} */
  2083. #endif
  2084. /* {{{ Returns directory path used for temporary files */
  2085. PHP_FUNCTION(sys_get_temp_dir)
  2086. {
  2087. ZEND_PARSE_PARAMETERS_NONE();
  2088. RETURN_STRING((char *)php_get_temporary_directory());
  2089. }
  2090. /* }}} */