Skip to content

Thread-safe C++ wrapper around getenv, setenv and unsetenv for POSIX systems

License

Notifications You must be signed in to change notification settings

karel-burda/safe-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version Language Standard License CI

Introduction

Simple C++ single-header thread-safe wrapper around setenv, 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: std::system_error if OS specific functions failed and std::invalid_argument if called with wrong arguments (empty variable name).

Aim of the wrapper is to make all calls thread-safe, which is implemented by an internal read-write lock.

Wrapper implements:

  • getenv
  • secure_getenv
  • setenv
  • unsetenv

Implemented and documented at the safe_env.hpp.

Example

#include <safe_env/safe_env.hpp>

// all functions are safe to be called from different threads
burda::env::setenv("env_name", "env_value", true /* to overwrite any actual environment variable */);
burda::env::setenv("env_name", "env_value2", false /* will not overwrite */);
std::string env_value = burda::env::getenv("env_name"); // contains "env_value"
burda::env::unsetenv("env_name");
env_value = burda::env::getenv("env_name"); // contains an empty std::string

See also main.cpp for the test example.

About

Thread-safe C++ wrapper around getenv, setenv and unsetenv for POSIX systems

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages