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_MKTEMP)
31 # ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
37 # ifdef HAVE_PROCESS_H
42 using boost::scoped_array;
53 int make_tempfile(char * templ)
55 #if defined(HAVE_MKSTEMP)
56 return ::mkstemp(templ);
57 #elif defined(HAVE_MKTEMP)
58 // This probably just barely works...
60 # if defined (HAVE_OPEN)
61 # if (!defined S_IRUSR)
62 # define S_IRUSR S_IREAD
63 # define S_IWUSR S_IWRITE
65 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
66 # elif defined (HAVE__OPEN)
68 _O_RDWR | _O_CREAT | _O_EXCL,
69 _S_IREAD | _S_IWRITE);
71 # error No open() function.
81 FileName const tempName(FileName const & dir, string const & mask)
83 string const tmpdir(dir.empty() ?
84 package().temp_dir().toFilesystemEncoding() :
85 dir.toFilesystemEncoding());
86 string tmpfl(addName(tmpdir, mask));
87 #if defined (HAVE_GETPID)
88 tmpfl += convert<string>(getpid());
89 #elif defined (HAVE__GETPID)
90 tmpfl += convert<string>(_getpid());
92 # error No getpid() function
96 // The supposedly safe mkstemp version
97 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
98 tmpfl.copy(tmpl.get(), string::npos);
99 tmpl[tmpfl.length()] = '\0'; // terminator
101 int const tmpf = make_tempfile(tmpl.get());
103 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
104 #if defined (HAVE_CLOSE)
106 #elif defined (HAVE__CLOSE)
109 # error No x() function.
111 LYXERR(Debug::FILES) << "Temporary file `" << t
112 << "' created." << endl;
116 << "LyX Error: Unable to create temporary file."
122 } // namespace support