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