Avoid using FILE* where possible.

Tidy up handling of potential error situations for the php:// wrapper.
This commit is contained in:
Wez Furlong 2003-03-18 16:40:29 +00:00
parent 40326f6adf
commit ce01fd9526
4 changed files with 62 additions and 54 deletions

View File

@ -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;
}

View File

@ -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);
if (fd == -1) {
return NULL;
}
return 0;
fp = fdopen(fd, "r+b");
if (fp == NULL) {
close(fd);
}
return fp;
}
/* }}} */

View File

@ -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 */

View File

@ -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) {