3 * Fiddling with environment variables
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 /*----- Data structures ---------------------------------------------------*/
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @env_get@ --- *
47 * Arguments: @sym_table *t@ = pointer to a symbol table
48 * @const char *name@ = pointer to variable name to look up
50 * Returns: Pointer to corresponding value string, or null.
52 * Use: Looks up an environment variable in the table and returns its
53 * value. If the variable can't be found, a null pointer is
57 char *env_get(sym_table *t, const char *name)
59 var *e = sym_find(t, name, -1, 0, 0);
60 return (e ? e->v : 0);
63 /* --- @env_put@ --- *
65 * Arguments: @sym_table *t@ = pointer to a symbol table
66 * @const char *name@ = pointer to variable name to set
67 * @const char *value@ = pointer to value string to assign
71 * Use: Assigns a value to a variable. If the @name@ contains an
72 * equals character, then it's assumed to be of the form
73 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
74 * @value@ is null, the variable is deleted. Finally, the
75 * normal case: @name@ is a plain name, and @value@ is a normal
76 * string causes the variable to be assigned the value in the
80 void env_put(sym_table *t, const char *name, const char *value)
84 /* --- Sort out the mess with `NAME=VALUE' forms --- */
87 size_t eq = strcspn(name, "=");
88 if (name[eq] == '=') {
89 q = x_alloc(t->t.a, eq + 1);
92 value = name + eq + 1;
97 /* --- Read the current value --- */
101 if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
102 x_free(t->t.a, v->v);
107 var *v = sym_find(t, name, -1, sizeof(*v), &found);
109 x_free(t->t.a, v->v);
110 v->v = x_strdup(t->t.a, value);
113 /* --- Tidying --- */
119 /* --- @env_import@ --- *
121 * Arguments: @sym_table *t@ = pointer to a symbol table
122 * @char **env@ = pointer to an environment list
126 * Use: Inserts all of the environment variables listed into a symbol
127 * table for rapid access. Equivalent to a lot of calls to
131 void env_import(sym_table *t, char **env)
139 /* --- @env_export@ --- *
141 * Arguments: @sym_table *t@ = pointer to a symbol table
143 * Returns: A big environment list.
145 * Use: Extracts an environment table from a symbol table
146 * representation of an environment. The table and all of the
147 * strings are in one big block allocated from the heap.
150 char **env_export(sym_table *t)
159 /* --- Work out sizes for everything --- */
161 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
163 sz += SYM_LEN(v) + strlen(v->v) + 2;
166 /* --- Allocate the big chunk of memory --- */
168 env = pp = xmalloc(n * sizeof(char *) + sz);
169 p = (char *)(env + n);
171 /* --- Dump the output in the big chunk of memory --- */
173 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
174 const char *name = SYM_NAME(v);
175 size_t nlen = strlen(name), vlen = strlen(v->v);
177 memcpy(p, name, nlen); p += nlen;
179 memcpy(p, v->v, vlen); p += vlen;
186 /* --- @env_destroy@ --- *
188 * Arguments: @sym_table *t@ = pointer to symbol table
192 * Use: Destroys all the variables in the symbol table.
195 void env_destroy(sym_table *t)
200 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
201 x_free(t->t.a, v->v);
205 /*----- That's all, folks -------------------------------------------------*/