global variable for getenv()?

十年热恋 提交于 2019-12-11 03:59:32

问题


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
}

回答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.



来源:https://stackoverflow.com/questions/3127614/global-variable-for-getenv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!