xref: /minix/external/bsd/kyua-testers/dist/env.c (revision ebfedea0)
1 // Copyright 2012 Google 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 are
6 // met:
7 //
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.
16 //
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.
28 
29 #if defined(HAVE_CONFIG_H)
30 #   include "config.h"
31 #endif
32 
33 #include "env.h"
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "error.h"
42 
43 
44 /// Sets an environment variable.
45 ///
46 /// \param name Name of the environment variable to set.
47 /// \param value Value to be set.
48 ///
49 /// \return An error object.
50 kyua_error_t
51 kyua_env_set(const char* name, const char* value)
52 {
53     kyua_error_t error;
54 
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);
59     else
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);
64     if (buffer == NULL)
65         error = kyua_oom_error_new();
66     else {
67         const size_t printed_length = snprintf(buffer, length, "%s=%s", name,
68                                                value);
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",
73                 name, value);
74             free(buffer);
75         } else
76             error = kyua_error_ok();
77     }
78 #else
79 #   error "Don't know how to set an environment variable."
80 #endif
81 
82     return error;
83 }
84 
85 
86 /// Unsets an environment variable.
87 ///
88 /// \param name Name of the environment variable to unset.
89 ///
90 /// \return An error object.
91 kyua_error_t
92 kyua_env_unset(const char* name)
93 {
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);
98     else
99         return kyua_error_ok();
100 #elif defined(HAVE_PUTENV)
101     return kyua_env_set(name, "");
102 #else
103 #   error "Don't know how to unset an environment variable."
104 #endif
105 }
106