PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/file.c

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