1 /****************************************************************************
2 * Copyright (c) 2006 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey 2006 *
31 ****************************************************************************/
33 #include <curses.priv.h>
35 #include <hashed_db.h>
39 MODULE_ID("$Id: hashed_db.c,v 1.13 2006/08/19 19:48:38 tom Exp $")
41 #if HASHED_DB_API >= 2
49 _nc_db_open(const char *path, bool modify)
53 #if HASHED_DB_API >= 4
54 db_create(&result, NULL, 0);
60 modify ? DB_CREATE : DB_RDONLY,
62 #elif HASHED_DB_API >= 3
63 db_create(&result, NULL, 0);
68 modify ? DB_CREATE : DB_RDONLY,
70 #elif HASHED_DB_API >= 2
73 if ((code = db_open(path,
75 modify ? DB_CREATE : DB_RDONLY,
80 T(("cannot open %s: %s", path, strerror(code)));
83 T(("opened %s", path));
87 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
92 T(("opened %s", path));
99 * Close the database. Do not attempt to use the 'db' handle after this call.
102 _nc_db_close(DB * db)
106 #if HASHED_DB_API >= 2
107 result = db->close(db, 0);
109 result = db->close(db);
115 * Write a record to the database.
117 * Returns 0 on success.
119 * FIXME: the FreeBSD cap_mkdb program assumes the database could have
120 * duplicates. There appears to be no good reason for that (review/fix).
123 _nc_db_put(DB * db, DBT * key, DBT * data)
126 #if HASHED_DB_API >= 2
127 /* remove any pre-existing value, since we do not want duplicates */
128 (void) db->del(db, NULL, key, 0);
129 result = db->put(db, NULL, key, data, DB_NOOVERWRITE);
131 result = db->put(db, key, data, R_NOOVERWRITE);
137 * Read a record from the database.
139 * Returns 0 on success.
142 _nc_db_get(DB * db, DBT * key, DBT * data)
146 memset(data, 0, sizeof(*data));
147 #if HASHED_DB_API >= 2
148 result = db->get(db, NULL, key, data, 0);
150 result = db->get(db, key, data, 0);
156 * Read the first record from the database, ignoring order.
158 * Returns 0 on success.
161 _nc_db_first(DB * db, DBT * key, DBT * data)
165 memset(key, 0, sizeof(*key));
166 memset(data, 0, sizeof(*data));
167 #if HASHED_DB_API >= 2
168 if ((result = db->cursor(db, NULL, &cursor, 0)) == 0) {
169 result = cursor->c_get(cursor, key, data, DB_FIRST);
172 result = db->seq(db, key, data, 0);
178 * Read the next record from the database, ignoring order.
180 * Returns 0 on success.
183 _nc_db_next(DB * db, DBT * key, DBT * data)
187 #if HASHED_DB_API >= 2
190 result = cursor->c_get(cursor, key, data, DB_NEXT);
195 result = db->seq(db, key, data, 0);
201 * Check if a record is a terminfo index record. Index records are those that
202 * contain only an alias pointing to a list of aliases.
205 _nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
208 int used = data->size - 1;
209 char *have = (char *) data->data;
216 * Update params in any case for consistency with _nc_db_have_data().
224 * Check if a record is the terminfo data record. Ignore index records, e.g.,
225 * those that contain only an alias pointing to a list of aliases.
228 _nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
231 int used = data->size - 1;
232 char *have = (char *) data->data;
235 if (data->size > key->size
236 && IS_TIC_MAGIC(have)) {
241 * Update params in any case to make it simple to follow a index record
242 * to the data record.
260 #endif /* USE_HASHED_DB */