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