[comp.unix.questions] Manipulating the Shell environment variables

gag@chinet.chi.il.us (Gregory A. Gulik) (03/21/89)

How can I change variables in the environment in a C program,
and have the change remain in effect when I exit it?

putenv() does not make the change permanent; the variable is
it's original value when the C program exits.

Help!


-- 
Gregory A. Gulik
gag@chinet.chi.il.us
-or-
gulik@depaul.edu

guy@auspex.UUCP (Guy Harris) (03/25/89)

>How can I change variables in the environment in a C program,
>and have the change remain in effect when I exit it?
>
>putenv() does not make the change permanent; the variable is
>it's original value when the C program exits.

The root of the problem is the incorrect use of "the" in the phrase "the
environment".  There is no single environment that can be referred to as
"*the* environment"; every process has its own environment.  A process
can modify its environment; the modifications will be propagated to any
processes started after the modification is made (because the
environment is, in all UNIX implementations I know of, part of the
process's address space, and is duplicated when a "fork" is done; when
an "execv" or "execl" is done, the current environment is passed across
the "exec"). 

The word "copy" is important here; if a process modifies *its*
environment, it has *no* effect on the environment of other processes. 
I presume the environment you want to change is that of the shell from
which you're running the C program in question; the only way to do that
is to politely ask that shell to change its environment.  You can, for
example, have your program cough up a bunch of environment variable
settings to its standard output, and have the shell pick them up and
execute those settings; this is, for example, how the "tset" command,
from BSD, is used to set TERM and TERMCAP.