4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews//
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// Automated Testing Framework (atf)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews//
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// Copyright (c) 2007 The NetBSD Foundation, Inc.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// All rights reserved.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews//
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// Redistribution and use in source and binary forms, with or without
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// modification, are permitted provided that the following conditions
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// are met:
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// 1. Redistributions of source code must retain the above copyright
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// notice, this list of conditions and the following disclaimer.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// 2. Redistributions in binary form must reproduce the above copyright
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// notice, this list of conditions and the following disclaimer in the
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// documentation and/or other materials provided with the distribution.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews//
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews//
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#if defined(HAVE_CONFIG_H)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "config.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#endif
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsextern "C" {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <errno.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <stdlib.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <string.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <cassert>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "env.hpp"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "exceptions.hpp"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsnamespace impl = tools::env;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#define IMPL_NAME "tools::env"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// ------------------------------------------------------------------------
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// Free functions.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews// ------------------------------------------------------------------------
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsstd::string
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsimpl::get(const std::string& name)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews{
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const char* val = getenv(name.c_str());
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews assert(val != NULL);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews return val;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsbool
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsimpl::has(const std::string& name)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews{
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews return getenv(name.c_str()) != NULL;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsvoid
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsimpl::set(const std::string& name, const std::string& val)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews{
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#if defined(HAVE_SETENV)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (setenv(name.c_str(), val.c_str(), 1) == -1)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews throw tools::system_error(IMPL_NAME "::set",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Cannot set environment variable '" + name +
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "' to '" + val + "'",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews errno);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#elif defined(HAVE_PUTENV)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const std::string buf = name + "=" + val;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (putenv(strdup(buf.c_str())) == -1)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews throw tools::system_error(IMPL_NAME "::set",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Cannot set environment variable '" + name +
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "' to '" + val + "'",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews errno);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#else
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# error "Don't know how to set an environment variable."
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#endif
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsvoid
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsimpl::unset(const std::string& name)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews{
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#if defined(HAVE_UNSETENV)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews unsetenv(name.c_str());
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#elif defined(HAVE_PUTENV)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const std::string buf = name + "=";
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (putenv(strdup(buf.c_str())) == -1)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews throw tools::system_error(IMPL_NAME "::unset",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Cannot unset environment variable '" +
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews name + "'", errno);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#else
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# error "Don't know how to unset an environment variable."
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#endif
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}