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.
16 #include "support/lyxlib.h"
17 #include "support/filetools.h"
18 #include "support/tostr.h"
22 #include <boost/scoped_array.hpp>
24 using boost::scoped_array;
32 int make_tempfile(char * templ)
34 #if defined(HAVE_MKSTEMP)
35 return ::mkstemp(templ);
36 #elif defined(HAVE_MKTEMP)
37 // This probably just barely works...
39 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
48 string const lyx::support::tempName(string const & dir, string const & mask)
50 string const tmpdir(dir.empty() ? os::getTmpDir() : dir);
51 string tmpfl(AddName(tmpdir, mask));
52 tmpfl += tostr(getpid());
55 // The supposedly safe mkstemp version
56 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
57 tmpfl.copy(tmpl.get(), string::npos);
58 tmpl[tmpfl.length()] = '\0'; // terminator
60 int const tmpf = make_tempfile(tmpl.get());
62 string const t(tmpl.get());
64 lyxerr[Debug::FILES] << "Temporary file `" << t
65 << "' created." << endl;
69 << "LyX Error: Unable to create temporary file."