2003-06-18 09:56:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file tempname.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2003-07-28 22:37:28 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-06-18 09:56:10 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-06-18 09:56:10 +00:00
|
|
|
|
*/
|
|
|
|
|
|
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"
|
2003-05-13 14:36:24 +00:00
|
|
|
|
#include "support/tostr.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;
|
|
|
|
|
|
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
|
|
|
|
{
|
2002-04-15 11:01:19 +00:00
|
|
|
|
#if defined(HAVE_MKSTEMP)
|
2000-11-15 03:22:08 +00:00
|
|
|
|
return ::mkstemp(templ);
|
2002-04-15 11:01:19 +00:00
|
|
|
|
#elif defined(HAVE_MKTEMP)
|
2000-11-15 03:22:08 +00:00
|
|
|
|
// 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
|
2002-04-15 11:01:19 +00:00
|
|
|
|
#error FIX FIX FIX
|
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
|
|
|
|
|
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
string const lyx::support::tempName(string const & dir, string const & mask)
|
2000-11-08 09:39:46 +00:00
|
|
|
|
{
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|