aboutsummaryrefslogblamecommitdiffstats
path: root/libexec_putenvf.c
blob: 73cd4d20c712e9f4481c3c14c699bcaf4b4439fb (plain) (tree)
1
2
3
4
5
6




                                                         
                                                                                                         
















                                                    
                                   



























                                                                                                                      
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


extern inline int libexec_putenvf(struct libexec_command *, enum libexec_insert_mode, const char *, ...);


#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;

	cmd = LIBEXEC_COMMAND_INIT;
	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