ndbm.c revision 1.12 1 /* $NetBSD: ndbm.c,v 1.12 1997/07/13 18:52:08 christos 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.12 1997/07/13 18:52:08 christos 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
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 /*
62 * Returns:
63 * *DBM on success
64 * NULL on failure
65 */
66 extern DBM *
67 dbm_open(file, flags, mode)
68 const char *file;
69 int flags, mode;
70 {
71 HASHINFO info;
72 char path[MAXPATHLEN];
73
74 info.bsize = 4096;
75 info.ffactor = 40;
76 info.nelem = 1;
77 info.cachesize = 0;
78 info.hash = NULL;
79 info.lorder = 0;
80 (void)strncpy(path, file, sizeof(path) - 1);
81 (void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1);
82 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
83 }
84
85 extern void
86 dbm_close(db)
87 DBM *db;
88 {
89 (void)(db->close)(db);
90 }
91
92 /*
93 * Returns:
94 * DATUM on success
95 * NULL on failure
96 */
97 extern datum
98 dbm_fetch(db, key)
99 DBM *db;
100 datum key;
101 {
102 datum retdata;
103 int status;
104 DBT dbtkey, dbtretdata;
105
106 dbtkey.data = key.dptr;
107 dbtkey.size = key.dsize;
108 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
109 if (status) {
110 dbtretdata.data = NULL;
111 dbtretdata.size = 0;
112 }
113 retdata.dptr = dbtretdata.data;
114 retdata.dsize = dbtretdata.size;
115 return (retdata);
116 }
117
118 /*
119 * Returns:
120 * DATUM on success
121 * NULL on failure
122 */
123 extern datum
124 dbm_firstkey(db)
125 DBM *db;
126 {
127 int status;
128 datum retkey;
129 DBT dbtretkey, dbtretdata;
130
131 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
132 if (status)
133 dbtretkey.data = NULL;
134 retkey.dptr = dbtretkey.data;
135 retkey.dsize = dbtretkey.size;
136 return (retkey);
137 }
138
139 /*
140 * Returns:
141 * DATUM on success
142 * NULL on failure
143 */
144 extern datum
145 dbm_nextkey(db)
146 DBM *db;
147 {
148 int status;
149 datum retkey;
150 DBT dbtretkey, dbtretdata;
151
152 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
153 if (status)
154 dbtretkey.data = NULL;
155 retkey.dptr = dbtretkey.data;
156 retkey.dsize = dbtretkey.size;
157 return (retkey);
158 }
159
160 /*
161 * Returns:
162 * 0 on success
163 * <0 failure
164 */
165 extern int
166 dbm_delete(db, key)
167 DBM *db;
168 datum key;
169 {
170 int status;
171 DBT dbtkey;
172
173 dbtkey.data = key.dptr;
174 dbtkey.size = key.dsize;
175 status = (db->del)(db, &dbtkey, 0);
176 if (status)
177 return (-1);
178 else
179 return (0);
180 }
181
182 /*
183 * Returns:
184 * 0 on success
185 * <0 failure
186 * 1 if DBM_INSERT and entry exists
187 */
188 extern int
189 dbm_store(db, key, data, flags)
190 DBM *db;
191 datum key, data;
192 int flags;
193 {
194 DBT dbtkey, dbtdata;
195
196 dbtkey.data = key.dptr;
197 dbtkey.size = key.dsize;
198 dbtdata.data = data.dptr;
199 dbtdata.size = data.dsize;
200 return ((db->put)(db, &dbtkey, &dbtdata,
201 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
202 }
203
204 extern int
205 dbm_error(db)
206 DBM *db;
207 {
208 HTAB *hp;
209
210 hp = (HTAB *)db->internal;
211 return (hp->err);
212 }
213
214 extern int
215 dbm_clearerr(db)
216 DBM *db;
217 {
218 HTAB *hp;
219
220 hp = (HTAB *)db->internal;
221 hp->err = 0;
222 return (0);
223 }
224
225 extern int
226 dbm_dirfno(db)
227 DBM *db;
228 {
229 return(((HTAB *)db->internal)->fp);
230 }
231