]> git.lyx.org Git - lyx.git/blob - src/support/getcwd.C
make "make distcheck" work
[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 __EMX
37         return ::_getcwd2(buffer, size);
38 #elif defined(_WIN32)
39         GetCurrentDirectory(size, buffer);
40         return buffer;
41 #else
42         return ::getcwd(buffer, size);
43 #endif
44 }
45
46 } // namespace anon
47
48
49 // Returns current working directory
50 string const lyx::support::getcwd()
51 {
52         int n = 256;    // Assume path is less than 256 chars
53         char * err;
54         scoped_array<char> tbuf(new char[n]);
55
56         // Safe. Hopefully all getcwds behave this way!
57         while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
58                 // Buffer too small, double the buffersize and try again
59                 n *= 2;
60                 tbuf.reset(new char[n]);
61         }
62
63         string result;
64         if (err)
65                 result = tbuf.get();
66         return result;
67 }