1/* $NetBSD: ndbm_wrap.c,v 1.3 2023/06/19 21:41:45 christos Exp $ */ 2 3/* 4 * Copyright (c) 2002 Kungliga Tekniska H��gskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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 33 * SUCH DAMAGE. 34 */ 35 36#include <config.h> 37 38#include "ndbm_wrap.h" 39#if defined(HAVE_DBHEADER) 40#include <db.h> 41#elif defined(HAVE_DB6_DB_H) 42#include <db6/db.h> 43#elif defined(HAVE_DB5_DB_H) 44#include <db5/db.h> 45#elif defined(HAVE_DB4_DB_H) 46#include <db4/db.h> 47#elif defined(HAVE_DB3_DB_H) 48#include <db3/db.h> 49#else 50#include <db.h> 51#endif 52 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <fcntl.h> 57 58/* XXX undefine open so this works on Solaris with large file support */ 59#undef open 60 61#define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0) 62#define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0) 63#define RETURN(X) return ((X) == 0) ? 0 : -1 64 65#ifdef HAVE_DB3 66static DBC *cursor; 67#endif 68 69#define D(X) ((DB*)(X)) 70 71ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 72dbm_close (DBM *db) 73{ 74#ifdef HAVE_DB3 75 D(db)->close(D(db), 0); 76 cursor = NULL; 77#else 78 D(db)->close(D(db)); 79#endif 80} 81 82ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 83dbm_delete (DBM *db, datum dkey) 84{ 85 DBT key; 86 DATUM2DBT(&dkey, &key); 87#ifdef HAVE_DB3 88 RETURN(D(db)->del(D(db), NULL, &key, 0)); 89#else 90 RETURN(D(db)->del(D(db), &key, 0)); 91#endif 92} 93 94datum 95dbm_fetch (DBM *db, datum dkey) 96{ 97 datum dvalue; 98 DBT key, value; 99 DATUM2DBT(&dkey, &key); 100 if(D(db)->get(D(db), 101#ifdef HAVE_DB3 102 NULL, 103#endif 104 &key, &value, 0) != 0) { 105 dvalue.dptr = NULL; 106 dvalue.dsize = 0; 107 } 108 else 109 DBT2DATUM(&value, &dvalue); 110 111 return dvalue; 112} 113 114static datum 115dbm_get (DB *db, int flags) 116{ 117 DBT key, value; 118 datum d; 119#ifdef HAVE_DB3 120 if(cursor == NULL) 121 db->cursor(db, NULL, &cursor, 0); 122 if(cursor->c_get(cursor, &key, &value, flags) != 0) { 123 d.dptr = NULL; 124 d.dsize = 0; 125 } else 126 DBT2DATUM(&value, &d); 127#else 128 db->seq(db, &key, &value, flags); 129 DBT2DATUM(&value, &d); 130#endif 131 return d; 132} 133 134#ifndef DB_FIRST 135#define DB_FIRST R_FIRST 136#define DB_NEXT R_NEXT 137#define DB_NOOVERWRITE R_NOOVERWRITE 138#define DB_KEYEXIST 1 139#endif 140 141ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL 142dbm_firstkey (DBM *db) 143{ 144 return dbm_get(D(db), DB_FIRST); 145} 146 147ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL 148dbm_nextkey (DBM *db) 149{ 150 return dbm_get(D(db), DB_NEXT); 151} 152 153ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL 154dbm_open (const char *file, int flags, mode_t mode) 155{ 156#ifdef HAVE_DB3 157 int myflags = 0; 158#endif 159 DB *db; 160 char *fn = malloc(strlen(file) + 4); 161 if(fn == NULL) 162 return NULL; 163 strcpy(fn, file); 164 strcat(fn, ".db"); 165#ifdef HAVE_DB3 166 if (flags & O_CREAT) 167 myflags |= DB_CREATE; 168 169 if (flags & O_EXCL) 170 myflags |= DB_EXCL; 171 172 if (flags & O_RDONLY) 173 myflags |= DB_RDONLY; 174 175 if (flags & O_TRUNC) 176 myflags |= DB_TRUNCATE; 177 if(db_create(&db, NULL, 0) != 0) { 178 free(fn); 179 return NULL; 180 } 181 182#if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0) 183 if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) { 184#else 185 if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) { 186#endif 187 free(fn); 188 db->close(db, 0); 189 return NULL; 190 } 191#else 192 db = dbopen(fn, flags, mode, DB_BTREE, NULL); 193#endif 194 free(fn); 195 return (DBM*)db; 196} 197 198ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 199dbm_store (DBM *db, datum dkey, datum dvalue, int flags) 200{ 201 int ret; 202 DBT key, value; 203 int myflags = 0; 204 if((flags & DBM_REPLACE) == 0) 205 myflags |= DB_NOOVERWRITE; 206 DATUM2DBT(&dkey, &key); 207 DATUM2DBT(&dvalue, &value); 208 ret = D(db)->put(D(db), 209#ifdef HAVE_DB3 210 NULL, 211#endif 212&key, &value, myflags); 213 if(ret == DB_KEYEXIST) 214 return 1; 215 RETURN(ret); 216} 217 218ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 219dbm_error (DBM *db) 220{ 221 return 0; 222} 223 224ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 225dbm_clearerr (DBM *db) 226{ 227 return 0; 228} 229 230