1 /* $NetBSD: env.c,v 1.1.1.1 2011/04/13 18:15:10 elric Exp $ */
4 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
39 * @page page_env Hx509 enviroment functions
41 * See the library functions here: @ref hx509_env
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().
58 hx509_env_add(hx509_context context
, hx509_env
*env
,
59 const char *key
, const char *value
)
63 n
= malloc(sizeof(*n
));
65 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
71 n
->name
= strdup(key
);
72 if (n
->name
== NULL
) {
76 n
->u
.string
= strdup(value
);
77 if (n
->u
.string
== NULL
) {
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().
109 hx509_env_add_binding(hx509_context context
, hx509_env
*env
,
110 const char *key
, hx509_env list
)
114 n
= malloc(sizeof(*n
));
116 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
122 n
->name
= strdup(key
);
123 if (n
->name
== NULL
) {
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.
156 hx509_env_lfind(hx509_context context
, hx509_env env
,
157 const char *key
, size_t len
)
160 if (strncmp(key
, env
->name
,len
) == 0
161 && env
->name
[len
] == '\0' && env
->type
== env_string
)
162 return env
->u
.string
;
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.
181 hx509_env_find(hx509_context context
, hx509_env env
, const char *key
)
184 if (strcmp(key
, env
->name
) == 0 && env
->type
== env_string
)
185 return env
->u
.string
;
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.
204 hx509_env_find_binding(hx509_context context
,
209 if (strcmp(key
, env
->name
) == 0 && env
->type
== env_list
)
217 env_free(hx509_env b
)
220 hx509_env next
= b
->next
;
222 if (b
->type
== env_string
)
224 else if (b
->type
== env_list
)
234 * Free an hx509_env enviroment context.
236 * @param env the enviroment to free.
242 hx509_env_free(hx509_env
*env
)