Re: getenv, putenv



Bhawna <bvnbhati@xxxxxxxxx> wrote:
Hi,

I am running a multithreaded application in which I am trying to set
an environment variable using putenv. The string I am using as
argument to putenv was created as

char *str=strdup(<local var>);

1) Don't use an auto variable.
2) The symbol "str" violates the implemenation's
name space if you have also included <strings.h>.


static char *my_env_var;

void foo(void)
{
char local_var[BUFSIZ];

... /* code that sets localvar to some specific value */
my_env_var = strdup(local_var);
putevn(my_env_var);
}

When I try to invoke same environment variable using another thread
within same process using getenv, I am not able to access the value of
env variable.

Can Anyone please let me know, what could be the probable reason for
this problem.

From the GNU man page:

The libc4 and libc5 and glibc 2.1.2 versions conform
to SUSv2: the pointer string given to putenv() is
used. In particular, this string becomes part of the
environment; changing it later will change the
environment. (Thus, it is an error is to call
putenv() with an automatic variable as the argument,
then return from the calling function while string is
still part of the environment.)

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx
.



Relevant Pages

  • Re: getenv, putenv
    ... an environment variable using putenv. ... The string needs to be alive for as it is still ... part of the environment, not some random pointer to it. ...
    (comp.os.linux.development.apps)
  • Re: getenv, putenv
    ... an environment variable using putenv. ... The string needs to be alive for as it is still ... part of the environment, not some random pointer to it. ...
    (comp.os.linux.development.apps)
  • Re: getenv, putenv
    ... an environment variable using putenv. ... The string needs to be alive for as it is still ... part of the environment, not some random pointer to it. ...
    (comp.os.linux.development.apps)
  • Re: getenv returns same string as putenv?
    ... As far as C is concerned, the environment is read-only. ... passed to putenv but I need to be able to free it later or I will have ... deciding who manages the memory could turn out ... Should putenvcopy the provided string, ...
    (comp.lang.c)
  • Re: A more structured approach
    ... For the purpose of a "string compare" routine that'll be ... you might care. ... some assembly language, you don't. ... But I would then pass that as a parameter to my "find environment ...
    (alt.lang.asm)