summaryrefslogtreecommitdiff
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
parent40326f6adf7110bd6676bf7445cff7483cf173fb (diff)
downloadphp-git-ce01fd9526d0fca767541e176d04c3a7079235c2.tar.gz
Avoid using FILE* where possible.
Tidy up handling of potential error situations for the php:// wrapper.
-rw-r--r--ext/standard/php_fopen_wrapper.c28
-rw-r--r--main/php_open_temporary_file.c68
-rw-r--r--main/php_open_temporary_file.h1
-rw-r--r--main/streams/plain_wrapper.c19
4 files changed, 62 insertions, 54 deletions
diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c
index 69e223a0b7..87ee5671df 100644
--- a/ext/standard/php_fopen_wrapper.c
+++ b/ext/standard/php_fopen_wrapper.c
@@ -171,15 +171,7 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, char *path, ch
fd = dup(STDOUT_FILENO);
} else if (!strcasecmp(path, "stderr")) {
fd = dup(STDERR_FILENO);
- }
-
- if (fd) {
- stream = php_stream_fopen_from_fd(fd, mode);
- if (stream == NULL)
- close(fd);
- }
-
- if (!strncasecmp(path, "filter/", 7)) {
+ } else if (!strncasecmp(path, "filter/", 7)) {
/* Save time/memory when chain isn't specified */
if (strchr(mode, 'r') || strchr(mode, '+')) {
mode_rw |= PHP_STREAM_FILTER_READ;
@@ -213,7 +205,23 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, char *path, ch
p = php_strtok_r(NULL, "/", &token);
}
efree(pathdup);
- }
+
+ return stream;
+ } else {
+ /* invalid php://thingy */
+ return NULL;
+ }
+
+ /* must be stdin, stderr or stdout */
+ if (fd == -1) {
+ /* failed to dup */
+ return NULL;
+ }
+
+ stream = php_stream_fopen_from_fd(fd, mode);
+ if (stream == NULL) {
+ close(fd);
+ }
return stream;
}
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;
}
/* }}} */
diff --git a/main/php_open_temporary_file.h b/main/php_open_temporary_file.h
index 0342f92ca7..ef9fb9d676 100644
--- a/main/php_open_temporary_file.h
+++ b/main/php_open_temporary_file.h
@@ -22,5 +22,6 @@
#define PHP_OPEN_TEMPORARY_FILE_H
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);
#endif /* PHP_OPEN_TEMPORARY_FILE_H */
diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
index 262c249c07..8aa56edb03 100644
--- a/main/streams/plain_wrapper.c
+++ b/main/streams/plain_wrapper.c
@@ -157,14 +157,14 @@ typedef struct {
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
{
- FILE *fp = php_open_temporary_file(dir, pfx, opened_path TSRMLS_CC);
+ int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);
- if (fp) {
- php_stream *stream = php_stream_fopen_from_file_rel(fp, "r+b");
+ if (fd != -1) {
+ php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b");
if (stream) {
return stream;
}
- fclose(fp);
+ close(fd);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
@@ -176,10 +176,10 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
{
char *opened_path = NULL;
- FILE *fp = php_open_temporary_file(NULL, "php", &opened_path TSRMLS_CC);
+ int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);
- if (fp) {
- php_stream *stream = php_stream_fopen_from_file_rel(fp, "r+b");
+ if (fd != -1) {
+ php_stream *stream = php_stream_fopen_from_fd_rel(fd, "r+b");
if (stream) {
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
@@ -188,7 +188,7 @@ PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
return stream;
}
- fclose(fp);
+ close(fd);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
@@ -493,9 +493,6 @@ static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
return SUCCESS;
case PHP_STREAM_AS_FD:
- /* fetch the fileno rather than using data->fd, since we may
- * have zeroed that member if someone requested the FILE*
- * first (see above case) */
PHP_STDIOP_GET_FD(fd, data);
if (fd < 0) {