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"
|
2006-12-05 22:36:31 +00:00
|
|
|
|
#include "support/os.h"
|
2000-01-17 21:01:30 +00:00
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
|
#include <cerrno>
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
|
|
|
|
|
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
|
2006-12-27 10:56:11 +00:00
|
|
|
|
FileName const 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();
|
2007-01-18 20:47:27 +00:00
|
|
|
|
return FileName(os::internal_path(to_utf8(from_filesystem8bit(result))));
|
2000-11-08 09:39:46 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
|
} // namespace support
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|