summaryrefslogtreecommitdiff
path: root/main/php_open_temporary_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/php_open_temporary_file.c')
-rw-r--r--main/php_open_temporary_file.c93
1 files changed, 35 insertions, 58 deletions
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c
index cbfecca0be..4b8a5636b9 100644
--- a/main/php_open_temporary_file.c
+++ b/main/php_open_temporary_file.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
@@ -94,10 +94,10 @@
* SUCH DAMAGE.
*/
-static int php_do_open_temporary_file(const char *path, const char *pfx, char **opened_path_p TSRMLS_DC)
+static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p)
{
char *trailing_slash;
- char *opened_path;
+ char opened_path[MAXPATHLEN];
char cwd[MAXPATHLEN];
cwd_state new_state;
int fd = -1;
@@ -125,9 +125,9 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
}
new_state.cwd = estrdup(cwd);
- new_state.cwd_length = strlen(cwd);
+ new_state.cwd_length = (int)strlen(cwd);
- if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
+ if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
efree(new_state.cwd);
return -1;
}
@@ -138,8 +138,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
trailing_slash = "/";
}
- if (spprintf(&opened_path, 0, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
- efree(opened_path);
+ if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
efree(new_state.cwd);
return -1;
}
@@ -150,7 +149,6 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
/* Some versions of windows set the temp file to be read-only,
* which means that opening it will fail... */
if (VCWD_CHMOD(opened_path, 0600)) {
- efree(opened_path);
efree(new_state.cwd);
return -1;
}
@@ -165,56 +163,35 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
}
#endif
- if (fd == -1 || !opened_path_p) {
- efree(opened_path);
- } else {
- *opened_path_p = opened_path;
+ if (fd != -1 && opened_path_p) {
+ *opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0);
}
efree(new_state.cwd);
return fd;
}
/* }}} */
-/* Cache the chosen temporary directory. */
-static
-#ifdef ZTS
-#ifdef PHP_WIN32
-__declspec(thread)
-#elif defined(__GNUC__)
-__thread
-#endif
-#endif
-char* temporary_directory;
-
-PHPAPI void php_shutdown_temporary_directory(void)
-{
- if (temporary_directory) {
- efree(temporary_directory);
- temporary_directory = NULL;
- }
-}
-
/*
* Determine where to place temporary files.
*/
-PHPAPI const char* php_get_temporary_directory(TSRMLS_D)
+PHPAPI const char* php_get_temporary_directory(void)
{
/* Did we determine the temporary directory already? */
- if (temporary_directory) {
- return temporary_directory;
+ if (PG(php_sys_temp_dir)) {
+ return PG(php_sys_temp_dir);
}
/* Is there a temporary directory "sys_temp_dir" in .ini defined? */
{
char *sys_temp_dir = PG(sys_temp_dir);
if (sys_temp_dir) {
- int len = strlen(sys_temp_dir);
+ int len = (int)strlen(sys_temp_dir);
if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
- temporary_directory = estrndup(sys_temp_dir, len - 1);
- return temporary_directory;
+ PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len - 1);
+ return PG(php_sys_temp_dir);
} else if (len >= 1 && sys_temp_dir[len - 1] != DEFAULT_SLASH) {
- temporary_directory = estrndup(sys_temp_dir, len);
- return temporary_directory;
+ PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len);
+ return PG(php_sys_temp_dir);
}
}
}
@@ -230,11 +207,11 @@ PHPAPI const char* php_get_temporary_directory(TSRMLS_D)
DWORD len = GetTempPath(sizeof(sTemp),sTemp);
assert(0 < len); /* should *never* fail! */
if (sTemp[len - 1] == DEFAULT_SLASH) {
- temporary_directory = estrndup(sTemp, len - 1);
+ PG(php_sys_temp_dir) = estrndup(sTemp, len - 1);
} else {
- temporary_directory = estrndup(sTemp, len);
+ PG(php_sys_temp_dir) = estrndup(sTemp, len);
}
- return temporary_directory;
+ return PG(php_sys_temp_dir);
}
#else
/* On Unix use the (usual) TMPDIR environment variable. */
@@ -244,24 +221,24 @@ PHPAPI const char* php_get_temporary_directory(TSRMLS_D)
int len = strlen(s);
if (s[len - 1] == DEFAULT_SLASH) {
- temporary_directory = estrndup(s, len - 1);
+ PG(php_sys_temp_dir) = estrndup(s, len - 1);
} else {
- temporary_directory = estrndup(s, len);
+ PG(php_sys_temp_dir) = estrndup(s, len);
}
- return temporary_directory;
+ return PG(php_sys_temp_dir);
}
}
#ifdef P_tmpdir
/* Use the standard default temporary directory. */
if (P_tmpdir) {
- temporary_directory = estrdup(P_tmpdir);
- return temporary_directory;
+ PG(php_sys_temp_dir) = estrdup(P_tmpdir);
+ return PG(php_sys_temp_dir);
}
#endif
/* Shouldn't ever(!) end up here ... last ditch default. */
- temporary_directory = estrndup("/tmp", sizeof("/tmp"));
- return temporary_directory;
+ PG(php_sys_temp_dir) = estrdup("/tmp");
+ return PG(php_sys_temp_dir);
#endif
}
@@ -272,7 +249,7 @@ PHPAPI const char* php_get_temporary_directory(TSRMLS_D)
* This function should do its best to return a file pointer to a newly created
* unique file, on every platform.
*/
-PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check TSRMLS_DC)
+PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, zend_bool open_basedir_check)
{
int fd;
const char *temp_dir;
@@ -286,17 +263,17 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **ope
if (!dir || *dir == '\0') {
def_tmp:
- temp_dir = php_get_temporary_directory(TSRMLS_C);
+ temp_dir = php_get_temporary_directory();
- if (temp_dir && *temp_dir != '\0' && (!open_basedir_check || !php_check_open_basedir(temp_dir TSRMLS_CC))) {
- return php_do_open_temporary_file(temp_dir, pfx, opened_path_p TSRMLS_CC);
+ if (temp_dir && *temp_dir != '\0' && (!open_basedir_check || !php_check_open_basedir(temp_dir))) {
+ return php_do_open_temporary_file(temp_dir, pfx, opened_path_p);
} else {
return -1;
}
}
/* Try the directory given as parameter. */
- fd = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC);
+ fd = php_do_open_temporary_file(dir, pfx, opened_path_p);
if (fd == -1) {
/* Use default temporary directory. */
goto def_tmp;
@@ -304,15 +281,15 @@ def_tmp:
return fd;
}
-PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
+PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p)
{
- return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0 TSRMLS_CC);
+ return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0);
}
-PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
+PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p)
{
FILE *fp;
- int fd = php_open_temporary_fd(dir, pfx, opened_path_p TSRMLS_CC);
+ int fd = php_open_temporary_fd(dir, pfx, opened_path_p);
if (fd == -1) {
return NULL;