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 [...]


How often don’t you have env, but you do have gcc and its libraries?
Uh heh heh. Actually my specific need was to write it out through syslog, and I couldn’t be bothered figuring out how to connect the stdout of
envto some/dev/logfile handle. Not that I didn’t haveenvavailable.(I can do it in Python with
subprocess, but no idea with normal POSIX stuff.)Key words here: quick ‘n dirty.
You can also do
which I think looks nicer than introducing a magic variable onto your stack
Cheers.