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