| 3597 | } |
| 3598 | |
| 3599 | BOOST_FILESYSTEM_DECL |
| 3600 | path current_path(error_code* ec) |
| 3601 | { |
| 3602 | #if defined(BOOST_FILESYSTEM_USE_WASI) |
| 3603 | // Windows CE has no current directory, so everything's relative to the root of the directory tree. |
| 3604 | // WASI also does not support current path. |
| 3605 | emit_error(BOOST_ERROR_NOT_SUPPORTED, ec, "boost::filesystem::current_path"); |
| 3606 | return path(); |
| 3607 | #elif defined(BOOST_FILESYSTEM_POSIX_API) |
| 3608 | struct local |
| 3609 | { |
| 3610 | static bool getcwd_error(error_code* ec) |
| 3611 | { |
| 3612 | const int err = errno; |
| 3613 | return error((err != ERANGE |
| 3614 | #if defined(__MSL__) && (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) |
| 3615 | // bug in some versions of the Metrowerks C lib on the Mac: wrong errno set |
| 3616 | && err != 0 |
| 3617 | #endif |
| 3618 | ) ? err : 0, |
| 3619 | ec, "boost::filesystem::current_path"); |
| 3620 | } |
| 3621 | }; |
| 3622 | |
| 3623 | path cur; |
| 3624 | char small_buf[small_path_size]; |
| 3625 | const char* p = ::getcwd(small_buf, sizeof(small_buf)); |
| 3626 | if (BOOST_LIKELY(!!p)) |
| 3627 | { |
| 3628 | cur = p; |
| 3629 | if (ec) |
| 3630 | ec->clear(); |
| 3631 | } |
| 3632 | else if (BOOST_LIKELY(!local::getcwd_error(ec))) |
| 3633 | { |
| 3634 | for (std::size_t path_max = sizeof(small_buf) * 2u;; path_max *= 2u) // loop 'til buffer large enough |
| 3635 | { |
| 3636 | if (BOOST_UNLIKELY(path_max > absolute_path_max)) |
| 3637 | { |
| 3638 | emit_error(ENAMETOOLONG, ec, "boost::filesystem::current_path"); |
| 3639 | break; |
| 3640 | } |
| 3641 | |
| 3642 | std::unique_ptr< char[] > buf(new char[path_max]); |
| 3643 | p = ::getcwd(buf.get(), path_max); |
| 3644 | if (BOOST_LIKELY(!!p)) |
| 3645 | { |
| 3646 | cur = buf.get(); |
| 3647 | if (ec) |
| 3648 | ec->clear(); |
| 3649 | break; |
| 3650 | } |
| 3651 | else if (BOOST_UNLIKELY(local::getcwd_error(ec))) |
| 3652 | { |
| 3653 | break; |
| 3654 | } |
| 3655 | } |
| 3656 | } |