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

时间:2021-05-23 22:58:55

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可以用于读取环境变量,但是我如何列出它们呢?

9 个解决方案

#1


85  

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

#2


40  

#include<stdio.h>

extern char **environ;

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

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

  return 0;
}

#3


14  

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

我想你应该检查一下环境。使用“包围”。

#4


8  

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:

编译器可以为提供附加环境变量信息的主函数提供非标准扩展。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.

第三个参数是环境变量信息——使用调试器查看格式——可能是一个空终止字符串指针列表。

#5


5  

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

#6


4  

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

#7


4  

In most environments you can declare your main as:

在大多数环境中,您可以将您的主服务器声明为:

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

envp contains all environment strings.

envp包含所有环境字符串。

#8


3  

LPTCH WINAPI GetEnvironmentStrings(void);

LPTCH WINAPI GetEnvironmentStrings(无效);

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上工作。

#9


1  

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

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

#1


85  

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

#2


40  

#include<stdio.h>

extern char **environ;

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

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

  return 0;
}

#3


14  

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

我想你应该检查一下环境。使用“包围”。

#4


8  

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:

编译器可以为提供附加环境变量信息的主函数提供非标准扩展。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.

第三个参数是环境变量信息——使用调试器查看格式——可能是一个空终止字符串指针列表。

#5


5  

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

#6


4  

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

#7


4  

In most environments you can declare your main as:

在大多数环境中,您可以将您的主服务器声明为:

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

envp contains all environment strings.

envp包含所有环境字符串。

#8


3  

LPTCH WINAPI GetEnvironmentStrings(void);

LPTCH WINAPI GetEnvironmentStrings(无效);

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上工作。

#9


1  

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

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