Home | History | Annotate | Line # | Download | only in hash
ndbmdatum.c revision 1.3
      1 /*	$NetBSD: ndbmdatum.c,v 1.3 2008/09/10 17:52:35 joerg Exp $	*/
      2 /*	from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Margo Seltzer.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __RCSID("$NetBSD: ndbmdatum.c,v 1.3 2008/09/10 17:52:35 joerg Exp $");
     38 
     39 /*
     40  * This package provides a dbm compatible interface to the new hashing
     41  * package described in db(3).
     42  */
     43 #include "namespace.h"
     44 #include <sys/param.h>
     45 
     46 #include <fcntl.h>
     47 #include <stdio.h>
     48 #include <string.h>
     49 
     50 #include <ndbm.h>
     51 #include "hash.h"
     52 
     53 /*
     54  * Returns:
     55  *	DATUM on success
     56  *	NULL on failure
     57  */
     58 datum
     59 dbm_fetch(DBM *db, datum key)
     60 {
     61 	datum retdata;
     62 	int status;
     63 	DBT dbtkey, dbtretdata;
     64 
     65 	dbtkey.data = key.dptr;
     66 	dbtkey.size = key.dsize;
     67 	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
     68 	if (status) {
     69 		dbtretdata.data = NULL;
     70 		dbtretdata.size = 0;
     71 	}
     72 	retdata.dptr = dbtretdata.data;
     73 	retdata.dsize = dbtretdata.size;
     74 	return (retdata);
     75 }
     76 
     77 /*
     78  * Returns:
     79  *	DATUM on success
     80  *	NULL on failure
     81  */
     82 datum
     83 dbm_firstkey(DBM *db)
     84 {
     85 	int status;
     86 	datum retkey;
     87 	DBT dbtretkey, dbtretdata;
     88 
     89 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
     90 	if (status)
     91 		dbtretkey.data = NULL;
     92 	retkey.dptr = dbtretkey.data;
     93 	retkey.dsize = dbtretkey.size;
     94 	return (retkey);
     95 }
     96 
     97 /*
     98  * Returns:
     99  *	DATUM on success
    100  *	NULL on failure
    101  */
    102 datum
    103 dbm_nextkey(DBM *db)
    104 {
    105 	int status;
    106 	datum retkey;
    107 	DBT dbtretkey, dbtretdata;
    108 
    109 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
    110 	if (status)
    111 		dbtretkey.data = NULL;
    112 	retkey.dptr = dbtretkey.data;
    113 	retkey.dsize = dbtretkey.size;
    114 	return (retkey);
    115 }
    116 
    117 /*
    118  * Returns:
    119  *	 0 on success
    120  *	<0 failure
    121  */
    122 int
    123 dbm_delete(DBM *db, datum key)
    124 {
    125 	int status;
    126 	DBT dbtkey;
    127 
    128 	dbtkey.data = key.dptr;
    129 	dbtkey.size = key.dsize;
    130 	status = (db->del)(db, &dbtkey, 0);
    131 	if (status)
    132 		return (-1);
    133 	else
    134 		return (0);
    135 }
    136 
    137 /*
    138  * Returns:
    139  *	 0 on success
    140  *	<0 failure
    141  *	 1 if DBM_INSERT and entry exists
    142  */
    143 int
    144 dbm_store(DBM *db, datum key, datum data, int flags)
    145 {
    146 	DBT dbtkey, dbtdata;
    147 
    148 	dbtkey.data = key.dptr;
    149 	dbtkey.size = key.dsize;
    150 	dbtdata.data = data.dptr;
    151 	dbtdata.size = data.dsize;
    152 	return ((db->put)(db, &dbtkey, &dbtdata,
    153 	    (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0)));
    154 }
    155