3 * $Id: env.c,v 1.4 2001/01/25 21:14:26 mdw Exp $
5 * Fiddling with environment variables
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2001/01/25 21:14:26 mdw
34 * Nowadays, @SYM_LEN@ doesn't include the terminating null, so revise the
35 * buffer-usage calculation.
37 * Revision 1.3 2001/01/20 11:48:10 mdw
38 * Use new @SYM_LEN@ macro for name lengths.
40 * Revision 1.2 2000/06/17 10:39:00 mdw
41 * Add support for arena management.
43 * Revision 1.1 1999/07/26 23:15:57 mdw
44 * Fiddling with environment variables.
48 /*----- Header files ------------------------------------------------------*/
57 /*----- Data structures ---------------------------------------------------*/
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @env_get@ --- *
68 * Arguments: @sym_table *t@ = pointer to a symbol table
69 * @const char *name@ = pointer to variable name to look up
71 * Returns: Pointer to corresponding value string, or null.
73 * Use: Looks up an environment variable in the table and returns its
74 * value. If the variable can't be found, a null pointer is
78 char *env_get(sym_table *t, const char *name)
80 var *e = sym_find(t, name, -1, 0, 0);
81 return (e ? e->v : 0);
84 /* --- @env_put@ --- *
86 * Arguments: @sym_table *t@ = pointer to a symbol table
87 * @const char *name@ = pointer to variable name to set
88 * @const char *value@ = pointer to value string to assign
92 * Use: Assigns a value to a variable. If the @name@ contains an
93 * equals character, then it's assumed to be of the form
94 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
95 * @value@ is null, the variable is deleted. Finally, the
96 * normal case: @name@ is a plain name, and @value@ is a normal
97 * string causes the variable to be assigned the value in the
101 void env_put(sym_table *t, const char *name, const char *value)
105 /* --- Sort out the mess with `NAME=VALUE' forms --- */
108 size_t eq = strcspn(name, "=");
109 if (name[eq] == '=') {
110 q = x_alloc(t->t.a, eq + 1);
113 value = name + eq + 1;
118 /* --- Read the current value --- */
122 if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
123 x_free(t->t.a, v->v);
128 var *v = sym_find(t, name, -1, sizeof(*v), &found);
130 x_free(t->t.a, v->v);
131 v->v = x_strdup(t->t.a, value);
134 /* --- Tidying --- */
140 /* --- @env_import@ --- *
142 * Arguments: @sym_table *t@ = pointer to a symbol table
143 * @char **env@ = pointer to an environment list
147 * Use: Inserts all of the environment variables listed into a symbol
148 * table for rapid access. Equivalent to a lot of calls to
152 void env_import(sym_table *t, char **env)
160 /* --- @env_export@ --- *
162 * Arguments: @sym_table *t@ = pointer to a symbol table
164 * Returns: A big environment list.
166 * Use: Extracts an environment table from a symbol table
167 * representation of an environment. The table and all of the
168 * strings are in one big block allocated from the heap.
171 char **env_export(sym_table *t)
180 /* --- Work out sizes for everything --- */
182 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
184 sz += SYM_LEN(v) + strlen(v->v) + 2;
187 /* --- Allocate the big chunk of memory --- */
189 env = pp = xmalloc(n * sizeof(char *) + sz);
190 p = (char *)(env + n);
192 /* --- Dump the output in the big chunk of memory --- */
194 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
195 const char *name = SYM_NAME(v);
196 size_t nlen = strlen(name), vlen = strlen(v->v);
198 memcpy(p, name, nlen); p += nlen;
200 memcpy(p, v->v, vlen); p += vlen;
207 /* --- @env_destroy@ --- *
209 * Arguments: @sym_table *t@ = pointer to symbol table
213 * Use: Destroys all the variables in the symbol table.
216 void env_destroy(sym_table *t)
221 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
222 x_free(t->t.a, v->v);
226 /*----- That's all, folks -------------------------------------------------*/