Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented putenv #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![CI](https://github.com/karel-burda/safe-env/actions/workflows/c-cpp.yml/badge.svg)

# Introduction
Simple C++ single-header **thread-safe** wrapper around `setenv`, `getenv`, `secure_getenv` and `unsetenv`.
Simple C++ single-header **thread-safe** wrapper around `setenv`, `putenv`, `getenv`, `secure_getenv` and `unsetenv`.
`getenv` and `secure_getenv` functions return copy of `std::string`, so the return value might be safely manipulated with.

Wrapper throws C++ exceptions in case these OS function fails -- `std::system_error` or if called with wrong arguments (`std::invalid_argument`).
Expand All @@ -16,6 +16,7 @@ Wrapper implements:
* `getenv`
* `secure_getenv`
* `setenv`
* `putenv`
* `unsetenv`

Implemented and documented at the [safe_env.hpp](include/safe_env/safe_env.hpp).
Expand All @@ -30,6 +31,7 @@ burda::env::setenv("name", "value2", false /* will not overwrite */);
burda::env::getenv("name"); // returns std::string "value"
burda::env::unsetenv("name");
burda::env::getenv("name"); // returns an empty std::string
burda::env::putenv("name=value3");
```

See also [main.cpp](main.cpp) for the test.
86 changes: 48 additions & 38 deletions include/safe_env/safe_env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,29 @@ inline void setenv(const std::string& name, const std::string& value, bool overw
/// @throws std::invalid_argument if name is empty, std::system_error
inline void unsetenv(const std::string& name);

/// @brief Sets environment variables using "putenv" in a thread-safe manner.
/// @param key_value_pairs features string in form of "env1=value1,env2=value2"
/// @throws std::invalid_argument if name is empty, std::system_error
inline void putenv(const std::string& key_value_pairs);

namespace detail
{
// due to performance reasons, we don't want to make this a static local variable
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
inline std::shared_mutex mtx;

template<typename F>
std::string read(const std::string& name, F getter);

template<typename F, typename... Args>
void write(const std::string& name, F setter, Args&&... args);
} // namespace detail
} // namespace burda::env

std::string burda::env::getenv(const std::string& name)
{
return burda::env::detail::read(name, ::getenv);
}

std::string burda::env::secure_getenv(const std::string& name)
{
return burda::env::detail::read(name, ::secure_getenv);
}

void burda::env::setenv(const std::string& name, const std::string& value, bool overwrite)
{
burda::env::detail::write(name, ::setenv, value.c_str(), static_cast<int>(overwrite));
}

void burda::env::unsetenv(const std::string& name)
inline void throw_if_empty(const std::string& arg)
{
burda::env::detail::write(name, ::unsetenv);
if (arg.empty()) [[unlikely]]
{
throw std::invalid_argument{"Argument is empty"};
}
}

template<typename F>
std::string burda::env::detail::read(const std::string& name, const F getter)
inline std::string read(const std::string& name, F getter)
{
if (name.empty()) [[unlikely]]
{
throw std::invalid_argument{"Environment variable name is empty"};
}
throw_if_empty(name);

std::shared_lock read_lock{mtx};

Expand All @@ -92,22 +74,50 @@ std::string burda::env::detail::read(const std::string& name, const F getter)
}

template<typename F, typename... Args>
void burda::env::detail::write(const std::string& name, const F setter, Args&&... args)
inline void write(F setter, Args&&... args)
{
if (name.empty()) [[unlikely]]
{
throw std::invalid_argument{"Environment variable name is empty"};
}

std::unique_lock write_lock{mtx};

if (const int ret = setter(name.c_str(), std::forward<Args>(args)...)) [[unlikely]]
if (const int ret = setter(std::forward<Args>(args)...)) [[unlikely]]
{
// we can unlock, as errno is thread-safe
write_lock.unlock();

throw std::system_error{errno, std::generic_category(), "Failed to set environment variable " + name};
throw std::system_error{errno, std::generic_category(), "Failed modify environment variable(s)"};
}
}
} // namespace detail
} // namespace burda::env

std::string burda::env::getenv(const std::string& name)
{
return burda::env::detail::read(name, ::getenv);
}

std::string burda::env::secure_getenv(const std::string& name)
{
return burda::env::detail::read(name, ::secure_getenv);
}

void burda::env::setenv(const std::string& name, const std::string& value, bool overwrite)
{
burda::env::detail::throw_if_empty(name);

burda::env::detail::write(::setenv, name, value.c_str(), static_cast<int>(overwrite));
}

void burda::env::unsetenv(const std::string& name)
{
burda::env::detail::throw_if_empty(name);

burda::env::detail::write(name, ::unsetenv);
}

void burda::env::putenv(const std::string& key_value_pairs)
{
burda::env::detail::throw_if_empty(key_value_pairs);

burda::env::detail::write(::putenv, key_value_pairs.c_str());
}

#endif // SAFE_ENV_SAFE_ENV_HPP
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void test_single_threaded()
env::unsetenv("foo");

assert(env::getenv("foo").empty());

env::putenv("foo=value3");

assert(env::getenv("foo") == "value3");
}

void test_multi_threaded()
Expand Down
Loading