1 /* $NetBSD: ndbm_wrap.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
4 * Copyright (c) 2002 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "ndbm_wrap.h"
39 #if defined(HAVE_DBHEADER)
41 #elif defined(HAVE_DB5_DB_H)
43 #elif defined(HAVE_DB4_DB_H)
45 #elif defined(HAVE_DB3_DB_H)
56 /* XXX undefine open so this works on Solaris with large file support */
59 #define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
60 #define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
61 #define RETURN(X) return ((X) == 0) ? 0 : -1
67 #define D(X) ((DB*)(X))
69 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
73 D(db
)->close(D(db
), 0);
80 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
81 dbm_delete (DBM
*db
, datum dkey
)
84 DATUM2DBT(&dkey
, &key
);
86 RETURN(D(db
)->del(D(db
), NULL
, &key
, 0));
88 RETURN(D(db
)->del(D(db
), &key
, 0));
93 dbm_fetch (DBM
*db
, datum dkey
)
97 DATUM2DBT(&dkey
, &key
);
102 &key
, &value
, 0) != 0) {
107 DBT2DATUM(&value
, &dvalue
);
113 dbm_get (DB
*db
, int flags
)
119 db
->cursor(db
, NULL
, &cursor
, 0);
120 if(cursor
->c_get(cursor
, &key
, &value
, flags
) != 0) {
124 DBT2DATUM(&value
, &datum
);
126 db
->seq(db
, &key
, &value
, flags
);
127 DBT2DATUM(&value
, &datum
);
133 #define DB_FIRST R_FIRST
134 #define DB_NEXT R_NEXT
135 #define DB_NOOVERWRITE R_NOOVERWRITE
136 #define DB_KEYEXIST 1
139 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
140 dbm_firstkey (DBM
*db
)
142 return dbm_get(D(db
), DB_FIRST
);
145 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
146 dbm_nextkey (DBM
*db
)
148 return dbm_get(D(db
), DB_NEXT
);
151 ROKEN_LIB_FUNCTION DBM
* ROKEN_LIB_CALL
152 dbm_open (const char *file
, int flags
, mode_t mode
)
158 char *fn
= malloc(strlen(file
) + 4);
165 myflags
|= DB_CREATE
;
170 if (flags
& O_RDONLY
)
171 myflags
|= DB_RDONLY
;
174 myflags
|= DB_TRUNCATE
;
175 if(db_create(&db
, NULL
, 0) != 0) {
180 #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
181 if(db
->open(db
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
) != 0) {
183 if(db
->open(db
, fn
, NULL
, DB_BTREE
, myflags
, mode
) != 0) {
190 db
= dbopen(fn
, flags
, mode
, DB_BTREE
, NULL
);
196 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
197 dbm_store (DBM
*db
, datum dkey
, datum dvalue
, int flags
)
202 if((flags
& DBM_REPLACE
) == 0)
203 myflags
|= DB_NOOVERWRITE
;
204 DATUM2DBT(&dkey
, &key
);
205 DATUM2DBT(&dvalue
, &value
);
206 ret
= D(db
)->put(D(db
),
210 &key
, &value
, myflags
);
211 if(ret
== DB_KEYEXIST
)
216 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
222 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
223 dbm_clearerr (DBM
*db
)