hx509: Remove unused variable
[heimdal.git] / lib / hx509 / env.c
blob79704382e228525caa13a9f0f9ee76dadee799de
1 /*
2 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
36 /**
37 * @page page_env Hx509 environment functions
39 * See the library functions here: @ref hx509_env
42 /**
43 * Add a new key/value pair to the hx509_env.
45 * @param context A hx509 context.
46 * @param env environment to add the environment variable too.
47 * @param key key to add
48 * @param value value to add
50 * @return An hx509 error code, see hx509_get_error_string().
52 * @ingroup hx509_env
55 HX509_LIB_FUNCTION int HX509_LIB_CALL
56 hx509_env_add(hx509_context context, hx509_env *env,
57 const char *key, const char *value)
59 hx509_env n;
61 n = malloc(sizeof(*n));
62 if (n == NULL) {
63 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
64 return ENOMEM;
67 n->type = env_string;
68 n->next = NULL;
69 n->name = strdup(key);
70 if (n->name == NULL) {
71 free(n);
72 return ENOMEM;
74 n->u.string = strdup(value);
75 if (n->u.string == NULL) {
76 free(n->name);
77 free(n);
78 return ENOMEM;
81 /* add to tail */
82 if (*env) {
83 hx509_env e = *env;
84 while (e->next)
85 e = e->next;
86 e->next = n;
87 } else
88 *env = n;
90 return 0;
93 /**
94 * Add a new key/binding pair to the hx509_env.
96 * @param context A hx509 context.
97 * @param env environment to add the environment variable too.
98 * @param key key to add
99 * @param list binding list to add
101 * @return An hx509 error code, see hx509_get_error_string().
103 * @ingroup hx509_env
106 HX509_LIB_FUNCTION int HX509_LIB_CALL
107 hx509_env_add_binding(hx509_context context, hx509_env *env,
108 const char *key, hx509_env list)
110 hx509_env n;
112 n = malloc(sizeof(*n));
113 if (n == NULL) {
114 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
115 return ENOMEM;
118 n->type = env_list;
119 n->next = NULL;
120 n->name = strdup(key);
121 if (n->name == NULL) {
122 free(n);
123 return ENOMEM;
125 n->u.list = list;
127 /* add to tail */
128 if (*env) {
129 hx509_env e = *env;
130 while (e->next)
131 e = e->next;
132 e->next = n;
133 } else
134 *env = n;
136 return 0;
141 * Search the hx509_env for a length based key.
143 * @param context A hx509 context.
144 * @param env environment to add the environment variable too.
145 * @param key key to search for.
146 * @param len length of key.
148 * @return the value if the key is found, NULL otherwise.
150 * @ingroup hx509_env
153 HX509_LIB_FUNCTION const char * HX509_LIB_CALL
154 hx509_env_lfind(hx509_context context, hx509_env env,
155 const char *key, size_t len)
157 while(env) {
158 if (strncmp(key, env->name ,len) == 0
159 && env->name[len] == '\0' && env->type == env_string)
160 return env->u.string;
161 env = env->next;
163 return NULL;
167 * Search the hx509_env for a key.
169 * @param context A hx509 context.
170 * @param env environment to add the environment variable too.
171 * @param key key to search for.
173 * @return the value if the key is found, NULL otherwise.
175 * @ingroup hx509_env
178 HX509_LIB_FUNCTION const char * HX509_LIB_CALL
179 hx509_env_find(hx509_context context, hx509_env env, const char *key)
181 while(env) {
182 if (strcmp(key, env->name) == 0 && env->type == env_string)
183 return env->u.string;
184 env = env->next;
186 return NULL;
190 * Search the hx509_env for a binding.
192 * @param context A hx509 context.
193 * @param env environment to add the environment variable too.
194 * @param key key to search for.
196 * @return the binding if the key is found, NULL if not found.
198 * @ingroup hx509_env
201 hx509_env
202 hx509_env_find_binding(hx509_context context,
203 hx509_env env,
204 const char *key)
206 while(env) {
207 if (strcmp(key, env->name) == 0 && env->type == env_list)
208 return env->u.list;
209 env = env->next;
211 return NULL;
214 static void
215 env_free(hx509_env b)
217 while(b) {
218 hx509_env next = b->next;
220 if (b->type == env_string)
221 free(b->u.string);
222 else if (b->type == env_list)
223 env_free(b->u.list);
225 free(b->name);
226 free(b);
227 b = next;
232 * Free an hx509_env environment context.
234 * @param env the environment to free.
236 * @ingroup hx509_env
239 HX509_LIB_FUNCTION void HX509_LIB_CALL
240 hx509_env_free(hx509_env *env)
242 if (*env)
243 env_free(*env);
244 *env = NULL;