1 /* $OpenBSD: ndbm.c,v 1.28 2023/02/17 17:59:36 miod Exp $ */
2
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 #include <errno.h>
36 #include <fcntl.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include <ndbm.h>
42 #include "hash.h"
43
44 /*
45 *
46 * This package provides dbm and ndbm compatible interfaces to DB.
47 * First are the DBM routines, which call the NDBM routines, and
48 * the NDBM routines, which call the DB routines.
49 */
50
51 static DBM *_dbm_open(const char *, const char *, int, mode_t);
52
53 /*
54 * Returns:
55 * *DBM on success
56 * NULL on failure
57 */
58 static DBM *
_dbm_open(const char * file,const char * suff,int flags,mode_t mode)59 _dbm_open(const char *file, const char *suff, int flags, mode_t mode)
60 {
61 HASHINFO info;
62 char path[PATH_MAX];
63 int len;
64
65 len = snprintf(path, sizeof path, "%s%s", file, suff);
66 if (len < 0 || len >= sizeof path) {
67 errno = ENAMETOOLONG;
68 return (NULL);
69 }
70 /* O_WRONLY not supported by db(3) but traditional ndbm allowed it. */
71 if ((flags & O_ACCMODE) == O_WRONLY) {
72 flags &= ~O_WRONLY;
73 flags |= O_RDWR;
74 }
75 info.bsize = 4096;
76 info.ffactor = 40;
77 info.nelem = 1;
78 info.cachesize = 0;
79 info.hash = NULL;
80 info.lorder = 0;
81 return ((DBM *)__hash_open(path, flags, mode, &info, 0));
82 }
83
84 /*
85 * Returns:
86 * *DBM on success
87 * NULL on failure
88 */
89 DBM *
dbm_open(const char * file,int flags,mode_t mode)90 dbm_open(const char *file, int flags, mode_t mode)
91 {
92
93 return(_dbm_open(file, DBM_SUFFIX, flags, mode));
94 }
95
96 /*
97 * Returns:
98 * Nothing.
99 */
100 void
dbm_close(DBM * db)101 dbm_close(DBM *db)
102 {
103
104 (void)(db->close)(db);
105 }
106 DEF_WEAK(dbm_close);
107
108 /*
109 * Returns:
110 * DATUM on success
111 * NULL on failure
112 */
113 datum
dbm_fetch(DBM * db,datum key)114 dbm_fetch(DBM *db, datum key)
115 {
116 datum retdata;
117 int status;
118 DBT dbtkey, dbtretdata;
119
120 dbtkey.data = key.dptr;
121 dbtkey.size = key.dsize;
122 status = (db->get)(db, &dbtkey, &dbtretdata, 0);
123 if (status) {
124 dbtretdata.data = NULL;
125 dbtretdata.size = 0;
126 }
127 retdata.dptr = dbtretdata.data;
128 retdata.dsize = dbtretdata.size;
129 return (retdata);
130 }
131 DEF_WEAK(dbm_fetch);
132
133 /*
134 * Returns:
135 * DATUM on success
136 * NULL on failure
137 */
138 datum
dbm_firstkey(DBM * db)139 dbm_firstkey(DBM *db)
140 {
141 int status;
142 datum retkey;
143 DBT dbtretkey, dbtretdata;
144
145 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
146 if (status)
147 dbtretkey.data = NULL;
148 retkey.dptr = dbtretkey.data;
149 retkey.dsize = dbtretkey.size;
150 return (retkey);
151 }
152 DEF_WEAK(dbm_firstkey);
153
154 /*
155 * Returns:
156 * DATUM on success
157 * NULL on failure
158 */
159 datum
dbm_nextkey(DBM * db)160 dbm_nextkey(DBM *db)
161 {
162 int status;
163 datum retkey;
164 DBT dbtretkey, dbtretdata;
165
166 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
167 if (status)
168 dbtretkey.data = NULL;
169 retkey.dptr = dbtretkey.data;
170 retkey.dsize = dbtretkey.size;
171 return (retkey);
172 }
173 DEF_WEAK(dbm_nextkey);
174
175 /*
176 * Returns:
177 * 0 on success
178 * <0 on failure
179 */
180 int
dbm_delete(DBM * db,datum key)181 dbm_delete(DBM *db, datum key)
182 {
183 int status;
184 DBT dbtkey;
185
186 dbtkey.data = key.dptr;
187 dbtkey.size = key.dsize;
188 status = (db->del)(db, &dbtkey, 0);
189 if (status)
190 return (-1);
191 else
192 return (0);
193 }
194 DEF_WEAK(dbm_delete);
195
196 /*
197 * Returns:
198 * 0 on success
199 * <0 on failure
200 * 1 if DBM_INSERT and entry exists
201 */
202 int
dbm_store(DBM * db,datum key,datum data,int flags)203 dbm_store(DBM *db, datum key, datum data, int flags)
204 {
205 DBT dbtkey, dbtdata;
206
207 dbtkey.data = key.dptr;
208 dbtkey.size = key.dsize;
209 dbtdata.data = data.dptr;
210 dbtdata.size = data.dsize;
211 return ((db->put)(db, &dbtkey, &dbtdata,
212 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
213 }
214 DEF_WEAK(dbm_store);
215
216 int
dbm_error(DBM * db)217 dbm_error(DBM *db)
218 {
219 HTAB *hp;
220
221 hp = (HTAB *)db->internal;
222 return (hp->err);
223 }
224
225 int
dbm_clearerr(DBM * db)226 dbm_clearerr(DBM *db)
227 {
228 HTAB *hp;
229
230 hp = (HTAB *)db->internal;
231 hp->err = 0;
232 return (0);
233 }
234
235 int
dbm_dirfno(DBM * db)236 dbm_dirfno(DBM *db)
237 {
238
239 return(((HTAB *)db->internal)->fp);
240 }
241
242 int
dbm_rdonly(DBM * dbp)243 dbm_rdonly(DBM *dbp)
244 {
245 HTAB *hashp = (HTAB *)dbp->internal;
246
247 /* Could use DBM_RDONLY instead if we wanted... */
248 return ((hashp->flags & O_ACCMODE) == O_RDONLY);
249 }
250 DEF_WEAK(dbm_rdonly);
251