xref: /freebsd/contrib/atf/atf-c/detail/env.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /* Copyright (c) 2007 The NetBSD Foundation, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25 
26 #include "atf-c/detail/env.h"
27 
28 #if defined(HAVE_CONFIG_H)
29 #include "config.h"
30 #endif
31 
32 #include <errno.h>
33 #include <stdlib.h>
34 
35 #include "atf-c/detail/sanity.h"
36 #include "atf-c/detail/text.h"
37 #include "atf-c/error.h"
38 
39 const char *
40 atf_env_get(const char *name)
41 {
42     const char* val = getenv(name);
43     PRE(val != NULL);
44     return val;
45 }
46 
47 const char *
48 atf_env_get_with_default(const char *name, const char *default_value)
49 {
50     const char* val = getenv(name);
51     if (val == NULL)
52         return default_value;
53     else
54         return val;
55 }
56 
57 bool
58 atf_env_has(const char *name)
59 {
60     return getenv(name) != NULL;
61 }
62 
63 atf_error_t
64 atf_env_set(const char *name, const char *val)
65 {
66     atf_error_t err;
67 
68 #if defined(HAVE_SETENV)
69     if (setenv(name, val, 1) == -1)
70         err = atf_libc_error(errno, "Cannot set environment variable "
71                              "'%s' to '%s'", name, val);
72     else
73         err = atf_no_error();
74 #elif defined(HAVE_PUTENV)
75     char *buf;
76 
77     err = atf_text_format(&buf, "%s=%s", name, val);
78     if (!atf_is_error(err)) {
79         if (putenv(buf) == -1)
80             err = atf_libc_error(errno, "Cannot set environment variable "
81                                  "'%s' to '%s'", name, val);
82         free(buf);
83     }
84 #else
85 #   error "Don't know how to set an environment variable."
86 #endif
87 
88     return err;
89 }
90 
91 atf_error_t
92 atf_env_unset(const char *name)
93 {
94     atf_error_t err;
95 
96 #if defined(HAVE_UNSETENV)
97     unsetenv(name);
98     err = atf_no_error();
99 #elif defined(HAVE_PUTENV)
100     char *buf;
101 
102     err = atf_text_format(&buf, "%s=", name);
103     if (!atf_is_error(err)) {
104         if (putenv(buf) == -1)
105             err = atf_libc_error(errno, "Cannot unset environment variable"
106                                  " '%s'", name);
107         free(buf);
108     }
109 #else
110 #   error "Don't know how to unset an environment variable."
111 #endif
112 
113     return err;
114 }
115