chiark / gitweb /
Use library annotations.
[mLib] / env.c
1 /* -*-c-*-
2  *
3  * $Id: env.c,v 1.1 1999/07/26 23:15:57 mdw Exp $
4  *
5  * Fiddling with environment variables
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
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.
18  * 
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.
23  * 
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
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: env.c,v $
33  * Revision 1.1  1999/07/26 23:15:57  mdw
34  * Fiddling with environment variables.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "alloc.h"
45 #include "sym.h"
46
47 /*----- Data structures ---------------------------------------------------*/
48
49 typedef struct var {
50   sym_base _base;
51   char *v;
52 } var;
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @env_get@ --- *
57  *
58  * Arguments:   @sym_table *t@ = pointer to a symbol table
59  *              @const char *name@ = pointer to variable name to look up
60  *
61  * Returns:     Pointer to corresponding value string, or null.
62  *
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
65  *              returned.
66  */
67
68 char *env_get(sym_table *t, const char *name)
69 {
70   var *e = sym_find(t, name, -1, 0, 0);
71   return (e ? e->v : 0);
72 }
73
74 /* --- @env_put@ --- *
75  *
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
79  *
80  * Returns:     ---
81  *
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
88  *              way you'd expect.
89  */
90
91 void env_put(sym_table *t, const char *name, const char *value)
92 {
93   char *q = 0;
94
95   /* --- Sort out the mess with `NAME=VALUE' forms --- */
96
97   {
98     size_t eq = strcspn(name, "=");
99     if (name[eq] == '=') {
100       q = xmalloc(eq + 1);
101       memcpy(q, name, eq);
102       q[eq] = 0;
103       value = name + eq + 1;
104       name = q;
105     }
106   }
107
108   /* --- Read the current value --- */
109
110   if (!value) {
111     var *v;
112     if ((v = sym_find(t, name, -1, 0, 0)) != 0) {
113       free(v->v);
114       sym_remove(t, v);
115     }
116   } else {
117     unsigned found;
118     var *v = sym_find(t, name, -1, sizeof(*v), &found);
119     if (found)
120       free(v->v);
121     v->v = xstrdup(value);
122   }
123
124   /* --- Tidying --- */
125
126   if (q)
127     free(q);
128 }
129
130 /* --- @env_import@ --- *
131  *
132  * Arguments:   @sym_table *t@ = pointer to a symbol table
133  *              @char **env@ = pointer to an environment list
134  *
135  * Returns:     ---
136  *
137  * Use:         Inserts all of the environment variables listed into a symbol
138  *              table for rapid access.  Equivalent to a lot of calls to
139  *              @env_put@.
140  */
141
142 void env_import(sym_table *t, char **env)
143 {
144   while (*env) {
145     env_put(t, *env, 0);
146     env++;
147   }
148 }
149
150 /* --- @env_export@ --- *
151  *
152  * Arguments:   @sym_table *t@ = pointer to a symbol table
153  *
154  * Returns:     A big environment list.
155  *
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.
159  */
160
161 char **env_export(sym_table *t)
162 {
163   size_t n = 1;
164   size_t sz = 0;
165   sym_iter i;
166   var *v;
167   char **env;
168   char *p, **pp;
169
170   /* --- Work out sizes for everything --- */
171
172   for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) {
173     n++;
174     sz += strlen(SYM_NAME(v)) + strlen(v->v) + 2;
175   }
176
177   /* --- Allocate the big chunk of memory --- */
178
179   env = pp = xmalloc(n * sizeof(char *) + sz);
180   p = (char *)(env + n);
181
182   /* --- Dump the output in the big chunk of memory --- */
183
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);
187     *pp++ = p;
188     memcpy(p, name, nlen); p += nlen;
189     *p++ = '=';
190     memcpy(p, v->v, vlen); p += vlen;
191     *p++ = 0;
192   }
193   *pp++ = 0;
194   return (env);
195 }
196
197 /* --- @env_destroy@ --- *
198  *
199  * Arguments:   @sym_table *t@ = pointer to symbol table
200  *
201  * Returns:     ---
202  *
203  * Use:         Destroys all the variables in the symbol table.
204  */
205
206 void env_destroy(sym_table *t)
207 {
208   sym_iter i;
209   var *v;
210
211   for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
212     free(v->v);
213   sym_destroy(t);
214 }
215
216 /*----- That's all, folks -------------------------------------------------*/