3 * Fiddling with environment variables
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Data structures ---------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @env_get@ --- *
48 * Arguments: @sym_table *t@ = pointer to a symbol table
49 * @const char *name@ = pointer to variable name to look up
51 * Returns: Pointer to corresponding value string, or null.
53 * Use: Looks up an environment variable in the table and returns its
54 * value. If the variable can't be found, a null pointer is
58 char *env_get(sym_table
*t
, const char *name
)
60 var
*e
= sym_find(t
, name
, -1, 0, 0);
61 return (e ? e
->v
: 0);
64 /* --- @env_put@ --- *
66 * Arguments: @sym_table *t@ = pointer to a symbol table
67 * @const char *name@ = pointer to variable name to set
68 * @const char *value@ = pointer to value string to assign
72 * Use: Assigns a value to a variable. If the @name@ contains an
73 * equals character, then it's assumed to be of the form
74 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
75 * @value@ is null, the variable is deleted. Finally, the
76 * normal case: @name@ is a plain name, and @value@ is a normal
77 * string causes the variable to be assigned the value in the
81 void env_put(sym_table
*t
, const char *name
, const char *value
)
85 /* --- Sort out the mess with `NAME=VALUE' forms --- */
88 size_t eq
= strcspn(name
, "=");
89 if (name
[eq
] == '=') {
90 q
= x_alloc(t
->t
.a
, eq
+ 1);
93 value
= name
+ eq
+ 1;
98 /* --- Read the current value --- */
102 if ((v
= sym_find(t
, name
, -1, 0, 0)) != 0) {
103 x_free(t
->t
.a
, v
->v
);
108 var
*v
= sym_find(t
, name
, -1, sizeof(*v
), &found
);
110 x_free(t
->t
.a
, v
->v
);
111 v
->v
= x_strdup(t
->t
.a
, value
);
114 /* --- Tidying --- */
120 /* --- @env_import@ --- *
122 * Arguments: @sym_table *t@ = pointer to a symbol table
123 * @char **env@ = pointer to an environment list
127 * Use: Inserts all of the environment variables listed into a symbol
128 * table for rapid access. Equivalent to a lot of calls to
132 void env_import(sym_table
*t
, char **env
)
140 /* --- @env_export@ --- *
142 * Arguments: @sym_table *t@ = pointer to a symbol table
144 * Returns: A big environment list.
146 * Use: Extracts an environment table from a symbol table
147 * representation of an environment. The table and all of the
148 * strings are in one big block allocated from the heap.
151 char **env_export(sym_table
*t
)
160 /* --- Work out sizes for everything --- */
162 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
164 sz
+= SYM_LEN(v
) + strlen(v
->v
) + 2;
167 /* --- Allocate the big chunk of memory --- */
169 env
= pp
= xmalloc(n
* sizeof(char *) + sz
);
170 p
= (char *)(env
+ n
);
172 /* --- Dump the output in the big chunk of memory --- */
174 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
175 const char *name
= SYM_NAME(v
);
176 size_t nlen
= strlen(name
), vlen
= strlen(v
->v
);
178 memcpy(p
, name
, nlen
); p
+= nlen
;
180 memcpy(p
, v
->v
, vlen
); p
+= vlen
;
187 /* --- @env_destroy@ --- *
189 * Arguments: @sym_table *t@ = pointer to symbol table
193 * Use: Destroys all the variables in the symbol table.
196 void env_destroy(sym_table
*t
)
201 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; )
202 x_free(t
->t
.a
, v
->v
);
206 /*----- That's all, folks -------------------------------------------------*/