]> git.saurik.com Git - apple/network_cmds.git/blob - makedbm.tproj/ypdb.c
network_cmds-245.1.3.tar.gz
[apple/network_cmds.git] / makedbm.tproj / ypdb.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* $OpenBSD: ypdb.c,v 1.5 1997/02/09 09:49:36 maja Exp $ */
24
25 /*
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 * All rights reserved.
29 *
30 * This code is derived from software contributed to Berkeley by
31 * Margo Seltzer.
32 *
33 * This code is derived from ndbm module of BSD4.4 db (hash) by
34 * Mats O Jansson
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
53 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
56 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <stdio.h>
68 #include <string.h>
69
70 #include "ypdb.h"
71
72 #ifdef YPDB_PATCH
73 extern DBM *__hash_open();
74 #else
75 extern DBM *__bt_open();
76 #endif
77
78 /*
79 * Returns:
80 * *DBM on success
81 * NULL on failure
82 */
83
84 extern DBM *
85 ypdb_open(file, flags, mode)
86 const char *file;
87 int flags, mode;
88 {
89 #ifdef YPDB_PATCH
90 HASHINFO info;
91 char path[MAXPATHLEN];
92
93 info.bsize = 4096;
94 info.ffactor = 40;
95 info.nelem = 1;
96 info.cachesize = NULL;
97 info.hash = NULL;
98 info.lorder = 0;
99 snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX);
100 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
101 #else
102 BTREEINFO info;
103 char path[MAXPATHLEN];
104 DBM *db;
105
106 info.flags = 0;
107 info.cachesize = 0;
108 info.maxkeypage = 0;
109 info.minkeypage = 0;
110 info.psize = 0;
111 info.compare = NULL;
112 info.prefix = NULL;
113 info.lorder = 0;
114 snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX);
115 db = (DBM *)__bt_open(path, flags, mode, &info, 0);
116 return (db);
117 #endif
118 }
119
120 /*
121 * Returns:
122 * *DBM on success
123 * NULL on failure
124 */
125
126 extern DBM *
127 ypdb_open_suf(file, flags, mode)
128 const char *file;
129 int flags, mode;
130 {
131 #ifdef YPDB_PATCH
132 HASHINFO info;
133
134 info.bsize = 4096;
135 info.ffactor = 40;
136 info.nelem = 1;
137 info.cachesize = NULL;
138 info.hash = NULL;
139 info.lorder = 0;
140 return ((DBM *)__hash_open(file, flags, mode, &info, 0));
141 #else
142 BTREEINFO info;
143 DBM *db;
144
145 info.flags = 0;
146 info.cachesize = 0;
147 info.maxkeypage = 0;
148 info.minkeypage = 0;
149 info.psize = 0;
150 info.compare = NULL;
151 info.prefix = NULL;
152 info.lorder = 0;
153 db = (DBM *)__bt_open(file, flags, mode, &info, 0);
154 return (db);
155 #endif
156 }
157
158 extern void
159 ypdb_close(db)
160 DBM *db;
161 {
162 (void)(db->close)(db);
163 }
164
165 /*
166 * Returns:
167 * DATUM on success
168 * NULL on failure
169 */
170
171 extern datum
172 ypdb_fetch(db, key)
173 DBM *db;
174 datum key;
175 {
176 datum retval;
177 int status;
178
179 status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
180 if (status) {
181 retval.dptr = NULL;
182 retval.dsize = 0;
183 }
184 return (retval);
185 }
186
187 /*
188 * Returns:
189 * DATUM on success
190 * NULL on failure
191 */
192
193 extern datum
194 ypdb_firstkey(db)
195 DBM *db;
196 {
197 int status;
198 datum retdata, retkey;
199
200 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
201 if (status)
202 retkey.dptr = NULL;
203 return (retkey);
204 }
205
206 /*
207 * Returns:
208 * DATUM on success
209 * NULL on failure
210 */
211
212 extern datum
213 ypdb_nextkey(db)
214 DBM *db;
215 {
216 int status;
217 datum retdata, retkey;
218
219 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
220 if (status)
221 retkey.dptr = NULL;
222 return (retkey);
223 }
224
225 /*
226 * Returns:
227 * DATUM on success
228 * NULL on failure
229 */
230
231 extern datum
232 ypdb_setkey(db, key)
233 DBM *db;
234 datum key;
235 {
236 int status;
237 datum retdata;
238 #ifdef YPDB_PATCH
239 datum retkey;
240
241 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
242 if (status)
243 retkey.dptr = NULL;
244 while ((retkey.dptr != NULL) &&
245 ((retkey.dsize != key.dsize) ||
246 (strncmp(key.dptr,retkey.dptr,retkey.dsize) != 0))) {
247 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
248 if (status)
249 retkey.dptr = NULL;
250 };
251 return (retkey);
252 #else
253 status = (db->seq)(db, (DBT *)&key, (DBT *)&retdata, R_CURSOR);
254 if (status)
255 key.dptr = NULL;
256 return (key);
257 #endif
258 }
259
260 /*
261 * Returns:
262 * 0 on success
263 * <0 failure
264 */
265
266 int
267 ypdb_delete(db, key)
268 DBM *db;
269 datum key;
270 {
271 int status;
272
273 status = (db->del)(db, (DBT *)&key, 0);
274 if (status)
275 return (-1);
276 else
277 return (0);
278 }
279
280 /*
281 * Returns:
282 * 0 on success
283 * <0 failure
284 * 1 if YPDB_INSERT and entry exists
285 */
286
287 int
288 ypdb_store(db, key, content, flags)
289 DBM *db;
290 datum key, content;
291 int flags;
292 {
293 return ((db->put)(db, (DBT *)&key, (DBT *)&content,
294 (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
295 }
296