1 /****************************************************************************
2 * Copyright (c) 2006,2008 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-on *
31 ****************************************************************************/
33 #include <curses.priv.h>
35 #include <hashed_db.h>
39 MODULE_ID("$Id: hashed_db.c,v 1.14 2008/12/13 20:59:02 tom Exp $")
41 #if HASHED_DB_API >= 2
49 _nc_db_open(const char *path
, bool modify
)
54 #if HASHED_DB_API >= 4
55 db_create(&result
, NULL
, 0);
56 if ((code
= result
->open(result
,
61 modify
? DB_CREATE
: DB_RDONLY
,
65 #elif HASHED_DB_API >= 3
66 db_create(&result
, NULL
, 0);
67 if ((code
= result
->open(result
,
71 modify
? DB_CREATE
: DB_RDONLY
,
75 #elif HASHED_DB_API >= 2
76 if ((code
= db_open(path
,
78 modify
? DB_CREATE
: DB_RDONLY
,
86 if ((result
= dbopen(path
,
87 modify
? (O_CREAT
| O_RDWR
) : O_RDONLY
,
95 T(("opened %s", path
));
97 T(("cannot open %s: %s", path
, strerror(code
)));
103 * Close the database. Do not attempt to use the 'db' handle after this call.
106 _nc_db_close(DB
* db
)
110 #if HASHED_DB_API >= 2
111 result
= db
->close(db
, 0);
113 result
= db
->close(db
);
119 * Write a record to the database.
121 * Returns 0 on success.
123 * FIXME: the FreeBSD cap_mkdb program assumes the database could have
124 * duplicates. There appears to be no good reason for that (review/fix).
127 _nc_db_put(DB
* db
, DBT
* key
, DBT
* data
)
130 #if HASHED_DB_API >= 2
131 /* remove any pre-existing value, since we do not want duplicates */
132 (void) db
->del(db
, NULL
, key
, 0);
133 result
= db
->put(db
, NULL
, key
, data
, DB_NOOVERWRITE
);
135 result
= db
->put(db
, key
, data
, R_NOOVERWRITE
);
141 * Read a record from the database.
143 * Returns 0 on success.
146 _nc_db_get(DB
* db
, DBT
* key
, DBT
* data
)
150 memset(data
, 0, sizeof(*data
));
151 #if HASHED_DB_API >= 2
152 result
= db
->get(db
, NULL
, key
, data
, 0);
154 result
= db
->get(db
, key
, data
, 0);
160 * Read the first record from the database, ignoring order.
162 * Returns 0 on success.
165 _nc_db_first(DB
* db
, DBT
* key
, DBT
* data
)
169 memset(key
, 0, sizeof(*key
));
170 memset(data
, 0, sizeof(*data
));
171 #if HASHED_DB_API >= 2
172 if ((result
= db
->cursor(db
, NULL
, &cursor
, 0)) == 0) {
173 result
= cursor
->c_get(cursor
, key
, data
, DB_FIRST
);
176 result
= db
->seq(db
, key
, data
, 0);
182 * Read the next record from the database, ignoring order.
184 * Returns 0 on success.
187 _nc_db_next(DB
* db
, DBT
* key
, DBT
* data
)
191 #if HASHED_DB_API >= 2
194 result
= cursor
->c_get(cursor
, key
, data
, DB_NEXT
);
199 result
= db
->seq(db
, key
, data
, 0);
205 * Check if a record is a terminfo index record. Index records are those that
206 * contain only an alias pointing to a list of aliases.
209 _nc_db_have_index(DBT
* key
, DBT
* data
, char **buffer
, int *size
)
212 int used
= data
->size
- 1;
213 char *have
= (char *) data
->data
;
220 * Update params in any case for consistency with _nc_db_have_data().
228 * Check if a record is the terminfo data record. Ignore index records, e.g.,
229 * those that contain only an alias pointing to a list of aliases.
232 _nc_db_have_data(DBT
* key
, DBT
* data
, char **buffer
, int *size
)
235 int used
= data
->size
- 1;
236 char *have
= (char *) data
->data
;
239 if (data
->size
> key
->size
240 && IS_TIC_MAGIC(have
)) {
245 * Update params in any case to make it simple to follow a index record
246 * to the data record.
264 #endif /* USE_HASHED_DB */