1 // Copyright 2012 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if defined(HAVE_CONFIG_H)
44 /// Sets an environment variable.
46 /// \param name Name of the environment variable to set.
47 /// \param value Value to be set.
49 /// \return An error object.
51 kyua_env_set(const char* name
, const char* value
)
55 #if defined(HAVE_SETENV)
56 if (setenv(name
, value
, 1) == -1)
57 error
= kyua_libc_error_new(
58 errno
, "Failed to set environment variable %s to %s", name
, value
);
60 error
= kyua_error_ok();
61 #elif defined(HAVE_PUTENV)
62 const size_t length
= strlen(name
) + strlen(value
) + 2;
63 char* buffer
= (char*)malloc(length
);
65 error
= kyua_oom_error_new();
67 const size_t printed_length
= snprintf(buffer
, length
, "%s=%s", name
,
69 assert(length
== printed_length
+ 1);
70 if (putenv(buffer
) == -1) {
71 error
= kyua_libc_error_new(
72 errno
, "Failed to set environment variable %s to %s",
76 error
= kyua_error_ok();
79 # error "Don't know how to set an environment variable."
86 /// Unsets an environment variable.
88 /// \param name Name of the environment variable to unset.
90 /// \return An error object.
92 kyua_env_unset(const char* name
)
94 #if defined(HAVE_UNSETENV)
95 if (unsetenv(name
) == -1)
96 return kyua_libc_error_new(
97 errno
, "Failed to unset environment variable %s", name
);
99 return kyua_error_ok();
100 #elif defined(HAVE_PUTENV)
101 return kyua_env_set(name
, "");
103 # error "Don't know how to unset an environment variable."