Dump environment of a C program

29 October 2009

Here is a quick ‘n dirty way to dump the environment of a C program without the aid of any external programs (i.e. if you can’t exec() into /usr/bin/env):

#include <unistd.h>
#include <stdio.h>
 
int main (int argc, char *argv[]) {
	extern char **environ;
	int e = 0;
 
	while (environ[e] != NULL) {
		printf("%s\r\n", environ[e]);
		e++;
	}
}

Outputs:

USERNAME=jeremy
DESKTOP_SESSION=gnome
PATH=/home/jeremy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/jeremy
[...]