在 C + + 中访问环境变量

我希望能够访问我正在编写的一个 c + + 程序中的 $HOME环境变量。如果我用 C 编写代码,我只会使用 getenv()函数,但我想知道是否有更好的方法来做到这一点。以下是我目前为止的代码:

std::string get_env_var( std::string const & key ) {
char * val;
val = getenv( key.c_str() );
std::string retval = "";
if (val != NULL) {
retval = val;
}
return retval;
}

我是否应该使用 getenv()来访问 C + + 中的环境变量?有没有什么我可能遇到的问题,我可以用一点点的知识来避免?

121725 次浏览

In c++ you have to use std::getenv and #include <cstdlib>

Why use GetEnvironmentVariable in Windows, from MSDN getenv:

getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.

And from MSDN GetEnvironment:

This function can retrieve either a system environment variable or a user environment variable.

There is nothing wrong with using getenv() in C++. It is defined by stdlib.h, or if you prefer the standard library implementation, you can include cstdlib and access the function via the std:: namespace (i.e., std::getenv()). Absolutely nothing wrong with this. In fact, if you are concerned about portability, either of these two versions is preferred.

If you are not concerned about portability and you are using managed C++, you can use the .NET equivalent - System::Environment::GetEnvironmentVariable(). If you want the non-.NET equivalent for Windows, you can simply use the GetEnvironmentVariable() Win32 function.

I would just refactor the code a little bit:

std::string getEnvVar( std::string const & key ) const
{
char * val = getenv( key.c_str() );
return val == NULL ? std::string("") : std::string(val);
}

A version of @Vlad's answer with some error checking and which distinguishes empty from missing values:

inline std::string get_env(const char* key) {
if (key == nullptr) {
throw std::invalid_argument("Null pointer passed as environment variable name");
}
if (*key == '\0') {
throw std::invalid_argument("Value requested for the empty-name environment variable");
}
const char* ev_val = getenv(key);
if (ev_val == nullptr) {
throw std::runtime_error("Environment variable not defined");
}
return std::string(ev_val);
}

Notes:

  • You could also replace the use of exceptions in the above with an std::optional<std::string> or, in the future, with an std::expected (if that ends up being standardized).
  • I've chosen safety over informativity here, by not concatenating the key into the what-string of the exception. If you make the alternative choice, try and limit copying from key to within reason (e.g. 100 characters? 200 characters?), and I'd also check these characters are printable, and sanitize those characters.

Yes, I know this is an old thread!
Still, common mistakes are, by definition, not new. :-)

The only reasons I see for not just using std::getenv(), would be to add a known default or to adopt common pattern/API in a framework. I would also avoid exceptions in this case (not generally though) simply because a non-value return is often enough a valid response for an environment variable. Adding the complexity of handling exceptions is counter-intuitive.

This is basically what I use:

const char* GetEnv( const char* tag, const char* def=nullptr ) noexcept {
const char* ret = std::getenv(tag);
return ret ? ret : def;
}
    

int main() {
int ret=0;
if( GetEnv("DEBUG_LOG") )  {
// Setup debug-logging
} else {
...
}
return (-1==ret?errno:0);
}

The difference between this and the other answers may seem small, but I find such small details are very rewarding when you form habits in how you code.

Just like the fact that getenv() returns a non-const pointer, which could easily lead to bad habits!