aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_setenvf.c
blob: 7589dcab4237748330d68b01c32af50df013d13f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libexec_setenvf(struct libexec_command *cmd, enum libexec_insert_mode how, const char *name, const char *value_fmt, ...)
{
	int ret;
	va_list args;
	va_start(args, value_fmt);
	ret = libexec_vsetenvf(cmd, how, name, value_fmt, args);
	va_end(args);
	return ret;
}


#else


int
main(void)
{
	 /* Correct usage is tested, indirectly, via
	  * libexec_setenvf_append.c,
	  * libexec_setenvf_noclobber.c,
	  * libexec_setenvf_noreplace.c,
	  * libexec_setenvf_prepend.c, and
	  * libexec_setenvf_replace.c */

	struct libexec_command cmd, ref;

	libexec_init_command(&cmd);
	memcpy(&ref, &cmd, sizeof(cmd));

	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf(&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_setenvf(&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_setenvf(&cmd, LIBEXEC_NOCLOBBER, "PATH", "%s:%s", "test", "path"));
	ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "test:path");
	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf(&cmd, LIBEXEC_NOCLOBBER, "PATH", "%s", "x"), -1);
	ASSERT_EQ_INT(errno, 0);
	ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "test:path");
	ASSERT_ZERO(libexec_setenvf(&cmd, LIBEXEC_REPLACE, "PATH", "%s%s", "my", "path"));
	ASSERT_EQ_STR(libexec_getenv(&cmd, "PATH"), "mypath");
	libexec_destroy_command(&cmd);

	return 0;
}


#endif