3 * $Id: env.c,v 1.3 2001/01/20 11:48:10 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.3 2001/01/20 11:48:10 mdw
34 * Use new @SYM_LEN@ macro for name lengths.
36 * Revision 1.2 2000/06/17 10:39:00 mdw
37 * Add support for arena management.
39 * Revision 1.1 1999/07/26 23:15:57 mdw
40 * Fiddling with environment variables.
44 /*----- Header files ------------------------------------------------------*/
53 /*----- Data structures ---------------------------------------------------*/
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- @env_get@ --- *
64 * Arguments: @sym_table *t@ = pointer to a symbol table
65 * @const char *name@ = pointer to variable name to look up
67 * Returns: Pointer to corresponding value string, or null.
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
74 char *env_get(sym_table *t, const char *name)
76 var *e = sym_find(t, name, -1, 0, 0);
77 return (e ? e->v : 0);
80 /* --- @env_put@ --- *
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
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
97 void env_put(sym_table *t, const char *name, const char *value)
101 /* --- Sort out the mess with `NAME=VALUE' forms --- */
104 size_t eq = strcspn(name, "=");
105 if (name[eq] == '=') {
106 q = x_alloc(t->t.a, eq + 1);
109 value = name + eq + 1;
114 /* --- Read the current value --- */
118 if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
119 x_free(t->t.a, v->v);
124 var *v = sym_find(t, name, -1, sizeof(*v), &found);
126 x_free(t->t.a, v->v);
127 v->v = x_strdup(t->t.a, value);
130 /* --- Tidying --- */
136 /* --- @env_import@ --- *
138 * Arguments: @sym_table *t@ = pointer to a symbol table
139 * @char **env@ = pointer to an environment list
143 * Use: Inserts all of the environment variables listed into a symbol
144 * table for rapid access. Equivalent to a lot of calls to
148 void env_import(sym_table *t, char **env)
156 /* --- @env_export@ --- *
158 * Arguments: @sym_table *t@ = pointer to a symbol table
160 * Returns: A big environment list.
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.
167 char **env_export(sym_table *t)
176 /* --- Work out sizes for everything --- */
178 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
180 sz += SYM_LEN(v) + strlen(v->v) + 1;
183 /* --- Allocate the big chunk of memory --- */
185 env = pp = xmalloc(n * sizeof(char *) + sz);
186 p = (char *)(env + n);
188 /* --- Dump the output in the big chunk of memory --- */
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);
194 memcpy(p, name, nlen); p += nlen;
196 memcpy(p, v->v, vlen); p += vlen;
203 /* --- @env_destroy@ --- *
205 * Arguments: @sym_table *t@ = pointer to symbol table
209 * Use: Destroys all the variables in the symbol table.
212 void env_destroy(sym_table *t)
217 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
218 x_free(t->t.a, v->v);
222 /*----- That's all, folks -------------------------------------------------*/