1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
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 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)ndbm.c	8.4 (Berkeley) 7/21/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: src/lib/libc/db/hash/ndbm.c,v 1.7 2007/01/09 00:27:51 imp Exp $");
38
39/*
40 * This package provides a dbm compatible interface to the new hashing
41 * package described in db(3).
42 */
43
44#include <sys/param.h>
45
46#include <stdio.h>
47#include <string.h>
48#include <errno.h>
49
50#include <db.h>
51#define _DBM
52typedef DB DBM;
53#include <ndbm.h>
54#include "hash.h"
55
56/*
57 * Returns:
58 * 	*DBM on success
59 *	 NULL on failure
60 */
61extern DBM *
62dbm_open(const char *file, int flags, mode_t mode)
63{
64	HASHINFO info;
65	char path[MAXPATHLEN];
66
67	info.bsize = 4096;
68	info.ffactor = 40;
69	info.nelem = 1;
70	info.cachesize = 0;
71	info.hash = NULL;
72	info.lorder = 0;
73
74	if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
75		errno = ENAMETOOLONG;
76		return(NULL);
77	}
78	(void)strcpy(path, file);
79	(void)strcat(path, DBM_SUFFIX);
80	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
81}
82
83extern void
84dbm_close(db)
85	DBM *db;
86{
87	(void)(db->close)(db);
88}
89
90/*
91 * Returns:
92 *	DATUM on success
93 *	NULL on failure
94 */
95extern datum
96dbm_fetch(db, key)
97	DBM *db;
98	datum key;
99{
100	datum retdata;
101	int status;
102	DBT dbtkey, dbtretdata;
103
104	dbtkey.data = key.dptr;
105	dbtkey.size = key.dsize;
106	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
107	if (status) {
108		dbtretdata.data = NULL;
109		dbtretdata.size = 0;
110	}
111	retdata.dptr = dbtretdata.data;
112	retdata.dsize = dbtretdata.size;
113	return (retdata);
114}
115
116/*
117 * Returns:
118 *	DATUM on success
119 *	NULL on failure
120 */
121extern datum
122dbm_firstkey(db)
123	DBM *db;
124{
125	int status;
126	datum retkey;
127	DBT dbtretkey, dbtretdata;
128	HTAB *htab = (HTAB *)(db->internal);
129
130	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
131	if (status) {
132		dbtretkey.data = NULL;
133		htab->nextkey_eof = 1;
134	} else
135		htab->nextkey_eof = 0;
136	retkey.dptr = dbtretkey.data;
137	retkey.dsize = dbtretkey.size;
138	return (retkey);
139}
140
141/*
142 * Returns:
143 *	DATUM on success
144 *	NULL on failure
145 */
146extern datum
147dbm_nextkey(db)
148	DBM *db;
149{
150	int status = 1;
151	datum retkey;
152	DBT dbtretkey, dbtretdata;
153	HTAB *htab = (HTAB *)(db->internal);
154
155	if (htab->nextkey_eof)
156		dbtretkey.data = NULL;
157	else {
158		status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
159		if (status) {
160			dbtretkey.data = NULL;
161			htab->nextkey_eof = 1;
162		}
163	}
164	retkey.dptr = dbtretkey.data;
165	retkey.dsize = dbtretkey.size;
166	return (retkey);
167}
168
169/*
170 * Returns:
171 *	 0 on success
172 *	<0 failure
173 */
174extern int
175dbm_delete(db, key)
176	DBM *db;
177	datum key;
178{
179	int status;
180	DBT dbtkey;
181
182	dbtkey.data = key.dptr;
183	dbtkey.size = key.dsize;
184	status = (db->del)(db, &dbtkey, 0);
185	if (status)
186		return (-1);
187	else
188		return (0);
189}
190
191/*
192 * Returns:
193 *	 0 on success
194 *	<0 failure
195 *	 1 if DBM_INSERT and entry exists
196 */
197extern int
198dbm_store(db, key, data, flags)
199	DBM *db;
200	datum key, data;
201	int flags;
202{
203	DBT dbtkey, dbtdata;
204
205	dbtkey.data = key.dptr;
206	dbtkey.size = key.dsize;
207	dbtdata.data = data.dptr;
208	dbtdata.size = data.dsize;
209	return ((db->put)(db, &dbtkey, &dbtdata,
210	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
211}
212
213extern int
214dbm_error(db)
215	DBM *db;
216{
217	HTAB *hp;
218
219	hp = (HTAB *)db->internal;
220	return (hp->error);
221}
222
223extern int
224dbm_clearerr(db)
225	DBM *db;
226{
227	HTAB *hp;
228
229	hp = (HTAB *)db->internal;
230	hp->error = 0;
231	return (0);
232}
233
234extern int
235dbm_dirfno(db)
236	DBM *db;
237{
238	return(((HTAB *)db->internal)->fp);
239}
240