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