ndbm.c revision 1.5 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 /* from: static char sccsid[] = "@(#)ndbm.c 8.2 (Berkeley) 9/11/93"; */
39 static char *rcsid = "$Id: ndbm.c,v 1.5 1993/09/17 01:06:48 cgd Exp $";
40 #endif /* LIBC_SCCS and not lint */
41
42 /*
43 * This package provides a dbm compatible interface to the new hashing
44 * package described in db(3).
45 */
46
47 #include <sys/param.h>
48
49 #include <ndbm.h>
50 #include <stdio.h>
51 #include <string.h>
52
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 HASHINFO info;
66 char path[MAXPATHLEN];
67
68 info.bsize = 4096;
69 info.ffactor = 40;
70 info.nelem = 1;
71 info.cachesize = NULL;
72 info.hash = NULL;
73 info.lorder = 0;
74 (void)strcpy(path, file);
75 (void)strcat(path, DBM_SUFFIX);
76 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
77 }
78
79 extern void
80 dbm_close(db)
81 DBM *db;
82 {
83 (void)(db->close)(db);
84 }
85
86 /*
87 * Returns:
88 * DATUM on success
89 * NULL on failure
90 */
91 extern datum
92 dbm_fetch(db, key)
93 DBM *db;
94 datum key;
95 {
96 datum retval;
97 int status;
98
99 status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
100 if (status) {
101 retval.dptr = NULL;
102 retval.dsize = 0;
103 }
104 return (retval);
105 }
106
107 /*
108 * Returns:
109 * DATUM on success
110 * NULL on failure
111 */
112 extern datum
113 dbm_firstkey(db)
114 DBM *db;
115 {
116 int status;
117 datum retdata, retkey;
118
119 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
120 if (status)
121 retkey.dptr = NULL;
122 return (retkey);
123 }
124
125 /*
126 * Returns:
127 * DATUM on success
128 * NULL on failure
129 */
130 extern datum
131 dbm_nextkey(db)
132 DBM *db;
133 {
134 int status;
135 datum retdata, retkey;
136
137 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
138 if (status)
139 retkey.dptr = NULL;
140 return (retkey);
141 }
142 /*
143 * Returns:
144 * 0 on success
145 * <0 failure
146 */
147 extern int
148 dbm_delete(db, key)
149 DBM *db;
150 datum key;
151 {
152 int status;
153
154 status = (db->del)(db, (DBT *)&key, 0);
155 if (status)
156 return (-1);
157 else
158 return (0);
159 }
160
161 /*
162 * Returns:
163 * 0 on success
164 * <0 failure
165 * 1 if DBM_INSERT and entry exists
166 */
167 extern int
168 dbm_store(db, key, content, flags)
169 DBM *db;
170 datum key, content;
171 int flags;
172 {
173 return ((db->put)(db, (DBT *)&key, (DBT *)&content,
174 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
175 }
176
177 extern int
178 dbm_error(db)
179 DBM *db;
180 {
181 HTAB *hp;
182
183 hp = (HTAB *)db->internal;
184 return (hp->errno);
185 }
186
187 extern int
188 dbm_clearerr(db)
189 DBM *db;
190 {
191 HTAB *hp;
192
193 hp = (HTAB *)db->internal;
194 hp->errno = 0;
195 return (0);
196 }
197
198 extern int
199 dbm_dirfno(db)
200 DBM *db;
201 {
202 return(((HTAB *)db->internal)->fp);
203 }
204