chiark / gitweb /
Makefile: Use check_PROGRAMS target.
[mLib] / env.c
CommitLineData
e19206c2 1/* -*-c-*-
2 *
8656dc50 3 * $Id: env.c,v 1.5 2004/04/08 01:36:11 mdw Exp $
e19206c2 4 *
5 * Fiddling with environment variables
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
d4efbcd9 10/*----- Licensing notice --------------------------------------------------*
e19206c2 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.
d4efbcd9 18 *
e19206c2 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.
d4efbcd9 23 *
e19206c2 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
e19206c2 30/*----- Header files ------------------------------------------------------*/
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "alloc.h"
37#include "sym.h"
38
39/*----- Data structures ---------------------------------------------------*/
40
41typedef struct var {
42 sym_base _base;
43 char *v;
44} var;
45
46/*----- Main code ---------------------------------------------------------*/
47
48/* --- @env_get@ --- *
49 *
50 * Arguments: @sym_table *t@ = pointer to a symbol table
51 * @const char *name@ = pointer to variable name to look up
52 *
53 * Returns: Pointer to corresponding value string, or null.
54 *
55 * Use: Looks up an environment variable in the table and returns its
56 * value. If the variable can't be found, a null pointer is
57 * returned.
58 */
59
60char *env_get(sym_table *t, const char *name)
61{
62 var *e = sym_find(t, name, -1, 0, 0);
63 return (e ? e->v : 0);
64}
65
66/* --- @env_put@ --- *
67 *
68 * Arguments: @sym_table *t@ = pointer to a symbol table
69 * @const char *name@ = pointer to variable name to set
70 * @const char *value@ = pointer to value string to assign
71 *
72 * Returns: ---
73 *
74 * Use: Assigns a value to a variable. If the @name@ contains an
75 * equals character, then it's assumed to be of the form
76 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
77 * @value@ is null, the variable is deleted. Finally, the
78 * normal case: @name@ is a plain name, and @value@ is a normal
79 * string causes the variable to be assigned the value in the
80 * way you'd expect.
81 */
82
83void env_put(sym_table *t, const char *name, const char *value)
84{
85 char *q = 0;
86
87 /* --- Sort out the mess with `NAME=VALUE' forms --- */
88
89 {
90 size_t eq = strcspn(name, "=");
91 if (name[eq] == '=') {
20eb516f 92 q = x_alloc(t->t.a, eq + 1);
e19206c2 93 memcpy(q, name, eq);
94 q[eq] = 0;
95 value = name + eq + 1;
96 name = q;
97 }
98 }
99
100 /* --- Read the current value --- */
101
102 if (!value) {
103 var *v;
104 if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
20eb516f 105 x_free(t->t.a, v->v);
e19206c2 106 sym_remove(t, v);
107 }
108 } else {
109 unsigned found;
110 var *v = sym_find(t, name, -1, sizeof(*v), &found);
111 if (found)
20eb516f 112 x_free(t->t.a, v->v);
113 v->v = x_strdup(t->t.a, value);
e19206c2 114 }
115
116 /* --- Tidying --- */
117
118 if (q)
20eb516f 119 xfree(q);
e19206c2 120}
121
122/* --- @env_import@ --- *
123 *
124 * Arguments: @sym_table *t@ = pointer to a symbol table
125 * @char **env@ = pointer to an environment list
126 *
127 * Returns: ---
128 *
129 * Use: Inserts all of the environment variables listed into a symbol
130 * table for rapid access. Equivalent to a lot of calls to
131 * @env_put@.
132 */
133
134void env_import(sym_table *t, char **env)
135{
136 while (*env) {
137 env_put(t, *env, 0);
138 env++;
139 }
140}
141
142/* --- @env_export@ --- *
143 *
144 * Arguments: @sym_table *t@ = pointer to a symbol table
145 *
146 * Returns: A big environment list.
147 *
148 * Use: Extracts an environment table from a symbol table
149 * representation of an environment. The table and all of the
150 * strings are in one big block allocated from the heap.
151 */
152
153char **env_export(sym_table *t)
154{
155 size_t n = 1;
156 size_t sz = 0;
157 sym_iter i;
158 var *v;
159 char **env;
160 char *p, **pp;
161
162 /* --- Work out sizes for everything --- */
163
164 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
165 n++;
f41204b3 166 sz += SYM_LEN(v) + strlen(v->v) + 2;
e19206c2 167 }
168
169 /* --- Allocate the big chunk of memory --- */
170
171 env = pp = xmalloc(n * sizeof(char *) + sz);
172 p = (char *)(env + n);
173
174 /* --- Dump the output in the big chunk of memory --- */
175
176 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
177 const char *name = SYM_NAME(v);
178 size_t nlen = strlen(name), vlen = strlen(v->v);
179 *pp++ = p;
180 memcpy(p, name, nlen); p += nlen;
181 *p++ = '=';
182 memcpy(p, v->v, vlen); p += vlen;
183 *p++ = 0;
184 }
185 *pp++ = 0;
186 return (env);
187}
188
189/* --- @env_destroy@ --- *
190 *
191 * Arguments: @sym_table *t@ = pointer to symbol table
192 *
193 * Returns: ---
194 *
195 * Use: Destroys all the variables in the symbol table.
196 */
197
198void env_destroy(sym_table *t)
199{
200 sym_iter i;
201 var *v;
202
203 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
20eb516f 204 x_free(t->t.a, v->v);
e19206c2 205 sym_destroy(t);
206}
207
208/*----- That's all, folks -------------------------------------------------*/