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;
57 int make_tempfile(char * templ)
59 #if defined(HAVE_MKSTEMP)
60 return ::mkstemp(templ);
61 #elif defined(HAVE_MKTEMP)
62 // This probably just barely works...
64 # if defined (HAVE_OPEN)
65 # if (!defined S_IRUSR)
66 # define S_IRUSR S_IREAD
67 # define S_IWUSR S_IWRITE
69 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
70 # elif defined (HAVE__OPEN)
72 _O_RDWR | _O_CREAT | _O_EXCL,
73 _S_IREAD | _S_IWRITE);
75 # error No open() function.
85 FileName const tempName(FileName const & dir, string const & mask)
87 string const tmpdir(dir.empty() ?
88 package().temp_dir().absFilename() :
90 string tmpfl(to_filesystem8bit(from_utf8(addName(tmpdir, mask))));
91 #if defined (HAVE_GETPID)
92 tmpfl += convert<string>(getpid());
93 #elif defined (HAVE__GETPID)
94 tmpfl += convert<string>(_getpid());
96 # error No getpid() function
100 // The supposedly safe mkstemp version
101 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
102 tmpfl.copy(tmpl.get(), string::npos);
103 tmpl[tmpfl.length()] = '\0'; // terminator
105 int const tmpf = make_tempfile(tmpl.get());
107 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
108 #if defined (HAVE_CLOSE)
110 #elif defined (HAVE__CLOSE)
113 # error No x() function.
115 LYXERR(Debug::FILES) << "Temporary file `" << t
116 << "' created." << endl;
120 << "LyX Error: Unable to create temporary file."
126 } // namespace support