views:

205

answers:

2

Is there a way to enumerate environment variables and retrieve values using C?

+6  A: 

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) manpage above).

Here's a little code demo I wrote:

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

void 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)
Matt Joiner
It's not defined in unistd.h, or if it is, your system is not POSIX compliant. POSIX requires you to explicitly declare `extern char **environ;` if you want to use it.
R..
@R, +1. On my system at least, it's only declared in `unistd.h` if `__USE_GNU` is set, which indicates it's an extension. (FWIW, `__environ`, also an extension, is declared unconditionally).
Matthew Flaschen
@R: Yes I did this in my example code.
Matt Joiner
@R..: up until the POSIX 2008 standard, you were correct; since then, [`<unistd.h>`](http://www.opengroup.org/onlinepubs/9699919799/functions/environ.html) has taken the sensible step of requiring it. It was the only variable without a declaration in a system header. The only oddball left that I know of is [`union semun`](http://www.opengroup.org/onlinepubs/9699919799/functions/semctl.html).
Jonathan Leffler
Apologies; I stand corrected. I've read previous versions of POSIX in detail but have not gotten around to reading POSIX 2008.
R..
@Jonathan, didn't know about this `semun` thing. scary.
Jens Gustedt
+4  A: 

The environment information can be passed as an extra parameter to main. I don't know if it is compliant or not, but it definitely works (tested on Ubuntu). Just define the extra argument and its an array of char pointers terminated by a NULL pointer. The following will print out the lot.

#include <stdio>

int main(int argc, char *argv[], char *envp[])
{
  int index = 0;
  while (envp[index])
    printf("%s\n", envp[index++];
}
torak
+1: never heard of this before
sje397
In practice this works on most unices, but it's not specified by POSIX. Use `extern char **environ;`.
R..
As Jonathan responded to one of my other comments, as of POSIX 2008 it *is* now part of unistd.h.
R..