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