aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_clear_environ.c
blob: df0c7cee5371b021239a7f2fff471a9075d510b6 (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
62
63
64
65
66
67
68
69
70
71
72
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libexec_clear_environ(struct libexec_command *cmd)
{
	size_t i;
	char **new_env;

	if (!cmd) {
		errno = EINVAL;
		return -1;
	}

	new_env = calloc(1, sizeof(*cmd->environ));
	if (!new_env)
		return -1;

	if (cmd->environ) {
		for (i = 0; cmd->environ[i]; i++)
			free(cmd->environ[i]);
		free(cmd->environ);
	}

	cmd->environ = new_env;
	return 0;
}


#else


static void
check_clear_from_default(void)
{
	struct libexec_command cmd, ref;
	char **old_env;

	cmd = LIBEXEC_COMMAND_INIT;
	memcpy(&ref, &cmd, sizeof(cmd));
	ASSERT_IS_NULL(cmd.environ);

	ASSERT_ZERO(libexec_clear_environ(&cmd));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_IS_NULL(cmd.environ[0]);

	old_env = cmd.environ;
	cmd.environ = NULL;
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
	cmd.environ = old_env;

	libexec_destroy_command(&cmd);
}


int
main(void)
{
	errno = 0;
	ASSERT_EQ_INT(libexec_clear_environ(NULL), -1);
	ASSERT_EQ_INT(errno, EINVAL);

	check_clear_from_default();
	/* XXX test malloc failure */
	/* XXX test that function deallocates */
	return 0;
}


#endif