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