summaryrefslogtreecommitdiff
path: root/main/php_open_temporary_file.c
diff options
context:
space:
mode:
authorWez Furlong <wez@php.net>2003-03-18 16:40:29 +0000
committerWez Furlong <wez@php.net>2003-03-18 16:40:29 +0000
commitce01fd9526d0fca767541e176d04c3a7079235c2 (patch)
tree0f1bac9b59f4f6b168e1af31f153bcc5f0d6307f /main/php_open_temporary_file.c
parent40326f6adf7110bd6676bf7445cff7483cf173fb (diff)
downloadphp-git-ce01fd9526d0fca767541e176d04c3a7079235c2.tar.gz
Avoid using FILE* where possible.
Tidy up handling of potential error situations for the php:// wrapper.
Diffstat (limited to 'main/php_open_temporary_file.c')
-rw-r--r--main/php_open_temporary_file.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c
index acf804b2e4..ac1f8c9dd8 100644
--- a/main/php_open_temporary_file.c
+++ b/main/php_open_temporary_file.c
@@ -100,24 +100,26 @@
* SUCH DAMAGE.
*/
-static FILE *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, char **opened_path_p TSRMLS_DC)
{
char *trailing_slash;
- FILE *fp;
char *opened_path;
-#ifndef PHP_WIN32
- int fd;
+ int fd = -1;
+ int open_flags = O_CREAT | O_TRUNC | O_RDWR
+#ifdef PHP_WIN32
+ | _O_BINARY
#endif
+ ;
#ifdef NETWARE
char *file_path = NULL;
#endif
if (!path) {
- return NULL;
+ return -1;
}
if (!(opened_path = emalloc(MAXPATHLEN))) {
- return NULL;
+ return -1;
}
if (IS_SLASH(path[strlen(path)-1])) {
@@ -130,38 +132,27 @@ static FILE *php_do_open_temporary_file(const char *path, const char *pfx, char
#ifdef PHP_WIN32
if (GetTempFileName(path, pfx, 0, opened_path)) {
- fp = VCWD_FOPEN(opened_path, "r+b");
- } else {
- fp = NULL;
+ fd = VCWD_OPEN(opened_path, open_flags);
}
#elif defined(NETWARE)
/* Using standard mktemp() implementation for NetWare */
file_path = mktemp(opened_path);
if (file_path) {
- fp = VCWD_FOPEN(file_path, "r+b");
- } else {
- fp = NULL;
+ fd = VCWD_OPEN(file_path, open_flags);
}
#elif defined(HAVE_MKSTEMP)
fd = mkstemp(opened_path);
- if (fd==-1) {
- fp = NULL;
- } else {
- fp = fdopen(fd, "r+b");
- }
#else
if (mktemp(opened_path)) {
- fp = VCWD_FOPEN(opened_path, "r+b");
- } else {
- fp = NULL;
+ fd = VCWD_OPEN(opened_path, open_flags);
}
#endif
- if (!fp || !opened_path_p) {
+ if (fd == -1 || !opened_path_p) {
efree(opened_path);
} else {
*opened_path_p = opened_path;
}
- return fp;
+ return fd;
}
/* }}} */
@@ -220,9 +211,9 @@ const char* get_temporary_directory()
* This function should do its best to return a file pointer to a newly created
* unique file, on every platform.
*/
-PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
+PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
{
- FILE* fp = 0;
+ int fd;
if (!pfx) {
pfx = "tmp.";
@@ -232,18 +223,29 @@ PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **op
}
/* Try the directory given as parameter. */
- fp = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC);
- if (fp) {
- return fp;
+ fd = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC);
+ if (fd == -1) {
+ /* Use default temporary directory. */
+ fd = php_do_open_temporary_file(get_temporary_directory(), pfx, opened_path_p TSRMLS_CC);
}
+ return fd;
+}
- /* Use default temporary directory. */
- fp = php_do_open_temporary_file(get_temporary_directory(), pfx, opened_path_p TSRMLS_CC);
- if (fp) {
- return fp;
- }
+PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
+{
+ FILE *fp;
+ int fd = php_open_temporary_fd(dir, pfx, opened_path_p TSRMLS_CC);
- return 0;
+ if (fd == -1) {
+ return NULL;
+ }
+
+ fp = fdopen(fd, "r+b");
+ if (fp == NULL) {
+ close(fd);
+ }
+
+ return fp;
}
/* }}} */