bug in putenv()!

Guy Harris guy at auspex.UUCP
Sat Feb 18 21:20:49 AEST 1989


>>	char envbuf[100];
>>
>>	sprintf(envbuf,"USERNAME=%s","hi");
>>	putenv(envbuf);

To quote from the SunOS 4.0 PUTENV(3) man page (which probably describes
most "putenv" implementations, since the code hasn't changed much as far
as I know - I think the 4.0 one is just the S5R2 one with a Sun SCCS
header stuck in...):

DESCRIPTION
     'string' points to a string  of  the  form  `name  =  value'
     putenv()  makes  the  value of the environment variable name
     equal to value by altering an existing variable or  creating
     a  new  one.   In  either  case,  the  string  pointed to by
     'string' becomes part of the environment,  so  altering  the
     string  will  change  the  environment.   The  space used by
     'string' is no longer used once a new  string-defining  name
     is passed to putenv.

WARNINGS
     ...

     A potential error is to  call  putenv()  with  an  automatic
     variable  as  the  argument,  then exit the calling function
     while 'string' is still part of the environment.

It sure looks as if you're handing "putenv" an automatic variable in
your example; do you return from the function in which that automatic is
declared before doing another "putenv" to modify "USERNAME"?  If so,
that's probably your problem.

>But it works right if done like this...
>>	putenv("USERNAME=hi");

The argument is static, not automatic, so the string stays around after
the function returns.

>The strange part is it works with numbers when doing a sprintf...
>>	sprintf(envbuf,"USERNUM=%d",13);
>>	putenv(envbuf);

Sheer luck, possibly.



More information about the Comp.unix.wizards mailing list