diff options
author | Sean Farley <scf@FreeBSD.org> | 2008-08-08 00:49:28 +0000 |
---|---|---|
committer | Sean Farley <scf@FreeBSD.org> | 2008-08-08 00:49:28 +0000 |
commit | a18bcf9339f93eebff55e2caa53f70a3800009f3 (patch) | |
tree | efc9eb44223393a28b63564973fdfc56869cd0ae | |
parent | a52f73e342c910fcf951f4c28a062c4e57021462 (diff) |
Notes
-rw-r--r-- | lib/libc/stdlib/getenv.c | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 74595b2eae47f..2abf7fc7b01ee 100644 --- a/lib/libc/stdlib/getenv.c +++ b/lib/libc/stdlib/getenv.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> + * Copyright (c) 2007-2008 Sean C. Farley <scf@FreeBSD.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -190,10 +190,6 @@ __findenv_environ(const char *name, size_t nameLen) { int envNdx; - /* Check for non-existant environment. */ - if (environ == NULL) - return (NULL); - /* Find variable within environ. */ for (envNdx = 0; environ[envNdx] != NULL; envNdx++) if (strncmpeq(environ[envNdx], name, nameLen)) @@ -430,11 +426,17 @@ getenv(const char *name) } /* - * Find environment variable via environ if no changes have been made - * via a *env() call or environ has been replaced by a running program, - * otherwise, use the rebuilt environment. + * An empty environment (environ or its first value) regardless if + * environ has been copied before will return a NULL. + * + * If the environment is not empty, find an environment variable via + * environ if environ has not been copied via an *env() call or been + * replaced by a running program, otherwise, use the rebuilt + * environment. */ - if (envVars == NULL || environ != intEnviron) + if (environ == NULL || environ[0] == NULL) + return (NULL); + else if (envVars == NULL || environ != intEnviron) return (__findenv_environ(name, nameLen)); else { envNdx = envVarsTotal - 1; @@ -525,8 +527,8 @@ __setenv(const char *name, size_t nameLen, const char *value, int overwrite) /* * If the program attempts to replace the array of environment variables - * (environ) environ, then deactivate all variables and merge in the new list - * from environ. + * (environ) environ or sets the first varible to NULL, then deactivate all + * variables and merge in the new list from environ. */ static int __merge_environ(void) @@ -534,8 +536,13 @@ __merge_environ(void) char **env; char *equals; - /* environ has been replaced. clean up everything. */ - if (envVarsTotal > 0 && environ != intEnviron) { + /* + * Internally-built environ has been replaced or cleared (detected by + * using the count of active variables against a NULL as the first value + * in environ). Clean up everything. + */ + if (intEnviron != NULL && (environ != intEnviron || (envActive > 0 && + environ[0] == NULL))) { /* Deactivate all environment variables. */ if (envActive > 0) { origEnviron = NULL; |