Operating System - Linux
1752815 Members
5742 Online
108789 Solutions
New Discussion юеВ

Re: Setting environment variable with putenv() after call to clearenv()

 
SOLVED
Go to solution
Steve Hosto
Occasional Contributor

Setting environment variable with putenv() after call to clearenv()

Call to putenv() failes to set environment variable only after a previous call to clearenv(). See attachment for C source code file.
2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Setting environment variable with putenv() after call to clearenv()

This is simply bad code and it has nothing to do with clearenv(). The problem is that env is only valid the 1st time it is used. You must add this line:
env = environ;
before every:
while (*env) {

or the while loop is never executed because *env is already NULL -- except for the very first while.
If it ain't broke, I can fix that.
Steve Hosto
Occasional Contributor

Re: Setting environment variable with putenv() after call to clearenv()

Killer and thanks. I had tried the putting the "env = environ;" line after one other while(*env) loop, but not all. Placing that line just before all the while(*env) loops solved the problem. Guess I was under the assumption reseting the env pointer just after the clearenv() call would be all that was needed...