If an environment variable might could have changed during program execution, get a fresh copy using getenv(). If you rely Relying on your old variable, you could be left might leave you with incorrect data or a dangling pointer.
...
After a call to setenv(), environment pointers to the old value and copies of the old value will may be incorrect.
| Code Block | ||
|---|---|---|
| ||
char *temp;
char *copy;
if ((temp = getenv("TEST_ENV")) != NULL) {
copy = malloc(strlen(temp) + 1);
if (copy != NULL) {
strcpy(copy, temp);
}
else {
/* handle error condition */
}
}
/* ...program code... */
setenv("TEST_ENV", var, 1);
/* ...program code... */
printf("TEST_ENV: %s\n", temp);
printf("TEST_ENV: %s\n", copy);
|
...