aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_putenv_prepend.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-05-05 10:24:40 +0200
committerMattias Andrée <maandree@kth.se>2024-05-05 10:24:40 +0200
commit3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e (patch)
treeb7717583b99f29028c85c3423cf43b104dfa8943 /libexec_putenv_prepend.c
downloadlibexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.gz
libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.bz2
libexec-3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--libexec_putenv_prepend.c235
1 files changed, 235 insertions, 0 deletions
diff --git a/libexec_putenv_prepend.c b/libexec_putenv_prepend.c
new file mode 100644
index 0000000..840978d
--- /dev/null
+++ b/libexec_putenv_prepend.c
@@ -0,0 +1,235 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+int
+libexec_putenv_prepend(struct libexec_command *cmd, const char *string)
+{
+ return libexec_putenv(cmd, LIBEXEC_PREPEND, string);
+}
+
+
+#else
+
+
+int
+main(void)
+{
+ struct libexec_command cmd, ref;
+ char **env;
+
+ errno = 0;
+ ASSERT_EQ_INT(libexec_putenv_prepend(NULL, "X=Y"), -1);
+ ASSERT_EQ_INT(errno, EINVAL);
+
+ libexec_init_command(&cmd);
+ memcpy(&ref, &cmd, sizeof(cmd));
+
+ ASSERT_ZERO(libexec_clear_environ(&cmd));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_IS_NULL(cmd.environ[0]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=B=C"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[1]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=D"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[2]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=D"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[3]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "AA=E"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[4]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "X"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "X");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[5]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "BB=F"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "BB=F");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "X");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[5]);
+ ASSERT_EQ_STR(cmd.environ[5], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[6]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "B=F"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "B=F");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "BB=F");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "X");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[5]);
+ ASSERT_EQ_STR(cmd.environ[5], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[6]);
+ ASSERT_EQ_STR(cmd.environ[6], "A=B=C");
+ ASSERT_IS_NULL(cmd.environ[7]);
+
+ env = cmd.environ;
+ cmd.environ = NULL;
+ ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
+ cmd.environ = env;
+
+ libexec_destroy_command(&cmd);
+
+ libexec_init_command(&cmd);
+ env = environ;
+ environ = calloc(2, sizeof(*environ));
+ ASSERT_NOT_NULL(environ);
+ environ[0] = strdup("X=0");
+ ASSERT_NOT_NULL(environ[0]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=B=C"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "X=0");
+ ASSERT_IS_NULL(cmd.environ[2]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=D"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "X=0");
+ ASSERT_IS_NULL(cmd.environ[3]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "A=D"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "X=0");
+ ASSERT_IS_NULL(cmd.environ[4]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "AA=E"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "X=0");
+ ASSERT_IS_NULL(cmd.environ[5]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "X"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "X");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[5]);
+ ASSERT_EQ_STR(cmd.environ[5], "X=0");
+ ASSERT_IS_NULL(cmd.environ[6]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "BB=F"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "BB=F");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "X");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[5]);
+ ASSERT_EQ_STR(cmd.environ[5], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[6]);
+ ASSERT_EQ_STR(cmd.environ[6], "X=0");
+ ASSERT_IS_NULL(cmd.environ[7]);
+
+ ASSERT_ZERO(libexec_putenv_prepend(&cmd, "B=F"));
+ ASSERT_NOT_NULL(cmd.environ);
+ ASSERT_NOT_NULL(cmd.environ[0]);
+ ASSERT_EQ_STR(cmd.environ[0], "B=F");
+ ASSERT_NOT_NULL(cmd.environ[1]);
+ ASSERT_EQ_STR(cmd.environ[1], "BB=F");
+ ASSERT_NOT_NULL(cmd.environ[2]);
+ ASSERT_EQ_STR(cmd.environ[2], "X");
+ ASSERT_NOT_NULL(cmd.environ[3]);
+ ASSERT_EQ_STR(cmd.environ[3], "AA=E");
+ ASSERT_NOT_NULL(cmd.environ[4]);
+ ASSERT_EQ_STR(cmd.environ[4], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[5]);
+ ASSERT_EQ_STR(cmd.environ[5], "A=D");
+ ASSERT_NOT_NULL(cmd.environ[6]);
+ ASSERT_EQ_STR(cmd.environ[6], "A=B=C");
+ ASSERT_NOT_NULL(cmd.environ[7]);
+ ASSERT_EQ_STR(cmd.environ[7], "X=0");
+ ASSERT_IS_NULL(cmd.environ[8]);
+
+ libexec_destroy_command(&cmd);
+ environ = env;
+ return 0;
+}
+
+
+#endif