1275988Sngie/* Copyright (c) 2007 The NetBSD Foundation, Inc. 2240116Smarcel * All rights reserved. 3240116Smarcel * 4240116Smarcel * Redistribution and use in source and binary forms, with or without 5240116Smarcel * modification, are permitted provided that the following conditions 6240116Smarcel * are met: 7240116Smarcel * 1. Redistributions of source code must retain the above copyright 8240116Smarcel * notice, this list of conditions and the following disclaimer. 9240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright 10240116Smarcel * notice, this list of conditions and the following disclaimer in the 11240116Smarcel * documentation and/or other materials provided with the distribution. 12240116Smarcel * 13240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 14240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 18240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24275988Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 25240116Smarcel 26275988Sngie#include "atf-c/detail/env.h" 27275988Sngie 28240116Smarcel#if defined(HAVE_CONFIG_H) 29275988Sngie#include "config.h" 30240116Smarcel#endif 31240116Smarcel 32240116Smarcel#include <errno.h> 33240116Smarcel#include <stdlib.h> 34240116Smarcel 35275988Sngie#include "atf-c/detail/sanity.h" 36275988Sngie#include "atf-c/detail/text.h" 37240116Smarcel#include "atf-c/error.h" 38240116Smarcel 39240116Smarcelconst char * 40240116Smarcelatf_env_get(const char *name) 41240116Smarcel{ 42240116Smarcel const char* val = getenv(name); 43240116Smarcel PRE(val != NULL); 44240116Smarcel return val; 45240116Smarcel} 46240116Smarcel 47275988Sngieconst char * 48275988Sngieatf_env_get_with_default(const char *name, const char *default_value) 49275988Sngie{ 50275988Sngie const char* val = getenv(name); 51275988Sngie if (val == NULL) 52275988Sngie return default_value; 53275988Sngie else 54275988Sngie return val; 55275988Sngie} 56275988Sngie 57240116Smarcelbool 58240116Smarcelatf_env_has(const char *name) 59240116Smarcel{ 60240116Smarcel return getenv(name) != NULL; 61240116Smarcel} 62240116Smarcel 63240116Smarcelatf_error_t 64240116Smarcelatf_env_set(const char *name, const char *val) 65240116Smarcel{ 66240116Smarcel atf_error_t err; 67240116Smarcel 68240116Smarcel#if defined(HAVE_SETENV) 69240116Smarcel if (setenv(name, val, 1) == -1) 70240116Smarcel err = atf_libc_error(errno, "Cannot set environment variable " 71240116Smarcel "'%s' to '%s'", name, val); 72240116Smarcel else 73240116Smarcel err = atf_no_error(); 74240116Smarcel#elif defined(HAVE_PUTENV) 75240116Smarcel char *buf; 76240116Smarcel 77240116Smarcel err = atf_text_format(&buf, "%s=%s", name, val); 78240116Smarcel if (!atf_is_error(err)) { 79240116Smarcel if (putenv(buf) == -1) 80240116Smarcel err = atf_libc_error(errno, "Cannot set environment variable " 81240116Smarcel "'%s' to '%s'", name, val); 82240116Smarcel free(buf); 83240116Smarcel } 84240116Smarcel#else 85240116Smarcel# error "Don't know how to set an environment variable." 86240116Smarcel#endif 87240116Smarcel 88240116Smarcel return err; 89240116Smarcel} 90240116Smarcel 91240116Smarcelatf_error_t 92240116Smarcelatf_env_unset(const char *name) 93240116Smarcel{ 94240116Smarcel atf_error_t err; 95240116Smarcel 96240116Smarcel#if defined(HAVE_UNSETENV) 97240116Smarcel unsetenv(name); 98240116Smarcel err = atf_no_error(); 99240116Smarcel#elif defined(HAVE_PUTENV) 100240116Smarcel char *buf; 101240116Smarcel 102240116Smarcel err = atf_text_format(&buf, "%s=", name); 103240116Smarcel if (!atf_is_error(err)) { 104240116Smarcel if (putenv(buf) == -1) 105240116Smarcel err = atf_libc_error(errno, "Cannot unset environment variable" 106240116Smarcel " '%s'", name); 107240116Smarcel free(buf); 108240116Smarcel } 109240116Smarcel#else 110240116Smarcel# error "Don't know how to unset an environment variable." 111240116Smarcel#endif 112240116Smarcel 113240116Smarcel return err; 114240116Smarcel} 115