在 C/C + + 中打印所有的环境变量

如何获得 C 和/或 C + + 中所有环境变量的列表?

我知道 ABc0可以用来读取环境变量,但是我怎样才能把它们全部列出来呢?

103573 次浏览
int main(int argc, char* argv[], char* envp[]) {
// loop through envp to get all environments as "NAME=val" until you hit NULL.
}

I think you should check environ. Use "man environ".

Your compiler may provide non-standard extensions to the main function that provides additional environment variable information. The MS compiler and most flavours of Unix have this version of main:

int main (int argc, char **argv, char **envp)

where the third parameter is the environment variable information - use a debugger to see what the format is - probably a null terminated list of string pointers.

LPTCH WINAPI GetEnvironmentStrings(void);

http://msdn.microsoft.com/en-us/library/ms683187%28VS.85%29.aspx

EDIT: only works on windows.

If you're running on a Windows operating system then you can also call GetEnvironmentStrings() which returns a block of null terminated strings.

In most environments you can declare your main as:

main(int argc,char* argv[], char** envp)

envp contains all environment strings.

The environment variables are made available to main() as the envp argument - a null terminated array of strings:

int main(int argc, char **argv, char **envp)
{
for (char **env = envp; *env != 0; env++)
{
char *thisEnv = *env;
printf("%s\n", thisEnv);
}
return 0;
}
#include <stdio.h>


extern char **environ;


int main() {
char **s = environ;


for (; *s; s++) {
printf("%s\n", *s);
}


return 0;
}
int main(int argc, char **argv, char** env) {
while (*env)
printf("%s\n", *env++);
return 0;
}

Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers. While Alex' answer:

#include <stdio.h>
extern char **environ;


int main() {
char **s = environ;
for (; *s; s++) {
printf("%s\n", *s);
}
return 0;
}

should work always, I wonder what happens to char **environ when you manipulate the environment in your C code (putenv, unsetenv). Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation). If we stick to a parameter passed to main and pass it on to the function requiring it, this pointer may not point to the current environment any more.

More or less portable C code solution seems for me as follows:

#include <stdlib.h>
void printenv() {
char ** env;
#if defined(WIN) && (_MSC_VER >= 1900)
env = *__p__environ();
#else
extern char ** environ;
env = environ;
#endif
for (env; *env; ++env) {
printf("%s\n", *env);
}
}

Explanations:

  1. Tested successfully on Linux, Windows, Solaris, AIX.

  2. Tested successfully on new versions of Visual Studio as well. The point is that since at least VS 2017 (probably earlier) the environ global variable is not recognized anymore. If you open the header C:\Program Files\Windows Kits\10\Include\x.x.x.x\ucrt\stdlib.h you will see that this global variable was replaced with the function __p__environ(). Unfortunately it is not documented well. No word about it in https://learn.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-170.

  3. The advantage of this approach is that it is also appropriate if you are not allowed to modify the main() function adding there envp[].

  4. Regarding GetEnvironmentStrings(), it returns me an empty list. Probably it works for C++ and not for C. I did not investigate it.