about summary refs log tree commit diff
path: root/src/env/unsetenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env/unsetenv.c')
-rw-r--r--src/env/unsetenv.c35
1 files changed, 17 insertions, 18 deletions
diff --git a/src/env/unsetenv.c b/src/env/unsetenv.c
index 35693354..8630e2d7 100644
--- a/src/env/unsetenv.c
+++ b/src/env/unsetenv.c
@@ -1,31 +1,30 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include "libc.h"
 
-extern char **__environ;
-extern char **__env_map;
+char *__strchrnul(const char *, int);
+
+static void dummy(char *old, char *new) {}
+weak_alias(dummy, __env_rm_add);
 
 int unsetenv(const char *name)
 {
-	int i, j;
-	size_t l = strlen(name);
-
-	if (!*name || strchr(name, '=')) {
+	size_t l = __strchrnul(name, '=') - name;
+	if (!l || name[l]) {
 		errno = EINVAL;
 		return -1;
 	}
-again:
-	for (i=0; __environ[i] && (memcmp(name, __environ[i], l) || __environ[i][l] != '='); i++);
-	if (__environ[i]) {
-		if (__env_map) {
-			for (j=0; __env_map[j] && __env_map[j] != __environ[i]; j++);
-			free (__env_map[j]);
-			for (; __env_map[j]; j++)
-				__env_map[j] = __env_map[j+1];
-		}
-		for (; __environ[i]; i++)
-			__environ[i] = __environ[i+1];
-		goto again;
+	if (__environ) {
+		char **e = __environ, **eo = e;
+		for (; *e; e++)
+			if (!strncmp(name, *e, l) && l[*e] == '=')
+				__env_rm_add(*e, 0);
+			else if (eo != e)
+				*eo++ = *e;
+			else
+				eo++;
+		if (eo != e) *eo = 0;
 	}
 	return 0;
 }