简体   繁体   English

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

[英]Printing all environment variables in C / C++

How do I get the list of all environment variables in C and/or C++?如何获取 C 和/或 C++ 中所有环境变量的列表?

I know that getenv can be used to read an environment variable, but how do I list them all?我知道getenv可用于读取环境变量,但如何列出它们?

The environment variables are made available to main() as the envp argument - a null terminated array of strings:环境变量作为envp参数可用于main() - 一个以空字符结尾的字符串数组:

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;
}

I think you should check environ .我认为你应该检查environ Use "man environ".使用“人环境”。

Your compiler may provide non-standard extensions to the main function that provides additional environment variable information.您的编译器可能会为提供额外环境变量信息的 main 函数提供非标准扩展。 The MS compiler and most flavours of Unix have this version of main: MS 编译器和大多数 Unix 版本都有这个版本的 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 http://msdn.microsoft.com/en-us/library/ms683187%28VS.85%29.aspx

EDIT: only works on windows.编辑:仅适用于 Windows。

int main(int argc, char **argv, char** env) {
   while (*env)
      printf("%s\n", *env++);
   return 0;
}
int main(int argc, char* argv[], char* envp[]) {
   // loop through envp to get all environments as "NAME=val" until you hit NULL.
}

In most environments you can declare your main as:在大多数环境中,您可以将 main 声明为:

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

envp contains all environment strings. envp 包含所有环境字符串。

如果您在 Windows 操作系统上运行,那么您还可以调用GetEnvironmentStrings()返回一个空终止字符串块。

More or less portable C code solution seems for me as follows:或多或少的可移植 C 代码解决方案对我来说似乎如下:

#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.在 Linux、Windows、Solaris、AIX 上测试成功。

  2. Tested successfully on new versions of Visual Studio as well.在新版本的 Visual Studio 上也成功测试。 The point is that since at least VS 2017 (probably earlier) the environ global variable is not recognized anymore.关键是至少从 VS 2017(可能更早)开始, environ全局变量不再被识别。 If you open the header C:\Program Files\Windows Kits\10\Include\xxxx\ucrt\stdlib.h you will see that this global variable was replaced with the function __p__environ() .如果您打开 header C:\Program Files\Windows Kits\10\Include\xxxx\ucrt\stdlib.h,您将看到此全局变量已替换为 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 .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[] .这种方法的优点是,如果您不允许修改 main() function 添加envp[] ,它也是合适的。

  4. Regarding GetEnvironmentStrings(), it returns me an empty list.关于 GetEnvironmentStrings(),它返回一个空列表。 Probably it works for C++ and not for C. I did not investigate it.可能它适用于 C++ 而不是 C。我没有调查它。

Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers.这里的大多数答案都指出了从大多数编译器支持的 main 参数中选择环境的可能性。 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).应该始终有效,我想知道当您在 C 代码(putenv、unsetenv)中操作char **environchar **environ会发生什么。 Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation).然后environ可能指向其他地方(重新分配时,可能取决于系统实现)。 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.如果我们坚持传递给 main 的参数并将其传递给需要它的函数,则此指针可能不再指向当前环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM