diff --git a/lib/tmpdir.c b/lib/tmpdir.c index 5590ac3..ef77c9c 100644 --- a/lib/tmpdir.c +++ b/lib/tmpdir.c @@ -31,9 +31,15 @@ # define __set_errno(Val) errno = (Val) #endif +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define TMP_FALLBACK "\\" +#else +# define TMP_FALLBACK "/tmp" +#endif + #include #ifndef P_tmpdir -# define P_tmpdir "/tmp" +# define P_tmpdir TMP_FALLBACK #endif #include @@ -49,6 +55,59 @@ # define __secure_getenv getenv #endif + + +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define WIN32_LEAN_AND_MEAN +# include + +static DWORD length; +static LPSTR str = NULL; + +static char * +get_tmp_dir (void) +{ + DWORD l = 0; + + if (NULL == str) + { + length = 255; + str = malloc (sizeof (*str) * length); + if ( str == NULL) + return NULL; + l = GetTempPathA (length, str); + } + + if ( l == 0) + return NULL; + + /* If this condition is true, then GetTempPath is + asking for a larger buffer. We must run it again + with the requested size. */ + if ( l > length ) + { + length = l; + free (str); + str = malloc (sizeof (*str) * length); + if ( str == NULL) + return NULL; + + l = GetTempPathA (length, str); + } + if ( l == 0) + return NULL; + + return str; +} +#else +static char * +get_tmp_dir (void) +{ + return __secure_getenv ("TMPDIR"); +} +#endif + + /* Pathname support. ISSLASH(C) tests whether C is a directory separator character. */ @@ -96,7 +155,7 @@ path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, if (try_tmpdir) { - d = __secure_getenv ("TMPDIR"); + d = get_tmp_dir (); if (d != NULL && direxists (d)) dir = d; else if (dir != NULL && direxists (dir)) @@ -108,8 +167,8 @@ path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, { if (direxists (P_tmpdir)) dir = P_tmpdir; - else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) - dir = "/tmp"; + else if (strcmp (P_tmpdir, TMP_FALLBACK) != 0 && direxists (TMP_FALLBACK)) + dir = TMP_FALLBACK; else { __set_errno (ENOENT);