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>
29 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
31 # ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
37 # ifdef HAVE_PROCESS_H
42 using boost::scoped_array;
52 int make_tempfile(char * templ)
54 #if defined(HAVE_MKSTEMP)
55 return ::mkstemp(templ);
56 #elif defined(HAVE_MKTEMP)
57 // This probably just barely works...
59 # if defined (HAVE_OPEN)
60 # if (!defined S_IRUSR)
61 # define S_IRUSR S_IREAD
62 # define S_IWUSR S_IWRITE
64 return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
65 # elif defined (HAVE__OPEN)
67 _O_RDWR | _O_CREAT | _O_EXCL,
68 _S_IREAD | _S_IWRITE);
70 # error No open() function.
80 string const lyx::support::tempName(string const & dir, string const & mask)
82 string const tmpdir(dir.empty() ? package().temp_dir() : dir);
83 string tmpfl(addName(tmpdir, mask));
84 #if defined (HAVE_GETPID)
85 tmpfl += convert<string>(getpid());
86 #elif defined (HAVE__GETPID)
87 tmpfl += convert<string>(_getpid());
89 # error No getpid() function
93 // The supposedly safe mkstemp version
94 scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
95 tmpfl.copy(tmpl.get(), string::npos);
96 tmpl[tmpfl.length()] = '\0'; // terminator
98 int const tmpf = make_tempfile(tmpl.get());
100 string const t(tmpl.get());
101 #if defined (HAVE_CLOSE)
103 #elif defined (HAVE__CLOSE)
106 # error No x() function.
108 lyxerr[Debug::FILES] << "Temporary file `" << t
109 << "' created." << endl;
113 << "LyX Error: Unable to create temporary file."