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