Re: cvs: php4 /main php_open_temporary_file.c
| From: | Rasmus Lerdorf | Date: | Thu, 09 May 2002 19:40:35 +0000 |
| Subject: | Re: cvs: php4 /main php_open_temporary_file.c | ||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-11766@lists.php.net to get a copy of this message | ||
Could you try not to mix whitespace changes with code changes? It is
really hard to pick out what you actually changed with all the whitespace
changes.
Do a cvs diff before you commit to make sure the diff is nice and clean
and it is easy to see the important change. If you feel the need to make
whitespace changes, please do so in a separate patch, but please keep such
changes to an absolute minimum as it makes multi-version spanning diffs
much harder to read.
-Rasmus
On Thu, 9 May 2002, Preston L. Bannister wrote:
> pbannister Thu May 9 15:36:04 2002 EDT
>
> Modified files:
> /php4/main php_open_temporary_file.c
> Log:
> Compute directory for temporary files once and use same directory for all subsequent calls.
>
> Index: php4/main/php_open_temporary_file.c
> diff -u php4/main/php_open_temporary_file.c:1.14 php4/main/php_open_temporary_file.c:1.15
> --- php4/main/php_open_temporary_file.c:1.14 Thu Feb 28 03:27:03 2002
> +++ php4/main/php_open_temporary_file.c Thu May 9 15:36:03 2002
> @@ -87,60 +87,108 @@
> * SUCH DAMAGE.
> */
>
> -static FILE *php_do_open_temporary_file(char *path, const char *pfx, char **opened_path_p
> TSRMLS_DC)
> +static FILE *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;
> + char *trailing_slash;
> + FILE *fp;
> + char *opened_path;
> #ifndef PHP_WIN32
> - int fd;
> + int fd;
> #endif
>
> - if (!path) {
> - return NULL;
> - }
> -
> - if (!(opened_path = emalloc(MAXPATHLEN))) {
> - return NULL;
> - }
> -
> - if (path[strlen(path)-1] == '/') {
> - trailing_slash = "";
> - } else {
> - trailing_slash = "/";
> - }
> + if (!path) {
> + return NULL;
> + }
> +
> + if (!(opened_path = emalloc(MAXPATHLEN))) {
> + return NULL;
> + }
> +
> + if (path[strlen(path)-1] == '/') {
> + trailing_slash = "";
> + } else {
> + trailing_slash = "/";
> + }
>
> - (void)snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", path, trailing_slash, pfx);
> + (void)snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", path, trailing_slash,
> pfx);
>
> #ifdef PHP_WIN32
> - if (GetTempFileName(path, pfx, 0, opened_path)) {
> - fp = VCWD_FOPEN(opened_path, "wb");
> - } else {
> - fp = NULL;
> - }
> + if (GetTempFileName(path, pfx, 0, opened_path)) {
> + fp = VCWD_FOPEN(opened_path, "wb");
> + } else {
> + fp = NULL;
> + }
> #elif defined(HAVE_MKSTEMP)
> - fd = mkstemp(opened_path);
> - if (fd==-1) {
> - fp = NULL;
> - } else {
> - fp = fdopen(fd, "wb");
> - }
> + fd = mkstemp(opened_path);
> + if (fd==-1) {
> + fp = NULL;
> + } else {
> + fp = fdopen(fd, "wb");
> + }
> #else
> - if (mktemp(opened_path)) {
> - fp = VCWD_FOPEN(opened_path, "wb");
> - } else {
> - fp = NULL;
> - }
> + if (mktemp(opened_path)) {
> + fp = VCWD_FOPEN(opened_path, "wb");
> + } else {
> + fp = NULL;
> + }
> #endif
> - if (!fp || !opened_path_p) {
> - efree(opened_path);
> - } else {
> - *opened_path_p = opened_path;
> - }
> - return fp;
> + if (!fp || !opened_path_p) {
> + efree(opened_path);
> + } else {
> + *opened_path_p = opened_path;
> + }
> + return fp;
> }
> /* }}} */
>
> +/*
> + * Determine where to place temporary files.
> + */
> +const char* get_temporary_directory()
> +{
> + /* Cache the chosen temporary directory. */
> + static char* temporary_directory;
> +
> + /* Did we determine the temporary directory already? */
> + if (temporary_directory) {
> + return temporary_directory;
> + }
> +
> +#ifdef PHP_WIN32
> + /* We can't count on the environment variables TEMP or TMP,
> + * and so must make the Win32 API call to get the default
> + * directory for temporary files. Note this call checks
> + * the environment values TMP and TEMP (in order) first.
> + */
> + {
> + char sTemp[MAX_PATH];
> + DWORD n = GetTempPath(sizeof(sTemp),sTemp);
> + assert(0 < n); /* should *never* fail! */
> + temporary_directory = strdup(sTemp);
> + return temporary_directory;
> + }
> +#else
> + /* On Unix use the (usual) TMPDIR environment variable. */
> + {
> + char* s = getenv("TMPDIR");
> + if (s) {
> + temporary_directory = strdup(s);
> + return temporary_directory;
> + }
> + }
> +#ifdef P_tmpdir
> + /* Use the standard default temporary directory. */
> + if (P_tmpdir) {
> + temporary_directory = P_tmpdir;
> + return temporary_directory;
> + }
> +#endif
> + /* Shouldn't ever(!) end up here ... last ditch default. */
> + temporary_directory = "/tmp";
> + return temporary_directory;
> +#endif
> +}
> +
> /* {{{ php_open_temporary_file
> *
> * Unlike tempnam(), the supplied dir argument takes precedence
> @@ -150,47 +198,28 @@
> */
> PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p
> TSRMLS_DC)
> {
> - static char path_tmp[] = "/tmp";
> - FILE *fp;
> -
> -
> - if (!pfx) {
> - pfx = "tmp.";
> - }
> -
> - if (opened_path_p) {
> - *opened_path_p = NULL;
> - }
> -
> - if ((fp=php_do_open_temporary_file((char *) dir, pfx, opened_path_p TSRMLS_CC))) {
> - return fp;
> - }
> -
> - if ((fp=php_do_open_temporary_file(getenv("TMPDIR"), pfx, opened_path_p
> TSRMLS_CC))) {
> - return fp;
> - }
> -#if PHP_WIN32
> - {
> - char *TempPath;
> -
> - TempPath = (char *) emalloc(MAXPATHLEN);
> - if (GetTempPath(MAXPATHLEN, TempPath)) {
> - fp = php_do_open_temporary_file(TempPath, pfx, opened_path_p TSRMLS_CC);
> - }
> - efree(TempPath);
> - return fp;
> - }
> -#else
> - if ((fp=php_do_open_temporary_file(P_tmpdir, pfx, opened_path_p TSRMLS_CC))) {
> - return fp;
> - }
> -
> - if ((fp=php_do_open_temporary_file(path_tmp, pfx, opened_path_p TSRMLS_CC))) {
> - return fp;
> - }
> -#endif
> + FILE* fp = 0;
> +
> + if (!pfx) {
> + pfx = "tmp.";
> + }
> + if (opened_path_p) {
> + *opened_path_p = NULL;
> + }
> +
> + /* Try the directory given as parameter. */
> + fp = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC);
> + if (fp) {
> + return fp;
> + }
> +
> + /* Use default temporary directory. */
> + fp = php_do_open_temporary_file(get_temporary_directory(), pfx, opened_path_p TSRMLS_CC);
> + if (fp) {
> + return fp;
> + }
>
> - return NULL;
> + return 0;
> }
> /* }}} */
>
>
>
>
> --
> PHP CVS Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>