2003-06-18 09:56:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file getcwd.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-01-17 21:01:30 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include <cerrno>
|
2005-01-20 15:38:14 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
2004-11-07 13:22:51 +00:00
|
|
|
|
|
2005-01-31 15:26:40 +00:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
using boost::scoped_array;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
inline
|
2000-11-08 09:39:46 +00:00
|
|
|
|
char * l_getcwd(char * buffer, size_t size)
|
2000-01-17 21:01:30 +00:00
|
|
|
|
{
|
2006-05-14 17:00:53 +00:00
|
|
|
|
#ifdef _WIN32
|
2005-01-31 15:26:40 +00:00
|
|
|
|
GetCurrentDirectory(size, buffer);
|
|
|
|
|
return buffer;
|
|
|
|
|
#else
|
|
|
|
|
return ::getcwd(buffer, size);
|
2000-01-17 21:01:30 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2000-11-08 09:39:46 +00:00
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
2000-11-08 09:39:46 +00:00
|
|
|
|
|
|
|
|
|
// Returns current working directory
|
2003-06-30 23:56:22 +00:00
|
|
|
|
string const lyx::support::getcwd()
|
2000-11-08 09:39:46 +00:00
|
|
|
|
{
|
2002-03-21 17:09:55 +00:00
|
|
|
|
int n = 256; // Assume path is less than 256 chars
|
2000-11-08 09:39:46 +00:00
|
|
|
|
char * err;
|
2003-11-03 17:47:28 +00:00
|
|
|
|
scoped_array<char> tbuf(new char[n]);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
|
|
|
|
// Safe. Hopefully all getcwds behave this way!
|
2003-11-03 17:47:28 +00:00
|
|
|
|
while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
|
2000-11-08 09:39:46 +00:00
|
|
|
|
// Buffer too small, double the buffersize and try again
|
2003-11-03 17:47:28 +00:00
|
|
|
|
n *= 2;
|
|
|
|
|
tbuf.reset(new char[n]);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
}
|
2000-11-08 09:39:46 +00:00
|
|
|
|
|
|
|
|
|
string result;
|
2003-11-03 17:47:28 +00:00
|
|
|
|
if (err)
|
|
|
|
|
result = tbuf.get();
|
2000-11-08 09:39:46 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|