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