summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorscf <scf@FreeBSD.org>2008-08-03 22:47:23 +0000
committerscf <scf@FreeBSD.org>2008-08-03 22:47:23 +0000
commitd2bab1788df2f6bf184574f94195d23111ed9d6b (patch)
treea9ddf45fea1441f2618188f066b3d4243568aa35 /lib
parent79ec5bdbfe7301890cc0634f50e7a58560e2f9a6 (diff)
downloadFreeBSD-src-d2bab1788df2f6bf184574f94195d23111ed9d6b.zip
FreeBSD-src-d2bab1788df2f6bf184574f94195d23111ed9d6b.tar.gz
Restructure and use different variables in the tests that involve
environ[0] to be more obvious that environ is not NULL before environ[0] is tested. Although I believe the previous code worked, this change improves code maintainability. Reviewed by: ache MFC after: 3 days
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdlib/getenv.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c
index 0f84082..2abf7fc 100644
--- a/lib/libc/stdlib/getenv.c
+++ b/lib/libc/stdlib/getenv.c
@@ -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,14 +426,18 @@ getenv(const char *name)
}
/*
- * Find environment variable via environ if no changes have been made
- * via a *env() call or environ has been replaced or cleared 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)
- return (__findenv_environ(name, nameLen));
- else if (environ[0] == NULL)
+ if (environ == NULL || environ[0] == NULL)
return (NULL);
+ else if (envVars == NULL || environ != intEnviron)
+ return (__findenv_environ(name, nameLen));
else {
envNdx = envVarsTotal - 1;
return (__findenv(name, nameLen, &envNdx, true));
@@ -537,10 +537,12 @@ __merge_environ(void)
char *equals;
/*
- * Internally-built environ has been replaced or cleared. clean up
- * everything.
+ * 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 (envVarsTotal > 0 && (environ != intEnviron || environ[0] == NULL)) {
+ if (intEnviron != NULL && (environ != intEnviron || (envActive > 0 &&
+ environ[0] == NULL))) {
/* Deactivate all environment variables. */
if (envActive > 0) {
origEnviron = NULL;
OpenPOWER on IntegriCloud