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