clearenv() is not supported yet.
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]>
---
win32/Kbuild | 1 +
win32/env.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 120 insertions(+), 0 deletions(-)
create mode 100644 win32/env.c
diff --git a/win32/Kbuild b/win32/Kbuild
index 315f808..26b0269 100644
--- a/win32/Kbuild
+++ b/win32/Kbuild
@@ -4,6 +4,7 @@
lib-y:=
+lib-$(CONFIG_TARGET_WIN32) += env.o
lib-$(CONFIG_TARGET_WIN32) += fnmatch.o
lib-$(CONFIG_TARGET_WIN32) += mingw.o
lib-$(CONFIG_TARGET_WIN32) += process.o
diff --git a/win32/env.c b/win32/env.c
new file mode 100644
index 0000000..376ad9d
--- /dev/null
+++ b/win32/env.c
@@ -0,0 +1,119 @@
+#include "libbb.h"
+
+char **copy_environ(const char *const *envp)
+{
+ char **env;
+ int i = 0;
+ while (envp[i])
+ i++;
+ env = xmalloc((i+1)*sizeof(*env));
+ for (i = 0; envp[i]; i++)
+ env[i] = xstrdup(envp[i]);
+ env[i] = NULL;
+ return env;
+}
+
+void free_environ(char **env)
+{
+ int i;
+ for (i = 0; env[i]; i++)
+ free(env[i]);
+ free(env);
+}
+
+static int lookup_env(char **env, const char *name, size_t nmln)
+{
+ int i;
+
+ for (i = 0; env[i]; i++) {
+ if (0 == strncmp(env[i], name, nmln)
+ && '=' == env[i][nmln])
+ /* matches */
+ return i;
+ }
+ return -1;
+}
+
+#undef getenv
+char *mingw_getenv(const char *name)
+{
+ char *result = getenv(name);
+ if (!result && !strcmp(name, "TMPDIR")) {
+ /* on Windows it is TMP and TEMP */
+ result = getenv("TMP");
+ if (!result)
+ result = getenv("TEMP");
+ }
+ return result;
+}
+
+int setenv(const char *name, const char *value, int replace)
+{
+ int out;
+ size_t namelen, valuelen;
+ char *envstr;
+
+ if (!name || !value) return -1;
+ if (!replace) {
+ char *oldval = NULL;
+ oldval = getenv(name);
+ if (oldval) return 0;
+ }
+
+ namelen = strlen(name);
+ valuelen = strlen(value);
+ envstr = malloc((namelen + valuelen + 2));
+ if (!envstr) return -1;
+
+ memcpy(envstr, name, namelen);
+ envstr[namelen] = '=';
+ memcpy(envstr + namelen + 1, value, valuelen);
+ envstr[namelen + valuelen + 1] = 0;
+
+ out = putenv(envstr);
+ /* putenv(3) makes the argument string part of the environment,
+ * and changing that string modifies the environment --- which
+ * means we do not own that storage anymore. Do not free
+ * envstr.
+ */
+
+ return out;
+}
+
+/*
+ * If name contains '=', then sets the variable, otherwise it unsets it
+ */
+char **env_setenv(char **env, const char *name)
+{
+ char *eq = strchrnul(name, '=');
+ int i = lookup_env(env, name, eq-name);
+
+ if (i < 0) {
+ if (*eq) {
+ for (i = 0; env[i]; i++)
+ ;
+ env = xrealloc(env, (i+2)*sizeof(*env));
+ env[i] = xstrdup(name);
+ env[i+1] = NULL;
+ }
+ }
+ else {
+ free(env[i]);
+ if (*eq)
+ env[i] = xstrdup(name);
+ else
+ for (; env[i]; i++)
+ env[i] = env[i+1];
+ }
+ return env;
+}
+
+void unsetenv(const char *env)
+{
+ env_setenv(environ, env);
+}
+
+int clearenv(void)
+{
+ bb_error_msg_and_die("clearenv() is not supported");
+}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox