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