ndbm.c revision 1.9.2.1 1 /* $NetBSD: ndbm.c,v 1.9.2.1 1996/09/16 18:39:54 jtc 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 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
42 #else
43 static char rcsid[] = "$NetBSD: ndbm.c,v 1.9.2.1 1996/09/16 18:39:54 jtc Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46
47 /*
48 * This package provides a dbm compatible interface to the new hashing
49 * package described in db(3).
50 */
51 #include "namespace.h"
52 #include <sys/param.h>
53
54 #include <stdio.h>
55 #include <string.h>
56
57 #include <ndbm.h>
58 #include "hash.h"
59
60 #ifdef __weak_alias
61 __weak_alias(dbm_open,_dbm_open);
62 __weak_alias(dbm_close,_dbm_close);
63 __weak_alias(dbm_fetch,_dbm_fetch);
64 __weak_alias(dbm_firstkey,_dbm_fetchkey);
65 __weak_alias(dbm_nextkey,_dbm_nextkey);
66 __weak_alias(dbm_delete,_dbm_delete);
67 __weak_alias(dbm_store,_dbm_store);
68 __weak_alias(dbm_error,_dbm_error);
69 __weak_alias(dbm_clearerr,_dbm_clearerr);
70 __weak_alias(dbm_dirfno,_dbm_dirfno);
71 #endif
72
73 /*
74 * Returns:
75 * *DBM on success
76 * NULL on failure
77 */
78 extern DBM *
79 dbm_open(file, flags, mode)
80 const char *file;
81 int flags, mode;
82 {
83 HASHINFO info;
84 char path[MAXPATHLEN];
85
86 info.bsize = 4096;
87 info.ffactor = 40;
88 info.nelem = 1;
89 info.cachesize = 0;
90 info.hash = NULL;
91 info.lorder = 0;
92 (void)strcpy(path, file);
93 (void)strcat(path, DBM_SUFFIX);
94 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
95 }
96
97 extern void
98 dbm_close(db)
99 DBM *db;
100 {
101 (void)(db->close)(db);
102 }
103
104 /*
105 * Returns:
106 * DATUM on success
107 * NULL on failure
108 */
109 extern datum
110 dbm_fetch(db, key)
111 DBM *db;
112 datum key;
113 {
114 datum retdata;
115 int status;
116 DBT dbtkey, dbtretdata;
117
118 dbtkey.data = key.dptr;
119 dbtkey.size = key.dsize;
120 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
121 if (status) {
122 dbtretdata.data = NULL;
123 dbtretdata.size = 0;
124 }
125 retdata.dptr = dbtretdata.data;
126 retdata.dsize = dbtretdata.size;
127 return (retdata);
128 }
129
130 /*
131 * Returns:
132 * DATUM on success
133 * NULL on failure
134 */
135 extern datum
136 dbm_firstkey(db)
137 DBM *db;
138 {
139 int status;
140 datum retkey;
141 DBT dbtretkey, dbtretdata;
142
143 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
144 if (status)
145 dbtretkey.data = NULL;
146 retkey.dptr = dbtretkey.data;
147 retkey.dsize = dbtretkey.size;
148 return (retkey);
149 }
150
151 /*
152 * Returns:
153 * DATUM on success
154 * NULL on failure
155 */
156 extern datum
157 dbm_nextkey(db)
158 DBM *db;
159 {
160 int status;
161 datum retkey;
162 DBT dbtretkey, dbtretdata;
163
164 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
165 if (status)
166 dbtretkey.data = NULL;
167 retkey.dptr = dbtretkey.data;
168 retkey.dsize = dbtretkey.size;
169 return (retkey);
170 }
171
172 /*
173 * Returns:
174 * 0 on success
175 * <0 failure
176 */
177 extern int
178 dbm_delete(db, key)
179 DBM *db;
180 datum key;
181 {
182 int status;
183 DBT dbtkey;
184
185 dbtkey.data = key.dptr;
186 dbtkey.size = key.dsize;
187 status = (db->del)(db, &dbtkey, 0);
188 if (status)
189 return (-1);
190 else
191 return (0);
192 }
193
194 /*
195 * Returns:
196 * 0 on success
197 * <0 failure
198 * 1 if DBM_INSERT and entry exists
199 */
200 extern int
201 dbm_store(db, key, data, flags)
202 DBM *db;
203 datum key, data;
204 int flags;
205 {
206 DBT dbtkey, dbtdata;
207
208 dbtkey.data = key.dptr;
209 dbtkey.size = key.dsize;
210 dbtdata.data = data.dptr;
211 dbtdata.size = data.dsize;
212 return ((db->put)(db, &dbtkey, &dbtdata,
213 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
214 }
215
216 extern int
217 dbm_error(db)
218 DBM *db;
219 {
220 HTAB *hp;
221
222 hp = (HTAB *)db->internal;
223 return (hp->errno);
224 }
225
226 extern int
227 dbm_clearerr(db)
228 DBM *db;
229 {
230 HTAB *hp;
231
232 hp = (HTAB *)db->internal;
233 hp->errno = 0;
234 return (0);
235 }
236
237 extern int
238 dbm_dirfno(db)
239 DBM *db;
240 {
241 return(((HTAB *)db->internal)->fp);
242 }
243