/* * This source code is released into the public domain. */ module; #include #include #include #include #include #include export module nihil:getenv; namespace nihil { /* * Find a variable by the given name in the environment by calling getenv_r(). */ export auto getenv(std::string_view varname) -> std::expected { // Start with a buffer of this size, and double it every iteration. constexpr auto bufinc = std::size_t{1024}; auto cvarname = std::string(varname); 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(std::make_error_code(std::errc(errno))); } } } // namespace nihil