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