Debian Codegraph @

utils/db1-ast/hash/ndbm.c

raw symbols SCIP
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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
39 #endif /* LIBC_SCCS and not lint */
40
41 /*
42 * This package provides a dbm compatible interface to the new hashing
43 * package described in db(3).
44 */
45
46 #include <sys/param.h>
47
48 #include <stdio.h>
49 #include <string.h>
50 #include <stdlib.h>
51
52 #include <ndbm.h>
53 #include "hash.h"
54
55 /*
56 * Returns:
57 * *DBM on success
58 * NULL on failure
59 */
60 extern DBM *
61 dbm_open(file, flags, mode)
62 const char *file;
63 int flags, mode;
64 {
65 DBM *db;
66 HASHINFO info;
67 const size_t len = strlen(file) + sizeof (DBM_SUFFIX);
68 #ifdef __GNUC__
69 char path[len];
70 #else
71 char *path = malloc(len);
72 if (path == NULL)
73 return NULL;
74 #endif
75
76 info.bsize = 4096;
77 info.ffactor = 40;
78 info.nelem = 1;
79 info.cachesize = 0;
80 info.hash = NULL;
81 info.lorder = 0;
82 snprintf(path, len, "%s%s", file, DBM_SUFFIX);
83 db = (DBM *)__hash_open(path, flags, mode, &info, 0);
84 #ifndef __GNUC__
85 free(path);
86 #endif
87 return db;
88 }
89
90 extern void
91 dbm_close(db)
92 DBM *db;
93 {
94 (void)(db->close)(db);
95 }
96
97 /*
98 * Returns:
99 * DATUM on success
100 * NULL on failure
101 */
102 extern datum
103 dbm_fetch(db, key)
104 DBM *db;
105 datum key;
106 {
107 datum retdata;
108 int status;
109 DBT dbtkey, dbtretdata;
110
111 dbtkey.data = key.dptr;
112 dbtkey.size = key.dsize;
113 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
114 if (status) {
115 dbtretdata.data = NULL;
116 dbtretdata.size = 0;
117 }
118 retdata.dptr = dbtretdata.data;
119 retdata.dsize = dbtretdata.size;
120 return (retdata);
121 }
122
123 /*
124 * Returns:
125 * DATUM on success
126 * NULL on failure
127 */
128 extern datum
129 dbm_firstkey(db)
130 DBM *db;
131 {
132 int status;
133 datum retkey;
134 DBT dbtretkey, dbtretdata;
135
136 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
137 if (status)
138 dbtretkey.data = NULL;
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 */
149 extern datum
150 dbm_nextkey(db)
151 DBM *db;
152 {
153 int status;
154 datum retkey;
155 DBT dbtretkey, dbtretdata;
156
157 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
158 if (status)
159 dbtretkey.data = NULL;
160 retkey.dptr = dbtretkey.data;
161 retkey.dsize = dbtretkey.size;
162 return (retkey);
163 }
164 /*
165 * Returns:
166 * 0 on success
167 * <0 failure
168 */
169 extern int
170 dbm_delete(db, key)
171 DBM *db;
172 datum key;
173 {
174 int status;
175 DBT dbtkey;
176
177 dbtkey.data = key.dptr;
178 dbtkey.size = key.dsize;
179 status = (db->del)(db, &dbtkey, 0);
180 if (status)
181 return (-1);
182 else
183 return (0);
184 }
185
186 /*
187 * Returns:
188 * 0 on success
189 * <0 failure
190 * 1 if DBM_INSERT and entry exists
191 */
192 extern int
193 dbm_store(db, key, data, flags)
194 DBM *db;
195 datum key, data;
196 int flags;
197 {
198 DBT dbtkey, dbtdata;
199
200 dbtkey.data = key.dptr;
201 dbtkey.size = key.dsize;
202 dbtdata.data = data.dptr;
203 dbtdata.size = data.dsize;
204 return ((db->put)(db, &dbtkey, &dbtdata,
205 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
206 }
207
208 extern int
209 dbm_error(db)
210 DBM *db;
211 {
212 HTAB *hp;
213
214 hp = (HTAB *)db->internal;
215 return (hp->errnum);
216 }
217
218 extern int
219 dbm_clearerr(db)
220 DBM *db;
221 {
222 HTAB *hp;
223
224 hp = (HTAB *)db->internal;
225 hp->errnum = 0;
226 return (0);
227 }
228
229 extern int
230 dbm_dirfno(db)
231 DBM *db;
232 {
233 return(((HTAB *)db->internal)->fp);
234 }