diff options
author | Mattias Andrée <maandree@kth.se> | 2024-05-05 10:24:40 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-05-05 10:24:40 +0200 |
commit | 3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e (patch) | |
tree | b7717583b99f29028c85c3423cf43b104dfa8943 /libexec_setenv.c | |
download | libexec-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 'libexec_setenv.c')
-rw-r--r-- | libexec_setenv.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/libexec_setenv.c b/libexec_setenv.c new file mode 100644 index 0000000..47bc8e9 --- /dev/null +++ b/libexec_setenv.c @@ -0,0 +1,73 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libexec_setenv(struct libexec_command *cmd, enum libexec_insert_mode how, const char *name, const char *value) +{ + int ret; + char *buf; + if (!name || !value || strchr(name, '=')) { + errno = EINVAL; + return -1; + } + buf = malloc(strlen(name) + strlen(value) + 2); + stpcpy(stpcpy(stpcpy(buf, name), "="), value); + ret = libexec_putenv(cmd, how, buf); + free(buf); + return ret; +} + + +#else + + +int +main(void) +{ + /* Correct usage is tested via + * libexec_setenv_append.c, + * libexec_setenv_noclobber.c, + * libexec_setenv_noreplace.c, + * libexec_setenv_prepend.c, and + * libexec_setenv_replace.c */ + + struct libexec_command cmd, ref; + int i; + + libexec_init_command(&cmd); + memcpy(&ref, &cmd, sizeof(cmd)); + + errno = 0; + ASSERT_EQ_INT(libexec_setenv(&cmd, (enum libexec_insert_mode)-1, "A", "B"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + errno = 0; + ASSERT_EQ_INT(libexec_setenv(&cmd, (enum libexec_insert_mode)LIBEXEC_INSERT_MODE__COUNT__, "A", "B"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + for (i = 0; i < LIBEXEC_INSERT_MODE__COUNT__; i++) { + errno = 0; + ASSERT_EQ_INT(libexec_setenv(&cmd, (enum libexec_insert_mode)i, "A=B", "C"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + errno = 0; + ASSERT_EQ_INT(libexec_setenv(&cmd, (enum libexec_insert_mode)i, "A", NULL), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + errno = 0; + ASSERT_EQ_INT(libexec_setenv(&cmd, (enum libexec_insert_mode)i, NULL, "B"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + } + + return 0; +} + + +#endif |