[comp.unix.wizards] Putenv

borzieri@KING.ICO.OLIVETTI.COM (Ivan Borzieri) (02/15/91)

Hi,

I URGENTLY need this information :

I wrote two c modules called by a fortran main.
in the first  c module I call the system function "putenv()" which should
set a variable in the environment.
In the second  c module I call the system function "getenv()" to read 
the value of the previous set variable.
The value returned by getenv() is NULL, id est, that variable
doesn't exist.

Now my question is : is this right ?
I know that in C-Shell scripts, when you set variables you loose them
as you exit the script.
Is it the same or this is a operating system bug ?

The system is SCO Unix System V 3.2

							Thanks,
							Ivan Borzieri

cpcahil@virtech.uucp (Conor P. Cahill) (02/15/91)

borzieri@KING.ICO.OLIVETTI.COM (Ivan Borzieri) writes:

>in the first  c module I call the system function "putenv()" which should
>set a variable in the environment.
>In the second  c module I call the system function "getenv()" to read 
>the value of the previous set variable.

the problem is *PROBABLY* that you are using a local variable in the
call to putenv.  local data is lost when you return from the function 
and thereafter unavailable.  the putenv(3) man page states that you 
shouldn't do this.

The argument to putenv should point to a static data area or a malloc'd 
data area.

All this is predicated on the fact that both modules (the one calling
putenv and the one calling getenv) are called from the same executable.
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

mike (02/16/91)

In an article, KING.ICO.OLIVETTI.COM!borzieri (Ivan Borzieri) writes:
>I wrote two c modules called by a fortran main.
>in the first  c module I call the system function "putenv()" which should
>set a variable in the environment.
>In the second  c module I call the system function "getenv()" to read 
>the value of the previous set variable.
>The value returned by getenv() is NULL, id est, that variable
>doesn't exist.
>Now my question is : is this right ?

I have found putenv() to be particuarly annoying, and wonderfully inconsistent
in implementation.  For example, not all putenv()'s guarantee that the
newly created variable is inherited by child processes.

Try this ditty to see if your putenv() is broken:

#include <stdio.h>

main()
{
	putenv("FOO=BAR");
	printf("FOO=%s\n",(char *)getenv("FOO"));

	if ( fork() )
		wait(NULL);
	else
		execlp("/bin/sh","/bin/sh",NULL);
}

When you run this program, you should see "FOO=BAR" and then a shell
prompt; type "echo $FOO" and you should get "BAR".  Press CTRL-D to jump
out of the subshell.  If you don't get this kind of response, I would
consider your putenv() broken.

If it is broken, then write some subroutines to handle the environment
yourself (_environ is a pointer to an array of pointers to char's that
is the current environment).  Use execle() or execve() to pass your own
table to a child process.  If you need more specific information on
doing this type of thing, send me mail, and I'll help.

>I know that in C-Shell scripts, when you set variables you loose them
>as you exit the script.
>Is it the same or this is a operating system bug ?

Neither.  Is is a "problem" with the way that the putenv() and getenv()
subroutines are implemented.  Shell variables are "lost" because scripts
are executed in subshells. 

Golden rule:  No child process may modify its parent's environment.

Hope this helps.
-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?