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