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"
14 #include "support/filetools.h"
15 #include "support/tostr.h"
16 #include "support/os.h"
20 #include <boost/scoped_array.hpp>
25 using boost::scoped_array;
33 int make_tempfile(char * templ)
35 #if defined(HAVE_MKSTEMP)
36 return ::mkstemp(templ);
37 #elif defined(HAVE_MKTEMP)
38 // This probably just barely works...
40 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
49 string const lyx::support::tempName(string const & dir, string const & mask)
51 string const tmpdir(dir.empty() ? os::getTmpDir() : dir);
52 string tmpfl(AddName(tmpdir, mask));
53 tmpfl += tostr(getpid());
56 // The supposedly safe mkstemp version
57 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
58 tmpfl.copy(tmpl.get(), string::npos);
59 tmpl[tmpfl.length()] = '\0'; // terminator
61 int const tmpf = make_tempfile(tmpl.get());
63 string const t(tmpl.get());
65 lyxerr[Debug::FILES] << "Temporary file `" << t
66 << "' created." << endl;
70 << "LyX Error: Unable to create temporary file."