etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hx509 / env.c
blob2b891b222d9e353bc3e65f192a4e6edca46fa306
1 /* $NetBSD: env.c,v 1.1.1.1 2011/04/13 18:15:10 elric Exp $ */
3 /*
4 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "hx_locl.h"
38 /**
39 * @page page_env Hx509 enviroment functions
41 * See the library functions here: @ref hx509_env
44 /**
45 * Add a new key/value pair to the hx509_env.
47 * @param context A hx509 context.
48 * @param env enviroment to add the enviroment variable too.
49 * @param key key to add
50 * @param value value to add
52 * @return An hx509 error code, see hx509_get_error_string().
54 * @ingroup hx509_env
57 int
58 hx509_env_add(hx509_context context, hx509_env *env,
59 const char *key, const char *value)
61 hx509_env n;
63 n = malloc(sizeof(*n));
64 if (n == NULL) {
65 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
66 return ENOMEM;
69 n->type = env_string;
70 n->next = NULL;
71 n->name = strdup(key);
72 if (n->name == NULL) {
73 free(n);
74 return ENOMEM;
76 n->u.string = strdup(value);
77 if (n->u.string == NULL) {
78 free(n->name);
79 free(n);
80 return ENOMEM;
83 /* add to tail */
84 if (*env) {
85 hx509_env e = *env;
86 while (e->next)
87 e = e->next;
88 e->next = n;
89 } else
90 *env = n;
92 return 0;
95 /**
96 * Add a new key/binding pair to the hx509_env.
98 * @param context A hx509 context.
99 * @param env enviroment to add the enviroment variable too.
100 * @param key key to add
101 * @param list binding list to add
103 * @return An hx509 error code, see hx509_get_error_string().
105 * @ingroup hx509_env
109 hx509_env_add_binding(hx509_context context, hx509_env *env,
110 const char *key, hx509_env list)
112 hx509_env n;
114 n = malloc(sizeof(*n));
115 if (n == NULL) {
116 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
117 return ENOMEM;
120 n->type = env_list;
121 n->next = NULL;
122 n->name = strdup(key);
123 if (n->name == NULL) {
124 free(n);
125 return ENOMEM;
127 n->u.list = list;
129 /* add to tail */
130 if (*env) {
131 hx509_env e = *env;
132 while (e->next)
133 e = e->next;
134 e->next = n;
135 } else
136 *env = n;
138 return 0;
143 * Search the hx509_env for a length based key.
145 * @param context A hx509 context.
146 * @param env enviroment to add the enviroment variable too.
147 * @param key key to search for.
148 * @param len length of key.
150 * @return the value if the key is found, NULL otherwise.
152 * @ingroup hx509_env
155 const char *
156 hx509_env_lfind(hx509_context context, hx509_env env,
157 const char *key, size_t len)
159 while(env) {
160 if (strncmp(key, env->name ,len) == 0
161 && env->name[len] == '\0' && env->type == env_string)
162 return env->u.string;
163 env = env->next;
165 return NULL;
169 * Search the hx509_env for a key.
171 * @param context A hx509 context.
172 * @param env enviroment to add the enviroment variable too.
173 * @param key key to search for.
175 * @return the value if the key is found, NULL otherwise.
177 * @ingroup hx509_env
180 const char *
181 hx509_env_find(hx509_context context, hx509_env env, const char *key)
183 while(env) {
184 if (strcmp(key, env->name) == 0 && env->type == env_string)
185 return env->u.string;
186 env = env->next;
188 return NULL;
192 * Search the hx509_env for a binding.
194 * @param context A hx509 context.
195 * @param env enviroment to add the enviroment variable too.
196 * @param key key to search for.
198 * @return the binding if the key is found, NULL if not found.
200 * @ingroup hx509_env
203 hx509_env
204 hx509_env_find_binding(hx509_context context,
205 hx509_env env,
206 const char *key)
208 while(env) {
209 if (strcmp(key, env->name) == 0 && env->type == env_list)
210 return env->u.list;
211 env = env->next;
213 return NULL;
216 static void
217 env_free(hx509_env b)
219 while(b) {
220 hx509_env next = b->next;
222 if (b->type == env_string)
223 free(b->u.string);
224 else if (b->type == env_list)
225 env_free(b->u.list);
227 free(b->name);
228 free(b);
229 b = next;
234 * Free an hx509_env enviroment context.
236 * @param env the enviroment to free.
238 * @ingroup hx509_env
241 void
242 hx509_env_free(hx509_env *env)
244 if (*env)
245 env_free(*env);
246 *env = NULL;