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/convert.h"
16 #include "support/os.h"
20 #include <boost/scoped_array.hpp>
25 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
27 # include <sys/stat.h>
30 using boost::scoped_array;
38 int make_tempfile(char * templ)
40 #if defined(HAVE_MKSTEMP)
41 return ::mkstemp(templ);
42 #elif defined(HAVE_MKTEMP)
43 // This probably just barely works...
45 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
54 string const lyx::support::tempName(string const & dir, string const & mask)
56 string const tmpdir(dir.empty() ? os::getTmpDir() : dir);
57 string tmpfl(AddName(tmpdir, mask));
58 tmpfl += convert<string>(getpid());
61 // The supposedly safe mkstemp version
62 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
63 tmpfl.copy(tmpl.get(), string::npos);
64 tmpl[tmpfl.length()] = '\0'; // terminator
66 int const tmpf = make_tempfile(tmpl.get());
68 string const t(tmpl.get());
70 lyxerr[Debug::FILES] << "Temporary file `" << t
71 << "' created." << endl;
75 << "LyX Error: Unable to create temporary file."