summaryrefslogtreecommitdiffstats
path: root/usr/klibc/setenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/klibc/setenv.c')
-rw-r--r--usr/klibc/setenv.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/usr/klibc/setenv.c b/usr/klibc/setenv.c
new file mode 100644
index 0000000..45a7aad
--- /dev/null
+++ b/usr/klibc/setenv.c
@@ -0,0 +1,42 @@
+/*
+ * setenv.c
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "env.h"
+
+int setenv(const char *name, const char *val, int overwrite)
+{
+ const char *z;
+ char *s;
+ size_t l1, l2;
+
+ if (!name || !name[0]) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ l1 = 0;
+ for (z = name; *z; z++) {
+ l1++;
+ if (*z == '=') {
+ errno = EINVAL;
+ return -1;
+ }
+ }
+
+ l2 = strlen(val);
+
+ s = malloc(l1 + l2 + 2);
+ if (!s)
+ return -1;
+
+ memcpy(s, name, l1);
+ s[l1] = '=';
+ memcpy(s + l1 + 1, val, l2 + 1);
+
+ return __put_env(s, l1 + 1, overwrite);
+}