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.c55
1 files changed, 48 insertions, 7 deletions
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c
index caddf1b390..7b0f88762d 100644
--- a/main/php_open_temporary_file.c
+++ b/main/php_open_temporary_file.c
@@ -98,7 +98,13 @@
static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p)
{
char *trailing_slash;
+#ifdef PHP_WIN32
+ char *opened_path = NULL;
+ size_t opened_path_len;
+ wchar_t *cwdw, *pfxw, pathw[MAXPATHLEN];
+#else
char opened_path[MAXPATHLEN];
+#endif
char cwd[MAXPATHLEN];
cwd_state new_state;
int fd = -1;
@@ -139,23 +145,47 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st
trailing_slash = "/";
}
+#ifndef PHP_WIN32
if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
efree(new_state.cwd);
return -1;
}
+#endif
#ifdef PHP_WIN32
+ cwdw = php_win32_ioutil_any_to_w(new_state.cwd);
+ pfxw = php_win32_ioutil_any_to_w(pfx);
+ if (!cwdw || !pfxw) {
+ free(cwdw);
+ free(pfxw);
+ efree(new_state.cwd);
+ return -1;
+ }
+
+ if (GetTempFileNameW(cwdw, pfxw, 0, pathw)) {
+ opened_path = php_win32_ioutil_conv_w_to_any(pathw, PHP_WIN32_CP_IGNORE_LEN, &opened_path_len);
+ if (!opened_path || opened_path_len >= MAXPATHLEN) {
+ free(cwdw);
+ free(pfxw);
+ efree(new_state.cwd);
+ return -1;
+ }
+ assert(strlen(opened_path) == opened_path_len);
- if (GetTempFileName(new_state.cwd, pfx, 0, opened_path)) {
/* 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)) {
+ free(cwdw);
+ free(pfxw);
efree(new_state.cwd);
+ free(opened_path);
return -1;
}
fd = VCWD_OPEN_MODE(opened_path, open_flags, 0600);
}
+ free(cwdw);
+ free(pfxw);
#elif defined(HAVE_MKSTEMP)
fd = mkstemp(opened_path);
#else
@@ -164,9 +194,16 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st
}
#endif
+#ifdef PHP_WIN32
+ if (fd != -1 && opened_path_p) {
+ *opened_path_p = zend_string_init(opened_path, opened_path_len, 0);
+ }
+ free(opened_path);
+#else
if (fd != -1 && opened_path_p) {
*opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0);
}
+#endif
efree(new_state.cwd);
return fd;
}
@@ -204,14 +241,18 @@ PHPAPI const char* php_get_temporary_directory(void)
* the environment values TMP and TEMP (in order) first.
*/
{
- char sTemp[MAX_PATH];
- DWORD len = GetTempPath(sizeof(sTemp),sTemp);
+ wchar_t sTemp[MAXPATHLEN];
+ char *tmp;
+ size_t len = GetTempPathW(MAXPATHLEN, sTemp);
assert(0 < len); /* should *never* fail! */
- if (sTemp[len - 1] == DEFAULT_SLASH) {
- PG(php_sys_temp_dir) = estrndup(sTemp, len - 1);
- } else {
- PG(php_sys_temp_dir) = estrndup(sTemp, len);
+
+ if (NULL == (tmp = php_win32_ioutil_conv_w_to_any(sTemp, len, &len))) {
+ return NULL;
}
+
+ PG(php_sys_temp_dir) = estrndup(tmp, len - 1);
+
+ free(tmp);
return PG(php_sys_temp_dir);
}
#else