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