]> git.lyx.org Git - lyx.git/blobdiff - src/support/getcwd.C
one small private fix in mathed, put noncopyable and tie into boost namespace
[lyx.git] / src / support / getcwd.C
index f8e096bba1ea4daa9c3919d933f552609f79b6d2..edf38b3ce59662364881f77d9d2031bfcfce4864 100644 (file)
@@ -1,10 +1,12 @@
 #include <config.h>
 
+#include <cerrno>
 #include <unistd.h>
 
 #include "support/lyxlib.h"
 
-char * lyx::getcwd(char * buffer, size_t size)
+static inline
+char * l_getcwd(char * buffer, size_t size)
 {
 #ifndef __EMX__
        return ::getcwd(buffer, size);
@@ -12,3 +14,25 @@ char * lyx::getcwd(char * buffer, size_t size)
        return ::_getcwd2(buffer, size);
 #endif
 }
+
+
+// Returns current working directory
+string const lyx::getcwd()
+{
+       int n = 256;    // Assume path is less than 256 chars
+       char * err;
+       char * tbuf = new char[n];
+       
+       // Safe. Hopefully all getcwds behave this way!
+       while (((err = l_getcwd(tbuf, n)) == 0) && (errno == ERANGE)) {
+               // Buffer too small, double the buffersize and try again
+               delete[] tbuf;
+               n = 2 * n;
+               tbuf = new char[n];
+       }
+
+       string result;
+       if (err) result = tbuf;
+       delete[] tbuf;
+       return result;
+}