This discusses the use of
environment variables in programs.
Next slide shows how to
get values and create values explicitly.
Getenv will simply returns
a value and if it's not defined,
it simply returns the null pointer.
Putenv puts it into the environment.
Now, it's important understand there,
it just puts it into the environment
for the program that's running.
So if you write a program that calls this function,
and you run it from your command interpreter,
the program will have that variable in its environment,
but the command interpreter will not.
Setenv is a mechanism similar to putenv.
Now, the next slide shows you
how to access these from
programs because when your program runs,
it's going to inherit the set of environment variables.
It's the third argument to the main routine.
If you look on the slide,
you'll see envp.
That's the environment.
That's a pointer to the environment variables,
and they're stored as a list of strings.
You can also access them globally.
There's a variable called environ,
that again is a pointer just like
envp to an array of
strings which are the environment variables.
You can also explicitly put them into a function
called execve or execvp or things like that.
So you can define which variables you want to parse.
If you don't define any,
all of them get parsed.
If you want to use them in your program,
use getenv for example, to grab them.
The little segment of code on this next slide
shows a process being
confined to the user's home directory area.
Now, the getenv home tells
you what the value of the environment variable home is,
which is typically the user's home directory.
In the next couple of lines,
if you see chroot that changes the root
processes notion of root to be that home directory.
If the home is not defined,
then it simply uses /tmp directory /tmp as the root.
We'll talk about chroot later on in this module.
Now, environment variables are also used
indirectly that is not named explicitly.
There are two library functions system and p open.
Which again we'll talk about in more detail later.
But those essentially run a sub-shell,
a command interpreter, which then
interprets the argument given to that function.
So what will happen here is,
system will run a shell usually bin/sh,
it may be bash it may be csh, or some other shell.
But it's usually bin/sh or bash,
and it will have that shell execute
the "echo -n today is; date".
Now, in order to find date which is simply
a command since there's no slash in the word date,
it looks for a date in
the first directory named in
the path environment variable.
If it doesn't find an executable there named date,
it will go to the second directory, and so forth.
It will continue going all the way down.
Then, the addition system sets
the environment variables shell
to whichever shell it's running.
In this example, it's bin/sh,
and it sets home to the user's home directory.