chiark / gitweb /
Distribute unihash manpage.
[mLib] / env.c
CommitLineData
e19206c2 1/* -*-c-*-
2 *
f41204b3 3 * $Id: env.c,v 1.4 2001/01/25 21:14:26 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 $
f41204b3 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.
36 *
412206fd 37 * Revision 1.3 2001/01/20 11:48:10 mdw
38 * Use new @SYM_LEN@ macro for name lengths.
39 *
20eb516f 40 * Revision 1.2 2000/06/17 10:39:00 mdw
41 * Add support for arena management.
42 *
e19206c2 43 * Revision 1.1 1999/07/26 23:15:57 mdw
44 * Fiddling with environment variables.
45 *
46 */
47
48/*----- Header files ------------------------------------------------------*/
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
54#include "alloc.h"
55#include "sym.h"
56
57/*----- Data structures ---------------------------------------------------*/
58
59typedef struct var {
60 sym_base _base;
61 char *v;
62} var;
63
64/*----- Main code ---------------------------------------------------------*/
65
66/* --- @env_get@ --- *
67 *
68 * Arguments: @sym_table *t@ = pointer to a symbol table
69 * @const char *name@ = pointer to variable name to look up
70 *
71 * Returns: Pointer to corresponding value string, or null.
72 *
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
75 * returned.
76 */
77
78char *env_get(sym_table *t, const char *name)
79{
80 var *e = sym_find(t, name, -1, 0, 0);
81 return (e ? e->v : 0);
82}
83
84/* --- @env_put@ --- *
85 *
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
89 *
90 * Returns: ---
91 *
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
98 * way you'd expect.
99 */
100
101void env_put(sym_table *t, const char *name, const char *value)
102{
103 char *q = 0;
104
105 /* --- Sort out the mess with `NAME=VALUE' forms --- */
106
107 {
108 size_t eq = strcspn(name, "=");
109 if (name[eq] == '=') {
20eb516f 110 q = x_alloc(t->t.a, eq + 1);
e19206c2 111 memcpy(q, name, eq);
112 q[eq] = 0;
113 value = name + eq + 1;
114 name = q;
115 }
116 }
117
118 /* --- Read the current value --- */
119
120 if (!value) {
121 var *v;
122 if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
20eb516f 123 x_free(t->t.a, v->v);
e19206c2 124 sym_remove(t, v);
125 }
126 } else {
127 unsigned found;
128 var *v = sym_find(t, name, -1, sizeof(*v), &found);
129 if (found)
20eb516f 130 x_free(t->t.a, v->v);
131 v->v = x_strdup(t->t.a, value);
e19206c2 132 }
133
134 /* --- Tidying --- */
135
136 if (q)
20eb516f 137 xfree(q);
e19206c2 138}
139
140/* --- @env_import@ --- *
141 *
142 * Arguments: @sym_table *t@ = pointer to a symbol table
143 * @char **env@ = pointer to an environment list
144 *
145 * Returns: ---
146 *
147 * Use: Inserts all of the environment variables listed into a symbol
148 * table for rapid access. Equivalent to a lot of calls to
149 * @env_put@.
150 */
151
152void env_import(sym_table *t, char **env)
153{
154 while (*env) {
155 env_put(t, *env, 0);
156 env++;
157 }
158}
159
160/* --- @env_export@ --- *
161 *
162 * Arguments: @sym_table *t@ = pointer to a symbol table
163 *
164 * Returns: A big environment list.
165 *
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.
169 */
170
171char **env_export(sym_table *t)
172{
173 size_t n = 1;
174 size_t sz = 0;
175 sym_iter i;
176 var *v;
177 char **env;
178 char *p, **pp;
179
180 /* --- Work out sizes for everything --- */
181
182 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
183 n++;
f41204b3 184 sz += SYM_LEN(v) + strlen(v->v) + 2;
e19206c2 185 }
186
187 /* --- Allocate the big chunk of memory --- */
188
189 env = pp = xmalloc(n * sizeof(char *) + sz);
190 p = (char *)(env + n);
191
192 /* --- Dump the output in the big chunk of memory --- */
193
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);
197 *pp++ = p;
198 memcpy(p, name, nlen); p += nlen;
199 *p++ = '=';
200 memcpy(p, v->v, vlen); p += vlen;
201 *p++ = 0;
202 }
203 *pp++ = 0;
204 return (env);
205}
206
207/* --- @env_destroy@ --- *
208 *
209 * Arguments: @sym_table *t@ = pointer to symbol table
210 *
211 * Returns: ---
212 *
213 * Use: Destroys all the variables in the symbol table.
214 */
215
216void env_destroy(sym_table *t)
217{
218 sym_iter i;
219 var *v;
220
221 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
20eb516f 222 x_free(t->t.a, v->v);
e19206c2 223 sym_destroy(t);
224}
225
226/*----- That's all, folks -------------------------------------------------*/