File tree Expand file tree Collapse file tree 1 file changed +16
-16
lines changed Expand file tree Collapse file tree 1 file changed +16
-16
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,10 @@ Date: January 2012
13
13
14
14
#include " file_util.h"
15
15
16
+ #include " invariant.h"
17
+
16
18
#include < cerrno>
19
+ #include < cstring>
17
20
18
21
#if defined(__linux__) || \
19
22
defined (__FreeBSD_kernel__) || \
@@ -36,29 +39,26 @@ Date: January 2012
36
39
#define chdir _chdir
37
40
#define popen _popen
38
41
#define pclose _pclose
39
- #else
40
- #include < cstring>
41
42
#endif
42
43
43
44
// / \return current working directory
44
45
std::string get_current_working_directory ()
45
46
{
46
- unsigned bsize=50 ;
47
-
48
- char *buf=reinterpret_cast <char *>(malloc (sizeof (char )*bsize));
49
- if (!buf)
50
- abort ();
51
-
47
+ #ifndef _WIN32
52
48
errno=0 ;
49
+ char *wd=realpath (" ." , nullptr );
50
+ INVARIANT (
51
+ wd!=nullptr && errno==0 ,
52
+ std::string (" realpath failed: " )+strerror (errno));
53
53
54
- while (buf && getcwd (buf, bsize- 1 )== nullptr && errno==ERANGE)
55
- {
56
- bsize*= 2 ;
57
- buf= reinterpret_cast < char *>( realloc (buf, sizeof ( char )*bsize)) ;
58
- }
59
-
60
- std::string working_directory=buf ;
61
- free (buf);
54
+ std::string working_directory=wd;
55
+ free (wd);
56
+ # else
57
+ char buffer[ 4096 ] ;
58
+ DWORD retval= GetCurrentDirectory ( 4096 , buffer);
59
+ CHECK_RETURN (retval> 0 );
60
+ std::string working_directory (buffer) ;
61
+ # endif
62
62
63
63
return working_directory;
64
64
}
You can’t perform that action at this time.
0 commit comments