3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "support/lyxlib.h"
15 #include "support/convert.h"
16 #include "support/filetools.h"
17 #include "support/Package.h"
21 #include <boost/scoped_array.hpp>
29 #if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
30 extern "C" int mkstemp(char *);
33 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
35 # ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
41 # ifdef HAVE_PROCESS_H
46 using boost::scoped_array;
56 int make_tempfile(char * templ)
58 #if defined(HAVE_MKSTEMP)
59 return ::mkstemp(templ);
60 #elif defined(HAVE_MKTEMP)
61 // This probably just barely works...
63 # if defined (HAVE_OPEN)
64 # if (!defined S_IRUSR)
65 # define S_IRUSR S_IREAD
66 # define S_IWUSR S_IWRITE
68 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
69 # elif defined (HAVE__OPEN)
71 _O_RDWR | _O_CREAT | _O_EXCL,
72 _S_IREAD | _S_IWRITE);
74 # error No open() function.
84 FileName const tempName(FileName const & dir, string const & mask)
86 string const tmpdir = dir.empty()
87 ? package().temp_dir().absFilename() : dir.absFilename();
88 string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, mask)));
89 #if defined (HAVE_GETPID)
90 tmpfl += convert<string>(getpid());
91 #elif defined (HAVE__GETPID)
92 tmpfl += convert<string>(_getpid());
94 # error No getpid() function
98 // The supposedly safe mkstemp version
99 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
100 tmpfl.copy(tmpl.get(), string::npos);
101 tmpl[tmpfl.length()] = '\0'; // terminator
103 int const tmpf = make_tempfile(tmpl.get());
105 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
106 #if defined (HAVE_CLOSE)
108 #elif defined (HAVE__CLOSE)
111 # error No x() function.
113 LYXERR(Debug::FILES, "Temporary file `" << t << "' created.");
116 LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
120 } // namespace support