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.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/env/unsetenv.c b/src/env/unsetenv.c
new file mode 100644
index 00000000..7493d970
--- /dev/null
+++ b/src/env/unsetenv.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+extern char **__environ;
+extern char **__env_map;
+
+int unsetenv(const char *name)
+{
+	int i, j;
+	size_t l = strlen(name);
+
+	if (!*name || strchr(name, '=')) {
+		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;
+	}
+	return 0;
+}