xref: /freebsd/lib/libc/db/hash/ndbm.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Margo Seltzer.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes /*
3658f0484fSRodney W. Grimes  * This package provides a dbm compatible interface to the new hashing
3758f0484fSRodney W. Grimes  * package described in db(3).
3858f0484fSRodney W. Grimes  */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <sys/param.h>
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <stdio.h>
4358f0484fSRodney W. Grimes #include <string.h>
44d53ec6c0SDaniel O'Callaghan #include <errno.h>
4558f0484fSRodney W. Grimes 
46f1e396bcSPaul Traina #include <ndbm.h>
4758f0484fSRodney W. Grimes #include "hash.h"
4858f0484fSRodney W. Grimes 
4958f0484fSRodney W. Grimes /*
5058f0484fSRodney W. Grimes  * Returns:
5158f0484fSRodney W. Grimes  * 	*DBM on success
5258f0484fSRodney W. Grimes  *	 NULL on failure
5358f0484fSRodney W. Grimes  */
5458f0484fSRodney W. Grimes extern DBM *
dbm_open(const char * file,int flags,mode_t mode)55de1d2695SEd Schouten dbm_open(const char *file, int flags, mode_t mode)
5658f0484fSRodney W. Grimes {
5758f0484fSRodney W. Grimes 	HASHINFO info;
5858f0484fSRodney W. Grimes 	char path[MAXPATHLEN];
5958f0484fSRodney W. Grimes 
6058f0484fSRodney W. Grimes 	info.bsize = 4096;
6158f0484fSRodney W. Grimes 	info.ffactor = 40;
6258f0484fSRodney W. Grimes 	info.nelem = 1;
63f1e396bcSPaul Traina 	info.cachesize = 0;
6458f0484fSRodney W. Grimes 	info.hash = NULL;
6558f0484fSRodney W. Grimes 	info.lorder = 0;
66d53ec6c0SDaniel O'Callaghan 
67d53ec6c0SDaniel O'Callaghan 	if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
68d53ec6c0SDaniel O'Callaghan 		errno = ENAMETOOLONG;
69d53ec6c0SDaniel O'Callaghan 		return(NULL);
70d53ec6c0SDaniel O'Callaghan 	}
7158f0484fSRodney W. Grimes 	(void)strcpy(path, file);
7258f0484fSRodney W. Grimes 	(void)strcat(path, DBM_SUFFIX);
7358f0484fSRodney W. Grimes 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
7458f0484fSRodney W. Grimes }
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes extern void
dbm_close(DBM * db)77d9556fb1SCraig Rodrigues dbm_close(DBM *db)
7858f0484fSRodney W. Grimes {
7958f0484fSRodney W. Grimes 	(void)(db->close)(db);
8058f0484fSRodney W. Grimes }
8158f0484fSRodney W. Grimes 
8258f0484fSRodney W. Grimes /*
8358f0484fSRodney W. Grimes  * Returns:
8458f0484fSRodney W. Grimes  *	DATUM on success
8558f0484fSRodney W. Grimes  *	NULL on failure
8658f0484fSRodney W. Grimes  */
8758f0484fSRodney W. Grimes extern datum
dbm_fetch(DBM * db,datum key)88d9556fb1SCraig Rodrigues dbm_fetch(DBM *db, datum key)
8958f0484fSRodney W. Grimes {
908618ef52SPaul Traina 	datum retdata;
9158f0484fSRodney W. Grimes 	int status;
928618ef52SPaul Traina 	DBT dbtkey, dbtretdata;
9358f0484fSRodney W. Grimes 
948618ef52SPaul Traina 	dbtkey.data = key.dptr;
958618ef52SPaul Traina 	dbtkey.size = key.dsize;
968618ef52SPaul Traina 	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
9758f0484fSRodney W. Grimes 	if (status) {
988618ef52SPaul Traina 		dbtretdata.data = NULL;
998618ef52SPaul Traina 		dbtretdata.size = 0;
10058f0484fSRodney W. Grimes 	}
1018618ef52SPaul Traina 	retdata.dptr = dbtretdata.data;
1028618ef52SPaul Traina 	retdata.dsize = dbtretdata.size;
1038618ef52SPaul Traina 	return (retdata);
10458f0484fSRodney W. Grimes }
10558f0484fSRodney W. Grimes 
10658f0484fSRodney W. Grimes /*
10758f0484fSRodney W. Grimes  * Returns:
10858f0484fSRodney W. Grimes  *	DATUM on success
10958f0484fSRodney W. Grimes  *	NULL on failure
11058f0484fSRodney W. Grimes  */
11158f0484fSRodney W. Grimes extern datum
dbm_firstkey(DBM * db)112d9556fb1SCraig Rodrigues dbm_firstkey(DBM *db)
11358f0484fSRodney W. Grimes {
11458f0484fSRodney W. Grimes 	int status;
1158618ef52SPaul Traina 	datum retkey;
1168618ef52SPaul Traina 	DBT dbtretkey, dbtretdata;
11758f0484fSRodney W. Grimes 
1188618ef52SPaul Traina 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
11958f0484fSRodney W. Grimes 	if (status)
1208618ef52SPaul Traina 		dbtretkey.data = NULL;
1218618ef52SPaul Traina 	retkey.dptr = dbtretkey.data;
1228618ef52SPaul Traina 	retkey.dsize = dbtretkey.size;
12358f0484fSRodney W. Grimes 	return (retkey);
12458f0484fSRodney W. Grimes }
12558f0484fSRodney W. Grimes 
12658f0484fSRodney W. Grimes /*
12758f0484fSRodney W. Grimes  * Returns:
12858f0484fSRodney W. Grimes  *	DATUM on success
12958f0484fSRodney W. Grimes  *	NULL on failure
13058f0484fSRodney W. Grimes  */
13158f0484fSRodney W. Grimes extern datum
dbm_nextkey(DBM * db)132d9556fb1SCraig Rodrigues dbm_nextkey(DBM *db)
13358f0484fSRodney W. Grimes {
13458f0484fSRodney W. Grimes 	int status;
1358618ef52SPaul Traina 	datum retkey;
1368618ef52SPaul Traina 	DBT dbtretkey, dbtretdata;
13758f0484fSRodney W. Grimes 
1388618ef52SPaul Traina 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
13958f0484fSRodney W. Grimes 	if (status)
1408618ef52SPaul Traina 		dbtretkey.data = NULL;
1418618ef52SPaul Traina 	retkey.dptr = dbtretkey.data;
1428618ef52SPaul Traina 	retkey.dsize = dbtretkey.size;
14358f0484fSRodney W. Grimes 	return (retkey);
14458f0484fSRodney W. Grimes }
1458618ef52SPaul Traina 
14658f0484fSRodney W. Grimes /*
14758f0484fSRodney W. Grimes  * Returns:
14858f0484fSRodney W. Grimes  *	 0 on success
14958f0484fSRodney W. Grimes  *	<0 failure
15058f0484fSRodney W. Grimes  */
15158f0484fSRodney W. Grimes extern int
dbm_delete(DBM * db,datum key)152d9556fb1SCraig Rodrigues dbm_delete(DBM *db, datum key)
15358f0484fSRodney W. Grimes {
15458f0484fSRodney W. Grimes 	int status;
1558618ef52SPaul Traina 	DBT dbtkey;
15658f0484fSRodney W. Grimes 
1578618ef52SPaul Traina 	dbtkey.data = key.dptr;
1588618ef52SPaul Traina 	dbtkey.size = key.dsize;
1598618ef52SPaul Traina 	status = (db->del)(db, &dbtkey, 0);
16058f0484fSRodney W. Grimes 	if (status)
16158f0484fSRodney W. Grimes 		return (-1);
16258f0484fSRodney W. Grimes 	else
16358f0484fSRodney W. Grimes 		return (0);
16458f0484fSRodney W. Grimes }
16558f0484fSRodney W. Grimes 
16658f0484fSRodney W. Grimes /*
16758f0484fSRodney W. Grimes  * Returns:
16858f0484fSRodney W. Grimes  *	 0 on success
16958f0484fSRodney W. Grimes  *	<0 failure
17058f0484fSRodney W. Grimes  *	 1 if DBM_INSERT and entry exists
17158f0484fSRodney W. Grimes  */
17258f0484fSRodney W. Grimes extern int
dbm_store(DBM * db,datum key,datum data,int flags)173d9556fb1SCraig Rodrigues dbm_store(DBM *db, datum key, datum data, int flags)
17458f0484fSRodney W. Grimes {
1758618ef52SPaul Traina 	DBT dbtkey, dbtdata;
1768618ef52SPaul Traina 
1778618ef52SPaul Traina 	dbtkey.data = key.dptr;
1788618ef52SPaul Traina 	dbtkey.size = key.dsize;
1798618ef52SPaul Traina 	dbtdata.data = data.dptr;
1808618ef52SPaul Traina 	dbtdata.size = data.dsize;
1818618ef52SPaul Traina 	return ((db->put)(db, &dbtkey, &dbtdata,
18258f0484fSRodney W. Grimes 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
18358f0484fSRodney W. Grimes }
18458f0484fSRodney W. Grimes 
18558f0484fSRodney W. Grimes extern int
dbm_error(DBM * db)186d9556fb1SCraig Rodrigues dbm_error(DBM *db)
18758f0484fSRodney W. Grimes {
18858f0484fSRodney W. Grimes 	HTAB *hp;
18958f0484fSRodney W. Grimes 
19058f0484fSRodney W. Grimes 	hp = (HTAB *)db->internal;
191f70177e7SJulian Elischer 	return (hp->error);
19258f0484fSRodney W. Grimes }
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes extern int
dbm_clearerr(DBM * db)195d9556fb1SCraig Rodrigues dbm_clearerr(DBM *db)
19658f0484fSRodney W. Grimes {
19758f0484fSRodney W. Grimes 	HTAB *hp;
19858f0484fSRodney W. Grimes 
19958f0484fSRodney W. Grimes 	hp = (HTAB *)db->internal;
200f70177e7SJulian Elischer 	hp->error = 0;
20158f0484fSRodney W. Grimes 	return (0);
20258f0484fSRodney W. Grimes }
20358f0484fSRodney W. Grimes 
20458f0484fSRodney W. Grimes extern int
dbm_dirfno(DBM * db)205d9556fb1SCraig Rodrigues dbm_dirfno(DBM *db)
20658f0484fSRodney W. Grimes {
20758f0484fSRodney W. Grimes 	return(((HTAB *)db->internal)->fp);
20858f0484fSRodney W. Grimes }
209