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>
|
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
2005-01-10 19:17:43 +00:00
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
|
#include "support/convert.h"
|
2005-01-10 19:17:43 +00:00
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
#include "support/package.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include <cstdlib>
|
2005-01-19 15:26:41 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2005-01-20 15:38:14 +00:00
|
|
|
|
# include <unistd.h>
|
2005-01-19 15:26:41 +00:00
|
|
|
|
#endif
|
2004-11-07 13:22:51 +00:00
|
|
|
|
|
2004-12-15 21:08:24 +00:00
|
|
|
|
#if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
|
|
|
|
|
# include <fcntl.h>
|
2005-04-26 10:30:24 +00:00
|
|
|
|
# ifdef HAVE_SYS_STAT_H
|
|
|
|
|
# include <sys/stat.h>
|
|
|
|
|
# endif
|
2005-05-03 20:32:55 +00:00
|
|
|
|
# ifdef HAVE_IO_H
|
|
|
|
|
# include <io.h>
|
|
|
|
|
# endif
|
|
|
|
|
# ifdef HAVE_PROCESS_H
|
|
|
|
|
# include <process.h>
|
|
|
|
|
# endif
|
2004-12-15 21:08:24 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
using boost::scoped_array;
|
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
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);
|
2005-05-03 20:32:55 +00:00
|
|
|
|
# if defined (HAVE_OPEN)
|
2001-05-17 15:11:01 +00:00
|
|
|
|
return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
2005-05-03 20:32:55 +00:00
|
|
|
|
# elif defined (HAVE__OPEN)
|
|
|
|
|
return ::_open(templ,
|
2006-04-05 23:39:11 +00:00
|
|
|
|
_O_RDWR | _O_CREAT | _O_EXCL,
|
|
|
|
|
_S_IREAD | _S_IWRITE);
|
2005-05-03 20:32:55 +00:00
|
|
|
|
# else
|
|
|
|
|
# error No open() function.
|
|
|
|
|
# endif
|
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
|
|
|
|
{
|
2005-01-10 19:17:43 +00:00
|
|
|
|
string const tmpdir(dir.empty() ? package().temp_dir() : dir);
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string tmpfl(addName(tmpdir, mask));
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#if defined (HAVE_GETPID)
|
2005-01-06 15:40:49 +00:00
|
|
|
|
tmpfl += convert<string>(getpid());
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#elif defined (HAVE__GETPID)
|
|
|
|
|
tmpfl += convert<string>(_getpid());
|
|
|
|
|
#else
|
|
|
|
|
# error No getpid() function
|
|
|
|
|
#endif
|
2000-11-14 02:01:57 +00:00
|
|
|
|
tmpfl += "XXXXXX";
|
|
|
|
|
|
|
|
|
|
// The supposedly safe mkstemp version
|
2003-11-03 17:47:28 +00:00
|
|
|
|
scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
|
|
|
|
|
tmpfl.copy(tmpl.get(), string::npos);
|
2000-11-14 02:01:57 +00:00
|
|
|
|
tmpl[tmpfl.length()] = '\0'; // terminator
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
int const tmpf = make_tempfile(tmpl.get());
|
2000-11-08 09:39:46 +00:00
|
|
|
|
if (tmpf != -1) {
|
2003-11-03 17:47:28 +00:00
|
|
|
|
string const t(tmpl.get());
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#if defined (HAVE_CLOSE)
|
2000-11-08 09:39:46 +00:00
|
|
|
|
::close(tmpf);
|
2005-05-03 20:32:55 +00:00
|
|
|
|
#elif defined (HAVE__CLOSE)
|
|
|
|
|
::_close(tmpf);
|
|
|
|
|
#else
|
|
|
|
|
# error No close() function.
|
|
|
|
|
#endif
|
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
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
}
|