aboutsummaryrefslogtreecommitdiffstats
path: root/vputenvf.c
blob: 0d66b0c41441daffe2ed34619ec323d28517cc6b (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
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


int
libsimple_vputenvf(const char *fmt, va_list ap)
{
	va_list ap2;
	int n;
	char *s;
	va_copy(ap2, ap);
	n = vsnprintf(NULL, 0, fmt, ap2);
	va_end(ap2);
	if (n < 0)
		return -1;
	s = alloca((size_t)n + 1);
	vsprintf(s, fmt, ap);
	return putenv(s);
}


#else
#include "test.h"

int
main(void)
{
	unsetenv("X");
	assert(!getenv("X"));
	unsetenv("Y");
	assert(!getenv("Y"));

	putenvf("X=xyz");
	assert(!strcmpnul(getenv("X"), "xyz"));
	putenvf("Y=xyz");
	assert(!strcmpnul(getenv("Y"), "xyz"));

	putenvf("X=x%sz", "abc");
	assert(!strcmpnul(getenv("X"), "xabcz"));
	putenvf("Y=x%sz", "abc");
	assert(!strcmpnul(getenv("Y"), "xabcz"));

	putenvf("X=%ix%sz%i", 10, "abc", -11);
	assert(!strcmpnul(getenv("X"), "10xabcz-11"));
	putenvf("Y=%ix%sz%i", 10, "abc", -11);
	assert(!strcmpnul(getenv("Y"), "10xabcz-11"));

	return 0;
}

#endif