aboutsummaryrefslogblamecommitdiffstats
path: root/libexec_clear_environ.c
blob: df0c7cee5371b021239a7f2fff471a9075d510b6 (plain) (tree)







































                                                         
                                   






























                                                         
/* 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