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>
28 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
30 # include <sys/stat.h>
33 using boost::scoped_array;
41 int make_tempfile(char * templ)
43 #if defined(HAVE_MKSTEMP)
44 return ::mkstemp(templ);
45 #elif defined(HAVE_MKTEMP)
46 // This probably just barely works...
48 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
57 string const lyx::support::tempName(string const & dir, string const & mask)
59 string const tmpdir(dir.empty() ? package().temp_dir() : dir);
60 string tmpfl(AddName(tmpdir, mask));
61 tmpfl += convert<string>(getpid());
64 // The supposedly safe mkstemp version
65 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
66 tmpfl.copy(tmpl.get(), string::npos);
67 tmpl[tmpfl.length()] = '\0'; // terminator
69 int const tmpf = make_tempfile(tmpl.get());
71 string const t(tmpl.get());
73 lyxerr[Debug::FILES] << "Temporary file `" << t
74 << "' created." << endl;
78 << "LyX Error: Unable to create temporary file."