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