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