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