diff options
author | Martin Storsjö <martin@martin.st> | 2020-11-04 23:46:12 +0200 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2021-02-02 23:03:19 +0200 |
commit | 0c71c914faa371ba502a2e1835f763104837cb9f (patch) | |
tree | 13993d818474ef30fb54b96ef0643a0fa466a16d /libcxx/src/filesystem/operations.cpp | |
parent | 3fb83853791cabd08fbc6cd35ed2b82c0ffcc2d6 (diff) | |
download | llvm-0c71c914faa371ba502a2e1835f763104837cb9f.zip llvm-0c71c914faa371ba502a2e1835f763104837cb9f.tar.gz llvm-0c71c914faa371ba502a2e1835f763104837cb9f.tar.bz2 |
[libcxx] Implement the current_path function for windows
Differential Revision: https://reviews.llvm.org/D91169
Diffstat (limited to 'libcxx/src/filesystem/operations.cpp')
-rw-r--r-- | libcxx/src/filesystem/operations.cpp | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp index fcb5c2d..429a585 100644 --- a/libcxx/src/filesystem/operations.cpp +++ b/libcxx/src/filesystem/operations.cpp @@ -1022,15 +1022,32 @@ void __create_symlink(path const& from, path const& to, error_code* ec) { path __current_path(error_code* ec) { ErrorHandler<path> err("current_path", ec); +#if defined(_LIBCPP_WIN32API) + // Common extension outside of POSIX getcwd() spec, without needing to + // preallocate a buffer. Also supported by a number of other POSIX libcs. + int size = 0; + path::value_type* ptr = nullptr; + typedef decltype(&::free) Deleter; + Deleter deleter = &::free; +#else auto size = ::pathconf(".", _PC_PATH_MAX); _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size"); - auto buff = unique_ptr<char[]>(new char[size + 1]); - char* ret; - if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) + auto buff = unique_ptr<path::value_type[]>(new path::value_type[size + 1]); + path::value_type* ptr = buff.get(); + + // Preallocated buffer, don't free the buffer in the second unique_ptr + // below. + struct Deleter { void operator()(void*) const {} }; + Deleter deleter; +#endif + + unique_ptr<path::value_type, Deleter> hold(detail::getcwd(ptr, size), + deleter); + if (hold.get() == nullptr) return err.report(capture_errno(), "call to getcwd failed"); - return {buff.get()}; + return {hold.get()}; } void __current_path(const path& p, error_code* ec) { |