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 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
58 # elif defined (HAVE__OPEN)
60 _O_RDWR | _O_CREAT | _O_EXCL,
61 _S_IREAD | _S_IWRITE);
63 # error No open() function.
73 string const lyx::support::tempName(string const & dir, string const & mask)
75 string const tmpdir(dir.empty() ? package().temp_dir() : dir);
76 string tmpfl(AddName(tmpdir, mask));
77 #if defined (HAVE_GETPID)
78 tmpfl += convert<string>(getpid());
79 #elif defined (HAVE__GETPID)
80 tmpfl += convert<string>(_getpid());
82 # error No getpid() function
86 // The supposedly safe mkstemp version
87 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
88 tmpfl.copy(tmpl.get(), string::npos);
89 tmpl[tmpfl.length()] = '\0'; // terminator
91 int const tmpf = make_tempfile(tmpl.get());
93 string const t(tmpl.get());
94 #if defined (HAVE_CLOSE)
96 #elif defined (HAVE__CLOSE)
99 # error No close() function.
101 lyxerr[Debug::FILES] << "Temporary file `" << t
102 << "' created." << endl;
106 << "LyX Error: Unable to create temporary file."