1 /* $NetBSD: ndbm.c,v 1.13 1997/07/21 14:06:40 jtc 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94"; 43 #else 44 __RCSID("$NetBSD: ndbm.c,v 1.13 1997/07/21 14:06:40 jtc Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 /* 49 * This package provides a dbm compatible interface to the new hashing 50 * package described in db(3). 51 */ 52 #include "namespace.h" 53 #include <sys/param.h> 54 55 #include <stdio.h> 56 #include <string.h> 57 58 #include <ndbm.h> 59 #include "hash.h" 60 61 #ifdef __weak_alias 62 __weak_alias(dbm_clearerr,_dbm_clearerr); 63 __weak_alias(dbm_close,_dbm_close); 64 __weak_alias(dbm_delete,_dbm_delete); 65 __weak_alias(dbm_dirfno,_dbm_dirfno); 66 __weak_alias(dbm_error,_dbm_error); 67 __weak_alias(dbm_fetch,_dbm_fetch); 68 __weak_alias(dbm_firstkey,_dbm_firstkey); 69 __weak_alias(dbm_nextkey,_dbm_nextkey); 70 __weak_alias(dbm_open,_dbm_open); 71 __weak_alias(dbm_store,_dbm_store); 72 #endif 73 74 /* 75 * Returns: 76 * *DBM on success 77 * NULL on failure 78 */ 79 extern DBM * 80 dbm_open(file, flags, mode) 81 const char *file; 82 int flags, mode; 83 { 84 HASHINFO info; 85 char path[MAXPATHLEN]; 86 87 info.bsize = 4096; 88 info.ffactor = 40; 89 info.nelem = 1; 90 info.cachesize = 0; 91 info.hash = NULL; 92 info.lorder = 0; 93 (void)strncpy(path, file, sizeof(path) - 1); 94 (void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1); 95 return ((DBM *)__hash_open(path, flags, mode, &info, 0)); 96 } 97 98 extern void 99 dbm_close(db) 100 DBM *db; 101 { 102 (void)(db->close)(db); 103 } 104 105 /* 106 * Returns: 107 * DATUM on success 108 * NULL on failure 109 */ 110 extern datum 111 dbm_fetch(db, key) 112 DBM *db; 113 datum key; 114 { 115 datum retdata; 116 int status; 117 DBT dbtkey, dbtretdata; 118 119 dbtkey.data = key.dptr; 120 dbtkey.size = key.dsize; 121 status = (db->get)(db, &dbtkey, &dbtretdata, 0); 122 if (status) { 123 dbtretdata.data = NULL; 124 dbtretdata.size = 0; 125 } 126 retdata.dptr = dbtretdata.data; 127 retdata.dsize = dbtretdata.size; 128 return (retdata); 129 } 130 131 /* 132 * Returns: 133 * DATUM on success 134 * NULL on failure 135 */ 136 extern datum 137 dbm_firstkey(db) 138 DBM *db; 139 { 140 int status; 141 datum retkey; 142 DBT dbtretkey, dbtretdata; 143 144 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST); 145 if (status) 146 dbtretkey.data = NULL; 147 retkey.dptr = dbtretkey.data; 148 retkey.dsize = dbtretkey.size; 149 return (retkey); 150 } 151 152 /* 153 * Returns: 154 * DATUM on success 155 * NULL on failure 156 */ 157 extern datum 158 dbm_nextkey(db) 159 DBM *db; 160 { 161 int status; 162 datum retkey; 163 DBT dbtretkey, dbtretdata; 164 165 status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT); 166 if (status) 167 dbtretkey.data = NULL; 168 retkey.dptr = dbtretkey.data; 169 retkey.dsize = dbtretkey.size; 170 return (retkey); 171 } 172 173 /* 174 * Returns: 175 * 0 on success 176 * <0 failure 177 */ 178 extern int 179 dbm_delete(db, key) 180 DBM *db; 181 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 195 /* 196 * Returns: 197 * 0 on success 198 * <0 failure 199 * 1 if DBM_INSERT and entry exists 200 */ 201 extern int 202 dbm_store(db, key, data, flags) 203 DBM *db; 204 datum key, data; 205 int flags; 206 { 207 DBT dbtkey, dbtdata; 208 209 dbtkey.data = key.dptr; 210 dbtkey.size = key.dsize; 211 dbtdata.data = data.dptr; 212 dbtdata.size = data.dsize; 213 return ((db->put)(db, &dbtkey, &dbtdata, 214 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0)); 215 } 216 217 extern int 218 dbm_error(db) 219 DBM *db; 220 { 221 HTAB *hp; 222 223 hp = (HTAB *)db->internal; 224 return (hp->err); 225 } 226 227 extern int 228 dbm_clearerr(db) 229 DBM *db; 230 { 231 HTAB *hp; 232 233 hp = (HTAB *)db->internal; 234 hp->err = 0; 235 return (0); 236 } 237 238 extern int 239 dbm_dirfno(db) 240 DBM *db; 241 { 242 return(((HTAB *)db->internal)->fp); 243 } 244