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"
|
2000-11-08 09:39:46 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2000-11-08 15:19:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
extern string system_tempdir;
|
|
|
|
|
2000-11-15 03:22:08 +00:00
|
|
|
static inline
|
|
|
|
int make_tempfile(char * templ)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
return ::mkstemp(templ);
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_MKTEMP
|
|
|
|
// This probably just barely works...
|
|
|
|
::mktemp(templ);
|
|
|
|
return ::open(templ, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
|
|
|
#else
|
|
|
|
#warning FIX FIX FIX
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
string const lyx::tempName(string const & dir, string const & mask)
|
|
|
|
{
|
2000-11-14 02:01:57 +00:00
|
|
|
string const tmpdir(dir.empty() ? system_tempdir : 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
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|