diff options
Diffstat (limited to 'libexec_putenvf_append.c')
-rw-r--r-- | libexec_putenvf_append.c | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/libexec_putenvf_append.c b/libexec_putenvf_append.c new file mode 100644 index 0000000..8ceb806 --- /dev/null +++ b/libexec_putenvf_append.c @@ -0,0 +1,240 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libexec_putenvf_append(struct libexec_command *cmd, const char *fmt, ...) +{ + int ret; + va_list args; + va_start(args, fmt); + ret = libexec_vputenvf_append(cmd, fmt, args); + va_end(args); + return ret; +} + + +#else + + +int +main(void) +{ + struct libexec_command cmd, ref; + char **env; + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf_append(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_putenvf_append(&cmd, "%s", "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_putenvf_append(&cmd, "%s%s", "A=", "D")); + 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], "A=D"); + ASSERT_IS_NULL(cmd.environ[2]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "A=D")); + 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], "A=D"); + ASSERT_NOT_NULL(cmd.environ[2]); + ASSERT_EQ_STR(cmd.environ[2], "A=D"); + ASSERT_IS_NULL(cmd.environ[3]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "AA%sE", "=")); + 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], "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], "AA=E"); + ASSERT_IS_NULL(cmd.environ[4]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "X")); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[4]); + ASSERT_EQ_STR(cmd.environ[4], "X"); + ASSERT_IS_NULL(cmd.environ[5]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "%s%s%s%s", "B", "B", "=", "F")); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[4]); + ASSERT_EQ_STR(cmd.environ[4], "X"); + ASSERT_NOT_NULL(cmd.environ[5]); + ASSERT_EQ_STR(cmd.environ[5], "BB=F"); + ASSERT_IS_NULL(cmd.environ[6]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "B=%s", "F")); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[4]); + ASSERT_EQ_STR(cmd.environ[4], "X"); + ASSERT_NOT_NULL(cmd.environ[5]); + ASSERT_EQ_STR(cmd.environ[5], "BB=F"); + ASSERT_NOT_NULL(cmd.environ[6]); + ASSERT_EQ_STR(cmd.environ[6], "B=F"); + 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_putenvf_append(&cmd, "%s=%s=%s", "A", "B", "C")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + ASSERT_NOT_NULL(cmd.environ[1]); + ASSERT_EQ_STR(cmd.environ[1], "A=B=C"); + ASSERT_IS_NULL(cmd.environ[2]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "A%sD", "=")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "A=D"); + ASSERT_IS_NULL(cmd.environ[3]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "%s", "A=D")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "A=D"); + ASSERT_NOT_NULL(cmd.environ[3]); + ASSERT_EQ_STR(cmd.environ[3], "A=D"); + ASSERT_IS_NULL(cmd.environ[4]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "%s%s%s", "A", "A=", "E")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "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], "AA=E"); + ASSERT_IS_NULL(cmd.environ[5]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "X")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[5]); + ASSERT_EQ_STR(cmd.environ[5], "X"); + ASSERT_IS_NULL(cmd.environ[6]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "BB=F")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[5]); + ASSERT_EQ_STR(cmd.environ[5], "X"); + ASSERT_NOT_NULL(cmd.environ[6]); + ASSERT_EQ_STR(cmd.environ[6], "BB=F"); + ASSERT_IS_NULL(cmd.environ[7]); + + ASSERT_ZERO(libexec_putenvf_append(&cmd, "B=F")); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_NOT_NULL(cmd.environ[0]); + ASSERT_EQ_STR(cmd.environ[0], "X=0"); + 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], "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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[5]); + ASSERT_EQ_STR(cmd.environ[5], "X"); + ASSERT_NOT_NULL(cmd.environ[6]); + ASSERT_EQ_STR(cmd.environ[6], "BB=F"); + ASSERT_NOT_NULL(cmd.environ[7]); + ASSERT_EQ_STR(cmd.environ[7], "B=F"); + ASSERT_IS_NULL(cmd.environ[8]); + + libexec_destroy_command(&cmd); + environ = env; + return 0; +} + + +#endif |