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