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 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
37 #endif /* LIBC_SCCS and not lint */
38 /*
39 * This package provides a dbm compatible interface to the new hashing
40 * package described in db(3).
41 */
42
43 #include <sys/param.h>
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <errno.h>
48
49 #include <ndbm.h>
50 #include "hash.h"
51
52 /*
53 * Returns:
54 * *DBM on success
55 * NULL on failure
56 */
57 extern DBM *
dbm_open(const char * file,int flags,mode_t mode)58 dbm_open(const char *file, int flags, mode_t mode)
59 {
60 HASHINFO info;
61 char path[MAXPATHLEN];
62
63 info.bsize = 4096;
64 info.ffactor = 40;
65 info.nelem = 1;
66 info.cachesize = 0;
67 info.hash = NULL;
68 info.lorder = 0;
69
70 if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
71 errno = ENAMETOOLONG;
72 return(NULL);
73 }
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
dbm_close(DBM * db)80 dbm_close(DBM *db)
81 {
82 (void)(db->close)(db);
83 }
84
85 /*
86 * Returns:
87 * DATUM on success
88 * NULL on failure
89 */
90 extern datum
dbm_fetch(DBM * db,datum key)91 dbm_fetch(DBM *db, datum key)
92 {
93 datum retdata;
94 int status;
95 DBT dbtkey, dbtretdata;
96
97 dbtkey.data = key.dptr;
98 dbtkey.size = key.dsize;
99 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
100 if (status) {
101 dbtretdata.data = NULL;
102 dbtretdata.size = 0;
103 }
104 retdata.dptr = dbtretdata.data;
105 retdata.dsize = dbtretdata.size;
106 return (retdata);
107 }
108
109 /*
110 * Returns:
111 * DATUM on success
112 * NULL on failure
113 */
114 extern datum
dbm_firstkey(DBM * db)115 dbm_firstkey(DBM *db)
116 {
117 int status;
118 datum retkey;
119 DBT dbtretkey, dbtretdata;
120
121 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
122 if (status)
123 dbtretkey.data = NULL;
124 retkey.dptr = dbtretkey.data;
125 retkey.dsize = dbtretkey.size;
126 return (retkey);
127 }
128
129 /*
130 * Returns:
131 * DATUM on success
132 * NULL on failure
133 */
134 extern datum
dbm_nextkey(DBM * db)135 dbm_nextkey(DBM *db)
136 {
137 int status;
138 datum retkey;
139 DBT dbtretkey, dbtretdata;
140
141 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
142 if (status)
143 dbtretkey.data = NULL;
144 retkey.dptr = dbtretkey.data;
145 retkey.dsize = dbtretkey.size;
146 return (retkey);
147 }
148
149 /*
150 * Returns:
151 * 0 on success
152 * <0 failure
153 */
154 extern int
dbm_delete(DBM * db,datum key)155 dbm_delete(DBM *db, datum key)
156 {
157 int status;
158 DBT dbtkey;
159
160 dbtkey.data = key.dptr;
161 dbtkey.size = key.dsize;
162 status = (db->del)(db, &dbtkey, 0);
163 if (status)
164 return (-1);
165 else
166 return (0);
167 }
168
169 /*
170 * Returns:
171 * 0 on success
172 * <0 failure
173 * 1 if DBM_INSERT and entry exists
174 */
175 extern int
dbm_store(DBM * db,datum key,datum data,int flags)176 dbm_store(DBM *db, datum key, datum data, int flags)
177 {
178 DBT dbtkey, dbtdata;
179
180 dbtkey.data = key.dptr;
181 dbtkey.size = key.dsize;
182 dbtdata.data = data.dptr;
183 dbtdata.size = data.dsize;
184 return ((db->put)(db, &dbtkey, &dbtdata,
185 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
186 }
187
188 extern int
dbm_error(DBM * db)189 dbm_error(DBM *db)
190 {
191 HTAB *hp;
192
193 hp = (HTAB *)db->internal;
194 return (hp->error);
195 }
196
197 extern int
dbm_clearerr(DBM * db)198 dbm_clearerr(DBM *db)
199 {
200 HTAB *hp;
201
202 hp = (HTAB *)db->internal;
203 hp->error = 0;
204 return (0);
205 }
206
207 extern int
dbm_dirfno(DBM * db)208 dbm_dirfno(DBM *db)
209 {
210 return(((HTAB *)db->internal)->fp);
211 }
212