1*0a6a1f1dSLionel Sambuc /* $NetBSD: ndbm_wrap.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2002 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include "ndbm_wrap.h"
39ebfedea0SLionel Sambuc #if defined(HAVE_DBHEADER)
40ebfedea0SLionel Sambuc #include <db.h>
41ebfedea0SLionel Sambuc #elif defined(HAVE_DB5_DB_H)
42ebfedea0SLionel Sambuc #include <db5/db.h>
43ebfedea0SLionel Sambuc #elif defined(HAVE_DB4_DB_H)
44ebfedea0SLionel Sambuc #include <db4/db.h>
45ebfedea0SLionel Sambuc #elif defined(HAVE_DB3_DB_H)
46ebfedea0SLionel Sambuc #include <db3/db.h>
47ebfedea0SLionel Sambuc #else
48ebfedea0SLionel Sambuc #include <db.h>
49ebfedea0SLionel Sambuc #endif
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc #include <stdio.h>
52ebfedea0SLionel Sambuc #include <stdlib.h>
53ebfedea0SLionel Sambuc #include <string.h>
54ebfedea0SLionel Sambuc #include <fcntl.h>
55ebfedea0SLionel Sambuc
56ebfedea0SLionel Sambuc /* XXX undefine open so this works on Solaris with large file support */
57ebfedea0SLionel Sambuc #undef open
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
60ebfedea0SLionel Sambuc #define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
61ebfedea0SLionel Sambuc #define RETURN(X) return ((X) == 0) ? 0 : -1
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc #ifdef HAVE_DB3
64ebfedea0SLionel Sambuc static DBC *cursor;
65ebfedea0SLionel Sambuc #endif
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc #define D(X) ((DB*)(X))
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
dbm_close(DBM * db)70ebfedea0SLionel Sambuc dbm_close (DBM *db)
71ebfedea0SLionel Sambuc {
72ebfedea0SLionel Sambuc #ifdef HAVE_DB3
73ebfedea0SLionel Sambuc D(db)->close(D(db), 0);
74ebfedea0SLionel Sambuc cursor = NULL;
75ebfedea0SLionel Sambuc #else
76ebfedea0SLionel Sambuc D(db)->close(D(db));
77ebfedea0SLionel Sambuc #endif
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc
80ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
dbm_delete(DBM * db,datum dkey)81ebfedea0SLionel Sambuc dbm_delete (DBM *db, datum dkey)
82ebfedea0SLionel Sambuc {
83ebfedea0SLionel Sambuc DBT key;
84ebfedea0SLionel Sambuc DATUM2DBT(&dkey, &key);
85ebfedea0SLionel Sambuc #ifdef HAVE_DB3
86ebfedea0SLionel Sambuc RETURN(D(db)->del(D(db), NULL, &key, 0));
87ebfedea0SLionel Sambuc #else
88ebfedea0SLionel Sambuc RETURN(D(db)->del(D(db), &key, 0));
89ebfedea0SLionel Sambuc #endif
90ebfedea0SLionel Sambuc }
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc datum
dbm_fetch(DBM * db,datum dkey)93ebfedea0SLionel Sambuc dbm_fetch (DBM *db, datum dkey)
94ebfedea0SLionel Sambuc {
95ebfedea0SLionel Sambuc datum dvalue;
96ebfedea0SLionel Sambuc DBT key, value;
97ebfedea0SLionel Sambuc DATUM2DBT(&dkey, &key);
98ebfedea0SLionel Sambuc if(D(db)->get(D(db),
99ebfedea0SLionel Sambuc #ifdef HAVE_DB3
100ebfedea0SLionel Sambuc NULL,
101ebfedea0SLionel Sambuc #endif
102ebfedea0SLionel Sambuc &key, &value, 0) != 0) {
103ebfedea0SLionel Sambuc dvalue.dptr = NULL;
104ebfedea0SLionel Sambuc dvalue.dsize = 0;
105ebfedea0SLionel Sambuc }
106ebfedea0SLionel Sambuc else
107ebfedea0SLionel Sambuc DBT2DATUM(&value, &dvalue);
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc return dvalue;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc static datum
dbm_get(DB * db,int flags)113ebfedea0SLionel Sambuc dbm_get (DB *db, int flags)
114ebfedea0SLionel Sambuc {
115ebfedea0SLionel Sambuc DBT key, value;
116ebfedea0SLionel Sambuc datum datum;
117ebfedea0SLionel Sambuc #ifdef HAVE_DB3
118ebfedea0SLionel Sambuc if(cursor == NULL)
119ebfedea0SLionel Sambuc db->cursor(db, NULL, &cursor, 0);
120ebfedea0SLionel Sambuc if(cursor->c_get(cursor, &key, &value, flags) != 0) {
121ebfedea0SLionel Sambuc datum.dptr = NULL;
122ebfedea0SLionel Sambuc datum.dsize = 0;
123ebfedea0SLionel Sambuc } else
124ebfedea0SLionel Sambuc DBT2DATUM(&value, &datum);
125ebfedea0SLionel Sambuc #else
126ebfedea0SLionel Sambuc db->seq(db, &key, &value, flags);
127*0a6a1f1dSLionel Sambuc DBT2DATUM(&value, &datum);
128ebfedea0SLionel Sambuc #endif
129ebfedea0SLionel Sambuc return datum;
130ebfedea0SLionel Sambuc }
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc #ifndef DB_FIRST
133ebfedea0SLionel Sambuc #define DB_FIRST R_FIRST
134ebfedea0SLionel Sambuc #define DB_NEXT R_NEXT
135ebfedea0SLionel Sambuc #define DB_NOOVERWRITE R_NOOVERWRITE
136ebfedea0SLionel Sambuc #define DB_KEYEXIST 1
137ebfedea0SLionel Sambuc #endif
138ebfedea0SLionel Sambuc
139ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
dbm_firstkey(DBM * db)140ebfedea0SLionel Sambuc dbm_firstkey (DBM *db)
141ebfedea0SLionel Sambuc {
142ebfedea0SLionel Sambuc return dbm_get(D(db), DB_FIRST);
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
dbm_nextkey(DBM * db)146ebfedea0SLionel Sambuc dbm_nextkey (DBM *db)
147ebfedea0SLionel Sambuc {
148ebfedea0SLionel Sambuc return dbm_get(D(db), DB_NEXT);
149ebfedea0SLionel Sambuc }
150ebfedea0SLionel Sambuc
151ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL
dbm_open(const char * file,int flags,mode_t mode)152ebfedea0SLionel Sambuc dbm_open (const char *file, int flags, mode_t mode)
153ebfedea0SLionel Sambuc {
154*0a6a1f1dSLionel Sambuc #ifdef HAVE_DB3
155ebfedea0SLionel Sambuc int myflags = 0;
156*0a6a1f1dSLionel Sambuc #endif
157*0a6a1f1dSLionel Sambuc DB *db;
158ebfedea0SLionel Sambuc char *fn = malloc(strlen(file) + 4);
159ebfedea0SLionel Sambuc if(fn == NULL)
160ebfedea0SLionel Sambuc return NULL;
161ebfedea0SLionel Sambuc strcpy(fn, file);
162ebfedea0SLionel Sambuc strcat(fn, ".db");
163ebfedea0SLionel Sambuc #ifdef HAVE_DB3
164ebfedea0SLionel Sambuc if (flags & O_CREAT)
165ebfedea0SLionel Sambuc myflags |= DB_CREATE;
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc if (flags & O_EXCL)
168ebfedea0SLionel Sambuc myflags |= DB_EXCL;
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc if (flags & O_RDONLY)
171ebfedea0SLionel Sambuc myflags |= DB_RDONLY;
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc if (flags & O_TRUNC)
174ebfedea0SLionel Sambuc myflags |= DB_TRUNCATE;
175ebfedea0SLionel Sambuc if(db_create(&db, NULL, 0) != 0) {
176ebfedea0SLionel Sambuc free(fn);
177ebfedea0SLionel Sambuc return NULL;
178ebfedea0SLionel Sambuc }
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
181ebfedea0SLionel Sambuc if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) {
182ebfedea0SLionel Sambuc #else
183ebfedea0SLionel Sambuc if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) {
184ebfedea0SLionel Sambuc #endif
185ebfedea0SLionel Sambuc free(fn);
186ebfedea0SLionel Sambuc db->close(db, 0);
187ebfedea0SLionel Sambuc return NULL;
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc #else
190ebfedea0SLionel Sambuc db = dbopen(fn, flags, mode, DB_BTREE, NULL);
191ebfedea0SLionel Sambuc #endif
192ebfedea0SLionel Sambuc free(fn);
193ebfedea0SLionel Sambuc return (DBM*)db;
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
197ebfedea0SLionel Sambuc dbm_store (DBM *db, datum dkey, datum dvalue, int flags)
198ebfedea0SLionel Sambuc {
199ebfedea0SLionel Sambuc int ret;
200ebfedea0SLionel Sambuc DBT key, value;
201ebfedea0SLionel Sambuc int myflags = 0;
202ebfedea0SLionel Sambuc if((flags & DBM_REPLACE) == 0)
203ebfedea0SLionel Sambuc myflags |= DB_NOOVERWRITE;
204ebfedea0SLionel Sambuc DATUM2DBT(&dkey, &key);
205ebfedea0SLionel Sambuc DATUM2DBT(&dvalue, &value);
206ebfedea0SLionel Sambuc ret = D(db)->put(D(db),
207ebfedea0SLionel Sambuc #ifdef HAVE_DB3
208ebfedea0SLionel Sambuc NULL,
209ebfedea0SLionel Sambuc #endif
210ebfedea0SLionel Sambuc &key, &value, myflags);
211ebfedea0SLionel Sambuc if(ret == DB_KEYEXIST)
212ebfedea0SLionel Sambuc return 1;
213ebfedea0SLionel Sambuc RETURN(ret);
214ebfedea0SLionel Sambuc }
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
217ebfedea0SLionel Sambuc dbm_error (DBM *db)
218ebfedea0SLionel Sambuc {
219ebfedea0SLionel Sambuc return 0;
220ebfedea0SLionel Sambuc }
221ebfedea0SLionel Sambuc
222ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
223ebfedea0SLionel Sambuc dbm_clearerr (DBM *db)
224ebfedea0SLionel Sambuc {
225ebfedea0SLionel Sambuc return 0;
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc
228