]> git.lyx.org Git - lyx.git/blob - src/support/getcwd.C
* lyxfunctional.h: delete compare_memfun and helper classes
[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 <cerrno>
14 #include <unistd.h>
15
16 #include "support/lyxlib.h"
17
18 #include <boost/scoped_array.hpp>
19
20 using boost::scoped_array;
21
22 using std::string;
23
24
25 namespace {
26
27 inline
28 char * l_getcwd(char * buffer, size_t size)
29 {
30 #ifndef __EMX__
31         return ::getcwd(buffer, size);
32 #else
33         return ::_getcwd2(buffer, size);
34 #endif
35 }
36
37 } // namespace anon
38
39
40 // Returns current working directory
41 string const lyx::support::getcwd()
42 {
43         int n = 256;    // Assume path is less than 256 chars
44         char * err;
45         scoped_array<char> tbuf(new char[n]);
46
47         // Safe. Hopefully all getcwds behave this way!
48         while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
49                 // Buffer too small, double the buffersize and try again
50                 n *= 2;
51                 tbuf.reset(new char[n]);
52         }
53
54         string result;
55         if (err)
56                 result = tbuf.get();
57         return result;
58 }