Home | History | Annotate | Line # | Download | only in hash
ndbm.c revision 1.1.1.1
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Margo Seltzer.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 static char sccsid[] = "@(#)ndbm.c	5.7 (Berkeley) 6/17/91";
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 /*
     42     This package provides a dbm compatible interface to the new hashing
     43     package described in db(3)
     44 */
     45 
     46 #include <sys/param.h>
     47 #include <ndbm.h>
     48 #include <db.h>
     49 #include <stdio.h>
     50 #include "hash.h"
     51 
     52 /*
     53     return 	*DBM on success
     54 		NULL on failure
     55 */
     56 extern DBM *
     57 dbm_open( file, flags, mode )
     58 const char 	*file;
     59 int	flags;
     60 int	mode;
     61 {
     62     HASHINFO	info;
     63     char path[MAXPATHLEN];
     64 
     65     info.bsize = 1024;
     66     info.ffactor = 5;
     67     info.nelem = 1;
     68     info.cachesize = NULL;
     69     info.hash = NULL;
     70     info.lorder = 0;
     71     (void)strcpy(path, file);
     72     (void)strcat(path, DBM_SUFFIX);
     73     return( hash_open ( path, flags, mode, &info ) );
     74 }
     75 
     76 extern void
     77 dbm_close(db)
     78 DBM	*db;
     79 {
     80     (void)(db->close) (db);
     81 }
     82 
     83 /*
     84     Returns 	DATUM on success
     85 		NULL on failure
     86 */
     87 extern datum
     88 dbm_fetch( db, key )
     89 DBM 	*db;
     90 datum	key;
     91 {
     92     int	status;
     93     datum	retval;
     94 
     95     status = (db->get) ( db, (DBT *)&key, (DBT *)&retval, 0 );
     96     if ( status ) {
     97 	retval.dptr = NULL;
     98 	retval.dsize = 0;
     99     }
    100     return(retval);
    101 }
    102 
    103 /*
    104     Returns 	DATUM on success
    105 		NULL on failure
    106 */
    107 extern datum
    108 dbm_firstkey(db)
    109 DBM 	*db;
    110 {
    111     int	status;
    112     datum	retkey;
    113     datum	retdata;
    114 
    115     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
    116     if ( status ) {
    117 	retkey.dptr = NULL;
    118     }
    119     return(retkey);
    120 }
    121 /*
    122     Returns 	DATUM on success
    123 		NULL on failure
    124 */
    125 extern datum
    126 dbm_nextkey(db)
    127 DBM 	*db;
    128 {
    129     int	status;
    130     datum	retkey;
    131     datum	retdata;
    132 
    133     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
    134     if ( status ) {
    135 	retkey.dptr = NULL;
    136     }
    137     return(retkey);
    138 }
    139 
    140 /*
    141     0 on success
    142     <0 failure
    143 */
    144 extern int
    145 dbm_delete(db, key)
    146 DBM 	*db;
    147 datum	key;
    148 {
    149     int	status;
    150 
    151     status = (db->del)( db, (DBT *)&key, 0 );
    152     if ( status ) {
    153 	return(-1);
    154     } else {
    155 	return(0);
    156     }
    157 }
    158 
    159 /*
    160     0 on success
    161     <0 failure
    162     1 if DBM_INSERT and entry exists
    163 */
    164 extern int
    165 dbm_store(db, key, content, flags)
    166 DBM 	*db;
    167 datum	key;
    168 datum	content;
    169 int	flags;
    170 {
    171     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
    172 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
    173 }
    174 
    175 extern int
    176 dbm_error(db)
    177 DBM	*db;
    178 {
    179     HTAB	*hp;
    180 
    181     hp = (HTAB *)db->internal;
    182     return ( hp->errno );
    183 }
    184 
    185 extern int
    186 dbm_clearerr(db)
    187 DBM	*db;
    188 {
    189     HTAB	*hp;
    190 
    191     hp = (HTAB *)db->internal;
    192     hp->errno = 0;
    193     return ( 0 );
    194 }
    195