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