summaryrefslogtreecommitdiffstats
path: root/usr/klibc/unsetenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/klibc/unsetenv.c')
-rw-r--r--usr/klibc/unsetenv.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/usr/klibc/unsetenv.c b/usr/klibc/unsetenv.c
new file mode 100644
index 0000000..c5cc95a
--- /dev/null
+++ b/usr/klibc/unsetenv.c
@@ -0,0 +1,44 @@
+/*
+ * unsetenv.c
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "env.h"
+
+int unsetenv(const char *name)
+{
+ size_t len;
+ char **p, *q;
+ const char *z;
+
+ if (!name || !name[0]) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ len = 0;
+ for (z = name; *z; z++) {
+ len++;
+ if (*z == '=') {
+ errno = EINVAL;
+ return -1;
+ }
+ }
+
+ if (!environ)
+ return 0;
+
+ for (p = environ; (q = *p); p++) {
+ if (!strncmp(name, q, len) && q[len] == '=')
+ break;
+ }
+
+ for (; (q = *p); p++) {
+ p[0] = p[1];
+ }
+
+ return 0;
+}