Home | History | Annotate | Line # | Download | only in hash
ndbm.c revision 1.17.2.1
      1 /*	$NetBSD: ndbm.c,v 1.17.2.1 2004/04/30 03:46:39 jmc 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 <sys/cdefs.h>
     36 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)ndbm.c	8.4 (Berkeley) 7/21/94";
     39 #else
     40 __RCSID("$NetBSD: ndbm.c,v 1.17.2.1 2004/04/30 03:46:39 jmc Exp $");
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 /*
     45  * This package provides a dbm compatible interface to the new hashing
     46  * package described in db(3).
     47  */
     48 #include "namespace.h"
     49 #include <sys/param.h>
     50 
     51 #include <fcntl.h>
     52 #include <stdio.h>
     53 #include <string.h>
     54 
     55 #include <ndbm.h>
     56 #include "hash.h"
     57 
     58 #ifdef __weak_alias
     59 __weak_alias(dbm_clearerr,_dbm_clearerr)
     60 __weak_alias(dbm_close,_dbm_close)
     61 __weak_alias(dbm_delete,_dbm_delete)
     62 __weak_alias(dbm_dirfno,_dbm_dirfno)
     63 __weak_alias(dbm_error,_dbm_error)
     64 __weak_alias(dbm_fetch,_dbm_fetch)
     65 __weak_alias(dbm_firstkey,_dbm_firstkey)
     66 __weak_alias(dbm_nextkey,_dbm_nextkey)
     67 __weak_alias(dbm_open,_dbm_open)
     68 __weak_alias(dbm_store,_dbm_store)
     69 #endif
     70 
     71 /*
     72  * Returns:
     73  * 	*DBM on success
     74  *	 NULL on failure
     75  */
     76 extern DBM *
     77 dbm_open(file, flags, mode)
     78 	const char *file;
     79 	int flags;
     80 	mode_t mode;
     81 {
     82 	HASHINFO info;
     83 	char path[MAXPATHLEN];
     84 
     85 	info.bsize = 4096;
     86 	info.ffactor = 40;
     87 	info.nelem = 1;
     88 	info.cachesize = 0;
     89 	info.hash = NULL;
     90 	info.lorder = 0;
     91 	(void)strncpy(path, file, sizeof(path) - 1);
     92 	(void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1);
     93 	if ((flags & O_ACCMODE) == O_WRONLY) {
     94 		flags &= ~O_WRONLY;
     95 		flags |= O_RDWR;
     96 	}
     97 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
     98 }
     99 
    100 extern void
    101 dbm_close(db)
    102 	DBM *db;
    103 {
    104 	(void)(db->close)(db);
    105 }
    106 
    107 /*
    108  * Returns:
    109  *	DATUM on success
    110  *	NULL on failure
    111  */
    112 extern datum
    113 dbm_fetch(db, key)
    114 	DBM *db;
    115 	datum key;
    116 {
    117 	datum retdata;
    118 	int status;
    119 	DBT dbtkey, dbtretdata;
    120 
    121 	dbtkey.data = key.dptr;
    122 	dbtkey.size = key.dsize;
    123 	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
    124 	if (status) {
    125 		dbtretdata.data = NULL;
    126 		dbtretdata.size = 0;
    127 	}
    128 	retdata.dptr = dbtretdata.data;
    129 	retdata.dsize = dbtretdata.size;
    130 	return (retdata);
    131 }
    132 
    133 /*
    134  * Returns:
    135  *	DATUM on success
    136  *	NULL on failure
    137  */
    138 extern datum
    139 dbm_firstkey(db)
    140 	DBM *db;
    141 {
    142 	int status;
    143 	datum retkey;
    144 	DBT dbtretkey, dbtretdata;
    145 
    146 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
    147 	if (status)
    148 		dbtretkey.data = NULL;
    149 	retkey.dptr = dbtretkey.data;
    150 	retkey.dsize = dbtretkey.size;
    151 	return (retkey);
    152 }
    153 
    154 /*
    155  * Returns:
    156  *	DATUM on success
    157  *	NULL on failure
    158  */
    159 extern datum
    160 dbm_nextkey(db)
    161 	DBM *db;
    162 {
    163 	int status;
    164 	datum retkey;
    165 	DBT dbtretkey, dbtretdata;
    166 
    167 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
    168 	if (status)
    169 		dbtretkey.data = NULL;
    170 	retkey.dptr = dbtretkey.data;
    171 	retkey.dsize = dbtretkey.size;
    172 	return (retkey);
    173 }
    174 
    175 /*
    176  * Returns:
    177  *	 0 on success
    178  *	<0 failure
    179  */
    180 extern int
    181 dbm_delete(db, key)
    182 	DBM *db;
    183 	datum key;
    184 {
    185 	int status;
    186 	DBT dbtkey;
    187 
    188 	dbtkey.data = key.dptr;
    189 	dbtkey.size = key.dsize;
    190 	status = (db->del)(db, &dbtkey, 0);
    191 	if (status)
    192 		return (-1);
    193 	else
    194 		return (0);
    195 }
    196 
    197 /*
    198  * Returns:
    199  *	 0 on success
    200  *	<0 failure
    201  *	 1 if DBM_INSERT and entry exists
    202  */
    203 extern int
    204 dbm_store(db, key, data, flags)
    205 	DBM *db;
    206 	datum key, data;
    207 	int flags;
    208 {
    209 	DBT dbtkey, dbtdata;
    210 
    211 	dbtkey.data = key.dptr;
    212 	dbtkey.size = key.dsize;
    213 	dbtdata.data = data.dptr;
    214 	dbtdata.size = data.dsize;
    215 	return ((db->put)(db, &dbtkey, &dbtdata,
    216 	    (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0)));
    217 }
    218 
    219 extern int
    220 dbm_error(db)
    221 	DBM *db;
    222 {
    223 	HTAB *hp;
    224 
    225 	hp = (HTAB *)db->internal;
    226 	return (hp->err);
    227 }
    228 
    229 extern int
    230 dbm_clearerr(db)
    231 	DBM *db;
    232 {
    233 	HTAB *hp;
    234 
    235 	hp = (HTAB *)db->internal;
    236 	hp->err = 0;
    237 	return (0);
    238 }
    239 
    240 extern int
    241 dbm_dirfno(db)
    242 	DBM *db;
    243 {
    244 	return(((HTAB *)db->internal)->fp);
    245 }
    246