11.10.Â UNIXÂ® Environment

An important UNIXÂ® concept is the environment, which is defined by
environment variables. Some are set by the system, others
by you, yet others by the shell, or any program
that loads another program.

11.10.1.Â How to Find Environment Variables

I said earlier that when a program starts executing, the stack
contains argc followed by the NULL-terminated
argv array, followed by something else. The
"something else" is the environment, or,
to be more precise, a NULL-terminated array of pointers to
environment variables. This is often referred
to as env.

The structure of env is the same as that of
argv, a list of memory addresses followed by a
NULL (0). In this case, there is no
"envc"—we figure out where the array ends
by searching for the final NULL.

The variables usually come in the name=value
format, but sometimes the =value part
may be missing. We need to account for that possibility.

11.10.2.Â webvars

I could just show you some code that prints the environment
the same way the UNIXÂ® env command does. But
I thought it would be more interesting to write a simple
assembly language CGI utility.

This code produces a 1,396-byte executable. Most of it is data,
i.e., the HTML mark-up we need to send out.

Assemble and link it as usual:

%nasm -f elf webvars.asm%ld -s -o webvars webvars.o

To use it, you need to upload webvars to your
web server. Depending on how your web server is set up, you
may have to store it in a special cgi-bin directory,
or perhaps rename it with a .cgi extension.

Then you need to use your browser to view its output.
To see its output on my web server, please go to
http://www.int80h.org/webvars/.
If curious about the additional environment variables
present in a password protected web directory, go to
http://www.int80h.org/private/,
using the name asm and password
programmer.