Printing All Environment Variables in C/C++

Printing all environment variables in C / 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;
}

How to list all environment variables in a c/c++ app

Use the environ global variable. It is a null-terminated pointer to an array of strings in the format name=value. Here's a miniature clone of env:

#include <stdlib.h>
#include <stdio.h>

extern char **environ;

int main(int argc, char **argv) {
for(char **current = environ; *current; current++) {
puts(*current);
}
return EXIT_SUCCESS;
}

How to get all environment variables set by a C program itself?

In Linux, and similar systems, when you run a process (such as executing a C program), the process is a child process of the process that creates it (usually a command-line shell or a desktop/GUI manager). The creating process is the parent process. Any “environment variables” set in the child process do not affect the parent process.

The child process can examine its own environment variables with getenv.

If the child process creates its own child process, with system or another routine, any environment variables created in that “grandchild” process will not affect its parent (our first child process).

Two common ways for a program to provide environment variables for another process to use are:

  • The program may create its own child process and specify environment variables to be created in the child process, as with the various exec* routines such as execle.
  • The program writes settings for environment variables to stdout or another stream, and a cooperating process reads those settings and sets its own environment variables accordingly. An example of this is using the command eval `ssh-agent -s` in a Bourne-type shell. This command tells the shell to execute the command ssh-agent -s and then to evaluate the output of that command as if it were commands.

Print the environment variables using environ


#include <unistd.h>
#include <stdio.h>

extern char **environ;
//...

int i = 0;
while(environ[i]) {
printf("%s\n", environ[i++]); // prints in form of "variable=value"
}

Display Name=Value of all environment variables in C++

Just in case this doesn't get closed and people wind up here. Usually, each value pointed to by *envp would be a string of the form:

<EnvironmentVariableName>=<EnvironmentValue>

Such as (taken from ideone.com):

TMPDIR=/home/Cat5sW
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/Cat5sW
LANG=en_US.UTF-8
SHLVL=0
HOME=/home/Cat5sW

You would then need to split the string, based on the '=' char.

As John has pointed out above, this may differ for some os/compiler combinations (although I've never encountered them).

idiomatic C++14/C++17 approach to enumerating environment variables

There is no other way to enumerate the environment variables than you have in your question or in the linked answers. The reason is that the interface to environment variables is defined in such a way that C code can access them, so you have to do it "the C way" even in C++. C++ does not define its own way to access environment variables.

List environment variables with C in UNIX

Take a look at the environ global variable.

extern char **environ;

It might be defined in unistd.h (take a look at the environ (5) man page above).

Here's a little code demo I wrote:

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

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

Here's how to use it:

matt@stanley:~/Desktop$ make enumenv CFLAGS=-std=c99
cc -std=c99 enumenv.c -o enumenv
matt@stanley:~/Desktop$ ./enumenv
ORBIT_SOCKETDIR=/tmp/orbit-matt
SSH_AGENT_PID=1474
TERM=xterm
SHELL=/bin/bash
... (so forth)


Related Topics



Leave a reply



Submit