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