Home | History | Annotate | Line # | Download | only in common
ypdb.c revision 1.2
      1 /*	$NetBSD: ypdb.c,v 1.2 1997/07/18 21:57:01 thorpej 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 extern DBM *
     60 ypdb_open(file, flags, mode)
     61 	const char *file;
     62 	int flags, mode;
     63 {
     64 #ifdef YPDB_PATCH
     65 	HASHINFO info;
     66 	char path[MAXPATHLEN];
     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 	char path[MAXPATHLEN];
     79 	DBM *db;
     80 
     81 	info.flags = 0;
     82 	info.cachesize = 0;
     83 	info.maxkeypage = 0;
     84 	info.minkeypage = 0;
     85 	info.psize = 0;
     86 	info.compare = NULL;
     87 	info.prefix = NULL;
     88 	info.lorder = 0;
     89 	snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX);
     90 	db = (DBM *)__bt_open(path, flags, mode, &info, 0);
     91 	return (db);
     92 #endif
     93 }
     94 
     95 extern void
     96 ypdb_close(db)
     97 	DBM *db;
     98 {
     99 	(void)(db->close)(db);
    100 }
    101 
    102 /*
    103  * Returns:
    104  *	DATUM on success
    105  *	NULL on failure
    106  */
    107 
    108 extern datum
    109 ypdb_fetch(db, key)
    110 	DBM *db;
    111 	datum key;
    112 {
    113 	datum retval;
    114 	int status;
    115 
    116 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
    117 	if (status) {
    118 		retval.dptr = NULL;
    119 		retval.dsize = 0;
    120 	}
    121 	return (retval);
    122 }
    123 
    124 /*
    125  * Returns:
    126  *	DATUM on success
    127  *	NULL on failure
    128  */
    129 
    130 extern datum
    131 ypdb_firstkey(db)
    132 	DBM *db;
    133 {
    134 	int status;
    135 	datum retdata, retkey;
    136 
    137 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
    138 	if (status)
    139 		retkey.dptr = NULL;
    140 	return (retkey);
    141 }
    142 
    143 /*
    144  * Returns:
    145  *	DATUM on success
    146  *	NULL on failure
    147  */
    148 
    149 extern datum
    150 ypdb_nextkey(db)
    151 	DBM *db;
    152 {
    153 	int status;
    154 	datum retdata, retkey;
    155 
    156 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
    157 	if (status)
    158 		retkey.dptr = NULL;
    159 	return (retkey);
    160 }
    161 
    162 /*
    163  * Returns:
    164  *	DATUM on success
    165  *	NULL on failure
    166  */
    167 
    168 extern datum
    169 ypdb_setkey(db, key)
    170 	DBM *db;
    171         datum key;
    172 {
    173 	int status;
    174 	datum retdata;
    175 #ifdef YPDB_PATCH
    176 	datum retkey;
    177 
    178 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
    179 	if (status)
    180 		retkey.dptr = NULL;
    181 	while ((retkey.dptr != NULL) &&
    182 	       ((retkey.dsize != key.dsize) ||
    183 		(strncmp(key.dptr,retkey.dptr,retkey.dsize) != 0))) {
    184 	  status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
    185 	  if (status)
    186 	  	retkey.dptr = NULL;
    187 	};
    188 	return (retkey);
    189 #else
    190 	status = (db->seq)(db, (DBT *)&key, (DBT *)&retdata, R_CURSOR);
    191 	if (status)
    192 		key.dptr = NULL;
    193 	return (key);
    194 #endif
    195 }
    196 
    197 /*
    198  * Returns:
    199  *	 0 on success
    200  *	<0 failure
    201  */
    202 
    203 int
    204 ypdb_delete(db, key)
    205 	DBM *db;
    206 	datum key;
    207 {
    208 	int status;
    209 
    210 	status = (db->del)(db, (DBT *)&key, 0);
    211 	if (status)
    212 		return (-1);
    213 	else
    214 		return (0);
    215 }
    216 
    217 /*
    218  * Returns:
    219  *	 0 on success
    220  *	<0 failure
    221  *	 1 if YPDB_INSERT and entry exists
    222  */
    223 
    224 int
    225 ypdb_store(db, key, content, flags)
    226 	DBM *db;
    227 	datum key, content;
    228 	int flags;
    229 {
    230 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
    231 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
    232 }
    233