开发者

Printing all environment variables in C / C++

How do I get the list of all environment variables in C and/or C++?

I know that getenv can b开发者_如何学编程e used to read an environment variable, but how do I list them all?


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


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.


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(int argc,char* argv[], char** envp)

envp contains all environment strings.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜