2000-11-08 09:39:46 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
#include "LString.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/filetools.h"
|
2001-07-29 17:39:01 +00:00
|
|
|
#include "support/lstrings.h"
|
2000-11-08 09:39:46 +00:00
|
|
|
#include "debug.h"
|
2001-05-17 15:11:01 +00:00
|
|
|
#include "os.h"
|
2000-11-08 09:39:46 +00:00
|
|
|
|
2000-11-08 15:19:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
2002-03-21 17:09:55 +00:00
|
|
|
int make_tempfile(char * templ)
|
2000-11-15 03:22:08 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
return ::mkstemp(templ);
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_MKTEMP
|
|
|
|
// This probably just barely works...
|
|
|
|
::mktemp(templ);
|
2001-05-17 15:11:01 +00:00
|
|
|
return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
2000-11-15 03:22:08 +00:00
|
|
|
#else
|
2001-04-17 00:19:49 +00:00
|
|
|
#ifdef WITH_WARNINGS
|
2000-11-15 03:22:08 +00:00
|
|
|
#warning FIX FIX FIX
|
|
|
|
#endif
|
|
|
|
#endif
|
2001-04-17 00:19:49 +00:00
|
|
|
#endif
|
2000-11-15 03:22:08 +00:00
|
|
|
}
|
2001-03-20 01:22:46 +00:00
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
string const lyx::tempName(string const & dir, string const & mask)
|
|
|
|
{
|
2001-05-17 15:11:01 +00:00
|
|
|
string const tmpdir(dir.empty() ? os::getTmpDir() : dir);
|
2000-11-08 09:39:46 +00:00
|
|
|
string tmpfl(AddName(tmpdir, mask));
|
|
|
|
tmpfl += tostr(getpid());
|
2000-11-14 02:01:57 +00:00
|
|
|
tmpfl += "XXXXXX";
|
|
|
|
|
|
|
|
// The supposedly safe mkstemp version
|
|
|
|
char * tmpl = new char[tmpfl.length() + 1]; // + 1 for '\0'
|
|
|
|
tmpfl.copy(tmpl, string::npos);
|
|
|
|
tmpl[tmpfl.length()] = '\0'; // terminator
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-11-15 03:22:08 +00:00
|
|
|
int const tmpf = make_tempfile(tmpl);
|
2000-11-08 09:39:46 +00:00
|
|
|
if (tmpf != -1) {
|
|
|
|
string const t(tmpl);
|
|
|
|
::close(tmpf);
|
|
|
|
delete [] tmpl;
|
2000-11-14 02:01:57 +00:00
|
|
|
lyxerr[Debug::FILES] << "Temporary file `" << t
|
|
|
|
<< "' created." << endl;
|
2000-11-08 09:39:46 +00:00
|
|
|
return t;
|
|
|
|
} else {
|
2000-11-14 02:01:57 +00:00
|
|
|
lyxerr[Debug::FILES]
|
|
|
|
<< "LyX Error: Unable to create temporary file."
|
|
|
|
<< endl;
|
2000-11-08 09:39:46 +00:00
|
|
|
delete [] tmpl;
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|