]> git.lyx.org Git - lyx.git/blob - src/support/getcwd.C
hopefully fix tex2lyx linking.
[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
19
20 namespace lyx {
21 #ifdef HAVE_UNISTD_H
22 # include <unistd.h>
23 #endif
24
25 #ifdef _WIN32
26 # include <windows.h>
27 #endif
28
29 using boost::scoped_array;
30
31 using std::string;
32
33
34 namespace {
35
36 inline
37 char * l_getcwd(char * buffer, size_t size)
38 {
39 #ifdef _WIN32
40         GetCurrentDirectory(size, buffer);
41         return buffer;
42 #else
43         return ::getcwd(buffer, size);
44 #endif
45 }
46
47 } // namespace anon
48
49
50 // Returns current working directory
51 string const lyx::support::getcwd()
52 {
53         int n = 256;    // Assume path is less than 256 chars
54         char * err;
55         scoped_array<char> tbuf(new char[n]);
56
57         // Safe. Hopefully all getcwds behave this way!
58         while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
59                 // Buffer too small, double the buffersize and try again
60                 n *= 2;
61                 tbuf.reset(new char[n]);
62         }
63
64         string result;
65         if (err)
66                 result = tbuf.get();
67         return result;
68 }
69
70
71 } // namespace lyx