From 3dfd6c11ed5599ab8baf4a6114f4ccb34150de6e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 5 May 2024 10:24:40 +0200 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libexec_putenvf_noclobber.c | 180 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 libexec_putenvf_noclobber.c (limited to 'libexec_putenvf_noclobber.c') diff --git a/libexec_putenvf_noclobber.c b/libexec_putenvf_noclobber.c new file mode 100644 index 0000000..988e381 --- /dev/null +++ b/libexec_putenvf_noclobber.c @@ -0,0 +1,180 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libexec_putenvf_noclobber(struct libexec_command *cmd, const char *fmt, ...) +{ + int ret; + va_list args; + va_start(args, fmt); + ret = libexec_vputenvf_noclobber(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_noclobber(NULL, "X=Y"), -1); + ASSERT_EQ_INT(errno, EINVAL); + + libexec_init_command(&cmd); + memcpy(&ref, &cmd, sizeof(cmd)); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf_noclobber(&cmd, NULL), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf_noclobber(&cmd, "%s", "X"), -1); + ASSERT_EQ_INT(errno, EINVAL); + ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd))); + + ASSERT_ZERO(libexec_clear_environ(&cmd)); + ASSERT_NOT_NULL(cmd.environ); + ASSERT_IS_NULL(cmd.environ[0]); + + ASSERT_ZERO(libexec_putenvf_noclobber(&cmd, "%s%s%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_noclobber(&cmd, "%s=%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]); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf_noclobber(&cmd, "%s", "A=D"), -1); + ASSERT_EQ_INT(errno, 0); + 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_noclobber(&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], "AA=E"); + ASSERT_IS_NULL(cmd.environ[2]); + + ASSERT_ZERO(libexec_putenvf_noclobber(&cmd, "%sB=F", "B")); + 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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[2]); + ASSERT_EQ_STR(cmd.environ[2], "BB=F"); + ASSERT_IS_NULL(cmd.environ[3]); + + ASSERT_ZERO(libexec_putenvf_noclobber(&cmd, "%sB=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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[2]); + ASSERT_EQ_STR(cmd.environ[2], "BB=F"); + ASSERT_NOT_NULL(cmd.environ[3]); + ASSERT_EQ_STR(cmd.environ[3], "B=F"); + ASSERT_IS_NULL(cmd.environ[4]); + + 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_noclobber(&cmd, "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_noclobber(&cmd, "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]); + + errno = 0; + ASSERT_EQ_INT(libexec_putenvf_noclobber(&cmd, "A=D"), -1); + ASSERT_EQ_INT(errno, 0); + 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_noclobber(&cmd, "AA=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], "AA=E"); + ASSERT_IS_NULL(cmd.environ[3]); + + ASSERT_ZERO(libexec_putenvf_noclobber(&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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[3]); + ASSERT_EQ_STR(cmd.environ[3], "BB=F"); + ASSERT_IS_NULL(cmd.environ[4]); + + ASSERT_ZERO(libexec_putenvf_noclobber(&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], "AA=E"); + ASSERT_NOT_NULL(cmd.environ[3]); + ASSERT_EQ_STR(cmd.environ[3], "BB=F"); + ASSERT_NOT_NULL(cmd.environ[4]); + ASSERT_EQ_STR(cmd.environ[4], "B=F"); + ASSERT_IS_NULL(cmd.environ[5]); + + libexec_destroy_command(&cmd); + environ = env; + return 0; +} + + +#endif -- cgit v1.2.3-70-g09d2