xref: /freebsd/stand/libsa/environment.c (revision 3e15b01d)
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 there's a set hook, let it do the work
70 		 * (unless we are working for one already).
71 		 */
72 		if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
73 			return (ev->ev_sethook(ev, flags, value));
74 
75 		/* If there is data in the variable, discard it. */
76 		if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
77 			free(ev->ev_value);
78 		ev->ev_value = NULL;
79 		ev->ev_flags &= ~EV_DYNAMIC;
80 
81 	} else {
82 
83 		/*
84 		 * New variable; create and sort into list
85 		 */
86 		ev = malloc(sizeof(struct env_var));
87 		ev->ev_name = strdup(name);
88 		ev->ev_value = NULL;
89 		ev->ev_flags = 0;
90 		/* hooks can only be set when the variable is instantiated */
91 		ev->ev_sethook = sethook;
92 		ev->ev_unsethook = unsethook;
93 
94 		/* Sort into list */
95 		ev->ev_prev = NULL;
96 		ev->ev_next = NULL;
97 		/* Search for the record to insert before */
98 		for (last = NULL, curr = environ; curr != NULL;
99 		    last = curr, curr = curr->ev_next) {
100 
101 			if (strcmp(ev->ev_name, curr->ev_name) < 0) {
102 				if (curr->ev_prev) {
103 					curr->ev_prev->ev_next = ev;
104 				} else {
105 					environ = ev;
106 				}
107 				ev->ev_next = curr;
108 				ev->ev_prev = curr->ev_prev;
109 				curr->ev_prev = ev;
110 				break;
111 			}
112 		}
113 		if (curr == NULL) {
114 			if (last == NULL) {
115 				environ = ev;
116 			} else {
117 				last->ev_next = ev;
118 				ev->ev_prev = last;
119 			}
120 		}
121 	}
122 
123 	/* If we have a new value, use it */
124 	if (flags & EV_VOLATILE) {
125 		ev->ev_value = strdup(value);
126 		ev->ev_flags |= EV_DYNAMIC;
127 	} else {
128 		ev->ev_value = (char *)value;
129 		ev->ev_flags |= flags & EV_DYNAMIC;
130 	}
131 
132 	return (0);
133 }
134 
135 /* coverity[ -tainted_string_return_content ] */
136 char *
getenv(const char * name)137 getenv(const char *name)
138 {
139 	struct env_var	*ev;
140 
141 	/* Set but no value gives empty string */
142 	if ((ev = env_getenv(name)) != NULL) {
143 		if (ev->ev_value != NULL)
144 			return (ev->ev_value);
145 		return ("");
146 	}
147 	return (NULL);
148 }
149 
150 int
setenv(const char * name,const char * value,int overwrite)151 setenv(const char *name, const char *value, int overwrite)
152 {
153 	/* No guarantees about state, always assume volatile */
154 	if (overwrite || (env_getenv(name) == NULL))
155 		return (env_setenv(name, EV_VOLATILE, value, NULL, NULL));
156 	return (0);
157 }
158 
159 int
putenv(char * string)160 putenv(char *string)
161 {
162 	char	*value, *copy;
163 	int	result;
164 
165 	copy = strdup(string);
166 	if ((value = strchr(copy, '=')) != NULL)
167 		*(value++) = 0;
168 	result = setenv(copy, value, 1);
169 	free(copy);
170 	return (result);
171 }
172 
173 int
unsetenv(const char * name)174 unsetenv(const char *name)
175 {
176 	struct env_var	*ev;
177 	int		err;
178 
179 	err = 0;
180 	if ((ev = env_getenv(name)) == NULL) {
181 		err = ENOENT;
182 	} else {
183 		if (ev->ev_unsethook != NULL)
184 			err = ev->ev_unsethook(ev);
185 		if (err == 0) {
186 			env_discard(ev);
187 		}
188 	}
189 	return (err);
190 }
191 
192 void
env_discard(struct env_var * ev)193 env_discard(struct env_var *ev)
194 {
195 	if (ev->ev_prev)
196 		ev->ev_prev->ev_next = ev->ev_next;
197 	if (ev->ev_next)
198 		ev->ev_next->ev_prev = ev->ev_prev;
199 	if (environ == ev)
200 		environ = ev->ev_next;
201 	free(ev->ev_name);
202 	if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
203 		free(ev->ev_value);
204 	free(ev);
205 }
206 
207 int
env_noset(struct env_var * ev __unused,int flags __unused,const void * value __unused)208 env_noset(struct env_var *ev __unused, int flags __unused,
209     const void *value __unused)
210 {
211 	return (EPERM);
212 }
213 
214 int
env_nounset(struct env_var * ev __unused)215 env_nounset(struct env_var *ev __unused)
216 {
217 	return (EPERM);
218 }
219