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