Path: blob/main/crypto/krb5/src/plugins/kdb/db2/libdb2/hash/dbm.c
34928 views
/*-1* Copyright (c) 1990, 19932* The Regents of the University of California. All rights reserved.3*4* This code is derived from software contributed to Berkeley by5* Margo Seltzer.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. All advertising materials mentioning features or use of this software16* must display the following acknowledgement:17* This product includes software developed by the University of18* California, Berkeley and its contributors.19* 4. Neither the name of the University nor the names of its contributors20* may be used to endorse or promote products derived from this software21* without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*/3536#if defined(LIBC_SCCS) && !defined(lint)37static char sccsid[] = "@(#)dbm.c 8.6 (Berkeley) 11/7/95";38#endif /* LIBC_SCCS and not lint */3940#include "db-int.h"4142#include <sys/param.h>4344#include <fcntl.h>45#include <stdio.h>46#include <string.h>4748#include "db-ndbm.h"49#include "db-dbm.h"50#include "hash.h"5152/* If the two size fields of datum and DBMT are not equal, then53* casting between structures will result in stack garbage being54* transferred. Has been observed for DEC Alpha OSF, but will handle55* the general case.56*/5758#define NEED_COPY5960/*61*62* This package provides dbm and ndbm compatible interfaces to DB.63* First are the DBM routines, which call the NDBM routines, and64* the NDBM routines, which call the DB routines.65*/66static DBM *__cur_db;6768static void no_open_db __P((void));6970int71kdb2_dbminit(char *file)72{73if (__cur_db != NULL)74(void)kdb2_dbm_close(__cur_db);75if ((__cur_db = kdb2_dbm_open(file, O_RDWR|O_BINARY, 0)) != NULL)76return (0);77if ((__cur_db = kdb2_dbm_open(file, O_RDONLY|O_BINARY, 0)) != NULL)78return (0);79return (-1);80}8182datum83kdb2_fetch(datum key)84{85datum item;8687if (__cur_db == NULL) {88no_open_db();89item.dptr = 0;90item.dsize = 0;91return (item);92}93return (kdb2_dbm_fetch(__cur_db, key));94}9596datum97kdb2_firstkey(void)98{99datum item;100101if (__cur_db == NULL) {102no_open_db();103item.dptr = 0;104item.dsize = 0;105return (item);106}107return (kdb2_dbm_firstkey(__cur_db));108}109110datum111kdb2_nextkey(datum key)112{113datum item;114115if (__cur_db == NULL) {116no_open_db();117item.dptr = 0;118item.dsize = 0;119return (item);120}121return (kdb2_dbm_nextkey(__cur_db));122}123124int125kdb2_delete(datum key)126{127if (__cur_db == NULL) {128no_open_db();129return (-1);130}131return (kdb2_dbm_delete(__cur_db, key));132}133134int135kdb2_store(datum key, datum dat)136{137if (__cur_db == NULL) {138no_open_db();139return (-1);140}141return (kdb2_dbm_store(__cur_db, key, dat, DBM_REPLACE));142}143144static void145no_open_db(void)146{147(void)fprintf(stderr, "dbm: no open database.\n");148}149150/*151* Returns:152* *DBM on success153* NULL on failure154*/155DBM *156kdb2_dbm_open(const char *file, int flags, int mode)157{158HASHINFO info;159char path[MAXPATHLEN];160161info.bsize = 4096;162info.ffactor = 40;163info.nelem = 1;164info.cachesize = 0;165info.hash = NULL;166info.lorder = 0;167(void)strncpy(path, file, sizeof(path) - 1);168path[sizeof(path) - 1] = '\0';169(void)strncat(path, DBM_SUFFIX, sizeof(path) - 1 - strlen(path));170return ((DBM *)__hash_open(path, flags, mode, &info, 0));171}172173/*174* Returns:175* Nothing.176*/177void178kdb2_dbm_close(DBM *db)179{180(void)(db->close)(db);181}182183/*184* Returns:185* DATUM on success186* NULL on failure187*/188datum189kdb2_dbm_fetch(DBM *db, datum key)190{191datum retval;192int status;193194#ifdef NEED_COPY195DBT k, r;196197k.data = key.dptr;198k.size = key.dsize;199status = (db->get)(db, &k, &r, 0);200retval.dptr = r.data;201retval.dsize = r.size;202#else203status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);204#endif205if (status) {206retval.dptr = NULL;207retval.dsize = 0;208}209return (retval);210}211212/*213* Returns:214* DATUM on success215* NULL on failure216*/217datum218kdb2_dbm_firstkey(DBM *db)219{220int status;221datum retkey;222223#ifdef NEED_COPY224DBT k, r;225226status = (db->seq)(db, &k, &r, R_FIRST);227retkey.dptr = k.data;228retkey.dsize = k.size;229#else230datum retdata;231232status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);233#endif234if (status)235retkey.dptr = NULL;236return (retkey);237}238239/*240* Returns:241* DATUM on success242* NULL on failure243*/244datum245kdb2_dbm_nextkey(DBM *db)246{247int status;248datum retkey;249250#ifdef NEED_COPY251DBT k, r;252253status = (db->seq)(db, &k, &r, R_NEXT);254retkey.dptr = k.data;255retkey.dsize = k.size;256#else257datum retdata;258259status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);260#endif261if (status)262retkey.dptr = NULL;263return (retkey);264}265266/*267* Returns:268* 0 on success269* <0 failure270*/271int272kdb2_dbm_delete(DBM *db, datum key)273{274int status;275276#ifdef NEED_COPY277DBT k;278279k.data = key.dptr;280k.size = key.dsize;281status = (db->del)(db, &k, 0);282#else283status = (db->del)(db, (DBT *)&key, 0);284#endif285if (status)286return (-1);287else288return (0);289}290291/*292* Returns:293* 0 on success294* <0 failure295* 1 if DBM_INSERT and entry exists296*/297int298kdb2_dbm_store(DBM *db, datum key, datum content, int flags)299{300#ifdef NEED_COPY301DBT k, c;302303k.data = key.dptr;304k.size = key.dsize;305c.data = content.dptr;306c.size = content.dsize;307return ((db->put)(db, &k, &c,308(flags == DBM_INSERT) ? R_NOOVERWRITE : 0));309#else310return ((db->put)(db, (DBT *)&key, (DBT *)&content,311(flags == DBM_INSERT) ? R_NOOVERWRITE : 0));312#endif313}314315int316kdb2_dbm_error(DBM *db)317{318HTAB *hp;319320hp = (HTAB *)db->internal;321return (hp->local_errno);322}323324int325kdb2_dbm_clearerr(DBM *db)326{327HTAB *hp;328329hp = (HTAB *)db->internal;330hp->local_errno = 0;331return (0);332}333334int335kdb2_dbm_dirfno(DBM *db)336{337return(((HTAB *)db->internal)->fp);338}339340341