1ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
2ad30f8e7SGabor Kovesdan
3ad30f8e7SGabor Kovesdan /*-
4*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause
5*d915a14eSPedro F. Giffuni *
6ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project,
7ad30f8e7SGabor Kovesdan * All rights reserved.
8ad30f8e7SGabor Kovesdan *
9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan * are met:
12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan *
18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan */
30ad30f8e7SGabor Kovesdan
31ad30f8e7SGabor Kovesdan #include <sys/types.h>
32ad30f8e7SGabor Kovesdan
33ad30f8e7SGabor Kovesdan #include <assert.h>
34ad30f8e7SGabor Kovesdan #include <errno.h>
35ad30f8e7SGabor Kovesdan #include <limits.h>
36ad30f8e7SGabor Kovesdan #include <paths.h>
37ad30f8e7SGabor Kovesdan #include <stdbool.h>
38ad30f8e7SGabor Kovesdan #include <stdio.h>
39ad30f8e7SGabor Kovesdan #include <stdlib.h>
40ad30f8e7SGabor Kovesdan #include <string.h>
41ad30f8e7SGabor Kovesdan
42ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
43ad30f8e7SGabor Kovesdan #include "citrus_types.h"
44ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
45ad30f8e7SGabor Kovesdan #include "citrus_region.h"
46ad30f8e7SGabor Kovesdan #include "citrus_memstream.h"
47ad30f8e7SGabor Kovesdan #include "citrus_mmap.h"
48ad30f8e7SGabor Kovesdan #include "citrus_lookup.h"
49ad30f8e7SGabor Kovesdan #include "citrus_db.h"
50ad30f8e7SGabor Kovesdan #include "citrus_db_hash.h"
51ad30f8e7SGabor Kovesdan #include "citrus_esdb.h"
52ad30f8e7SGabor Kovesdan #include "citrus_esdb_file.h"
53ad30f8e7SGabor Kovesdan
54ad30f8e7SGabor Kovesdan #define ESDB_DIR "esdb.dir"
55ad30f8e7SGabor Kovesdan #define ESDB_ALIAS "esdb.alias"
56ad30f8e7SGabor Kovesdan
57ad30f8e7SGabor Kovesdan /*
58ad30f8e7SGabor Kovesdan * _citrus_esdb_alias:
59ad30f8e7SGabor Kovesdan * resolve encoding scheme name aliases.
60ad30f8e7SGabor Kovesdan */
61ad30f8e7SGabor Kovesdan const char *
_citrus_esdb_alias(const char * esname,char * buf,size_t bufsize)62ad30f8e7SGabor Kovesdan _citrus_esdb_alias(const char *esname, char *buf, size_t bufsize)
63ad30f8e7SGabor Kovesdan {
64ad30f8e7SGabor Kovesdan
65ad30f8e7SGabor Kovesdan return (_lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize,
66ad30f8e7SGabor Kovesdan _LOOKUP_CASE_IGNORE));
67ad30f8e7SGabor Kovesdan }
68ad30f8e7SGabor Kovesdan
69ad30f8e7SGabor Kovesdan
70ad30f8e7SGabor Kovesdan /*
71ad30f8e7SGabor Kovesdan * conv_esdb:
72ad30f8e7SGabor Kovesdan * external representation -> local structure.
73ad30f8e7SGabor Kovesdan */
74ad30f8e7SGabor Kovesdan static int
conv_esdb(struct _citrus_esdb * esdb,struct _region * fr)75ad30f8e7SGabor Kovesdan conv_esdb(struct _citrus_esdb *esdb, struct _region *fr)
76ad30f8e7SGabor Kovesdan {
77ad30f8e7SGabor Kovesdan struct _citrus_db *db;
78ad30f8e7SGabor Kovesdan const char *str;
79ad30f8e7SGabor Kovesdan char buf[100];
80ad30f8e7SGabor Kovesdan uint32_t csid, i, num_charsets, tmp, version;
81ad30f8e7SGabor Kovesdan int ret;
82ad30f8e7SGabor Kovesdan
83ad30f8e7SGabor Kovesdan /* open db */
84ad30f8e7SGabor Kovesdan ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL);
85ad30f8e7SGabor Kovesdan if (ret)
86ad30f8e7SGabor Kovesdan goto err0;
87ad30f8e7SGabor Kovesdan
88ad30f8e7SGabor Kovesdan /* check version */
89ad30f8e7SGabor Kovesdan ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL);
90ad30f8e7SGabor Kovesdan if (ret)
91ad30f8e7SGabor Kovesdan goto err1;
92ad30f8e7SGabor Kovesdan switch (version) {
93ad30f8e7SGabor Kovesdan case 0x00000001:
94ad30f8e7SGabor Kovesdan /* current version */
95ad30f8e7SGabor Kovesdan /* initial version */
96ad30f8e7SGabor Kovesdan break;
97ad30f8e7SGabor Kovesdan default:
98ad30f8e7SGabor Kovesdan ret = EFTYPE;
99ad30f8e7SGabor Kovesdan goto err1;
100ad30f8e7SGabor Kovesdan }
101ad30f8e7SGabor Kovesdan
102ad30f8e7SGabor Kovesdan /* get encoding/variable */
103ad30f8e7SGabor Kovesdan ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL);
104ad30f8e7SGabor Kovesdan if (ret)
105ad30f8e7SGabor Kovesdan goto err1;
106ad30f8e7SGabor Kovesdan esdb->db_encname = strdup(str);
107ad30f8e7SGabor Kovesdan if (esdb->db_encname == NULL) {
108ad30f8e7SGabor Kovesdan ret = errno;
109ad30f8e7SGabor Kovesdan goto err1;
110ad30f8e7SGabor Kovesdan }
111ad30f8e7SGabor Kovesdan
112ad30f8e7SGabor Kovesdan esdb->db_len_variable = 0;
113ad30f8e7SGabor Kovesdan esdb->db_variable = NULL;
114ad30f8e7SGabor Kovesdan ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL);
115ad30f8e7SGabor Kovesdan if (ret == 0) {
116ad30f8e7SGabor Kovesdan esdb->db_len_variable = strlen(str) + 1;
117ad30f8e7SGabor Kovesdan esdb->db_variable = strdup(str);
118ad30f8e7SGabor Kovesdan if (esdb->db_variable == NULL) {
119ad30f8e7SGabor Kovesdan ret = errno;
120ad30f8e7SGabor Kovesdan goto err2;
121ad30f8e7SGabor Kovesdan }
122ad30f8e7SGabor Kovesdan } else if (ret != ENOENT)
123ad30f8e7SGabor Kovesdan goto err2;
124ad30f8e7SGabor Kovesdan
125ad30f8e7SGabor Kovesdan /* get number of charsets */
126ad30f8e7SGabor Kovesdan ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS,
127ad30f8e7SGabor Kovesdan &num_charsets, NULL);
128ad30f8e7SGabor Kovesdan if (ret)
129ad30f8e7SGabor Kovesdan goto err3;
130ad30f8e7SGabor Kovesdan esdb->db_num_charsets = num_charsets;
131ad30f8e7SGabor Kovesdan
132ad30f8e7SGabor Kovesdan /* get invalid character */
133ad30f8e7SGabor Kovesdan ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL);
134ad30f8e7SGabor Kovesdan if (ret == 0) {
135ad30f8e7SGabor Kovesdan esdb->db_use_invalid = 1;
136ad30f8e7SGabor Kovesdan esdb->db_invalid = tmp;
137ad30f8e7SGabor Kovesdan } else if (ret == ENOENT)
138ad30f8e7SGabor Kovesdan esdb->db_use_invalid = 0;
139ad30f8e7SGabor Kovesdan else
140ad30f8e7SGabor Kovesdan goto err3;
141ad30f8e7SGabor Kovesdan
142ad30f8e7SGabor Kovesdan /* get charsets */
143ad30f8e7SGabor Kovesdan esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets));
144ad30f8e7SGabor Kovesdan if (esdb->db_charsets == NULL) {
145ad30f8e7SGabor Kovesdan ret = errno;
146ad30f8e7SGabor Kovesdan goto err3;
147ad30f8e7SGabor Kovesdan }
148ad30f8e7SGabor Kovesdan for (i = 0; i < num_charsets; i++) {
149ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf),
150ad30f8e7SGabor Kovesdan _CITRUS_ESDB_SYM_CSID_PREFIX "%d", i);
151ad30f8e7SGabor Kovesdan ret = _db_lookup32_by_s(db, buf, &csid, NULL);
152ad30f8e7SGabor Kovesdan if (ret)
153ad30f8e7SGabor Kovesdan goto err4;
154ad30f8e7SGabor Kovesdan esdb->db_charsets[i].ec_csid = csid;
155ad30f8e7SGabor Kovesdan
156ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf),
157ad30f8e7SGabor Kovesdan _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i);
158ad30f8e7SGabor Kovesdan ret = _db_lookupstr_by_s(db, buf, &str, NULL);
159ad30f8e7SGabor Kovesdan if (ret)
160ad30f8e7SGabor Kovesdan goto err4;
161ad30f8e7SGabor Kovesdan esdb->db_charsets[i].ec_csname = strdup(str);
162ad30f8e7SGabor Kovesdan if (esdb->db_charsets[i].ec_csname == NULL) {
163ad30f8e7SGabor Kovesdan ret = errno;
164ad30f8e7SGabor Kovesdan goto err4;
165ad30f8e7SGabor Kovesdan }
166ad30f8e7SGabor Kovesdan }
167ad30f8e7SGabor Kovesdan
168ad30f8e7SGabor Kovesdan _db_close(db);
169ad30f8e7SGabor Kovesdan return (0);
170ad30f8e7SGabor Kovesdan
171ad30f8e7SGabor Kovesdan err4:
172ad30f8e7SGabor Kovesdan for (; i > 0; i--)
173ad30f8e7SGabor Kovesdan free(esdb->db_charsets[i - 1].ec_csname);
174ad30f8e7SGabor Kovesdan free(esdb->db_charsets);
175ad30f8e7SGabor Kovesdan err3:
176ad30f8e7SGabor Kovesdan free(esdb->db_variable);
177ad30f8e7SGabor Kovesdan err2:
178ad30f8e7SGabor Kovesdan free(esdb->db_encname);
179ad30f8e7SGabor Kovesdan err1:
180ad30f8e7SGabor Kovesdan _db_close(db);
181ad30f8e7SGabor Kovesdan if (ret == ENOENT)
182ad30f8e7SGabor Kovesdan ret = EFTYPE;
183ad30f8e7SGabor Kovesdan err0:
184ad30f8e7SGabor Kovesdan return (ret);
185ad30f8e7SGabor Kovesdan }
186ad30f8e7SGabor Kovesdan
187ad30f8e7SGabor Kovesdan /*
188ad30f8e7SGabor Kovesdan * _citrus_esdb_open:
189ad30f8e7SGabor Kovesdan * open an ESDB file.
190ad30f8e7SGabor Kovesdan */
191ad30f8e7SGabor Kovesdan int
_citrus_esdb_open(struct _citrus_esdb * db,const char * esname)192ad30f8e7SGabor Kovesdan _citrus_esdb_open(struct _citrus_esdb *db, const char *esname)
193ad30f8e7SGabor Kovesdan {
194ad30f8e7SGabor Kovesdan struct _region fr;
195ad30f8e7SGabor Kovesdan const char *realname, *encfile;
196ad30f8e7SGabor Kovesdan char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX];
197ad30f8e7SGabor Kovesdan int ret;
198ad30f8e7SGabor Kovesdan
199ad30f8e7SGabor Kovesdan snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS);
200ad30f8e7SGabor Kovesdan realname = _lookup_alias(path, esname, buf1, sizeof(buf1),
201ad30f8e7SGabor Kovesdan _LOOKUP_CASE_IGNORE);
202ad30f8e7SGabor Kovesdan
203ad30f8e7SGabor Kovesdan snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR);
204ad30f8e7SGabor Kovesdan encfile = _lookup_simple(path, realname, buf2, sizeof(buf2),
205ad30f8e7SGabor Kovesdan _LOOKUP_CASE_IGNORE);
206ad30f8e7SGabor Kovesdan if (encfile == NULL)
207ad30f8e7SGabor Kovesdan return (ENOENT);
208ad30f8e7SGabor Kovesdan
209ad30f8e7SGabor Kovesdan /* open file */
210ad30f8e7SGabor Kovesdan snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile);
211ad30f8e7SGabor Kovesdan ret = _map_file(&fr, path);
212ad30f8e7SGabor Kovesdan if (ret)
213ad30f8e7SGabor Kovesdan return (ret);
214ad30f8e7SGabor Kovesdan
215ad30f8e7SGabor Kovesdan ret = conv_esdb(db, &fr);
216ad30f8e7SGabor Kovesdan
217ad30f8e7SGabor Kovesdan _unmap_file(&fr);
218ad30f8e7SGabor Kovesdan
219ad30f8e7SGabor Kovesdan return (ret);
220ad30f8e7SGabor Kovesdan }
221ad30f8e7SGabor Kovesdan
222ad30f8e7SGabor Kovesdan /*
223ad30f8e7SGabor Kovesdan * _citrus_esdb_close:
224ad30f8e7SGabor Kovesdan * free an ESDB.
225ad30f8e7SGabor Kovesdan */
226ad30f8e7SGabor Kovesdan void
_citrus_esdb_close(struct _citrus_esdb * db)227ad30f8e7SGabor Kovesdan _citrus_esdb_close(struct _citrus_esdb *db)
228ad30f8e7SGabor Kovesdan {
229ad30f8e7SGabor Kovesdan
230ad30f8e7SGabor Kovesdan for (int i = 0; i < db->db_num_charsets; i++)
231ad30f8e7SGabor Kovesdan free(db->db_charsets[i].ec_csname);
232ad30f8e7SGabor Kovesdan db->db_num_charsets = 0;
233ad30f8e7SGabor Kovesdan free(db->db_charsets); db->db_charsets = NULL;
234ad30f8e7SGabor Kovesdan free(db->db_encname); db->db_encname = NULL;
235ad30f8e7SGabor Kovesdan db->db_len_variable = 0;
236ad30f8e7SGabor Kovesdan free(db->db_variable); db->db_variable = NULL;
237ad30f8e7SGabor Kovesdan }
238ad30f8e7SGabor Kovesdan
239ad30f8e7SGabor Kovesdan /*
240ad30f8e7SGabor Kovesdan * _citrus_esdb_free_list:
241ad30f8e7SGabor Kovesdan * free the list.
242ad30f8e7SGabor Kovesdan */
243ad30f8e7SGabor Kovesdan void
_citrus_esdb_free_list(char ** list,size_t num)244ad30f8e7SGabor Kovesdan _citrus_esdb_free_list(char **list, size_t num)
245ad30f8e7SGabor Kovesdan {
246ad30f8e7SGabor Kovesdan
247ad30f8e7SGabor Kovesdan for (size_t i = 0; i < num; i++)
248ad30f8e7SGabor Kovesdan free(list[i]);
249ad30f8e7SGabor Kovesdan free(list);
250ad30f8e7SGabor Kovesdan }
251ad30f8e7SGabor Kovesdan
252ad30f8e7SGabor Kovesdan /*
253ad30f8e7SGabor Kovesdan * _citrus_esdb_get_list:
254ad30f8e7SGabor Kovesdan * get esdb entries.
255ad30f8e7SGabor Kovesdan */
256ad30f8e7SGabor Kovesdan int
_citrus_esdb_get_list(char *** rlist,size_t * rnum,bool sorted)257ad30f8e7SGabor Kovesdan _citrus_esdb_get_list(char ***rlist, size_t *rnum, bool sorted)
258ad30f8e7SGabor Kovesdan {
259ad30f8e7SGabor Kovesdan struct _citrus_lookup *cla, *cld;
260ad30f8e7SGabor Kovesdan struct _region key, data;
261ad30f8e7SGabor Kovesdan char **list, **q;
262ad30f8e7SGabor Kovesdan char buf[PATH_MAX];
263ad30f8e7SGabor Kovesdan size_t num;
264ad30f8e7SGabor Kovesdan int ret;
265ad30f8e7SGabor Kovesdan
266ad30f8e7SGabor Kovesdan ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS,
267ad30f8e7SGabor Kovesdan _LOOKUP_CASE_IGNORE);
268ad30f8e7SGabor Kovesdan if (ret)
269ad30f8e7SGabor Kovesdan goto quit0;
270ad30f8e7SGabor Kovesdan
271ad30f8e7SGabor Kovesdan ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR,
272ad30f8e7SGabor Kovesdan _LOOKUP_CASE_IGNORE);
273ad30f8e7SGabor Kovesdan if (ret)
274ad30f8e7SGabor Kovesdan goto quit1;
275ad30f8e7SGabor Kovesdan
276ad30f8e7SGabor Kovesdan /* count number of entries */
277ad30f8e7SGabor Kovesdan num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld);
278ad30f8e7SGabor Kovesdan
279ad30f8e7SGabor Kovesdan _lookup_seq_rewind(cla);
280ad30f8e7SGabor Kovesdan _lookup_seq_rewind(cld);
281ad30f8e7SGabor Kovesdan
282ad30f8e7SGabor Kovesdan /* allocate list pointer space */
283ad30f8e7SGabor Kovesdan list = malloc(num * sizeof(char *));
284ad30f8e7SGabor Kovesdan num = 0;
285ad30f8e7SGabor Kovesdan if (list == NULL) {
286ad30f8e7SGabor Kovesdan ret = errno;
287ad30f8e7SGabor Kovesdan goto quit3;
288ad30f8e7SGabor Kovesdan }
289ad30f8e7SGabor Kovesdan
290ad30f8e7SGabor Kovesdan /* get alias entries */
291ad30f8e7SGabor Kovesdan while ((ret = _lookup_seq_next(cla, &key, &data)) == 0) {
292546781a7SPedro F. Giffuni /* XXX: sorted? */
293ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf), "%.*s/%.*s",
294ad30f8e7SGabor Kovesdan (int)_region_size(&data),
295ad30f8e7SGabor Kovesdan (const char *)_region_head(&data),
296ad30f8e7SGabor Kovesdan (int)_region_size(&key),
297ad30f8e7SGabor Kovesdan (const char *)_region_head(&key));
298ad30f8e7SGabor Kovesdan _bcs_convert_to_upper(buf);
299ad30f8e7SGabor Kovesdan list[num] = strdup(buf);
300ad30f8e7SGabor Kovesdan if (list[num] == NULL) {
301ad30f8e7SGabor Kovesdan ret = errno;
302ad30f8e7SGabor Kovesdan goto quit3;
303ad30f8e7SGabor Kovesdan }
304ad30f8e7SGabor Kovesdan num++;
305ad30f8e7SGabor Kovesdan }
306ad30f8e7SGabor Kovesdan if (ret != ENOENT)
307ad30f8e7SGabor Kovesdan goto quit3;
308ad30f8e7SGabor Kovesdan /* get dir entries */
309ad30f8e7SGabor Kovesdan while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) {
310ad30f8e7SGabor Kovesdan if (!sorted)
311ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf), "%.*s",
312ad30f8e7SGabor Kovesdan (int)_region_size(&key),
313ad30f8e7SGabor Kovesdan (const char *)_region_head(&key));
314ad30f8e7SGabor Kovesdan else {
315ad30f8e7SGabor Kovesdan /* check duplicated entry */
316ad30f8e7SGabor Kovesdan char *p;
317ad30f8e7SGabor Kovesdan char buf1[PATH_MAX];
318ad30f8e7SGabor Kovesdan
319ad30f8e7SGabor Kovesdan snprintf(buf1, sizeof(buf1), "%.*s",
320ad30f8e7SGabor Kovesdan (int)_region_size(&data),
321ad30f8e7SGabor Kovesdan (const char *)_region_head(&data));
322ad30f8e7SGabor Kovesdan if ((p = strchr(buf1, '/')) != NULL)
323277a765cSBryan Drewery memmove(buf1, p + 1, strlen(p) - 1);
324ad30f8e7SGabor Kovesdan if ((p = strstr(buf1, ".esdb")) != NULL)
325ad30f8e7SGabor Kovesdan *p = '\0';
326ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf), "%s/%.*s", buf1,
327ad30f8e7SGabor Kovesdan (int)_region_size(&key),
328ad30f8e7SGabor Kovesdan (const char *)_region_head(&key));
329ad30f8e7SGabor Kovesdan }
330ad30f8e7SGabor Kovesdan _bcs_convert_to_upper(buf);
331ad30f8e7SGabor Kovesdan ret = _lookup_seq_lookup(cla, buf, NULL);
332ad30f8e7SGabor Kovesdan if (ret) {
333ad30f8e7SGabor Kovesdan if (ret != ENOENT)
334ad30f8e7SGabor Kovesdan goto quit3;
335ad30f8e7SGabor Kovesdan /* not duplicated */
336ad30f8e7SGabor Kovesdan list[num] = strdup(buf);
337ad30f8e7SGabor Kovesdan if (list[num] == NULL) {
338ad30f8e7SGabor Kovesdan ret = errno;
339ad30f8e7SGabor Kovesdan goto quit3;
340ad30f8e7SGabor Kovesdan }
341ad30f8e7SGabor Kovesdan num++;
342ad30f8e7SGabor Kovesdan }
343ad30f8e7SGabor Kovesdan }
344ad30f8e7SGabor Kovesdan if (ret != ENOENT)
345ad30f8e7SGabor Kovesdan goto quit3;
346ad30f8e7SGabor Kovesdan
347ad30f8e7SGabor Kovesdan ret = 0;
348ad30f8e7SGabor Kovesdan /* XXX: why reallocing the list space posteriorly?
349ad30f8e7SGabor Kovesdan shouldn't be done earlier? */
3509f36610fSPedro F. Giffuni q = reallocarray(list, num, sizeof(char *));
351ad30f8e7SGabor Kovesdan if (!q) {
352ad30f8e7SGabor Kovesdan ret = ENOMEM;
353ad30f8e7SGabor Kovesdan goto quit3;
354ad30f8e7SGabor Kovesdan }
355ad30f8e7SGabor Kovesdan list = q;
356ad30f8e7SGabor Kovesdan *rlist = list;
357ad30f8e7SGabor Kovesdan *rnum = num;
358ad30f8e7SGabor Kovesdan quit3:
359ad30f8e7SGabor Kovesdan if (ret)
360ad30f8e7SGabor Kovesdan _citrus_esdb_free_list(list, num);
361ad30f8e7SGabor Kovesdan _lookup_seq_close(cld);
362ad30f8e7SGabor Kovesdan quit1:
363ad30f8e7SGabor Kovesdan _lookup_seq_close(cla);
364ad30f8e7SGabor Kovesdan quit0:
365ad30f8e7SGabor Kovesdan return (ret);
366ad30f8e7SGabor Kovesdan }
367