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
2010-08-13 03:40:10
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..
2010-08-13 04:03:48
@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
2010-08-13 04:20:26
@R: Yes I did this in my example code.
Matt Joiner
2010-08-13 05:32:36
@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
2010-08-13 06:03:25
Apologies; I stand corrected. I've read previous versions of POSIX in detail but have not gotten around to reading POSIX 2008.
R..
2010-08-13 06:22:28
@Jonathan, didn't know about this `semun` thing. scary.
Jens Gustedt
2010-08-13 06:26:25
+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
2010-08-13 03:51:28