I know that when programming in c++ I can access individual environment variables with getenv
.
I also know that, in the os x terminal, I can list ALL of the current environment variables using env
.
I'm interested in getting a complete list of the environment variables that are available to my running c++ program. Is there a c/c++ function that will list them? In other words, is there a way to call env
from my c++ code?
Use the environ
global variable. It is a null-terminated pointer to an array of strings in the format name=value
. Here's a miniature clone of env
:
#include <stdlib.h>
#include <stdio.h>
extern char **environ;
int main(int argc, char **argv) {
for(char **current = environ; *current; current++) {
puts(*current);
}
return EXIT_SUCCESS;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With