chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / sys / env.c
1 /* -*-c-*-
2  *
3  * Fiddling with environment variables
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "alloc.h"
34 #include "sym.h"
35
36 /*----- Data structures ---------------------------------------------------*/
37
38 typedef struct var {
39   sym_base _base;
40   char *v;
41 } var;
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @env_get@ --- *
46  *
47  * Arguments:   @sym_table *t@ = pointer to a symbol table
48  *              @const char *name@ = pointer to variable name to look up
49  *
50  * Returns:     Pointer to corresponding value string, or null.
51  *
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
54  *              returned.
55  */
56
57 char *env_get(sym_table *t, const char *name)
58 {
59   var *e = sym_find(t, name, -1, 0, 0);
60   return (e ? e->v : 0);
61 }
62
63 /* --- @env_put@ --- *
64  *
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
68  *
69  * Returns:     ---
70  *
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
77  *              way you'd expect.
78  */
79
80 void env_put(sym_table *t, const char *name, const char *value)
81 {
82   char *q = 0;
83
84   /* --- Sort out the mess with `NAME=VALUE' forms --- */
85
86   {
87     size_t eq = strcspn(name, "=");
88     if (name[eq] == '=') {
89       q = x_alloc(t->t.a, eq + 1);
90       memcpy(q, name, eq);
91       q[eq] = 0;
92       value = name + eq + 1;
93       name = q;
94     }
95   }
96
97   /* --- Read the current value --- */
98
99   if (!value) {
100     var *v;
101     if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
102       x_free(t->t.a, v->v);
103       sym_remove(t, v);
104     }
105   } else {
106     unsigned found;
107     var *v = sym_find(t, name, -1, sizeof(*v), &found);
108     if (found)
109       x_free(t->t.a, v->v);
110     v->v = x_strdup(t->t.a, value);
111   }
112
113   /* --- Tidying --- */
114
115   if (q)
116     xfree(q);
117 }
118
119 /* --- @env_import@ --- *
120  *
121  * Arguments:   @sym_table *t@ = pointer to a symbol table
122  *              @char **env@ = pointer to an environment list
123  *
124  * Returns:     ---
125  *
126  * Use:         Inserts all of the environment variables listed into a symbol
127  *              table for rapid access.  Equivalent to a lot of calls to
128  *              @env_put@.
129  */
130
131 void env_import(sym_table *t, char **env)
132 {
133   while (*env) {
134     env_put(t, *env, 0);
135     env++;
136   }
137 }
138
139 /* --- @env_export@ --- *
140  *
141  * Arguments:   @sym_table *t@ = pointer to a symbol table
142  *
143  * Returns:     A big environment list.
144  *
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.
148  */
149
150 char **env_export(sym_table *t)
151 {
152   size_t n = 1;
153   size_t sz = 0;
154   sym_iter i;
155   var *v;
156   char **env;
157   char *p, **pp;
158
159   /* --- Work out sizes for everything --- */
160
161   for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
162     n++;
163     sz += SYM_LEN(v) + strlen(v->v) + 2;
164   }
165
166   /* --- Allocate the big chunk of memory --- */
167
168   env = pp = xmalloc(n * sizeof(char *) + sz);
169   p = (char *)(env + n);
170
171   /* --- Dump the output in the big chunk of memory --- */
172
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);
176     *pp++ = p;
177     memcpy(p, name, nlen); p += nlen;
178     *p++ = '=';
179     memcpy(p, v->v, vlen); p += vlen;
180     *p++ = 0;
181   }
182   *pp++ = 0;
183   return (env);
184 }
185
186 /* --- @env_destroy@ --- *
187  *
188  * Arguments:   @sym_table *t@ = pointer to symbol table
189  *
190  * Returns:     ---
191  *
192  * Use:         Destroys all the variables in the symbol table.
193  */
194
195 void env_destroy(sym_table *t)
196 {
197   sym_iter i;
198   var *v;
199
200   for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
201     x_free(t->t.a, v->v);
202   sym_destroy(t);
203 }
204
205 /*----- That's all, folks -------------------------------------------------*/