2 * Copyright (c) 1998 Michael Smith.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Manage an environment-like space in which string variables may be stored.
32 * Provide support for some method-like operations for setting/retrieving
33 * variables in order to allow some type strength.
40 static void env_discard(struct env_var *ev);
42 struct env_var *environ = NULL;
45 * Look up (name) and return it's env_var structure.
48 env_getenv(const char *name)
52 for (ev = environ; ev != NULL; ev = ev->ev_next)
53 if (!strcmp(ev->ev_name, name))
61 * If the EV_VOLATILE flag is set, a copy of the variable is made.
62 * If EV_DYNAMIC is set, the the variable has been allocated with
63 * malloc and ownership transferred to the environment.
64 * If (value) is NULL, the variable is set but has no value.
67 env_setenv(const char *name, int flags, const void *value,
68 ev_sethook_t sethook, ev_unsethook_t unsethook)
70 struct env_var *ev, *curr, *last;
72 if ((ev = env_getenv(name)) != NULL) {
74 * If there's a set hook, let it do the work (unless we are working
77 if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
78 return(ev->ev_sethook(ev, flags, value));
82 * New variable; create and sort into list
84 ev = malloc(sizeof(struct env_var));
85 ev->ev_name = strdup(name);
87 /* hooks can only be set when the variable is instantiated */
88 ev->ev_sethook = sethook;
89 ev->ev_unsethook = unsethook;
94 /* Search for the record to insert before */
95 for (last = NULL, curr = environ;
97 last = curr, curr = curr->ev_next) {
99 if (strcmp(ev->ev_name, curr->ev_name) < 0) {
101 curr->ev_prev->ev_next = ev;
106 ev->ev_prev = curr->ev_prev;
121 /* If there is data in the variable, discard it */
122 if (ev->ev_value != NULL)
125 /* If we have a new value, use it */
126 if (flags & EV_VOLATILE) {
127 ev->ev_value = strdup(value);
129 ev->ev_value = (char *)value;
132 /* Keep the flag components that are relevant */
133 ev->ev_flags = flags & (EV_DYNAMIC);
139 getenv(const char *name)
143 /* Set but no value gives empty string */
144 if ((ev = env_getenv(name)) != NULL) {
145 if (ev->ev_value != NULL)
146 return(ev->ev_value);
153 setenv(const char *name, const char *value, int overwrite)
155 /* No guarantees about state, always assume volatile */
156 if (overwrite || (env_getenv(name) == NULL))
157 return(env_setenv(name, EV_VOLATILE, value, NULL, NULL));
162 putenv(const char *string)
167 copy = strdup(string);
168 if ((value = strchr(copy, '=')) != NULL)
170 result = setenv(copy, value, 1);
176 unsetenv(const char *name)
182 if ((ev = env_getenv(name)) == NULL) {
185 if (ev->ev_unsethook != NULL)
186 err = ev->ev_unsethook(ev);
195 env_discard(struct env_var *ev)
198 ev->ev_prev->ev_next = ev->ev_next;
200 ev->ev_next->ev_prev = ev->ev_prev;
202 environ = ev->ev_next;
204 if (ev->ev_flags & EV_DYNAMIC)
210 env_noset(struct env_var *ev, int flags, const void *value)
216 env_nounset(struct env_var *ev)