2
* Copyright (c) 1999 Hideki Sakurada
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
24
/* BerkeleyDB environment */
25
int env_initialized = 0;
27
void errcall(const char *, char *);
30
struct dic *dic_db_open(struct dic *d, char *path) {
31
struct dic_db_internal *internal;
33
if (!env_initialized) {
34
env.db_errcall = errcall;
35
if ((errno = db_appinit(NULL, NULL, &env, DB_USE_ENVIRON)) != 0) {
36
err(LOG_ERR, "dic_db_open: db_appinit failed\n");
42
if ((internal = malloc(sizeof(struct dic_db_internal))) == NULL) {
43
err(LOG_ERR, "dic_db_open(%s): %s\n", path, strerror(errno));
46
memset(internal, 0, sizeof(struct dic_db_internal));
49
db_open(path, DB_UNKNOWN, DB_RDONLY, 0, &env, NULL, &(internal->db)))
51
err(LOG_ERR, "dic_db_open(%s): %s\n", path, strerror(errno));
54
d->internal = (void *)internal;
55
d->search = dic_db_search;
56
d->close = dic_db_close;
60
char *dic_db_search(struct dic *d, char *keystr, int keylen) {
61
struct dic_db_internal *internal = (struct dic_db_internal *)(d->internal);
62
DB *db = internal->db;
71
data.ulen = DIC_BUFSIZE - 1; /* -1 for '\0' */
72
data.flags = DB_DBT_USERMEM;
74
if ((errno = db->get(db, NULL, &key, &data, 0)) == DB_NOTFOUND) {
76
} else if (errno == 0) { /* found */
77
*((char *)(data.data) + data.size) = '\0';
78
return (char *)(data.data);
80
err(LOG_WARNING, "dic_db_search: %s (may be too long entry)\n",
87
int dic_db_close(struct dic *d) {
88
DB *db = ((struct dic_db_internal *)(d->internal))->db;
89
if ((errno = db->close(db, 0)) != 0) {
90
err(LOG_ERR, "dic_db_close: %s\n", strerror(errno));
99
void errcall(const char *foo, char *message) {
100
err(LOG_ERR, "dic_db: %s\n", message);