/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Margo Seltzer.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334/*35* This package provides a dbm compatible interface to the new hashing36* package described in db(3).37*/3839#include <sys/param.h>4041#include <stdio.h>42#include <string.h>43#include <errno.h>4445#include <ndbm.h>46#include "hash.h"4748/*49* Returns:50* *DBM on success51* NULL on failure52*/53extern DBM *54dbm_open(const char *file, int flags, mode_t mode)55{56HASHINFO info;57char path[MAXPATHLEN];5859info.bsize = 4096;60info.ffactor = 40;61info.nelem = 1;62info.cachesize = 0;63info.hash = NULL;64info.lorder = 0;6566if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {67errno = ENAMETOOLONG;68return(NULL);69}70(void)strcpy(path, file);71(void)strcat(path, DBM_SUFFIX);72return ((DBM *)__hash_open(path, flags, mode, &info, 0));73}7475extern void76dbm_close(DBM *db)77{78(void)(db->close)(db);79}8081/*82* Returns:83* DATUM on success84* NULL on failure85*/86extern datum87dbm_fetch(DBM *db, datum key)88{89datum retdata;90int status;91DBT dbtkey, dbtretdata;9293dbtkey.data = key.dptr;94dbtkey.size = key.dsize;95status = (db->get)(db, &dbtkey, &dbtretdata, 0);96if (status) {97dbtretdata.data = NULL;98dbtretdata.size = 0;99}100retdata.dptr = dbtretdata.data;101retdata.dsize = dbtretdata.size;102return (retdata);103}104105/*106* Returns:107* DATUM on success108* NULL on failure109*/110extern datum111dbm_firstkey(DBM *db)112{113int status;114datum retkey;115DBT dbtretkey, dbtretdata;116117status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);118if (status)119dbtretkey.data = NULL;120retkey.dptr = dbtretkey.data;121retkey.dsize = dbtretkey.size;122return (retkey);123}124125/*126* Returns:127* DATUM on success128* NULL on failure129*/130extern datum131dbm_nextkey(DBM *db)132{133int status;134datum retkey;135DBT dbtretkey, dbtretdata;136137status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);138if (status)139dbtretkey.data = NULL;140retkey.dptr = dbtretkey.data;141retkey.dsize = dbtretkey.size;142return (retkey);143}144145/*146* Returns:147* 0 on success148* <0 failure149*/150extern int151dbm_delete(DBM *db, datum key)152{153int status;154DBT dbtkey;155156dbtkey.data = key.dptr;157dbtkey.size = key.dsize;158status = (db->del)(db, &dbtkey, 0);159if (status)160return (-1);161else162return (0);163}164165/*166* Returns:167* 0 on success168* <0 failure169* 1 if DBM_INSERT and entry exists170*/171extern int172dbm_store(DBM *db, datum key, datum data, int flags)173{174DBT dbtkey, dbtdata;175176dbtkey.data = key.dptr;177dbtkey.size = key.dsize;178dbtdata.data = data.dptr;179dbtdata.size = data.dsize;180return ((db->put)(db, &dbtkey, &dbtdata,181(flags == DBM_INSERT) ? R_NOOVERWRITE : 0));182}183184extern int185dbm_error(DBM *db)186{187HTAB *hp;188189hp = (HTAB *)db->internal;190return (hp->error);191}192193extern int194dbm_clearerr(DBM *db)195{196HTAB *hp;197198hp = (HTAB *)db->internal;199hp->error = 0;200return (0);201}202203extern int204dbm_dirfno(DBM *db)205{206return(((HTAB *)db->internal)->fp);207}208209210