xref: /netbsd/sys/arch/ia64/stand/common/environment.c (revision 7d1220ac)
1*7d1220acSsnj /*	$NetBSD: environment.c,v 1.4 2014/10/18 08:33:25 snj Exp $	*/
2ba7cbe76Scherry 
3ba7cbe76Scherry 
4ba7cbe76Scherry /*
5ba7cbe76Scherry  * Copyright (c) 1998 Michael Smith.
6ba7cbe76Scherry  * All rights reserved.
7ba7cbe76Scherry  *
8ba7cbe76Scherry  * Redistribution and use in source and binary forms, with or without
9ba7cbe76Scherry  * modification, are permitted provided that the following conditions
10ba7cbe76Scherry  * are met:
11ba7cbe76Scherry  * 1. Redistributions of source code must retain the above copyright
12ba7cbe76Scherry  *    notice, this list of conditions and the following disclaimer.
13ba7cbe76Scherry  * 2. Redistributions in binary form must reproduce the above copyright
14ba7cbe76Scherry  *    notice, this list of conditions and the following disclaimer in the
15ba7cbe76Scherry  *    documentation and/or other materials provided with the distribution.
16ba7cbe76Scherry  *
17ba7cbe76Scherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18ba7cbe76Scherry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ba7cbe76Scherry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ba7cbe76Scherry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21ba7cbe76Scherry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ba7cbe76Scherry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ba7cbe76Scherry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ba7cbe76Scherry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ba7cbe76Scherry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ba7cbe76Scherry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ba7cbe76Scherry  * SUCH DAMAGE.
28ba7cbe76Scherry  */
29ba7cbe76Scherry 
30ba7cbe76Scherry #include <sys/cdefs.h>
31ba7cbe76Scherry 
32ba7cbe76Scherry /*
33ba7cbe76Scherry  * Manage an environment-like space in which string variables may be stored.
34ba7cbe76Scherry  * Provide support for some method-like operations for setting/retrieving
35ba7cbe76Scherry  * variables in order to allow some type strength.
36ba7cbe76Scherry  */
37ba7cbe76Scherry 
38ba7cbe76Scherry #include <lib/libsa/stand.h>
3993b81f4cSkiyohara #include <lib/libsa/loadfile.h>
40ba7cbe76Scherry #include <lib/libkern/libkern.h>
41ba7cbe76Scherry 
42ba7cbe76Scherry #include <bootstrap.h>
43ba7cbe76Scherry 
44ba7cbe76Scherry static void	env_discard(struct env_var *ev);
45ba7cbe76Scherry 
46ba7cbe76Scherry struct env_var	*environ = NULL;
47ba7cbe76Scherry 
48ba7cbe76Scherry /*
49*7d1220acSsnj  * Look up (name) and return its env_var structure.
50ba7cbe76Scherry  */
51ba7cbe76Scherry struct env_var	*
env_getenv(const char * name)52ba7cbe76Scherry env_getenv(const char *name)
53ba7cbe76Scherry {
54ba7cbe76Scherry     struct env_var	*ev;
55ba7cbe76Scherry 
56ba7cbe76Scherry     for (ev = environ; ev != NULL; ev = ev->ev_next)
57ba7cbe76Scherry 	if (!strcmp(ev->ev_name, name))
58ba7cbe76Scherry 	    break;
59ba7cbe76Scherry     return(ev);
60ba7cbe76Scherry }
61ba7cbe76Scherry 
62ba7cbe76Scherry /*
63ba7cbe76Scherry  * Some notes:
64ba7cbe76Scherry  *
65ba7cbe76Scherry  * If the EV_VOLATILE flag is set, a copy of the variable is made.
660ae57f90Smbalmer  * If EV_DYNAMIC is set, the variable has been allocated with
67ba7cbe76Scherry  * malloc and ownership transferred to the environment.
68ba7cbe76Scherry  * If (value) is NULL, the variable is set but has no value.
69ba7cbe76Scherry  */
70ba7cbe76Scherry int
env_setenv(const char * name,int flags,const void * value,ev_sethook_t sethook,ev_unsethook_t unsethook)71ba7cbe76Scherry env_setenv(const char *name, int flags, const void *value,
72ba7cbe76Scherry 	   ev_sethook_t sethook, ev_unsethook_t unsethook)
73ba7cbe76Scherry {
74ba7cbe76Scherry     struct env_var	*ev, *curr, *last;
75ba7cbe76Scherry 
76ba7cbe76Scherry     if ((ev = env_getenv(name)) != NULL) {
77ba7cbe76Scherry 	/*
78ba7cbe76Scherry 	 * If there's a set hook, let it do the work (unless we are working
79ba7cbe76Scherry 	 * for one already.
80ba7cbe76Scherry 	 */
81ba7cbe76Scherry 	if ((ev->ev_sethook != NULL) && !(flags & EV_NOHOOK))
82ba7cbe76Scherry 	    return(ev->ev_sethook(ev, flags, value));
83ba7cbe76Scherry     } else {
84ba7cbe76Scherry 
85ba7cbe76Scherry 	/*
86ba7cbe76Scherry 	 * New variable; create and sort into list
87ba7cbe76Scherry 	 */
88ba7cbe76Scherry 	ev = alloc(sizeof(struct env_var));
89ba7cbe76Scherry 	ev->ev_name = strdup(name);
90ba7cbe76Scherry 	ev->ev_value = NULL;
91ba7cbe76Scherry 	/* hooks can only be set when the variable is instantiated */
92ba7cbe76Scherry 	ev->ev_sethook = sethook;
93ba7cbe76Scherry 	ev->ev_unsethook = unsethook;
94ba7cbe76Scherry 
95ba7cbe76Scherry 	/* Sort into list */
96ba7cbe76Scherry 	ev->ev_prev = NULL;
97ba7cbe76Scherry 	ev->ev_next = NULL;
98ba7cbe76Scherry 	/* Search for the record to insert before */
99ba7cbe76Scherry 	for (last = NULL, curr = environ;
100ba7cbe76Scherry 	     curr != NULL;
101ba7cbe76Scherry 	     last = curr, curr = curr->ev_next) {
102ba7cbe76Scherry 
103ba7cbe76Scherry 	    if (strcmp(ev->ev_name, curr->ev_name) < 0) {
104ba7cbe76Scherry 		if (curr->ev_prev) {
105ba7cbe76Scherry 		    curr->ev_prev->ev_next = ev;
106ba7cbe76Scherry 		} else {
107ba7cbe76Scherry 		    environ = ev;
108ba7cbe76Scherry 		}
109ba7cbe76Scherry 		ev->ev_next = curr;
110ba7cbe76Scherry 		ev->ev_prev = curr->ev_prev;
111ba7cbe76Scherry 		curr->ev_prev = ev;
112ba7cbe76Scherry 		break;
113ba7cbe76Scherry 	    }
114ba7cbe76Scherry 	}
115ba7cbe76Scherry 	if (curr == NULL) {
116ba7cbe76Scherry 	    if (last == NULL) {
117ba7cbe76Scherry 		environ = ev;
118ba7cbe76Scherry 	    } else {
119ba7cbe76Scherry 		last->ev_next = ev;
120ba7cbe76Scherry 		ev->ev_prev = last;
121ba7cbe76Scherry 	    }
122ba7cbe76Scherry 	}
123ba7cbe76Scherry     }
124ba7cbe76Scherry 
125ba7cbe76Scherry     /* If there is data in the variable, discard it */
126ba7cbe76Scherry     if (ev->ev_value != NULL)
127ba7cbe76Scherry 	free(ev->ev_value);
128ba7cbe76Scherry 
129ba7cbe76Scherry     /* If we have a new value, use it */
130ba7cbe76Scherry     if (flags & EV_VOLATILE) {
131ba7cbe76Scherry 	ev->ev_value = strdup(value);
132ba7cbe76Scherry     } else {
133ba7cbe76Scherry 	ev->ev_value = (void *) value;
134ba7cbe76Scherry     }
135ba7cbe76Scherry 
136ba7cbe76Scherry     /* Keep the flag components that are relevant */
137ba7cbe76Scherry     ev->ev_flags = flags & (EV_DYNAMIC);
138ba7cbe76Scherry 
139ba7cbe76Scherry     return(0);
140ba7cbe76Scherry }
141ba7cbe76Scherry 
142ba7cbe76Scherry char *
getenv(const char * name)143ba7cbe76Scherry getenv(const char *name)
144ba7cbe76Scherry {
145ba7cbe76Scherry     struct env_var	*ev;
146ba7cbe76Scherry 
147ba7cbe76Scherry     /* Set but no value gives empty string */
148ba7cbe76Scherry     if ((ev = env_getenv(name)) != NULL) {
149ba7cbe76Scherry 	if (ev->ev_value != NULL)
150ba7cbe76Scherry 	    return(ev->ev_value);
151ba7cbe76Scherry 	return("");
152ba7cbe76Scherry     }
153ba7cbe76Scherry     return(NULL);
154ba7cbe76Scherry }
155ba7cbe76Scherry 
156ba7cbe76Scherry int
setenv(const char * name,const char * value,int overwrite)157ba7cbe76Scherry setenv(const char *name, const char *value, int overwrite)
158ba7cbe76Scherry {
159ba7cbe76Scherry     /* No guarantees about state, always assume volatile */
160ba7cbe76Scherry     if (overwrite || (env_getenv(name) == NULL))
161ba7cbe76Scherry 	return(env_setenv(name, EV_VOLATILE, value, NULL, NULL));
162ba7cbe76Scherry     return(0);
163ba7cbe76Scherry }
164ba7cbe76Scherry 
165ba7cbe76Scherry int
putenv(const char * string)166ba7cbe76Scherry putenv(const char *string)
167ba7cbe76Scherry {
168ba7cbe76Scherry     char	*value, *copy;
169ba7cbe76Scherry     int		result;
170ba7cbe76Scherry 
171ba7cbe76Scherry     copy = strdup(string);
172ba7cbe76Scherry     if ((value = strchr(copy, '=')) != NULL)
173ba7cbe76Scherry 	*(value++) = 0;
174ba7cbe76Scherry     result = setenv(copy, value, 1);
175ba7cbe76Scherry     free(copy);
176ba7cbe76Scherry     return(result);
177ba7cbe76Scherry }
178ba7cbe76Scherry 
179ba7cbe76Scherry int
unsetenv(const char * name)180ba7cbe76Scherry unsetenv(const char *name)
181ba7cbe76Scherry {
182ba7cbe76Scherry     struct env_var	*ev;
183ba7cbe76Scherry     int			err;
184ba7cbe76Scherry 
185ba7cbe76Scherry     err = 0;
186ba7cbe76Scherry     if ((ev = env_getenv(name)) == NULL) {
187ba7cbe76Scherry 	err = ENOENT;
188ba7cbe76Scherry     } else {
189ba7cbe76Scherry 	if (ev->ev_unsethook != NULL)
190ba7cbe76Scherry 	    err = ev->ev_unsethook(ev);
191ba7cbe76Scherry 	if (err == 0) {
192ba7cbe76Scherry 	    env_discard(ev);
193ba7cbe76Scherry 	}
194ba7cbe76Scherry     }
195ba7cbe76Scherry     return(err);
196ba7cbe76Scherry }
197ba7cbe76Scherry 
198ba7cbe76Scherry static void
env_discard(struct env_var * ev)199ba7cbe76Scherry env_discard(struct env_var *ev)
200ba7cbe76Scherry {
201ba7cbe76Scherry     if (ev->ev_prev)
202ba7cbe76Scherry 	ev->ev_prev->ev_next = ev->ev_next;
203ba7cbe76Scherry     if (ev->ev_next)
204ba7cbe76Scherry 	ev->ev_next->ev_prev = ev->ev_prev;
205ba7cbe76Scherry     if (environ == ev)
206ba7cbe76Scherry 	environ = ev->ev_next;
207ba7cbe76Scherry     free(ev->ev_name);
208ba7cbe76Scherry     if (ev->ev_flags & EV_DYNAMIC)
209ba7cbe76Scherry 	free(ev->ev_value);
210ba7cbe76Scherry     free(ev);
211ba7cbe76Scherry }
212ba7cbe76Scherry 
213ba7cbe76Scherry int
env_noset(struct env_var * ev,int flags,const void * value)214ba7cbe76Scherry env_noset(struct env_var *ev, int flags, const void *value)
215ba7cbe76Scherry {
216ba7cbe76Scherry     return(EPERM);
217ba7cbe76Scherry }
218ba7cbe76Scherry 
219ba7cbe76Scherry int
env_nounset(struct env_var * ev)220ba7cbe76Scherry env_nounset(struct env_var *ev)
221ba7cbe76Scherry {
222ba7cbe76Scherry     return(EPERM);
223ba7cbe76Scherry }
224