1748227 Members
4252 Online
108759 Solutions
New Discussion юеВ

getenv

 
SOLVED
Go to solution

getenv

I have problem with getenv since its return NUll for a Environment variable.
I have already found zero for putenv return type.
so why getenv does not return env. variable value.

Thanks,
Subrat
12 REPLIES 12
Peter Godron
Honored Contributor

Re: getenv

Subrat,
could you please include how you are using the putenv/getenv. Please provide list code.

Re: getenv

Hi

MSP_ListParamByEntity(serverName, entityType, entityName
, MSD_MAXLIST, paramElmArray, startParamName, &returnCount);

if(result != MSE_SUCCESS && result != MSE_SUCCESS_END)break;

for(i = 0;i < returnCount;i++)
{
envVar[0] = '\0';
sprintf(envVar,"%s=%s",
paramElmArray[i].param_name,
paramElmArray[i].param_value);

err = putenv(envVar);
if(err != 0)
{
return MSE_ERROR;
}
}
}


for getenv :
char *lic1;

lic1=getenv("NSIM_ACOL");
//if(lic1!= NULL)
printf("\nLIC_PARAM VALUE:%s",lic1);



MSP_ListParamByEntity get the record from databse and then put in enviorment by putenv.

But when i have call getenv just below after for loop .putenv its return error.

Thanks,
subrat
James R. Ferguson
Acclaimed Contributor

Re: getenv

Hi Subrat:

In addition to checking the return value from 'putenv()', you ought to print the value of 'errno'. 'putenv()' can fail with ENOMEM or EILSEQ --- insufficient space to exnpand the environment, or an invalid multibyte character sequence.

Regards!

...JRF...

Re: getenv

Hi,

I have found err=0 for all variable for putenv.

I have one thing notice that ....
only i found last varibale which is put by putenv.for other its given NULL.

I think its overwrite.

Regards,
subrat

A. Clay Stephenson
Acclaimed Contributor

Re: getenv

I don't see where envVar is declared but an extremely common problem is to call putenv() with an auto storage class local variable inside a function. When the variable goes out of scope the environment is no longer defined. The fix is to use a static storage class local variable or a global variable.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: getenv

>Clay: I don't see where envVar is declared

Ah, exactly. putenv(3) says:
the string pointed to by string becomes part of the environment, so altering the string changes the environment.

>The fix is to use a static storage class local variable or a global variable.

In this case since it is in a for-loop, malloc should be done to have unique strings for each.
A. Clay Stephenson
Acclaimed Contributor

Re: getenv

Okay, good point about dynamically allocating the memory -- but it's up to you to make sure there are no memory leaks now --- and that's Chapter Two.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: getenv

... and now that I've thought about it, maybe a single static variable for multiple putenv()'s ain't such a bad idea because the observed behavior of the code would be interesting and point to the correct approach.

If it ain't broke, I can fix that.

Re: getenv

Hi Clay

Yes. I think you are right.
can you give suggestion for how to allows put in environment more than one variable .

Thanks
Subrat