3 * $Id: env.c,v 1.2 2000/06/17 10:39:00 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.2 2000/06/17 10:39:00 mdw
34 * Add support for arena management.
36 * Revision 1.1 1999/07/26 23:15:57 mdw
37 * Fiddling with environment variables.
41 /*----- Header files ------------------------------------------------------*/
50 /*----- Data structures ---------------------------------------------------*/
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @env_get@ --- *
61 * Arguments: @sym_table *t@ = pointer to a symbol table
62 * @const char *name@ = pointer to variable name to look up
64 * Returns: Pointer to corresponding value string, or null.
66 * Use: Looks up an environment variable in the table and returns its
67 * value. If the variable can't be found, a null pointer is
71 char *env_get(sym_table
*t
, const char *name
)
73 var
*e
= sym_find(t
, name
, -1, 0, 0);
74 return (e ? e
->v
: 0);
77 /* --- @env_put@ --- *
79 * Arguments: @sym_table *t@ = pointer to a symbol table
80 * @const char *name@ = pointer to variable name to set
81 * @const char *value@ = pointer to value string to assign
85 * Use: Assigns a value to a variable. If the @name@ contains an
86 * equals character, then it's assumed to be of the form
87 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
88 * @value@ is null, the variable is deleted. Finally, the
89 * normal case: @name@ is a plain name, and @value@ is a normal
90 * string causes the variable to be assigned the value in the
94 void env_put(sym_table
*t
, const char *name
, const char *value
)
98 /* --- Sort out the mess with `NAME=VALUE' forms --- */
101 size_t eq
= strcspn(name
, "=");
102 if (name
[eq
] == '=') {
103 q
= x_alloc(t
->t
.a
, eq
+ 1);
106 value
= name
+ eq
+ 1;
111 /* --- Read the current value --- */
115 if ((v
= sym_find(t
, name
, -1, 0, 0)) != 0) {
116 x_free(t
->t
.a
, v
->v
);
121 var
*v
= sym_find(t
, name
, -1, sizeof(*v
), &found
);
123 x_free(t
->t
.a
, v
->v
);
124 v
->v
= x_strdup(t
->t
.a
, value
);
127 /* --- Tidying --- */
133 /* --- @env_import@ --- *
135 * Arguments: @sym_table *t@ = pointer to a symbol table
136 * @char **env@ = pointer to an environment list
140 * Use: Inserts all of the environment variables listed into a symbol
141 * table for rapid access. Equivalent to a lot of calls to
145 void env_import(sym_table
*t
, char **env
)
153 /* --- @env_export@ --- *
155 * Arguments: @sym_table *t@ = pointer to a symbol table
157 * Returns: A big environment list.
159 * Use: Extracts an environment table from a symbol table
160 * representation of an environment. The table and all of the
161 * strings are in one big block allocated from the heap.
164 char **env_export(sym_table
*t
)
173 /* --- Work out sizes for everything --- */
175 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
177 sz
+= strlen(SYM_NAME(v
)) + strlen(v
->v
) + 2;
180 /* --- Allocate the big chunk of memory --- */
182 env
= pp
= xmalloc(n
* sizeof(char *) + sz
);
183 p
= (char *)(env
+ n
);
185 /* --- Dump the output in the big chunk of memory --- */
187 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
188 const char *name
= SYM_NAME(v
);
189 size_t nlen
= strlen(name
), vlen
= strlen(v
->v
);
191 memcpy(p
, name
, nlen
); p
+= nlen
;
193 memcpy(p
, v
->v
, vlen
); p
+= vlen
;
200 /* --- @env_destroy@ --- *
202 * Arguments: @sym_table *t@ = pointer to symbol table
206 * Use: Destroys all the variables in the symbol table.
209 void env_destroy(sym_table
*t
)
214 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; )
215 x_free(t
->t
.a
, v
->v
);
219 /*----- That's all, folks -------------------------------------------------*/