[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tmpdir on windows

From: Bruno Haible
Subject: Re: tmpdir on windows
Date: Tue, 23 Aug 2011 08:43:33 +0200
User-agent: KMail/1.13.6 (Linux/; KDE/4.6.0; x86_64; ; )

Hello John,

John Darrington wrote:
> In PSPP we are using the path_search function from the tmpdir module to 
> determine the path for temporary files.
> Windows users have complained that it puts temporary files in places they
> consider to be inappropriate.  I did a bit of research, and found that the 
> "normal" path on windows depends on exactly which version of windows it is.
> So I came up with this patch, which uses the w32 native functions to get this 
> path.
> Our windows port maintainer has been using it for a few weeks now and hasn't
> discovered any problems.

Thanks for the report and patch. Indeed gnulib has been using GetTempPath in
the 'tmpfile' module, but the 'tmpdir' module needs it as well (when a
dir = NULL argument is passed).

I agree with the general idea of the patch. But not with the details:

  - There is no need for a macro TMP_FALLBACK. This is precisely what
    P_tmpdir is meant to be. On mingw and msvc systems, a macro _P_tmpdir
    is defined to "\\", and in msvc systems, sometimes, P_tmpdir is defined
    to _P_tmpdir. So all we have to do is to make sure that on on mingw and
    msvc systems, P_tmpdir is defined to _P_tmpdir always.

  - The function get_tmp_dir is overly complicated. We can use a large enough

  - There is no need to write GetTempPathA instead of GetTempPath. Gnulib
    assumes that the macro UNICODE is not defined.

  - Why should the code not look at $TMPDIR on Windows? This is the environment
    variable Unixy scripts will set. GetTempPath looks at the environment
    variables $TMP, $TEMP, $USERPROFILE, so looking at $TMPDIR is not redundant.

I'm applying this patch instead:

2011-08-23  Bruno Haible  <address@hidden>

        tmpdir: Use a good default directory on native Windows.
        * lib/tmpdir.c: Include <windows.h>, pathmax.h.
        (P_tmpdir): Default to _P_tmpdir on native Windows.
        (path_search): On native Windows, try the value returned by GetTempPath
        before trying P_tmpdir.
        * modules/tmpdir (Depends-on): Add pathmax.
        Suggested by John Darrington <address@hidden>.

--- lib/tmpdir.c.orig   Tue Aug 23 08:35:27 2011
+++ lib/tmpdir.c        Tue Aug 23 08:22:33 2011
@@ -33,11 +33,22 @@
 #include <stdio.h>
 #ifndef P_tmpdir
-# define P_tmpdir "/tmp"
+# ifdef _P_tmpdir /* native Windows */
+#  define P_tmpdir _P_tmpdir
+# else
+#  define P_tmpdir "/tmp"
+# endif
 #include <sys/stat.h>
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
+# include <windows.h>
+#include "pathmax.h"
 #if _LIBC
 # define struct_stat64 struct stat64
@@ -106,6 +117,19 @@
   if (dir == NULL)
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+      char dirbuf[PATH_MAX];
+      DWORD retval;
+      /* Find Windows temporary file directory.
+         We try this before P_tmpdir because Windows defines P_tmpdir to "\\"
+         and will therefore try to put all temporary files in the root
+         directory (unless $TMPDIR is set).  */
+      retval = GetTempPath (PATH_MAX, dirbuf);
+      if (retval > 0 && retval < PATH_MAX && direxists (dirbuf))
+        dir = dirbuf;
+      else
       if (direxists (P_tmpdir))
         dir = P_tmpdir;
       else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
--- modules/tmpdir.orig Tue Aug 23 08:35:28 2011
+++ modules/tmpdir      Tue Aug 23 08:23:28 2011
@@ -9,6 +9,7 @@

In memoriam Swami Lakshmanananda 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]