Info | ||
---|---|---|
| ||
Perhaps this might be more appropriate: ENV31-C. Use environ instead of envp. |
Under many UNIX systems Under many hosted environments it is possible to access the environment through a modified form of main
:
...
In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program.
However, any environment variables added using the setenv()
or putenv()
functions might cause environment memory to be reallocated, leaving envp
pointing to the wrong location. To illustrate:
Code Block |
---|
extern char **environ;
int main(int argc, char **argv, char **envp) {
printf("environ: %p\n", environ);
printf("envp: %p\n", envp);
putenv("MY_NEW_VAR=new_value", 1);
printf("--Added MY_NEW_VAR--\n");
printf("environ: %p\n", environ);
printf("envp: %p\n", envp);
}
|
Yields:
Code Block |
---|
% ./envp-environ
environ: 0xbf8656ec
envp: 0xbf8656ec
--Added MY_NEW_VAR--
environ: 0x804a008
envp: 0xbf8656ec
%
|
will not show up in the envp
array. If you need to directly access or manipulate the environment, it is safer to use environ
.
...