aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_clear_environ.c
diff options
context:
space:
mode:
Diffstat (limited to 'libexec_clear_environ.c')
-rw-r--r--libexec_clear_environ.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/libexec_clear_environ.c b/libexec_clear_environ.c
new file mode 100644
index 0000000..9f09561
--- /dev/null
+++ b/libexec_clear_environ.c
@@ -0,0 +1,72 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+int
+libexec_clear_environ(struct libexec_command *cmd)
+{
+ size_t i;
+ char **new_env;
+
+ if (!cmd) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ new_env = calloc(1, sizeof(*cmd->environ));
+ if (!new_env)
+ return -1;
+
+ if (cmd->environ) {
+ for (i = 0; cmd->environ[i]; i++)
+ free(cmd->environ[i]);
+ free(cmd->environ);
+ }
+
+ cmd->environ = new_env;
+ return 0;
+}
+
+
+#else
+
+
+static void
+check_clear_from_default(void)
+{
+ struct libexec_command cmd, ref;
+ char **old_env;
+
+ libexec_init_command(&cmd);
+ memcpy(&ref, &cmd, sizeof(cmd));
+ ASSERT_IS_NULL(cmd.environ);
+
+ ASSERT_ZERO(libexec_clear_environ(&cmd));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_IS_NULL(cmd.environ[0]);
+
+ old_env = cmd.environ;
+ cmd.environ = NULL;
+ ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
+ cmd.environ = old_env;
+
+ libexec_destroy_command(&cmd);
+}
+
+
+int
+main(void)
+{
+ errno = 0;
+ ASSERT_EQ_INT(libexec_clear_environ(NULL), -1);
+ ASSERT_EQ_INT(errno, EINVAL);
+
+ check_clear_from_default();
+ /* XXX test malloc failure */
+ /* XXX test that function deallocates */
+ return 0;
+}
+
+
+#endif