]> git.saurik.com Git - apple/libc.git/blame - db/hash/ndbm.c
Libc-320.1.3.tar.gz
[apple/libc.git] / db / hash / ndbm.c
CommitLineData
e9ce8d39 1/*
9385eb3d 2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
e9ce8d39
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
e9ce8d39
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
e9ce8d39
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
9385eb3d 23/*-
e9ce8d39
A
24 * Copyright (c) 1990, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Margo Seltzer.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
9385eb3d
A
59#if defined(LIBC_SCCS) && !defined(lint)
60static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
61#endif /* LIBC_SCCS and not lint */
62#include <sys/cdefs.h>
e9ce8d39
A
63
64/*
65 * This package provides a dbm compatible interface to the new hashing
66 * package described in db(3).
67 */
68
69#include <sys/param.h>
70
e9ce8d39
A
71#include <stdio.h>
72#include <string.h>
9385eb3d 73#include <errno.h>
e9ce8d39 74
9385eb3d 75#include <ndbm.h>
e9ce8d39
A
76#include "hash.h"
77
78/*
79 * Returns:
80 * *DBM on success
81 * NULL on failure
82 */
83extern DBM *
84dbm_open(file, flags, mode)
85 const char *file;
86 int flags, mode;
87{
88 HASHINFO info;
89 char path[MAXPATHLEN];
90
91 info.bsize = 4096;
92 info.ffactor = 40;
93 info.nelem = 1;
9385eb3d 94 info.cachesize = 0;
e9ce8d39
A
95 info.hash = NULL;
96 info.lorder = 0;
9385eb3d
A
97
98 if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
99 errno = ENAMETOOLONG;
100 return(NULL);
101 }
e9ce8d39
A
102 (void)strcpy(path, file);
103 (void)strcat(path, DBM_SUFFIX);
104 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
105}
106
107extern void
108dbm_close(db)
109 DBM *db;
110{
111 (void)(db->close)(db);
112}
113
114/*
115 * Returns:
116 * DATUM on success
117 * NULL on failure
118 */
119extern datum
120dbm_fetch(db, key)
121 DBM *db;
122 datum key;
123{
9385eb3d 124 datum retdata;
e9ce8d39 125 int status;
9385eb3d 126 DBT dbtkey, dbtretdata;
e9ce8d39 127
9385eb3d
A
128 dbtkey.data = key.dptr;
129 dbtkey.size = key.dsize;
130 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
e9ce8d39 131 if (status) {
9385eb3d
A
132 dbtretdata.data = NULL;
133 dbtretdata.size = 0;
e9ce8d39 134 }
9385eb3d
A
135 retdata.dptr = dbtretdata.data;
136 retdata.dsize = dbtretdata.size;
137 return (retdata);
e9ce8d39
A
138}
139
140/*
141 * Returns:
142 * DATUM on success
143 * NULL on failure
144 */
145extern datum
146dbm_firstkey(db)
147 DBM *db;
148{
149 int status;
9385eb3d
A
150 datum retkey;
151 DBT dbtretkey, dbtretdata;
e9ce8d39 152
9385eb3d 153 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
e9ce8d39 154 if (status)
9385eb3d
A
155 dbtretkey.data = NULL;
156 retkey.dptr = dbtretkey.data;
157 retkey.dsize = dbtretkey.size;
e9ce8d39
A
158 return (retkey);
159}
160
161/*
162 * Returns:
163 * DATUM on success
164 * NULL on failure
165 */
166extern datum
167dbm_nextkey(db)
168 DBM *db;
169{
170 int status;
9385eb3d
A
171 datum retkey;
172 DBT dbtretkey, dbtretdata;
e9ce8d39 173
9385eb3d 174 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
e9ce8d39 175 if (status)
9385eb3d
A
176 dbtretkey.data = NULL;
177 retkey.dptr = dbtretkey.data;
178 retkey.dsize = dbtretkey.size;
e9ce8d39
A
179 return (retkey);
180}
9385eb3d 181
e9ce8d39
A
182/*
183 * Returns:
184 * 0 on success
185 * <0 failure
186 */
187extern int
188dbm_delete(db, key)
189 DBM *db;
190 datum key;
191{
192 int status;
9385eb3d 193 DBT dbtkey;
e9ce8d39 194
9385eb3d
A
195 dbtkey.data = key.dptr;
196 dbtkey.size = key.dsize;
197 status = (db->del)(db, &dbtkey, 0);
e9ce8d39
A
198 if (status)
199 return (-1);
200 else
201 return (0);
202}
203
204/*
205 * Returns:
206 * 0 on success
207 * <0 failure
208 * 1 if DBM_INSERT and entry exists
209 */
210extern int
9385eb3d 211dbm_store(db, key, data, flags)
e9ce8d39 212 DBM *db;
9385eb3d 213 datum key, data;
e9ce8d39
A
214 int flags;
215{
9385eb3d
A
216 DBT dbtkey, dbtdata;
217
218 dbtkey.data = key.dptr;
219 dbtkey.size = key.dsize;
220 dbtdata.data = data.dptr;
221 dbtdata.size = data.dsize;
222 return ((db->put)(db, &dbtkey, &dbtdata,
e9ce8d39
A
223 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
224}
225
226extern int
227dbm_error(db)
228 DBM *db;
229{
230 HTAB *hp;
231
232 hp = (HTAB *)db->internal;
233 return (hp->error);
234}
235
236extern int
237dbm_clearerr(db)
238 DBM *db;
239{
240 HTAB *hp;
241
242 hp = (HTAB *)db->internal;
243 hp->error = 0;
244 return (0);
245}
246
247extern int
248dbm_dirfno(db)
249 DBM *db;
250{
251 return(((HTAB *)db->internal)->fp);
252}