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_putenvf.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_putenvf.c')
-rw-r--r-- | libexec_putenvf.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/libexec_putenvf.c b/libexec_putenvf.c new file mode 100644 index 0000000..7729fc4 --- /dev/null +++ b/libexec_putenvf.c @@ -0,0 +1,61 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libexec_putenvf(struct libexec_command *cmd, enum libexec_insert_mode how, const char *fmt, ...) +{ + int ret; + va_list args; + va_start(args, fmt); + ret = libexec_vputenvf(cmd, how, fmt, args); + va_end(args); + return ret; +} + + +#else + + +int +main(void) +{ + /* Correct usage is tested, indirectly, via + * libexec_putenvf_append.c, + * libexec_putenvf_noclobber.c, + * libexec_putenvf_noreplace.c, + * libexec_putenvf_prepend.c, and + * libexec_putenvf_replace.c */ + + struct libexec_command cmd, ref; + + libexec_init_command(&cmd); + memcpy(&ref, &cmd, sizeof(cmd)); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf(&cmd, (enum libexec_insert_mode)-1, "A=%s", "B"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf(&cmd, (enum libexec_insert_mode)LIBEXEC_INSERT_MODE__COUNT__, "A=%s", "B"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + libexec_clear_environ(&cmd); + ASSERT_ZERO(libexec_putenvf(&cmd, LIBEXEC_NOCLOBBER, "PATH=%s:%s", "test", "path")); + ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "test:path"); + errno = 0; + ASSERT_EQ_INT(libexec_putenvf(&cmd, LIBEXEC_NOCLOBBER, "PATH=%s", "x"), -1); + ASSERT_EQ_INT(errno, 0); + ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "test:path"); + ASSERT_ZERO(libexec_putenvf(&cmd, LIBEXEC_REPLACE, "PATH=%s%s", "my", "path")); + ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "mypath"); + libexec_destroy_command(&cmd); + + return 0; +} + + +#endif |