3 * $Id: env.c,v 1.5 2004/04/08 01:36:11 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 /*----- Header files ------------------------------------------------------*/
39 /*----- Data structures ---------------------------------------------------*/
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @env_get@ --- *
50 * Arguments: @sym_table *t@ = pointer to a symbol table
51 * @const char *name@ = pointer to variable name to look up
53 * Returns: Pointer to corresponding value string, or null.
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
60 char *env_get(sym_table
*t
, const char *name
)
62 var
*e
= sym_find(t
, name
, -1, 0, 0);
63 return (e ? e
->v
: 0);
66 /* --- @env_put@ --- *
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
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
83 void env_put(sym_table
*t
, const char *name
, const char *value
)
87 /* --- Sort out the mess with `NAME=VALUE' forms --- */
90 size_t eq
= strcspn(name
, "=");
91 if (name
[eq
] == '=') {
92 q
= x_alloc(t
->t
.a
, eq
+ 1);
95 value
= name
+ eq
+ 1;
100 /* --- Read the current value --- */
104 if ((v
= sym_find(t
, name
, -1, 0, 0)) != 0) {
105 x_free(t
->t
.a
, v
->v
);
110 var
*v
= sym_find(t
, name
, -1, sizeof(*v
), &found
);
112 x_free(t
->t
.a
, v
->v
);
113 v
->v
= x_strdup(t
->t
.a
, value
);
116 /* --- Tidying --- */
122 /* --- @env_import@ --- *
124 * Arguments: @sym_table *t@ = pointer to a symbol table
125 * @char **env@ = pointer to an environment list
129 * Use: Inserts all of the environment variables listed into a symbol
130 * table for rapid access. Equivalent to a lot of calls to
134 void env_import(sym_table
*t
, char **env
)
142 /* --- @env_export@ --- *
144 * Arguments: @sym_table *t@ = pointer to a symbol table
146 * Returns: A big environment list.
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.
153 char **env_export(sym_table
*t
)
162 /* --- Work out sizes for everything --- */
164 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
166 sz
+= SYM_LEN(v
) + strlen(v
->v
) + 2;
169 /* --- Allocate the big chunk of memory --- */
171 env
= pp
= xmalloc(n
* sizeof(char *) + sz
);
172 p
= (char *)(env
+ n
);
174 /* --- Dump the output in the big chunk of memory --- */
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
);
180 memcpy(p
, name
, nlen
); p
+= nlen
;
182 memcpy(p
, v
->v
, vlen
); p
+= vlen
;
189 /* --- @env_destroy@ --- *
191 * Arguments: @sym_table *t@ = pointer to symbol table
195 * Use: Destroys all the variables in the symbol table.
198 void env_destroy(sym_table
*t
)
203 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; )
204 x_free(t
->t
.a
, v
->v
);
208 /*----- That's all, folks -------------------------------------------------*/