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>
36 # ifdef HAVE_PROCESS_H
41 using boost::scoped_array;
49 int make_tempfile(char * templ)
51 #if defined(HAVE_MKSTEMP)
52 return ::mkstemp(templ);
53 #elif defined(HAVE_MKTEMP)
54 // This probably just barely works...
56 # if defined (HAVE_OPEN)
57 # if (!defined S_IRUSR)
58 # define S_IRUSR S_IREAD
59 # define S_IWUSR S_IWRITE
61 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
62 # elif defined (HAVE__OPEN)
64 _O_RDWR | _O_CREAT | _O_EXCL,
65 _S_IREAD | _S_IWRITE);
67 # error No open() function.
77 string const lyx::support::tempName(string const & dir, string const & mask)
79 string const tmpdir(dir.empty() ? package().temp_dir() : dir);
80 string tmpfl(addName(tmpdir, mask));
81 #if defined (HAVE_GETPID)
82 tmpfl += convert<string>(getpid());
83 #elif defined (HAVE__GETPID)
84 tmpfl += convert<string>(_getpid());
86 # error No getpid() function
90 // The supposedly safe mkstemp version
91 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
92 tmpfl.copy(tmpl.get(), string::npos);
93 tmpl[tmpfl.length()] = '\0'; // terminator
95 int const tmpf = make_tempfile(tmpl.get());
97 string const t(tmpl.get());
98 #if defined (HAVE_CLOSE)
100 #elif defined (HAVE__CLOSE)
103 # error No close() function.
105 lyxerr[Debug::FILES] << "Temporary file `" << t
106 << "' created." << endl;
110 << "LyX Error: Unable to create temporary file."