+/* static */
+wxString
+wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
+{
+ wxString path, dir, name;
+
+ // use the directory specified by the prefix
+ SplitPath(prefix, &dir, &name, NULL /* extension */);
+
+ if (dir.empty())
+ {
+ dir = wxGetenv(_T("TMPDIR"));
+ if (dir.empty())
+ {
+ dir = wxGetenv(_T("TMP"));
+ if (dir.empty())
+ {
+ dir = wxGetenv(_T("TEMP"));
+ }
+ }
+ }
+
+#if defined(__WXWINCE__)
+ if (dir.empty())
+ {
+ // FIXME. Create \temp dir?
+ if (DirExists(wxT("\\temp")))
+ dir = wxT("\\temp");
+ }
+ path = dir + wxT("\\") + name;
+ int i = 1;
+ while (FileExists(path))
+ {
+ path = dir + wxT("\\") + name ;
+ path << i;
+ i ++;
+ }
+
+#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
+
+ if ( dir.empty() )
+ {
+ if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempPath"));
+ }
+
+ if ( dir.empty() )
+ {
+ // GetTempFileName() fails if we pass it an empty string
+ dir = _T('.');
+ }
+ }
+ else // we have a dir to create the file in
+ {
+ // ensure we use only the back slashes as GetTempFileName(), unlike all
+ // the other APIs, is picky and doesn't accept the forward ones
+ dir.Replace(_T("/"), _T("\\"));
+ }
+
+ if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempFileName"));
+
+ path.clear();
+ }
+
+#else // !Windows
+ if ( dir.empty() )
+ {
+ // default
+#if defined(__DOS__) || defined(__OS2__)
+ dir = _T(".");
+#elif defined(__WXMAC__)
+ dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
+#else
+ dir = _T("/tmp");
+#endif
+ }
+
+ path = dir;
+
+ if ( !wxEndsWithPathSeparator(dir) &&
+ (name.empty() || !wxIsPathSeparator(name[0u])) )
+ {
+ path += wxFILE_SEP_PATH;
+ }
+
+ path += name;
+
+#if defined(HAVE_MKSTEMP)
+ // scratch space for mkstemp()
+ path += _T("XXXXXX");
+
+ // we need to copy the path to the buffer in which mkstemp() can modify it
+ wxCharBuffer buf( wxConvFile.cWX2MB( path ) );
+
+ // cast is safe because the string length doesn't change
+ int fdTemp = mkstemp( (char*)(const char*) buf );
+ if ( fdTemp == -1 )
+ {
+ // this might be not necessary as mkstemp() on most systems should have
+ // already done it but it doesn't hurt neither...
+ path.clear();
+ }
+ else // mkstemp() succeeded
+ {
+ path = wxConvFile.cMB2WX( (const char*) buf );
+
+ // avoid leaking the fd
+ if ( fileTemp )
+ {
+ fileTemp->Attach(fdTemp);
+ }
+ else
+ {
+ close(fdTemp);
+ }
+ }
+#else // !HAVE_MKSTEMP
+
+#ifdef HAVE_MKTEMP
+ // same as above
+ path += _T("XXXXXX");
+
+ wxCharBuffer buf = wxConvFile.cWX2MB( path );
+ if ( !mktemp( (const char*) buf ) )
+ {
+ path.clear();
+ }
+ else
+ {
+ path = wxConvFile.cMB2WX( (const char*) buf );
+ }
+#else // !HAVE_MKTEMP (includes __DOS__)
+ // generate the unique file name ourselves
+ #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
+ path << (unsigned int)getpid();
+ #endif
+
+ wxString pathTry;
+
+ static const size_t numTries = 1000;
+ for ( size_t n = 0; n < numTries; n++ )
+ {
+ // 3 hex digits is enough for numTries == 1000 < 4096
+ pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
+ if ( !FileExists(pathTry) )
+ {
+ break;
+ }
+
+ pathTry.clear();
+ }
+
+ path = pathTry;
+#endif // HAVE_MKTEMP/!HAVE_MKTEMP
+
+#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
+
+#endif // Windows/!Windows
+
+ if ( path.empty() )
+ {
+ wxLogSysError(_("Failed to create a temporary file name"));
+ }
+ else if ( fileTemp && !fileTemp->IsOpened() )
+ {
+ // open the file - of course, there is a race condition here, this is
+ // why we always prefer using mkstemp()...
+ //
+ // NB: GetTempFileName() under Windows creates the file, so using
+ // write_excl there would fail
+ if ( !fileTemp->Open(path,
+#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
+ wxFile::write,
+#else
+ wxFile::write_excl,
+#endif
+ wxS_IRUSR | wxS_IWUSR) )
+ {
+ // FIXME: If !ok here should we loop and try again with another
+ // file name? That is the standard recourse if open(O_EXCL)
+ // fails, though of course it should be protected against
+ // possible infinite looping too.
+
+ wxLogError(_("Failed to open temporary file."));
+
+ path.clear();
+ }
+ }
+
+ return path;
+}
+
+#endif // wxUSE_FILE
+