diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-21 12:27:20 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-21 12:27:20 +0100 |
| commit | 243d958df14b85788232aca623b83826115a5eb9 (patch) | |
| tree | d1f698296b053359a5563731cda8c51df9ab9a6c /modules/getenv.ccm | |
| parent | 8a36eb498e1a1c2cf2e886356faa4ce67e52e874 (diff) | |
| download | nihil-243d958df14b85788232aca623b83826115a5eb9.tar.gz nihil-243d958df14b85788232aca623b83826115a5eb9.tar.bz2 | |
rename modules/ to nihil/
Diffstat (limited to 'modules/getenv.ccm')
| -rw-r--r-- | modules/getenv.ccm | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/modules/getenv.ccm b/modules/getenv.ccm deleted file mode 100644 index 7397b79..0000000 --- a/modules/getenv.ccm +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include <cstdint> -#include <expected> -#include <string> -#include <system_error> -#include <vector> - -#include <unistd.h> - -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<std::string, std::error_code> -{ - // 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<char>(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 |
