From 034cd404a129103a8dd7747e6bd00ffd5550da93 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Mon, 30 Jun 2025 07:51:23 +0100 Subject: refactoring --- nihil.posix/getenv.ccm | 55 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'nihil.posix/getenv.ccm') diff --git a/nihil.posix/getenv.ccm b/nihil.posix/getenv.ccm index 465f7e7..5967bf7 100644 --- a/nihil.posix/getenv.ccm +++ b/nihil.posix/getenv.ccm @@ -1,11 +1,15 @@ -/* - * This source code is released into the public domain. - */ - +// This source code is released into the public domain. module; +#include #include #include +#include +#include + +#include + +#include "nihil.hh" export module nihil.posix:getenv; @@ -13,11 +17,44 @@ import nihil.error; namespace nihil { -/* - * Find a variable by the given name in the environment by calling getenv_r(). - */ -export [[nodiscard]] auto getenv(std::string_view varname) - -> std::expected; +// Find a variable by the given name in the environment by calling getenv_r() if available, +// or getenv() if not. In either case the value is copied, so will not be affected by +// future calls to setenv(). +export [[nodiscard]] auto getenv(std::string_view varname) -> std::expected +{ + auto cvarname = std::string(varname); + +#ifdef NIHIL_HAVE_GETENV_R + // Start with a buffer of this size, and double it every iteration. + constexpr auto bufinc = std::size_t{1024}; + + auto buf = std::vector(bufinc); + for (;;) { + auto const ret = ::getenv_r(cvarname.c_str(), buf.data(), buf.size()); + + if (ret == 0) + return {std::string(buf.data())}; + + if (ret == -1 && errno == ERANGE) { + buf.resize(buf.size() * 2); + continue; + } + + return std::unexpected(error(std::errc(errno))); + } +#else // NIHIL_HAVE_GETENV_R + errno = 0; + auto *v = ::getenv(cvarname.c_str()); // NOLINT + + if (v != nullptr) + return {std::string(v)}; + + if (errno != 0) + return std::unexpected(error(std::errc(errno))); + + return std::unexpected(error(std::errc::no_such_file_or_directory)); +#endif // NIHIL_HAVE_GETENV_R +} } // namespace nihil -- cgit v1.2.3