]> git.saurik.com Git - apple/libc.git/blame - db/hash/ndbm-fbsd.c
Libc-763.11.tar.gz
[apple/libc.git] / db / hash / ndbm-fbsd.c
CommitLineData
224c7076
A
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.
224c7076
A
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>
1f2f436a 37__FBSDID("$FreeBSD: src/lib/libc/db/hash/ndbm.c,v 1.7 2007/01/09 00:27:51 imp Exp $");
224c7076
A
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(file, flags, mode)
63 const char *file;
64 int flags;
65 mode_t mode;
66{
67 HASHINFO info;
68 char path[MAXPATHLEN];
69
70 info.bsize = 4096;
71 info.ffactor = 40;
72 info.nelem = 1;
73 info.cachesize = 0;
74 info.hash = NULL;
75 info.lorder = 0;
76
77 if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
78 errno = ENAMETOOLONG;
79 return(NULL);
80 }
81 (void)strcpy(path, file);
82 (void)strcat(path, DBM_SUFFIX);
83 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
84}
85
86extern void
87dbm_close(db)
88 DBM *db;
89{
90 (void)(db->close)(db);
91}
92
93/*
94 * Returns:
95 * DATUM on success
96 * NULL on failure
97 */
98extern datum
99dbm_fetch(db, key)
100 DBM *db;
101 datum key;
102{
103 datum retdata;
104 int status;
105 DBT dbtkey, dbtretdata;
106
107 dbtkey.data = key.dptr;
108 dbtkey.size = key.dsize;
109 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
110 if (status) {
111 dbtretdata.data = NULL;
112 dbtretdata.size = 0;
113 }
114 retdata.dptr = dbtretdata.data;
115 retdata.dsize = dbtretdata.size;
116 return (retdata);
117}
118
119/*
120 * Returns:
121 * DATUM on success
122 * NULL on failure
123 */
124extern datum
125dbm_firstkey(db)
126 DBM *db;
127{
128 int status;
129 datum retkey;
130 DBT dbtretkey, dbtretdata;
131 HTAB *htab = (HTAB *)(db->internal);
132
133 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
134 if (status) {
135 dbtretkey.data = NULL;
136 htab->nextkey_eof = 1;
137 } else
138 htab->nextkey_eof = 0;
139 retkey.dptr = dbtretkey.data;
140 retkey.dsize = dbtretkey.size;
141 return (retkey);
142}
143
144/*
145 * Returns:
146 * DATUM on success
147 * NULL on failure
148 */
149extern datum
150dbm_nextkey(db)
151 DBM *db;
152{
153 int status = 1;
154 datum retkey;
155 DBT dbtretkey, dbtretdata;
156 HTAB *htab = (HTAB *)(db->internal);
157
158 if (htab->nextkey_eof)
159 dbtretkey.data = NULL;
160 else {
161 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
162 if (status) {
163 dbtretkey.data = NULL;
164 htab->nextkey_eof = 1;
165 }
166 }
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 */
177extern int
178dbm_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 */
200extern int
201dbm_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
216extern int
217dbm_error(db)
218 DBM *db;
219{
220 HTAB *hp;
221
222 hp = (HTAB *)db->internal;
223 return (hp->error);
224}
225
226extern int
227dbm_clearerr(db)
228 DBM *db;
229{
230 HTAB *hp;
231
232 hp = (HTAB *)db->internal;
233 hp->error = 0;
234 return (0);
235}
236
237extern int
238dbm_dirfno(db)
239 DBM *db;
240{
241 return(((HTAB *)db->internal)->fp);
242}