]> git.lyx.org Git - lyx.git/blob - src/support/getcwd.C
MacOSX compile fix.
[lyx.git] / src / support / getcwd.C
1 /**
2  * \file getcwd.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14
15 #include <boost/scoped_array.hpp>
16
17 #include <cerrno>
18 #ifdef HAVE_UNISTD_H
19 # include <unistd.h>
20 #endif
21
22 #ifdef _WIN32
23 # include <windows.h>
24 #endif
25
26 using boost::scoped_array;
27
28 using std::string;
29
30
31 namespace {
32
33 inline
34 char * l_getcwd(char * buffer, size_t size)
35 {
36 #ifdef _WIN32
37         GetCurrentDirectory(size, buffer);
38         return buffer;
39 #else
40         return ::getcwd(buffer, size);
41 #endif
42 }
43
44 } // namespace anon
45
46
47 // Returns current working directory
48 string const lyx::support::getcwd()
49 {
50         int n = 256;    // Assume path is less than 256 chars
51         char * err;
52         scoped_array<char> tbuf(new char[n]);
53
54         // Safe. Hopefully all getcwds behave this way!
55         while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
56                 // Buffer too small, double the buffersize and try again
57                 n *= 2;
58                 tbuf.reset(new char[n]);
59         }
60
61         string result;
62         if (err)
63                 result = tbuf.get();
64         return result;
65 }