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/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
20 #include <boost/scoped_array.hpp>
28 #if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
29 extern "C" int mkstemp(char *);
32 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
34 # ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
40 # ifdef HAVE_PROCESS_H
47 using boost::scoped_array;
55 int make_tempfile(char * templ)
57 #if defined(HAVE_MKSTEMP)
58 return ::mkstemp(templ);
59 #elif defined(HAVE_MKTEMP)
60 // This probably just barely works...
62 # if defined (HAVE_OPEN)
63 # if (!defined S_IRUSR)
64 # define S_IRUSR S_IREAD
65 # define S_IWUSR S_IWRITE
67 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
68 # elif defined (HAVE__OPEN)
70 _O_RDWR | _O_CREAT | _O_EXCL,
71 _S_IREAD | _S_IWRITE);
73 # error No open() function.
83 FileName const tempName(FileName const & dir, string const & mask)
85 string const tmpdir = dir.empty()
86 ? package().temp_dir().absFilename() : dir.absFilename();
87 string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, mask)));
88 #if defined (HAVE_GETPID)
89 tmpfl += convert<string>(getpid());
90 #elif defined (HAVE__GETPID)
91 tmpfl += convert<string>(_getpid());
93 # error No getpid() function
97 // The supposedly safe mkstemp version
98 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
99 tmpfl.copy(tmpl.get(), string::npos);
100 tmpl[tmpfl.length()] = '\0'; // terminator
102 int const tmpf = make_tempfile(tmpl.get());
104 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
105 #if defined (HAVE_CLOSE)
107 #elif defined (HAVE__CLOSE)
110 # error No x() function.
112 LYXERR(Debug::FILES, "Temporary file `" << t << "' created.");
115 LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
119 } // namespace support