]> git.lyx.org Git - lyx.git/blobdiff - src/support/getcwd.C
hopefully fix tex2lyx linking.
[lyx.git] / src / support / getcwd.C
index f8e096bba1ea4daa9c3919d933f552609f79b6d2..40b61a6a2609410f5e9e394ad683d5c580551182 100644 (file)
@@ -1,14 +1,71 @@
-#include <config.h>
+/**
+ * \file getcwd.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
-#include <unistd.h>
+#include <config.h>
 
 #include "support/lyxlib.h"
 
-char * lyx::getcwd(char * buffer, size_t size)
+#include <boost/scoped_array.hpp>
+
+#include <cerrno>
+
+
+namespace lyx {
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef _WIN32
+# include <windows.h>
+#endif
+
+using boost::scoped_array;
+
+using std::string;
+
+
+namespace {
+
+inline
+char * l_getcwd(char * buffer, size_t size)
 {
-#ifndef __EMX__
-       return ::getcwd(buffer, size);
+#ifdef _WIN32
+       GetCurrentDirectory(size, buffer);
+       return buffer;
 #else
-       return ::_getcwd2(buffer, size);
+       return ::getcwd(buffer, size);
 #endif
 }
+
+} // namespace anon
+
+
+// Returns current working directory
+string const lyx::support::getcwd()
+{
+       int n = 256;    // Assume path is less than 256 chars
+       char * err;
+       scoped_array<char> tbuf(new char[n]);
+
+       // Safe. Hopefully all getcwds behave this way!
+       while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
+               // Buffer too small, double the buffersize and try again
+               n *= 2;
+               tbuf.reset(new char[n]);
+       }
+
+       string result;
+       if (err)
+               result = tbuf.get();
+       return result;
+}
+
+
+} // namespace lyx