e19206c2 |
1 | /* -*-c-*- |
2 | * |
412206fd |
3 | * $Id: env.c,v 1.3 2001/01/20 11:48:10 mdw Exp $ |
e19206c2 |
4 | * |
5 | * Fiddling with environment variables |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * mLib is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: env.c,v $ |
412206fd |
33 | * Revision 1.3 2001/01/20 11:48:10 mdw |
34 | * Use new @SYM_LEN@ macro for name lengths. |
35 | * |
20eb516f |
36 | * Revision 1.2 2000/06/17 10:39:00 mdw |
37 | * Add support for arena management. |
38 | * |
e19206c2 |
39 | * Revision 1.1 1999/07/26 23:15:57 mdw |
40 | * Fiddling with environment variables. |
41 | * |
42 | */ |
43 | |
44 | /*----- Header files ------------------------------------------------------*/ |
45 | |
46 | #include <stdio.h> |
47 | #include <stdlib.h> |
48 | #include <string.h> |
49 | |
50 | #include "alloc.h" |
51 | #include "sym.h" |
52 | |
53 | /*----- Data structures ---------------------------------------------------*/ |
54 | |
55 | typedef struct var { |
56 | sym_base _base; |
57 | char *v; |
58 | } var; |
59 | |
60 | /*----- Main code ---------------------------------------------------------*/ |
61 | |
62 | /* --- @env_get@ --- * |
63 | * |
64 | * Arguments: @sym_table *t@ = pointer to a symbol table |
65 | * @const char *name@ = pointer to variable name to look up |
66 | * |
67 | * Returns: Pointer to corresponding value string, or null. |
68 | * |
69 | * Use: Looks up an environment variable in the table and returns its |
70 | * value. If the variable can't be found, a null pointer is |
71 | * returned. |
72 | */ |
73 | |
74 | char *env_get(sym_table *t, const char *name) |
75 | { |
76 | var *e = sym_find(t, name, -1, 0, 0); |
77 | return (e ? e->v : 0); |
78 | } |
79 | |
80 | /* --- @env_put@ --- * |
81 | * |
82 | * Arguments: @sym_table *t@ = pointer to a symbol table |
83 | * @const char *name@ = pointer to variable name to set |
84 | * @const char *value@ = pointer to value string to assign |
85 | * |
86 | * Returns: --- |
87 | * |
88 | * Use: Assigns a value to a variable. If the @name@ contains an |
89 | * equals character, then it's assumed to be of the form |
90 | * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if |
91 | * @value@ is null, the variable is deleted. Finally, the |
92 | * normal case: @name@ is a plain name, and @value@ is a normal |
93 | * string causes the variable to be assigned the value in the |
94 | * way you'd expect. |
95 | */ |
96 | |
97 | void env_put(sym_table *t, const char *name, const char *value) |
98 | { |
99 | char *q = 0; |
100 | |
101 | /* --- Sort out the mess with `NAME=VALUE' forms --- */ |
102 | |
103 | { |
104 | size_t eq = strcspn(name, "="); |
105 | if (name[eq] == '=') { |
20eb516f |
106 | q = x_alloc(t->t.a, eq + 1); |
e19206c2 |
107 | memcpy(q, name, eq); |
108 | q[eq] = 0; |
109 | value = name + eq + 1; |
110 | name = q; |
111 | } |
112 | } |
113 | |
114 | /* --- Read the current value --- */ |
115 | |
116 | if (!value) { |
117 | var *v; |
118 | if ((v = sym_find(t, name, -1, 0, 0)) != 0) { |
20eb516f |
119 | x_free(t->t.a, v->v); |
e19206c2 |
120 | sym_remove(t, v); |
121 | } |
122 | } else { |
123 | unsigned found; |
124 | var *v = sym_find(t, name, -1, sizeof(*v), &found); |
125 | if (found) |
20eb516f |
126 | x_free(t->t.a, v->v); |
127 | v->v = x_strdup(t->t.a, value); |
e19206c2 |
128 | } |
129 | |
130 | /* --- Tidying --- */ |
131 | |
132 | if (q) |
20eb516f |
133 | xfree(q); |
e19206c2 |
134 | } |
135 | |
136 | /* --- @env_import@ --- * |
137 | * |
138 | * Arguments: @sym_table *t@ = pointer to a symbol table |
139 | * @char **env@ = pointer to an environment list |
140 | * |
141 | * Returns: --- |
142 | * |
143 | * Use: Inserts all of the environment variables listed into a symbol |
144 | * table for rapid access. Equivalent to a lot of calls to |
145 | * @env_put@. |
146 | */ |
147 | |
148 | void env_import(sym_table *t, char **env) |
149 | { |
150 | while (*env) { |
151 | env_put(t, *env, 0); |
152 | env++; |
153 | } |
154 | } |
155 | |
156 | /* --- @env_export@ --- * |
157 | * |
158 | * Arguments: @sym_table *t@ = pointer to a symbol table |
159 | * |
160 | * Returns: A big environment list. |
161 | * |
162 | * Use: Extracts an environment table from a symbol table |
163 | * representation of an environment. The table and all of the |
164 | * strings are in one big block allocated from the heap. |
165 | */ |
166 | |
167 | char **env_export(sym_table *t) |
168 | { |
169 | size_t n = 1; |
170 | size_t sz = 0; |
171 | sym_iter i; |
172 | var *v; |
173 | char **env; |
174 | char *p, **pp; |
175 | |
176 | /* --- Work out sizes for everything --- */ |
177 | |
178 | for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) { |
179 | n++; |
412206fd |
180 | sz += SYM_LEN(v) + strlen(v->v) + 1; |
e19206c2 |
181 | } |
182 | |
183 | /* --- Allocate the big chunk of memory --- */ |
184 | |
185 | env = pp = xmalloc(n * sizeof(char *) + sz); |
186 | p = (char *)(env + n); |
187 | |
188 | /* --- Dump the output in the big chunk of memory --- */ |
189 | |
190 | for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) { |
191 | const char *name = SYM_NAME(v); |
192 | size_t nlen = strlen(name), vlen = strlen(v->v); |
193 | *pp++ = p; |
194 | memcpy(p, name, nlen); p += nlen; |
195 | *p++ = '='; |
196 | memcpy(p, v->v, vlen); p += vlen; |
197 | *p++ = 0; |
198 | } |
199 | *pp++ = 0; |
200 | return (env); |
201 | } |
202 | |
203 | /* --- @env_destroy@ --- * |
204 | * |
205 | * Arguments: @sym_table *t@ = pointer to symbol table |
206 | * |
207 | * Returns: --- |
208 | * |
209 | * Use: Destroys all the variables in the symbol table. |
210 | */ |
211 | |
212 | void env_destroy(sym_table *t) |
213 | { |
214 | sym_iter i; |
215 | var *v; |
216 | |
217 | for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) |
20eb516f |
218 | x_free(t->t.a, v->v); |
e19206c2 |
219 | sym_destroy(t); |
220 | } |
221 | |
222 | /*----- That's all, folks -------------------------------------------------*/ |