| 1 | /* |
| 2 | * Copyright (c) 1998 Michael Smith. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. 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 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 | * SUCH DAMAGE. |
| 25 | * |
| 26 | * $FreeBSD: src/lib/libstand/environment.c,v 1.3.2.1 2000/09/10 01:24:16 ps Exp $ |
| 27 | * $DragonFly: src/lib/libstand/environment.c,v 1.2 2003/06/17 04:26:51 dillon Exp $ |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Manage an environment-like space in which string variables may be stored. |
| 33 | * Provide support for some method-like operations for setting/retrieving |
| 34 | * variables in order to allow some type strength. |
| 35 | */ |
| 36 | |
| 37 | #include "stand.h" |
| 38 | |
| 39 | #include <string.h> |
| 40 | |
| 41 | static void env_discard(struct env_var *ev); |
| 42 | |
| 43 | struct env_var *environ = NULL; |
| 44 | |
| 45 | /* |
| 46 | * Look up (name) and return it's env_var structure. |
| 47 | */ |
| 48 | struct env_var * |
| 49 | env_getenv(const char *name) |
| 50 | { |
| 51 | struct env_var *ev; |
| 52 | |
| 53 | for (ev = environ; ev != NULL; ev = ev->ev_next) |
| 54 | if (!strcmp(ev->ev_name, name)) |
| 55 | break; |
| 56 | return(ev); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Some notes: |
| 61 | * |
| 62 | * If the EV_VOLATILE flag is set, a copy of the variable is made. |
| 63 | * If EV_DYNAMIC is set, the the variable has been allocated with |
| 64 | * malloc and ownership transferred to the environment. |
| 65 | * If (value) is NULL, the variable is set but has no value. |
| 66 | */ |
| 67 | int |
| 68 | env_setenv(const char *name, int flags, const void *value, |
| 69 | ev_sethook_t sethook, ev_unsethook_t unsethook) |
| 70 | { |
| 71 | struct env_var *ev, *curr, *last; |
| 72 | |
| 73 | if ((ev = env_getenv(name)) != NULL) { |
| 74 | /* |
| 75 | * If there's a set hook, let it do the work (unless we are working |
| 76 | * for one already. |
| 77 | */ |
| 78 | if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK)) |
| 79 | return(ev->ev_sethook(ev, flags, value)); |
| 80 | } else { |
| 81 | |
| 82 | /* |
| 83 | * New variable; create and sort into list |
| 84 | */ |
| 85 | ev = malloc(sizeof(struct env_var)); |
| 86 | ev->ev_name = strdup(name); |
| 87 | ev->ev_value = NULL; |
| 88 | /* hooks can only be set when the variable is instantiated */ |
| 89 | ev->ev_sethook = sethook; |
| 90 | ev->ev_unsethook = unsethook; |
| 91 | |
| 92 | /* Sort into list */ |
| 93 | ev->ev_prev = NULL; |
| 94 | ev->ev_next = NULL; |
| 95 | /* Search for the record to insert before */ |
| 96 | for (last = NULL, curr = environ; |
| 97 | curr != NULL; |
| 98 | last = curr, curr = curr->ev_next) { |
| 99 | |
| 100 | if (strcmp(ev->ev_name, curr->ev_name) < 0) { |
| 101 | if (curr->ev_prev) { |
| 102 | curr->ev_prev->ev_next = ev; |
| 103 | } else { |
| 104 | environ = ev; |
| 105 | } |
| 106 | ev->ev_next = curr; |
| 107 | ev->ev_prev = curr->ev_prev; |
| 108 | curr->ev_prev = ev; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | if (curr == NULL) { |
| 113 | if (last == NULL) { |
| 114 | environ = ev; |
| 115 | } else { |
| 116 | last->ev_next = ev; |
| 117 | ev->ev_prev = last; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* If there is data in the variable, discard it */ |
| 123 | if (ev->ev_value != NULL) |
| 124 | free(ev->ev_value); |
| 125 | |
| 126 | /* If we have a new value, use it */ |
| 127 | if (flags & EV_VOLATILE) { |
| 128 | ev->ev_value = strdup(value); |
| 129 | } else { |
| 130 | ev->ev_value = (char *)value; |
| 131 | } |
| 132 | |
| 133 | /* Keep the flag components that are relevant */ |
| 134 | ev->ev_flags = flags & (EV_DYNAMIC); |
| 135 | |
| 136 | return(0); |
| 137 | } |
| 138 | |
| 139 | char * |
| 140 | getenv(const char *name) |
| 141 | { |
| 142 | struct env_var *ev; |
| 143 | |
| 144 | /* Set but no value gives empty string */ |
| 145 | if ((ev = env_getenv(name)) != NULL) { |
| 146 | if (ev->ev_value != NULL) |
| 147 | return(ev->ev_value); |
| 148 | return(""); |
| 149 | } |
| 150 | return(NULL); |
| 151 | } |
| 152 | |
| 153 | int |
| 154 | setenv(const char *name, const char *value, int overwrite) |
| 155 | { |
| 156 | /* No guarantees about state, always assume volatile */ |
| 157 | if (overwrite || (env_getenv(name) == NULL)) |
| 158 | return(env_setenv(name, EV_VOLATILE, value, NULL, NULL)); |
| 159 | return(0); |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | putenv(const char *string) |
| 164 | { |
| 165 | char *value, *copy; |
| 166 | int result; |
| 167 | |
| 168 | copy = strdup(string); |
| 169 | if ((value = strchr(copy, '=')) != NULL) |
| 170 | *(value++) = 0; |
| 171 | result = setenv(copy, value, 1); |
| 172 | free(copy); |
| 173 | return(result); |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | unsetenv(const char *name) |
| 178 | { |
| 179 | struct env_var *ev; |
| 180 | int err; |
| 181 | |
| 182 | err = 0; |
| 183 | if ((ev = env_getenv(name)) == NULL) { |
| 184 | err = ENOENT; |
| 185 | } else { |
| 186 | if (ev->ev_unsethook != NULL) |
| 187 | err = ev->ev_unsethook(ev); |
| 188 | if (err == 0) { |
| 189 | env_discard(ev); |
| 190 | } |
| 191 | } |
| 192 | return(err); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | env_discard(struct env_var *ev) |
| 197 | { |
| 198 | if (ev->ev_prev) |
| 199 | ev->ev_prev->ev_next = ev->ev_next; |
| 200 | if (ev->ev_next) |
| 201 | ev->ev_next->ev_prev = ev->ev_prev; |
| 202 | if (environ == ev) |
| 203 | environ = ev->ev_next; |
| 204 | free(ev->ev_name); |
| 205 | if (ev->ev_flags & EV_DYNAMIC) |
| 206 | free(ev->ev_value); |
| 207 | free(ev); |
| 208 | } |
| 209 | |
| 210 | int |
| 211 | env_noset(struct env_var *ev, int flags, const void *value) |
| 212 | { |
| 213 | return(EPERM); |
| 214 | } |
| 215 | |
| 216 | int |
| 217 | env_nounset(struct env_var *ev) |
| 218 | { |
| 219 | return(EPERM); |
| 220 | } |