Printing all environment variables in C / C++

C++C

C++ Problem Overview


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

I know that getenv can be used to read an environment variable, but how do I list them all?

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

#include <stdio.h>

extern char **environ;

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

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

  return 0;
}

Solution 3 - C++

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

Solution 4 - C++

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.

Solution 5 - C++

LPTCH WINAPI GetEnvironmentStrings(void);

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

EDIT: only works on windows.

Solution 6 - C++

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

Solution 7 - C++

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

Solution 8 - C++

In most environments you can declare your main as:

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

envp contains all environment strings.

Solution 9 - C++

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

Solution 10 - C++

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.

Solution 11 - C++

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://docs.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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayView Question on Stackoverflow
Solution 1 - C++Alex BrownView Answer on Stackoverflow
Solution 2 - C++user1602017View Answer on Stackoverflow
Solution 3 - C++Dyno FuView Answer on Stackoverflow
Solution 4 - C++SkizzView Answer on Stackoverflow
Solution 5 - C++whunmrView Answer on Stackoverflow
Solution 6 - C++AlexView Answer on Stackoverflow
Solution 7 - C++kennytmView Answer on Stackoverflow
Solution 8 - C++Sebastiaan MView Answer on Stackoverflow
Solution 9 - C++Len HolgateView Answer on Stackoverflow
Solution 10 - C++toasterView Answer on Stackoverflow
Solution 11 - C++Alexander SamoylovView Answer on Stackoverflow