1*5fae793bSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith.
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome 
29199767f8SToomas Soome /*
30199767f8SToomas Soome  * Manage an environment-like space in which string variables may be stored.
31199767f8SToomas Soome  * Provide support for some method-like operations for setting/retrieving
32199767f8SToomas Soome  * variables in order to allow some type strength.
33199767f8SToomas Soome  */
34199767f8SToomas Soome 
35199767f8SToomas Soome #include "stand.h"
36199767f8SToomas Soome 
37199767f8SToomas Soome #include <string.h>
38199767f8SToomas Soome 
39*5fae793bSToomas Soome struct env_var *environ = NULL;
40199767f8SToomas Soome 
41199767f8SToomas Soome /*
42199767f8SToomas Soome  * Look up (name) and return it's env_var structure.
43199767f8SToomas Soome  */
44199767f8SToomas Soome struct env_var	*
env_getenv(const char * name)45199767f8SToomas Soome env_getenv(const char *name)
46199767f8SToomas Soome {
47*5fae793bSToomas Soome 	struct env_var	*ev;
48*5fae793bSToomas Soome 
49*5fae793bSToomas Soome 	for (ev = environ; ev != NULL; ev = ev->ev_next)
50*5fae793bSToomas Soome 		if (strcmp(ev->ev_name, name) == 0)
51*5fae793bSToomas Soome 			break;
52*5fae793bSToomas Soome 	return (ev);
53199767f8SToomas Soome }
54199767f8SToomas Soome 
55199767f8SToomas Soome /*
56199767f8SToomas Soome  * Some notes:
57199767f8SToomas Soome  *
58199767f8SToomas Soome  * If the EV_VOLATILE flag is set, a copy of the variable is made.
59199767f8SToomas Soome  * If EV_DYNAMIC is set, the variable has been allocated with
60199767f8SToomas Soome  * malloc and ownership transferred to the environment.
61199767f8SToomas Soome  * If (value) is NULL, the variable is set but has no value.
62199767f8SToomas Soome  */
63199767f8SToomas Soome int
env_setenv(const char * name,int flags,const void * value,ev_sethook_t sethook,ev_unsethook_t unsethook)64199767f8SToomas Soome env_setenv(const char *name, int flags, const void *value,
65*5fae793bSToomas Soome     ev_sethook_t sethook, ev_unsethook_t unsethook)
66199767f8SToomas Soome {
67*5fae793bSToomas Soome 	struct env_var	*ev, *curr, *last;
68*5fae793bSToomas Soome 
69*5fae793bSToomas Soome 	if ((ev = env_getenv(name)) != NULL) {
70*5fae793bSToomas Soome 		/*
71*5fae793bSToomas Soome 		 * If there's a set hook, let it do the work
72*5fae793bSToomas Soome 		 * (unless we are working for one already).
73*5fae793bSToomas Soome 		 */
74*5fae793bSToomas Soome 		if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
75*5fae793bSToomas Soome 			return (ev->ev_sethook(ev, flags, value));
76*5fae793bSToomas Soome 
77*5fae793bSToomas Soome 		/* If there is data in the variable, discard it. */
78*5fae793bSToomas Soome 		if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
79*5fae793bSToomas Soome 			free(ev->ev_value);
80*5fae793bSToomas Soome 		ev->ev_value = NULL;
81*5fae793bSToomas Soome 		ev->ev_flags &= ~EV_DYNAMIC;
82*5fae793bSToomas Soome 
83*5fae793bSToomas Soome 	} else {
84*5fae793bSToomas Soome 		/*
85*5fae793bSToomas Soome 		 * New variable; create and sort into list
86*5fae793bSToomas Soome 		 */
87*5fae793bSToomas Soome 		ev = malloc(sizeof (struct env_var));
88*5fae793bSToomas Soome 		ev->ev_name = strdup(name);
89*5fae793bSToomas Soome 		ev->ev_value = NULL;
90*5fae793bSToomas Soome 		ev->ev_flags = 0;
91*5fae793bSToomas Soome 		/* hooks can only be set when the variable is instantiated */
92*5fae793bSToomas Soome 		ev->ev_sethook = sethook;
93*5fae793bSToomas Soome 		ev->ev_unsethook = unsethook;
94*5fae793bSToomas Soome 
95*5fae793bSToomas Soome 		/* Sort into list */
96*5fae793bSToomas Soome 		ev->ev_prev = NULL;
97*5fae793bSToomas Soome 		ev->ev_next = NULL;
98*5fae793bSToomas Soome 		/* Search for the record to insert before */
99*5fae793bSToomas Soome 		for (last = NULL, curr = environ; curr != NULL;
100*5fae793bSToomas Soome 		    last = curr, curr = curr->ev_next) {
101*5fae793bSToomas Soome 
102*5fae793bSToomas Soome 			if (strcmp(ev->ev_name, curr->ev_name) < 0) {
103*5fae793bSToomas Soome 				if (curr->ev_prev) {
104*5fae793bSToomas Soome 					curr->ev_prev->ev_next = ev;
105*5fae793bSToomas Soome 				} else {
106*5fae793bSToomas Soome 					environ = ev;
107*5fae793bSToomas Soome 				}
108*5fae793bSToomas Soome 				ev->ev_next = curr;
109*5fae793bSToomas Soome 				ev->ev_prev = curr->ev_prev;
110*5fae793bSToomas Soome 				curr->ev_prev = ev;
111*5fae793bSToomas Soome 				break;
112*5fae793bSToomas Soome 			}
113*5fae793bSToomas Soome 		}
114*5fae793bSToomas Soome 		if (curr == NULL) {
115*5fae793bSToomas Soome 			if (last == NULL) {
116*5fae793bSToomas Soome 				environ = ev;
117*5fae793bSToomas Soome 			} else {
118*5fae793bSToomas Soome 				last->ev_next = ev;
119*5fae793bSToomas Soome 				ev->ev_prev = last;
120*5fae793bSToomas Soome 			}
121199767f8SToomas Soome 		}
122199767f8SToomas Soome 	}
123*5fae793bSToomas Soome 
124*5fae793bSToomas Soome 	/* If we have a new value, use it */
125*5fae793bSToomas Soome 	if (flags & EV_VOLATILE) {
126*5fae793bSToomas Soome 		ev->ev_value = strdup(value);
127*5fae793bSToomas Soome 		ev->ev_flags |= EV_DYNAMIC;
128*5fae793bSToomas Soome 	} else {
129*5fae793bSToomas Soome 		ev->ev_value = (char *)value;
130*5fae793bSToomas Soome 		ev->ev_flags |= flags & EV_DYNAMIC;
131199767f8SToomas Soome 	}
132*5fae793bSToomas Soome 
133*5fae793bSToomas Soome 	return (0);
134199767f8SToomas Soome }
135199767f8SToomas Soome 
136199767f8SToomas Soome char *
getenv(const char * name)137199767f8SToomas Soome getenv(const char *name)
138199767f8SToomas Soome {
139*5fae793bSToomas Soome 	struct env_var	*ev;
140*5fae793bSToomas Soome 
141*5fae793bSToomas Soome 	/* Set but no value gives empty string */
142*5fae793bSToomas Soome 	if ((ev = env_getenv(name)) != NULL) {
143*5fae793bSToomas Soome 		if (ev->ev_value != NULL)
144*5fae793bSToomas Soome 			return (ev->ev_value);
145*5fae793bSToomas Soome 		return ("");
146*5fae793bSToomas Soome 	}
147*5fae793bSToomas Soome 	return (NULL);
148199767f8SToomas Soome }
149199767f8SToomas Soome 
150199767f8SToomas Soome int
setenv(const char * name,const char * value,int overwrite)151199767f8SToomas Soome setenv(const char *name, const char *value, int overwrite)
152199767f8SToomas Soome {
153*5fae793bSToomas Soome 	/* No guarantees about state, always assume volatile */
154*5fae793bSToomas Soome 	if (overwrite || (env_getenv(name) == NULL))
155*5fae793bSToomas Soome 		return (env_setenv(name, EV_VOLATILE, value, NULL, NULL));
156*5fae793bSToomas Soome 	return (0);
157199767f8SToomas Soome }
158199767f8SToomas Soome 
159199767f8SToomas Soome int
putenv(const char * string)160199767f8SToomas Soome putenv(const char *string)
161199767f8SToomas Soome {
162*5fae793bSToomas Soome 	char	*value, *copy;
163*5fae793bSToomas Soome 	int	result;
164*5fae793bSToomas Soome 
165*5fae793bSToomas Soome 	copy = strdup(string);
166*5fae793bSToomas Soome 	if ((value = strchr(copy, '=')) != NULL)
167*5fae793bSToomas Soome 		*(value++) = 0;
168*5fae793bSToomas Soome 	result = setenv(copy, value, 1);
169*5fae793bSToomas Soome 	free(copy);
170*5fae793bSToomas Soome 	return (result);
171199767f8SToomas Soome }
172199767f8SToomas Soome 
173199767f8SToomas Soome int
unsetenv(const char * name)174199767f8SToomas Soome unsetenv(const char *name)
175199767f8SToomas Soome {
176*5fae793bSToomas Soome 	struct env_var	*ev;
177*5fae793bSToomas Soome 	int		err;
178*5fae793bSToomas Soome 
179*5fae793bSToomas Soome 	err = 0;
180*5fae793bSToomas Soome 	if ((ev = env_getenv(name)) == NULL) {
181*5fae793bSToomas Soome 		err = ENOENT;
182*5fae793bSToomas Soome 	} else {
183*5fae793bSToomas Soome 		if (ev->ev_unsethook != NULL)
184*5fae793bSToomas Soome 			err = ev->ev_unsethook(ev);
185*5fae793bSToomas Soome 		if (err == 0) {
186*5fae793bSToomas Soome 			env_discard(ev);
187*5fae793bSToomas Soome 		}
188199767f8SToomas Soome 	}
189*5fae793bSToomas Soome 	return (err);
190199767f8SToomas Soome }
191199767f8SToomas Soome 
192*5fae793bSToomas Soome void
env_discard(struct env_var * ev)193199767f8SToomas Soome env_discard(struct env_var *ev)
194199767f8SToomas Soome {
195*5fae793bSToomas Soome 	if (ev->ev_prev)
196*5fae793bSToomas Soome 		ev->ev_prev->ev_next = ev->ev_next;
197*5fae793bSToomas Soome 	if (ev->ev_next)
198*5fae793bSToomas Soome 		ev->ev_next->ev_prev = ev->ev_prev;
199*5fae793bSToomas Soome 	if (environ == ev)
200*5fae793bSToomas Soome 		environ = ev->ev_next;
201*5fae793bSToomas Soome 	free(ev->ev_name);
202*5fae793bSToomas Soome 	if (ev->ev_value != NULL && (ev->ev_flags & EV_DYNAMIC) != 0)
203*5fae793bSToomas Soome 		free(ev->ev_value);
204*5fae793bSToomas Soome 	free(ev);
205199767f8SToomas Soome }
206199767f8SToomas Soome 
207199767f8SToomas Soome int
env_noset(struct env_var * ev __unused,int flags __unused,const void * value __unused)208199767f8SToomas Soome env_noset(struct env_var *ev __unused, int flags __unused,
209199767f8SToomas Soome     const void *value __unused)
210199767f8SToomas Soome {
211*5fae793bSToomas Soome 	return (EPERM);
212199767f8SToomas Soome }
213199767f8SToomas Soome 
214199767f8SToomas Soome int
env_nounset(struct env_var * ev __unused)215199767f8SToomas Soome env_nounset(struct env_var *ev __unused)
216199767f8SToomas Soome {
217*5fae793bSToomas Soome 	return (EPERM);
218199767f8SToomas Soome }
219