3 * $Id: env.c,v 1.1 1999/07/26 23:15:57 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.1 1999/07/26 23:15:57 mdw
34 * Fiddling with environment variables.
38 /*----- Header files ------------------------------------------------------*/
47 /*----- Data structures ---------------------------------------------------*/
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @env_get@ --- *
58 * Arguments: @sym_table *t@ = pointer to a symbol table
59 * @const char *name@ = pointer to variable name to look up
61 * Returns: Pointer to corresponding value string, or null.
63 * Use: Looks up an environment variable in the table and returns its
64 * value. If the variable can't be found, a null pointer is
68 char *env_get(sym_table
*t
, const char *name
)
70 var
*e
= sym_find(t
, name
, -1, 0, 0);
71 return (e ? e
->v
: 0);
74 /* --- @env_put@ --- *
76 * Arguments: @sym_table *t@ = pointer to a symbol table
77 * @const char *name@ = pointer to variable name to set
78 * @const char *value@ = pointer to value string to assign
82 * Use: Assigns a value to a variable. If the @name@ contains an
83 * equals character, then it's assumed to be of the form
84 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
85 * @value@ is null, the variable is deleted. Finally, the
86 * normal case: @name@ is a plain name, and @value@ is a normal
87 * string causes the variable to be assigned the value in the
91 void env_put(sym_table
*t
, const char *name
, const char *value
)
95 /* --- Sort out the mess with `NAME=VALUE' forms --- */
98 size_t eq
= strcspn(name
, "=");
99 if (name
[eq
] == '=') {
103 value
= name
+ eq
+ 1;
108 /* --- Read the current value --- */
112 if ((v
= sym_find(t
, name
, -1, 0, 0)) != 0) {
118 var
*v
= sym_find(t
, name
, -1, sizeof(*v
), &found
);
121 v
->v
= xstrdup(value
);
124 /* --- Tidying --- */
130 /* --- @env_import@ --- *
132 * Arguments: @sym_table *t@ = pointer to a symbol table
133 * @char **env@ = pointer to an environment list
137 * Use: Inserts all of the environment variables listed into a symbol
138 * table for rapid access. Equivalent to a lot of calls to
142 void env_import(sym_table
*t
, char **env
)
150 /* --- @env_export@ --- *
152 * Arguments: @sym_table *t@ = pointer to a symbol table
154 * Returns: A big environment list.
156 * Use: Extracts an environment table from a symbol table
157 * representation of an environment. The table and all of the
158 * strings are in one big block allocated from the heap.
161 char **env_export(sym_table
*t
)
170 /* --- Work out sizes for everything --- */
172 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
174 sz
+= strlen(SYM_NAME(v
)) + strlen(v
->v
) + 2;
177 /* --- Allocate the big chunk of memory --- */
179 env
= pp
= xmalloc(n
* sizeof(char *) + sz
);
180 p
= (char *)(env
+ n
);
182 /* --- Dump the output in the big chunk of memory --- */
184 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; ) {
185 const char *name
= SYM_NAME(v
);
186 size_t nlen
= strlen(name
), vlen
= strlen(v
->v
);
188 memcpy(p
, name
, nlen
); p
+= nlen
;
190 memcpy(p
, v
->v
, vlen
); p
+= vlen
;
197 /* --- @env_destroy@ --- *
199 * Arguments: @sym_table *t@ = pointer to symbol table
203 * Use: Destroys all the variables in the symbol table.
206 void env_destroy(sym_table
*t
)
211 for (sym_mkiter(&i
, t
); (v
= sym_next(&i
)) != 0; )
216 /*----- That's all, folks -------------------------------------------------*/