dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libsecdb / common / secdb.c
blobcfa690aa417d6d576d34aa88c99369303c017c48
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <secdb.h>
30 #include <ctype.h>
32 /* From libnsl */
33 extern char *_strdup_null(char *);
34 extern char *_strtok_escape(char *, char *, char **);
35 extern char *_strpbrk_escape(char *, char *);
36 extern char *_unescape(char *, char *);
38 char *_do_unescape(char *);
42 * kva_match(): Given a key-value array and a key, return a pointer to the
43 * value that matches the key.
45 char *
46 kva_match(kva_t *kva, char *key)
48 int i;
49 kv_t *data;
51 if (kva == NULL || key == NULL) {
52 return (NULL);
54 data = kva->data;
55 for (i = 0; i < kva->length; i++) {
56 if (strcmp(data[i].key, key) == 0) {
57 return (data[i].value);
61 return (NULL);
65 * _kva_free(): Free up memory.
67 void
68 _kva_free(kva_t *kva)
70 int i;
71 kv_t *data;
73 if (kva == NULL) {
74 return;
76 data = kva->data;
77 for (i = 0; i < kva->length; i++) {
78 if (data[i].key != NULL) {
79 free(data[i].key);
80 data[i].key = NULL;
82 if (data[i].value != NULL) {
83 free(data[i].value);
84 data[i].value = NULL;
87 free(kva->data);
88 free(kva);
92 * _kva_free_value(): Free up memory (value) for all the occurrences of
93 * the given key.
95 void
96 _kva_free_value(kva_t *kva, char *key)
98 int ctr;
99 kv_t *data;
101 if (kva == NULL) {
102 return;
105 ctr = kva->length;
106 data = kva->data;
108 while (ctr--) {
109 if (strcmp(data->key, key) == 0 && data->value != NULL) {
110 free(data->value);
111 data->value = NULL;
113 data++;
118 * new_kva(): Allocate a key-value array.
120 kva_t *
121 _new_kva(int size)
123 kva_t *new_kva;
125 if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
126 return (NULL);
128 if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
129 free(new_kva);
130 return (NULL);
133 return (new_kva);
137 * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
138 * (del), place the values into the key value array (nkva).
140 kva_t *
141 _str2kva(char *s, char *ass, char *del)
143 int n = 0;
144 int m;
145 int size = KV_ADD_KEYS;
146 char *buf;
147 char *p;
148 char *pair;
149 char *key;
150 char *last_pair;
151 char *last_key;
152 kv_t *data;
153 kva_t *nkva;
155 if (s == NULL ||
156 ass == NULL ||
157 del == NULL ||
158 *s == '\0' ||
159 *s == '\n' ||
160 (strlen(s) <= 1)) {
161 return (NULL);
163 p = s;
164 while ((p = _strpbrk_escape(p, ass)) != NULL) {
165 n++;
166 p++;
168 if (n > size) {
169 m = n/size;
170 if (n%size) {
171 ++m;
173 size = m * KV_ADD_KEYS;
175 if ((nkva = _new_kva(size)) == NULL) {
176 return (NULL);
178 data = nkva->data;
179 nkva->length = 0;
180 if ((buf = strdup(s)) == NULL) {
181 return (NULL);
183 pair = _strtok_escape(buf, del, &last_pair);
184 do {
185 key = _strtok_escape(pair, ass, &last_key);
186 if (key != NULL) {
187 data[nkva->length].key = _do_unescape(key);
188 data[nkva->length].value = _do_unescape(last_key);
189 nkva->length++;
191 } while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
192 free(buf);
193 return (nkva);
197 * _kva2str(): Given an array of key-value pairs, place them into a string
198 * (buf). Use delimeter (del) to separate pairs. Use assignment character
199 * (ass) to separate keys and values.
201 * Return Values: 0 Success 1 Buffer too small
204 _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
206 int i;
207 int len;
208 int off = 0;
209 kv_t *data;
211 if (kva == NULL) {
212 return (0);
215 buf[0] = '\0';
216 data = kva->data;
218 for (i = 0; i < kva->length; i++) {
219 if (data[i].value != NULL) {
220 len = snprintf(buf + off, buflen - off, "%s%s%s%s",
221 data[i].key, ass, data[i].value, del);
222 if (len < 0 || len + off >= buflen) {
223 return (1);
225 off += len;
229 return (0);
233 _insert2kva(kva_t *kva, char *key, char *value)
235 int i;
236 kv_t *data;
238 if (kva == NULL) {
239 return (0);
241 data = kva->data;
242 for (i = 0; i < kva->length; i++) {
243 if (strcmp(data[i].key, key) == 0) {
244 free(data[i].value);
245 data[i].value = _strdup_null(value);
246 return (0);
249 return (1);
252 kva_t *
253 _kva_dup(kva_t *old_kva)
255 int i;
256 int size;
257 kv_t *old_data;
258 kv_t *new_data;
259 kva_t *nkva = NULL;
261 if (old_kva == NULL) {
262 return (NULL);
264 old_data = old_kva->data;
265 size = old_kva->length;
266 if ((nkva = _new_kva(size)) == NULL) {
267 return (NULL);
269 new_data = nkva->data;
270 nkva->length = old_kva->length;
271 for (i = 0; i < nkva->length; i++) {
272 new_data[i].key = _strdup_null(old_data[i].key);
273 new_data[i].value = _strdup_null(old_data[i].value);
276 return (nkva);
279 static void
280 strip_spaces(char **valuep)
282 char *p, *start;
284 /* Find first non-white space character and return pointer to it */
285 for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
288 *valuep = start = p;
290 if (*p == '\0')
291 return;
293 p = p + strlen(p) - 1;
295 /* Remove trailing spaces */
296 while (p > start && isspace((unsigned char)*p))
297 p--;
299 p[1] = '\0';
302 char *
303 _do_unescape(char *src)
305 char *tmp = NULL;
306 char *dst = NULL;
308 if (src == NULL) {
309 dst = _strdup_null(src);
310 } else {
311 strip_spaces(&src);
312 tmp = _unescape(src, "=;:,\\");
313 dst = (tmp == NULL) ? _strdup_null(src) : tmp;
316 return (dst);
321 * Some utilities for handling comma-separated lists.
323 char *
324 _argv_to_csl(char **strings)
326 int len = 0;
327 int i = 0;
328 char *newstr = NULL;
330 if (strings == NULL)
331 return (NULL);
332 for (i = 0; strings[i] != NULL; i++) {
333 len += strlen(strings[i]) + 1;
335 if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) {
336 (void) memset(newstr, 0, len);
337 for (i = 0; strings[i] != NULL; i++) {
338 (void) strcat(newstr, strings[i]);
339 (void) strcat(newstr, ",");
341 newstr[len-1] = '\0';
342 return (newstr);
343 } else
344 return (NULL);
348 char **
349 _csl_to_argv(char *csl)
351 int len = 0;
352 int ncommas = 0;
353 int i = 0;
354 char **spc = NULL;
355 char *copy = NULL;
356 char *pc;
357 char *lasts = NULL;
359 len = strlen(csl);
360 for (i = 0; i < len; i++) {
361 if (csl[i] == ',')
362 ncommas++;
364 if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
365 return (NULL);
367 copy = strdup(csl);
368 for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
369 pc = strtok_r(NULL, ",", &lasts), i++) {
370 spc[i] = strdup(pc);
372 spc[i] = NULL;
373 free(copy);
374 return (spc);
378 void
379 _free_argv(char **p_argv)
381 char **p_a;
383 for (p_a = p_argv; *p_a != NULL; p_a++)
384 free(*p_a);
385 free(p_argv);
389 #ifdef DEBUG
390 void
391 print_kva(kva_t *kva)
393 int i;
394 kv_t *data;
396 if (kva == NULL) {
397 (void) printf(" (empty)\n");
398 return;
400 data = kva->data;
401 for (i = 0; i < kva->length; i++) {
402 (void) printf(" %s = %s\n",
403 data[i].key != NULL ? data[i].key : "NULL",
404 data[i].value != NULL ? data[i].value : "NULL");
407 #endif /* DEBUG */