Neeladri Vishweswaran
Neeladri Vishweswaran

Reputation: 1763

global variable for getenv()?

Which is the global variable which holds all the environmental variables for getenv() ? In what glibc file is this var filled with env vars ?

I believe it to be **environ but when I set an env var in bash it only ouputs the SSH_AGENT_PID env var. Why is SSH_AGENT_PID set and why is it the only one that is set ?


DOCUMENT_ROOT='/foopath/'; export DOCUMENT_ROOT

int main(void)
{
extern char **environ;
printf("%s\n", *environ); // outputs: SSH_AGENT_PID=2822
}

Upvotes: 2

Views: 1776

Answers (1)

char **environ is NULL-terminated array of strings, so you should try:

extern char **environ;
char **p;
for (p = environ; *p; p++) {
    printf ("%s\n", *p);
}

In other words, environ[0] is pointer to first env variable, environ[1] to second etc. Last element in environ array is NULL.

Upvotes: 6

Related Questions