wez Tue Mar 18 11:40:29 2003 EDT Modified files: /php4/ext/standard php_fopen_wrapper.c /php4/main php_open_temporary_file.c php_open_temporary_file.h /php4/main/streams plain_wrapper.c Log: Avoid using FILE* where possible. Tidy up handling of potential error situations for the php:// wrapper. Index: php4/ext/standard/php_fopen_wrapper.c diff -u php4/ext/standard/php_fopen_wrapper.c:1.35 php4/ext/standard/php_fopen_wrapper.c:1.36 --- php4/ext/standard/php_fopen_wrapper.c:1.35 Fri Feb 28 12:26:28 2003 +++ php4/ext/standard/php_fopen_wrapper.c Tue Mar 18 11:40:28 2003 @@ -17,7 +17,7 @@ | Hartmut Holzgraefe <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: php_fopen_wrapper.c,v 1.35 2003/02/28 17:26:28 iliaa Exp $ */ +/* $Id: php_fopen_wrapper.c,v 1.36 2003/03/18 16:40:28 wez Exp $ */ #include <stdio.h> #include <stdlib.h> @@ -171,15 +171,7 @@ 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 @@ 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; } Index: php4/main/php_open_temporary_file.c diff -u php4/main/php_open_temporary_file.c:1.24 php4/main/php_open_temporary_file.c:1.25 --- php4/main/php_open_temporary_file.c:1.24 Mon Feb 17 08:29:56 2003 +++ php4/main/php_open_temporary_file.c Tue Mar 18 11:40:29 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_open_temporary_file.c,v 1.24 2003/02/17 13:29:56 zeev Exp $ */ +/* $Id: php_open_temporary_file.c,v 1.25 2003/03/18 16:40:29 wez Exp $ */ #include "php.h" @@ -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 @@ #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 @@ * 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 @@ } /* 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; } /* }}} */ Index: php4/main/php_open_temporary_file.h diff -u php4/main/php_open_temporary_file.h:1.7 php4/main/php_open_temporary_file.h:1.8 --- php4/main/php_open_temporary_file.h:1.7 Wed Feb 19 03:40:19 2003 +++ php4/main/php_open_temporary_file.h Tue Mar 18 11:40:29 2003 @@ -16,11 +16,12 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_open_temporary_file.h,v 1.7 2003/02/19 08:40:19 sniper Exp $ */ +/* $Id: php_open_temporary_file.h,v 1.8 2003/03/18 16:40:29 wez Exp $ */ #ifndef PHP_OPEN_TEMPORARY_FILE_H #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 */ Index: php4/main/streams/plain_wrapper.c diff -u php4/main/streams/plain_wrapper.c:1.10 php4/main/streams/plain_wrapper.c:1.11 --- php4/main/streams/plain_wrapper.c:1.10 Sat Mar 15 08:29:56 2003 +++ php4/main/streams/plain_wrapper.c Tue Mar 18 11:40:29 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: plain_wrapper.c,v 1.10 2003/03/15 13:29:56 wez Exp $ */ +/* $Id: plain_wrapper.c,v 1.11 2003/03/18 16:40:29 wez Exp $ */ #include "php.h" #include "php_globals.h" @@ -157,14 +157,14 @@ 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_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 @@ return stream; } - fclose(fp); + close(fd); php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream"); @@ -493,9 +493,6 @@ 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) {
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php