summaryrefslogtreecommitdiff
path: root/lib/libc/compat/stdlib
diff options
context:
space:
mode:
authortron <tron@NetBSD.org>2010-11-14 18:11:42 +0000
committertron <tron@NetBSD.org>2010-11-14 18:11:42 +0000
commitfbf4aa1699796a9be2edf5319b864af126e3ea87 (patch)
treec3e1a0b86b8b035e29ebcf96f900cd6bb05d95be /lib/libc/compat/stdlib
parentdff67e454b6911f93e556ee11b0f81930b1a6b22 (diff)
Improve and simplify implementation of *env(3) functions:
- Use RB tree to keep track of memory allocated via setenv(3) as suggested by Enami Tsugutomo in private e-mail. This simplifies the code a lot as we no longer need to keep the size of "environ" in sync with an array of allocated environment variables. It also makes it possible to free environment variables in unsetenv(3) if something has changed the order of the "environ" array. - Fix a bug in getenv(3) and getenv_r(3) which would return bogus results e.g. for " getenv("A=B") " if an environment variable "A" with value "B=C" exists. - Clean up the internal functions: - Don't expose the read/write lock for the environment to other parts of "libc". Provide locking functions instead. - Use "bool" to report success or failure. - Use "ssize_t" or "size_t" instead of "int" for indexes. - Provide internal functions with simpler interfaces e.g. don't combine return values and reference arguments. - Don't copy "environ" into an allocated block unless we really need to grow it. Code reviewed by Joerg Sonnenberger and Christos Zoulas, tested by Joerg Sonnenberger and me. These changes also fix problems in zsh 4.3.* and pam_ssh according to Joerg.
Diffstat (limited to 'lib/libc/compat/stdlib')
-rw-r--r--lib/libc/compat/stdlib/compat_unsetenv.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/lib/libc/compat/stdlib/compat_unsetenv.c b/lib/libc/compat/stdlib/compat_unsetenv.c
index 1faef42d546..3c89812c9b4 100644
--- a/lib/libc/compat/stdlib/compat_unsetenv.c
+++ b/lib/libc/compat/stdlib/compat_unsetenv.c
@@ -1,4 +1,4 @@
-/* $NetBSD: compat_unsetenv.c,v 1.2 2010/09/23 17:30:49 christos Exp $ */
+/* $NetBSD: compat_unsetenv.c,v 1.3 2010/11/14 18:11:42 tron Exp $ */
/*
* Copyright (c) 1987, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "from: @(#)setenv.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: compat_unsetenv.c,v 1.2 2010/09/23 17:30:49 christos Exp $");
+__RCSID("$NetBSD: compat_unsetenv.c,v 1.3 2010/11/14 18:11:42 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -54,16 +54,12 @@ __RCSID("$NetBSD: compat_unsetenv.c,v 1.2 2010/09/23 17:30:49 christos Exp $");
__weak_alias(unsetenv,_unsetenv)
#endif
-#ifdef _REENTRANT
-extern rwlock_t __environ_lock;
-#endif
+#include "env.h"
__warn_references(unsetenv,
"warning: reference to compatibility unsetenv();"
" include <stdlib.h> for correct reference")
-extern char **environ;
-
/*
* unsetenv(name) --
* Delete environmental variable "name".
@@ -71,15 +67,21 @@ extern char **environ;
void
unsetenv(const char *name)
{
- char **p;
- int offset;
+ size_t l_name;
_DIAGASSERT(name != NULL);
- rwlock_wrlock(&__environ_lock);
- while (__findenv(name, &offset)) /* if set multiple times */
- for (p = &environ[offset];; ++p)
- if (!(*p = *(p + 1)))
- break;
- rwlock_unlock(&__environ_lock);
+ l_name = strlen(name);
+ if (__writelockenv()) {
+ ssize_t offset;
+
+ while ((offset = __getenvslot(name, l_name, false)) != -1) {
+ char **p;
+ for (p = &environ[offset]; ; ++p) {
+ if ((*p = *(p + 1)) == NULL)
+ break;
+ }
+ }
+ (void)__unlockenv();
+ }
}