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 /*
30  * Manage an environment-like space in which string variables may be stored.
31  * Provide support for some method-like operations for setting/retrieving
32  * variables in order to allow some type strength.
33  */
34 
35 #include "stand.h"
36 
37 #include <string.h>
38 
39 struct env_var *environ = NULL;
40 
41 /*
42  * Look up (name) and return it's env_var structure.
43  */
44 struct env_var	*
env_getenv(const char * name)45 env_getenv(const char *name)
46 {
47 	struct env_var	*ev;
48 
49 	for (ev = environ; ev != NULL; ev = ev->ev_next)
50 		if (strcmp(ev->ev_name, name) == 0)
51 			break;
52 	return (ev);
53 }
54 
55 /*
56  * Some notes:
57  *
58  * If the EV_VOLATILE flag is set, a copy of the variable is made.
59  * If EV_DYNAMIC is set, the variable has been allocated with
60  * malloc and ownership transferred to the environment.
61  * If (value) is NULL, the variable is set but has no value.
62  */
63 int
env_setenv(const char * name,int flags,const void * value,ev_sethook_t sethook,ev_unsethook_t unsethook)64 env_setenv(const char *name, int flags, const void *value,
65     ev_sethook_t sethook, ev_unsethook_t unsethook)
66 {
67 	struct env_var	*ev, *curr, *last;
68 
69 	if ((ev = env_getenv(name)) != NULL) {
70 		/*
71 		 * If there's a set hook, let it do the work
72 		 * (unless we are working for one already).
73 		 */
74 		if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
75 			return (ev->ev_sethook(ev, flags, value));
76 
77 		/* If there is data in the variable, discard it. */
78 		if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
79 			free(ev->ev_value);
80 		ev->ev_value = NULL;
81 		ev->ev_flags &= ~EV_DYNAMIC;
82 
83 	} else {
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 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(const char * string)160 putenv(const 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