xref: /freebsd/stand/libsa/environment.c (revision 510e473ff3a6d4620c603c1b9ba06fb2bf5745fd)
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 
27 /*
28  * Manage an environment-like space in which string variables may be stored.
29  * Provide support for some method-like operations for setting/retrieving
30  * variables in order to allow some type strength.
31  */
32 
33 #include "stand.h"
34 
35 #include <string.h>
36 
37 struct env_var	*environ = NULL;
38 
39 /*
40  * Look up (name) and return it's env_var structure.
41  */
42 struct env_var	*
env_getenv(const char * name)43 env_getenv(const char *name)
44 {
45 	struct env_var	*ev;
46 
47 	for (ev = environ; ev != NULL; ev = ev->ev_next)
48 		if (!strcmp(ev->ev_name, name))
49 			break;
50 	return (ev);
51 }
52 
53 /*
54  * Some notes:
55  *
56  * If the EV_VOLATILE flag is set, a copy of the variable is made.
57  * If EV_DYNAMIC is set, the variable has been allocated with
58  * malloc and ownership transferred to the environment.
59  * If (value) is NULL, the variable is set but has no value.
60  */
61 int
env_setenv(const char * name,int flags,const void * value,ev_sethook_t sethook,ev_unsethook_t unsethook)62 env_setenv(const char *name, int flags, const void *value,
63     ev_sethook_t sethook, ev_unsethook_t unsethook)
64 {
65 	struct env_var	*ev, *curr, *last;
66 
67 	if ((ev = env_getenv(name)) != NULL) {
68 		/*
69 		 * If the new value doesn't have NOKENV set, we'll drop the flag
70 		 * if it's set on the entry so that the override propagates
71 		 * correctly.  We do this *before* sending it to the hook in
72 		 * case the hook declines to operate on it (e.g., because the
73 		 * value matches what was already set) -- we would still want
74 		 * the explicitly set value to propagate.
75 		 */
76 		if (!(flags & EV_NOKENV))
77 			ev->ev_flags &= ~EV_NOKENV;
78 
79 		/*
80 		 * If there's a set hook, let it do the work
81 		 * (unless we are working for one already).
82 		 */
83 		if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
84 			return (ev->ev_sethook(ev, flags, value));
85 
86 		/* If there is data in the variable, discard it. */
87 		if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
88 			free(ev->ev_value);
89 		ev->ev_value = NULL;
90 		ev->ev_flags &= ~EV_DYNAMIC;
91 	} else {
92 
93 		/*
94 		 * New variable; create and sort into list
95 		 */
96 		ev = malloc(sizeof(struct env_var));
97 		ev->ev_name = strdup(name);
98 		ev->ev_value = NULL;
99 		ev->ev_flags = 0;
100 		/* hooks can only be set when the variable is instantiated */
101 		ev->ev_sethook = sethook;
102 		ev->ev_unsethook = unsethook;
103 
104 		/* Sort into list */
105 		ev->ev_prev = NULL;
106 		ev->ev_next = NULL;
107 		/* Search for the record to insert before */
108 		for (last = NULL, curr = environ; curr != NULL;
109 		    last = curr, curr = curr->ev_next) {
110 
111 			if (strcmp(ev->ev_name, curr->ev_name) < 0) {
112 				if (curr->ev_prev) {
113 					curr->ev_prev->ev_next = ev;
114 				} else {
115 					environ = ev;
116 				}
117 				ev->ev_next = curr;
118 				ev->ev_prev = curr->ev_prev;
119 				curr->ev_prev = ev;
120 				break;
121 			}
122 		}
123 		if (curr == NULL) {
124 			if (last == NULL) {
125 				environ = ev;
126 			} else {
127 				last->ev_next = ev;
128 				ev->ev_prev = last;
129 			}
130 		}
131 	}
132 
133 	/* If we have a new value, use it */
134 	if (flags & EV_VOLATILE) {
135 		ev->ev_value = strdup(value);
136 		flags |= EV_DYNAMIC;
137 	} else {
138 		ev->ev_value = (char *)value;
139 	}
140 
141 	ev->ev_flags |= flags & (EV_DYNAMIC | EV_NOKENV);
142 
143 	return (0);
144 }
145 
146 /* coverity[ -tainted_string_return_content ] */
147 char *
getenv(const char * name)148 getenv(const char *name)
149 {
150 	struct env_var	*ev;
151 
152 	/* Set but no value gives empty string */
153 	if ((ev = env_getenv(name)) != NULL) {
154 		if (ev->ev_value != NULL)
155 			return (ev->ev_value);
156 		return ("");
157 	}
158 	return (NULL);
159 }
160 
161 int
setenv(const char * name,const char * value,int overwrite)162 setenv(const char *name, const char *value, int overwrite)
163 {
164 	/* No guarantees about state, always assume volatile */
165 	if (overwrite || (env_getenv(name) == NULL))
166 		return (env_setenv(name, EV_VOLATILE, value, NULL, NULL));
167 	return (0);
168 }
169 
170 int
putenv(char * string)171 putenv(char *string)
172 {
173 	char	*value, *copy;
174 	int	result;
175 
176 	copy = strdup(string);
177 	if ((value = strchr(copy, '=')) != NULL)
178 		*(value++) = 0;
179 	result = setenv(copy, value, 1);
180 	free(copy);
181 	return (result);
182 }
183 
184 int
unsetenv(const char * name)185 unsetenv(const char *name)
186 {
187 	struct env_var	*ev;
188 	int		err;
189 
190 	err = 0;
191 	if ((ev = env_getenv(name)) == NULL) {
192 		err = ENOENT;
193 	} else {
194 		if (ev->ev_unsethook != NULL)
195 			err = ev->ev_unsethook(ev);
196 		if (err == 0) {
197 			env_discard(ev);
198 		}
199 	}
200 	return (err);
201 }
202 
203 void
env_discard(struct env_var * ev)204 env_discard(struct env_var *ev)
205 {
206 	if (ev->ev_prev)
207 		ev->ev_prev->ev_next = ev->ev_next;
208 	if (ev->ev_next)
209 		ev->ev_next->ev_prev = ev->ev_prev;
210 	if (environ == ev)
211 		environ = ev->ev_next;
212 	free(ev->ev_name);
213 	if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
214 		free(ev->ev_value);
215 	free(ev);
216 }
217 
218 int
env_noset(struct env_var * ev __unused,int flags __unused,const void * value __unused)219 env_noset(struct env_var *ev __unused, int flags __unused,
220     const void *value __unused)
221 {
222 	return (EPERM);
223 }
224 
225 int
env_nounset(struct env_var * ev __unused)226 env_nounset(struct env_var *ev __unused)
227 {
228 	return (EPERM);
229 }
230